9

I am writing a program using execl to execute my exe file which is testing and it's work very well and display the output in the Linux CLI. But I have not idea how to change the execl to execv, although I know both of the system call will give the same value. I am confused with the array argument for execv system call

This is my execl sample program

int main(void)
{
   int childpid;
   if((childpid = fork()) == -1 )
{
   perror("can't fork");
   exit(1);
}
 else if(childpid == 0)
{
  execl("./testing","","",(char *)0);
  exit(0);
}
else
{
printf("finish");
exit(0);
}
}

can I know how to change the execl to execv. What I read from online, we must set the file path for my exe file and the argument of array . What type of argument need to set for the array in order to ask the program to execute the testing exe file ? https://support.sas.com/documentation/onlinedoc/sasc/doc/lr2/execv.htm

Is it the link consist of the thing I want ? But what I read from it ,the command is request the list the file,not execute the file. Correct me I make any mistake

jww
  • 97,681
  • 90
  • 411
  • 885
doe
  • 163
  • 1
  • 1
  • 7
  • 2
    Why do you want to use `execv`? What are you actually trying to accomplish? – Kevin Aug 21 '15 at 13:57
  • 1
    Did you try the example on the page you link? Here the *offical* Linux man-page: http://man7.org/linux/man-pages/man3/exec.3.html – alk Aug 21 '15 at 14:04
  • http://linux.die.net/man/3/execv -- the difference is that `execv` wants a single pointer to a `char *` array, whereas `execl` accepts a variadic list of `char *` arguments. – tripleee Aug 21 '15 at 14:06
  • Related if not a duplicate: http://stackoverflow.com/q/10790719/694576 – alk Aug 21 '15 at 14:13
  • 1
    Like Kevin said, why change it? With the exception of `execvpe`, which AFAIK is essentially an alias for the "real" `execve`, it's basically all syntactic sugar. So use the one that already works. – Brian McFarland Aug 21 '15 at 14:28
  • because I see the execv will bring out the same result for me, I just wanna try only – doe Aug 21 '15 at 15:19

2 Answers2

28

In order to see the difference, here is a line of code executing a ls -l -R -a

  • with execl(3):

    execl("/bin/ls", "ls", "-l", "-R", "-a", NULL);
    
  • with execv(3):

    char* arr[] = {"ls", "-l", "-R", "-a", NULL};
    execv("/bin/ls", arr);
    

The char(*)[] sent to execv will be passed to /bin/ls as argv (in int main(int argc, char **argv))

PiCTo
  • 924
  • 1
  • 11
  • 23
4rzael
  • 679
  • 6
  • 17
  • Does C support inline arrays like `execv("/bin/ls", ["ls", "-l", "-R", "-a", NULL]);`? When compiling on a linux machine with `gcc --std=c99 file.c` I get an error: `error: expected expression before '[' token`. Couldn't find anything about this elsewhere... Any ideas? – sleighty Feb 05 '18 at 22:22
  • 1
    No, my bad, it was just to explain the difference. I will edit – 4rzael Feb 06 '18 at 19:35
  • Oh thank you haha I was going nuts trying to look for a way to make that work. – sleighty Feb 08 '18 at 22:30
  • I know this is old, but shouldn't `char* arr[] = {"ls", "-l", "-R", "-a", NULL}; execv("/bin/ls", arr);` be `char* arr[] = {"ls", "-l", "-R", "-a", NULL}; execv("/bin/", arr);`? – vpappano Jun 18 '22 at 14:51
9

According to the man page the use of execv is quite simple. The first argument is the path as a string to the program you want to execute. The second is an array of string that will be used as the arguments of the program you want to execute. It is the kind of array you get if you get the argv array in your main function.

So the array you will pass as a parameter will be the array received in the main function of the program you execute with execv.

By convention, the first argument should be the program name (the one you try to execute) but it is not mandatory (but strongly recommended since it is the behaviour a lot of programs are expecting). Each other string in the array should be an individual argument.

And of course, the array should be terminated with a NULL pointer to mark the end.

Array example: ["prog_name", "arg1", "arg2", "arg3", NULL]

[] is your array, each string separated with a coma is a frame of your array and at the end you have the null frame.

I hope I am clear enough!

Roger
  • 657
  • 5
  • 18
  • thank you ,your guide absolute help me but what is the argument type I need to specify in the array ?Is it similar with the 4rzael answer ? – doe Aug 21 '15 at 18:14
  • Yes it is similar. Your array is a string (char *) array. So it is more like `["prog_name", "arg1", "arg2", "arg3", NULL]` – Roger Aug 21 '15 at 20:29
  • if the command has syntax of arg1 val1 arg2 val2 (such as iptables -A INPUT -p udp) is it that the valX is treated as argument ? – ransh Nov 21 '18 at 17:16