我想从我的C程序运行一个csh
命令。我这样做是使用下面的代码:从内部运行csh c
char *csh_command[3];
...
csh_command[2]=(char*)malloc((length_command+strlen("\"\""))*sizeof(char));
csh_command[0]=(char*)malloc((strlen("-f")+1)*sizeof(char));
csh_command[1]=(char*)malloc((strlen("-c")+1)*sizeof(char));
strcat (csh_command[0],"-f");
strcat (csh_command[1],"-c");
strcat (csh_command[2],"\"");
strcat (csh_command[2],full_command);
strcat (csh_command[2],"\"");
pid=fork();
if (pid == 0){
execvp("csh", csh_command);
}
什么,我在这里做的是创建一个包含参数数组的csh
: -f,-c“[运行命令]”
但结果不是预期的结果。子进程只是在后台运行,不做任何事情。
我们试着运行ls | wc -l
使用它。
问题在哪里?
编辑:
的length_command
变量已经为\0
+1。问题是数组未在NULL
中终止。并且"
是不必要的。
请不要写关于不投射“malloc”的结果。我意识到这一点,仍然选择这样做。 – SIMEL 2011-04-15 18:28:05
我想传递给execvp的参数数组必须以'NULL'指针结束? – 2011-04-15 18:30:37
'csh_command'是否包含您期望拥有的字符串? 'sizeof(char)'是1,所以不需要在'malloc'中乘它? – Mahesh 2011-04-15 18:31:11