Read documentation of execv(3) and of execve(2) and of perror(3). At the very least, you should code
int main(int argc, char *argv[]) {
char *loc = getL();
char *args[] = { loc, "ls", NULL };
int i;
execv(args[0], args);
perror("execv");
free(loc);
}
You should compile with gcc -Wall -g
then use the gdb
debugger.
Your usage of execv
is obviously wrong (you need a full path, e.g. "/bin/ls"
, and the order of arguments is wrong). You probably want exevcp(3) and you should in fact code at least:
char *args = { "ls", loc, NULL };
execvp("ls", args);
perror("execvp")
If you insist on using specifically execv(3) you could try
char *args = { "ls", loc, NULL };
execv("/bin/ls", args);
perror("execv")
I don't understand what your code is supposed to do. You might be interested by glob(7) & glob(3).
You probably should read Advanced Linux Programming. It seems that there are several concepts that you don't understand well enough. I guess that strace(1) could be useful to you (at least by running strace ls *.c
to understand what is happening).
Maybe your getL
is exactly what the GNU function get_current_dir_name(3) is doing, but then the (char**)
cast inside it is grossly wrong. And you should better clear the buffer buff
using memset(3) before calling getcwd(2) (and you should test against failure of ̀ mallocand of
getcwd`)
Perhaps you want opendir(3), readdir(3), asprintf(3), stat(2); with all these, you could even avoid running ls
If you are coding some shell, you should strace
some existing shell, and after having read all the references I am giving here, study the source code of free software shells like sash and GNU bash