开发者

How do I use multiple arguments from an array to construct an execl() call in C?

I have a string array in C named args[] - n开发者_开发百科ow 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} 


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜