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 chang
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!
In order to see the difference between execl and execv, here is a line of code executing a
ls -l -R -a
execl("/bin/ls", "ls", "-l", "-R", "-a", NULL);
char* arr[] = {"ls", "-l", "-R", "-a", NULL};
execv("/bin/ls", arr);
The array of char* sent to execv will be passed to /bin/ls as argv (in int main(int argc, char **argv)
)
Here is the execl(3) Linux manual page for more detail.