calling ls with execve

c, linux, unix

Solution

Since `execve` does not return on success, it's obvious that the call is failing somehow. It returns -1 on error, which would be treated as true (non-zero). That's why it's saying okay.

The most likely reason is that you haven't specified the full path to the executable, which is a requirement for `execve`. Alternatively, if you want the path to be searched, use `execvpe` instead. See http://linux.die.net/man/3/exec for the gory details.

Problem

the following code should call a command with `excve` for some reason i doesn't print the contents of the current directory, but still the program returns `"ok"` ``` 1 #include <stdio.h> 2 #include <unistd.h> 3 int main () 4 { char *argv[]={"ls",NULL};· 5 char *env[]={"PATH=/usr/local/sbin/:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games",NULL}; 6 if(execve("ls",argv,env)) 7 { printf("ok\n"); 8 } 9 else 10 { printf("not ok\n"); 11 } 12 return 0; 13 } ```

Original source