[英]Why do these two execvp produce different results?
I am wondering why the below produce different results: 我想知道为什么以下内容会产生不同的结果:
char *const temp[] = {"cal","4","2019",NULL};
execvp(temp[0],temp);
perror("Return from execlp not expected");
exit(EXIT_FAILURE);
when executed will produce a calendar with ONLY the month april 执行时将产生一个日历,其中仅包含一个月的四月
char *const temp[] = {"cal"};
char *const temp2[] ={"4","2019",NULL};
execvp(temp[0],temp2);
perror("Return from execlp not expected");
exit(EXIT_FAILURE);
when executed will produce a calendar with ALL the months 执行时将产生一个包含所有月份的日历
I want to get the second form to work correctly, as for my problem i have 2 arrays, one to store all my commands, and the other to store all my arguments for the command. 我想让第二种形式正常工作,因为我的问题是我有2个数组,一个数组存储我的所有命令,另一个数组存储我的所有命令参数。 eg
例如
array1[0] = command
array2[0] = arg1 ar2 arg3 // the arguments for the command in array1[0]
And within a loop until i hit the end of my command array use fork and excute these commands in a child class so that i can go through and execute all the commands in the array. 在一个循环中,直到我到达命令数组的末尾为止,使用fork并在子类中执行这些命令,以便我可以遍历并执行数组中的所有命令。
As per execvp man 根据execvp man
int execvp(const char *file, char *const argv[]);
The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program.
execv(),execvp()和execvpe()函数提供了一个指向以空字符结尾的字符串的指针的数组,这些字符串表示新程序可用的参数列表。 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
指针数组必须以NULL指针终止
argv[0]
is always assumed to be a name of the program, even if its not used by the execvp
like in your case. argv[0]
始终被认为是程序的名称,即使execvp
不使用它,就像您的情况一样。 The cal
program itself starts interpreting the command line from argv[1]
. cal
程序本身开始从argv[1]
解释命令行。 (See @kiran Biradar answer.) (请参阅@kiran Biradar答案。)
You'll need to copy the (pointers to the) arguments into a bigger array, and set a dummy argument for argv[0]
. 您需要将(指针指向)参数复制到更大的数组中,并为
argv[0]
设置一个虚拟参数。 Here's some short code to do that: 这是一些简短的代码可以做到这一点:
char **combine_args(char *arg0, char **tail) {
size_t n = 0;
while(tail[n]) ++n;
char **ret = malloc(sizeof(char*)*(n+2));
ret[0] = arg0;
memcpy(ret + 1, tail, sizeof(char*)*n);
ret[n+1] = 0;
return ret;
}
Next you can use it like this: 接下来,您可以像这样使用它:
char *const temp[] = {"cal"};
char *const temp2[] ={"4","2019",NULL};
char **argv = combine_args(temp[0], temp2);
execvp(argv[0], argv);
perror("Return from execlp not expected");
exit(EXIT_FAILURE);
If you do it in a loop and you can change the temp2
array, then you can do it like this, without any allocations: 如果您循环执行此操作,并且可以更改
temp2
数组,则可以像这样进行操作,而无需进行任何分配:
char *const temp[] = {"cal", "cal2", "cal3", NULL};
char *temp2[] ={NULL, "4","2019",NULL};
for(int i = 0; temp[i]; ++i)
{
// ... fork here ...
{
temp2[0] = temp[i];
execvp(temp2[0], temp2);
perror("Return from execlp not expected");
exit(EXIT_FAILURE);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.