I have a string array in C named args[]
- now how can I use this list of arguments to construct a proper call to execl()
?
So if the array contains:
{"/bin/ls","ls","-a","-l"}
...how can I eventually construct an execl()
call that is:
execl("/bin/ls","ls","-a","-l",NULL);
I must be thinking about this wrong, as I can't find anything online, just talk about defining functions that can take a variable number of arguments.
Taken directly from "man execl"
The execv() and execvp() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated 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[]);
If you have an array that you want to pass to one of the exec*
family, you should use execv
rather than execl
.
Your array should be terminated by a NULL pointer, which yours currently isn't:
{"/bin/ls","ls","-a","-l", NULL}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With