从 C 内部运行 csh
我想从我的 C 程序运行 csh
命令。我使用以下代码完成了此操作:
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
终止。并且 "
是不必要的。
I want to run from my C program a csh
command. I did this using the following code:
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);
}
what I do here is creating an array containing the parameters for csh
:
-f, -c, "[the command to run]"
But the result is not what expected. The child process just runs in the background without doing anything.
We've tried to run ls | wc -l
using it.
where is the problem?
EDIT:
The length_command
variable already has a +1 for \0
. The problem was with the array not terminated in NULL
. And the "
were unnecessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到的第一个错误
尝试:
length_command
值是多少?它至少应该是strlen(full_command)+1
更新:
另一个问题可能与
strcat
有关,因为您使用的是未初始化的字符串并且它们可能包含一些垃圾。至少执行以下操作:或者仅使用
strdup
而不是malloc
+strcat
First error I see it that
Try:
And what is the
length_command
value? it should be at leaststrlen(full_command)+1
UPDATE:
Another issue could be related to
strcat
, because you are using not initialized strings and they can contain some garbage. Do at least following:Or just use
strdup
instead ofmalloc
+strcat
有一些问题会使您的代码比实际情况更加复杂和错误:
malloc()
+strcat()
=strdup()
除非您计划使用通用的 < code>cshcmd() 函数或其他东西,为什么要复制字符串文字而不是直接使用它们?
execvp()
参数数组必须以NULL
结尾。数组初始值设定项和匿名数组在这种情况下是一种福音 - 您可能不会错过它们末尾的 NULL。当然,除非您必须使用完全动态的结构...
您不应该在命令参数中使用那些额外的双引号。这些用于使用命令行时运行的 shell,并在调用 exec() 时被删除。换句话说,
csh
应该看到ls -1 | wc -l
,而不是"ls -1 | wc -l"
。哦,
exec()
中参数数组的第一个元素始终是argv[0]
。这可能应该是csh
的内容,而不是一个选项。编辑:
strcat()
之前,您是否清除了malloc()
分配的内存?strcat()
会很高兴地溢出你的缓冲区,具体取决于它已经拥有的随机内容......A few issues that make your code more complex and incorrect than it has to be:
malloc()
+strcat()
=strdup()
Unless you are planning on having a generic
cshcmd()
function or something, why are you copying the string literals instead of using them directly?The
execvp()
argument array must end with aNULL
.Array initializers and anonymous arrays are a blessing in cases like this - you would probably not miss the NULL at the end with them. Unless, of course, you have to use fully dynamic structures...
You should not have those extra double quotes in the command argument. Those are intended for the running shell when you use the command line and are stripped away when calling
exec()
. In other words,csh
should seels -1 | wc -l
, not"ls -1 | wc -l"
.Oh, and the first element of the argument array in
exec()
is alwaysargv[0]
. That should probably be something along the linescsh
, not an option.EDIT:
malloc()
before usingstrcat()
?strcat()
will happily overflow your buffer, depending on whatever random contents it already has...第一个问题是你的 malloc 中有一个离一错误 - 没有空间用于终止 null (`\0')
第二个问题是
execvp
:此外,
execvp()
使用 shell 并在您的路径中搜索给定的命令。当前"csh"
的第一个参数是您要运行的可执行文件。您谈到想要运行wc
;这是第一个论点。First issue is you have an off-by-one error in your malloc - there's no room for the terminating null (`\0')
Second issue is with
execvp
:In addition,
execvp()
uses a shell and searches your path for the given command. The first argument where you have"csh"
currently is the executable you want to run. You talk about wanting to runwc
; that would be the first argument.