execlp multiple “programs”

荒凉一梦 提交于 2019-12-02 00:48:31

The first "argument" is what becomes argv[0], so you should call with something like:

execlp("bash", "bash", "-c", "base64 -d | tar -zvt", NULL);

Edit A small explanation what the above function does: The exec family of functions executes a program. In the above call the program in question is "bash" (first argument). Bash, like all other programs, have a main function that is the starting point of the program. And like all other main functions, the one in Bash receives two arguments, commonly called argc and argv. argv is an array of zero-terminated strings, and argc is the number of entries in the argv array. argc will always be at least 1, meaning that there is always one entry at argv[0]. This first entry is the "name" of the program, most often the path of the program file. All other program arguments on the command line is put into argv[1] to argv[argc - 1].

What execlp does is use the first argument to find the program to be executed, and all the other arguments will be put into the programs argv array in the order they are given. This means that the above call to execlp will call the program "bash" and set the argv array of Bash to this:

argv[0] = "bash"
argv[1] = "-c"
argv[2] = "base64 -d | tar -zvt"

Also, argc of Bash will be set to 3.

If the second "bash" is changed to "foo", then argv[0] of Bash will be set to "foo" as well.

I hope this clears it up a little bit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!