The first argument is the path of the file to be executed (/bin/ls
, /home/development/myproject/foo
). The remaining arguments correspond to the argv
vector passed to main
. Imagine typing the following in your shell:
$ ./foo bar bletch
The executable path is ./foo
- that's the first argument passed to execlp
. By convention, argv[0]
is supposed to be the string used to invoke the program, so the complete argv
vector is {"./foo", "bar", "bletch", NULL}
. Hence,
execlp( "./foo", /* executable path */
"./foo", /* argv[0] */
"bar", /* argv[1] */
"bletch", /* argv[2] */
NULL /* argv[3] */
);
It's possible that you may not want argv[0]
to be the same as the actual command path (say because the path is aliased or you don't want to expose the exact path), in which case you may use something like
execlp( "./secret/path/to/foo", "./foo", "bar", "bletch", NULL );