从 C 内部运行 csh

发布于 2024-11-01 06:33:14 字数 828 浏览 2 评论 0原文

我想从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2024-11-08 06:33:14

我看到的第一个错误

指针数组必须是
由 NULL 指针终止。

尝试:

char *csh_command[4];
csh_command[3] = 0;

length_command 值是多少?它至少应该是 strlen(full_command)+1

更新:

另一个问题可能与 strcat 有关,因为您使用的是未初始化的字符串并且它们可能包含一些垃圾。至少执行以下操作:

char *str = (char *)malloc(strlen("blabla")+1);
str[0] = '\0';
strcat(str, "blabla");

或者仅使用 strdup 而不是 malloc+strcat

First error I see it that

The array of pointers must be
terminated by a NULL pointer.

Try:

char *csh_command[4];
csh_command[3] = 0;

And what is the length_command value? it should be at least strlen(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:

char *str = (char *)malloc(strlen("blabla")+1);
str[0] = '\0';
strcat(str, "blabla");

Or just use strdup instead of malloc+strcat

只为一人 2024-11-08 06:33:14

有一些问题会使您的代码比实际情况更加复杂和错误:

  • 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 a NULL.

  • 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 see ls -1 | wc -l, not "ls -1 | wc -l".

  • Oh, and the first element of the argument array in exec() is always argv[0]. That should probably be something along the lines csh, not an option.

EDIT:

  • One more thing: Do you clear the memory allocated by malloc() before using strcat()? strcat() will happily overflow your buffer, depending on whatever random contents it already has...
迷荒 2024-11-08 06:33:14

第一个问题是你的 malloc 中有一个离一错误 - 没有空间用于终止 null (`\0')

length_command + strlen("\"\"") + 1

第二个问题是 execvp

指针数组必须以 NULL 指针终止。

此外,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')

length_command + strlen("\"\"") + 1

Second issue is with execvp:

The array of pointers must be terminated by a NULL pointer.

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 run wc; that would be the first argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文