The code I'm using is this:
main()
{ char* arg[3];
arg[0] = "cat":
arg[1] =
"someFileName.txt";
arg[2] = '\0';
execve("cat", arg, '\0');
/* this should never be
executed */
}
ok, so after compiling this, I run the file and then nothing hapens, just
the prompt appears, no error message, no file contents shown, nothing.
Could anyone tell me why this is hapening??
I've tryied changing de command "cat" for some "ls" but I've got the same
results.
Please, help me. I need to solve this urgently.
Thx a lot, Adrián.
Unix systems calls such as execve return an error code and set errno if
an error occured. The error you will get is a good hint.
You seem to use '\0' as a null pointer constant. It does work, but it is
confusing. Lookup more common null pointer constants in your C reference
manual.
Philip Homburg
"Adrian Ducet" <adu...@sion.com> wrote in message
news:<bhuq8t$idj$1...@inews.gazeta.pl>...
> Hi, I'm trying to make a simple prog in c that makes a "cat" call using
> execve (or execl, is the same for me).
> The problem is that it seems not to work.
Well, this one works for me (minix 2.0.0):
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
char *args[3];
args[0] = "/bin/cat";
args[1] = "/usr/src/.profile";
args[2] = "";
execve(args[0], args, NULL);
return(0);
}
try to found the error in your code. :-)
-- Luiz Capitulino
Well, you got the parms to execve() wrong
The prototype for execve() is
int execve(char *filename, char *argv[], char *envp[]);
where
- filename is a pointer to a string containing the path to the binary
to be executed
- argv is an array of pointers to char, filled in in the manner of a C
main()'s argv; argv[0] points to the name of the binary,
argv[1] points to the first parameter,
argv[2] points to the 2nd parameter, etc
with argv[LAST] set to NULL,
- envp is an array of pointers to char, filled in in the manner of a C
main()'s envp.
In your case, you probably want something like (in C89 style)...
int main(void)
{
char *path, *argv[3], *envp[1];
path = "cat"; /* path to binary to execute */
argv[0] = "cat"; /* cat's argv[0] is "cat" */
argv[1] = "aFile.txt"; /* cat's argv[1] is "aFile.txt" */
argv[2] = 0; /* terminate argv list */
envp[0] = 0; /* empty environment for cat */
execve(path,argv,envp);
return 0;
}
--
Lew Pitcher
Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
> char *path, *argv[3], *envp[1];
>
> path = "cat"; /* path to binary to execute */
> argv[0] = "cat"; /* cat's argv[0] is "cat" */
> argv[1] = "aFile.txt"; /* cat's argv[1] is "aFile.txt" */
> argv[2] = 0; /* terminate argv list */
> envp[0] = 0; /* empty environment for cat */
>
> execve(path,argv,envp);
You probably want to have
path = "/bin/cat";
execve doesn't do any PATH expansion, so the above snippet would go wrong
when there's no executable named "cat" in the current directory. Which
probably isn't what you want..
--
Martijn van Buul - Pi...@dohd.org - http://www.stack.nl/~martijnb/
Geek code: G-- - Visit OuterSpace: mud.stack.nl 3333
Research is what I'm doing when I don't know what I'm doing (W. von Braun)