I have a sample program:
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int arg = 0; arg < argc; ++arg)
{
printf("%s\n", argv[arg]);
}
return 0;
}
When I compile and run, it behaves as I expect:
>>> gcc callee.c -std=c99 -o callee
>>> ./callee foo bar baz quux
./callee
foo
bar
baz
quux
But now I want to call it from a different program:
#include <unistd.h>
int main(int argc, char *argv[])
{
execl("callee", "foo", "bar", "baz", "quux", (char *)NULL);
return 0;
}
And the program name isn't passed to argv[0] anymore!
>>> gcc -std=c99 caller.c -o caller
>>> ./caller
foo
bar
baz
quux
I suppose I can get my desired behavior by passing the program name a second time when calling execl
, but I'm concerned whether that's the right way to do it.
man exec
just says "The first argument, by convention, should point to the filename associated with the file being executed," which seems to support my attempt, so I'm not sure what I'm missing.