-1

I am writing a program in C that forks and uses execv to call a command that is passed in to the original function.

So I would type in something like "./program echo "hello"" and the child of the fork in my program would use the bash command "echo hello".

The execv man page is pretty vague, it says "The first argument, by convention, should point to the filename associated with the file being executed." I am trying to figure out if I'm understanding this right.

From examples I've seen, this is how execv should be called:

execv(argv[0], argv);

But I know that argv[0] would just contain "./program". So I am thinking this just exec's to the same function its inside of, but it is not what I've seen in examples.

From what I know it should be this:

execv(argv[1],argv+1);

or

execv(argv[1],argv+2);

Can anyone help me understand this? Thanks.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
zdale
  • 5
  • 1
  • 5

1 Answers1

0

The FIRST parameter to execv is the application to exec. "/bin/echo" -- you might not want to rely on the availability of environment variables to find your application, so full path is preferred.

The SECOND parameter to execv is a NULL terminated array of const strings that repeat the "/bin/echo" of the first parameter, so:

char *const params = { "/bin/echo", "hello", NULL };

execv("/bin/echo", params);
Blunt Jackson
  • 616
  • 4
  • 17