(Note: in all explanation below, it is assumed that you use the convention of calling argc
and argv
to the parameters of main()
routine, if you don't call them like here, then you'll need to change the names where they appear below.)
The parameters to execl()
are, in order:
- the first is the file name to be loaded for execution. This is used by the kernel to get the program loaded on memory. This string is used only by the kernel and the executed program doesn't have access to it.
- the second is
argv[0]
, this is, the program name (again). You can put any other string that will be seen from the inside of the new program as argv[0]
. (see below for a funny effect of this)
- the third, etc. are the command line arguments, and will appear as
argv[1]
, argv[2]
, etc. upto a final NULL
value that indicates to execl
the end of the list of arguments. This is also seen in the program as argv[argc]
, and is also equal to NULL
.
so, to call ls
, you need to use:
execl("/bin/ls", "ls", NULL);
or, if you want to pass it arguments:
execl("/bin/ls", "ls", "-l", "/etc/passwd", NULL);
the first call you used gives you an error because you pass NULL
as the program name (argv[0]
), the second is accepted but as a call to ls with a wrong name (which ls
internally doesn't mind if you change it) and no command line parameters, so it lists the directory "."
.
To be valid you could call execl
as:
execl("/bin/ls", "What the hell?", "badfsafds", NULL);
and you should get:
What the hell?: badfsafds: No such file or directory
and you'll see that, despite of not checking that it has been called with a different name than the file that was loaded, it uses it to announce itself in the error message.