How to use multiple arguments from an array to create an execl () call in C?

I have a string array in C named args[] - now how can I use this argument list to build the correct execl() call?

So if the array contains:

 {"/bin/ls","ls","-a","-l"} 

... how can I end up building an execl() call that:

 execl("/bin/ls","ls","-a","-l",NULL); 

I have to think about it wrong, because I can not find anything on the Internet, just talk about defining functions that can take a variable number of arguments.

+4
source share
3 answers

Taken directly from "man execl"

The execv () and execvp () functions provide an array of pointers to null-terminated strings that represent a list of arguments available for the new program. The first argument, by convention, should point to the file name associated with the executable. Array pointers must be interrupted by a NULL pointer.

EDIT: Here are the prototypes.

 int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); 
+5
source

If you have an array that you want to pass to one of the exec* family , you should use execv , not execl .

Your array should be interrupted by a NULL pointer, which you currently do not have:

 {"/bin/ls","ls","-a","-l", NULL} 
+7
source

First make sure your args [] array has a NULL pointer as the last element, and then calls

execv(args[0], &args[1]);

+3
source

Source: https://habr.com/ru/post/1306567/


All Articles