2

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.

  • "Should be" does not mean "MUST BE" or "IS". You can decide to do differently which is not encouraged. But this also means whatever you decide, you have to do it accordingly. No one does it for you. Just add the missing string to your parameter list. – Gerhardh Dec 12 '18 at 12:33
  • 1
    See `man execve` and the examples, especially the second one (`execve.c`). – James Brown Dec 12 '18 at 12:59

0 Answers0