C - 将 execvp 与用户输入一起使用
我目前正在尝试让我的 C 程序从用户那里读取 Unix 参数。到目前为止,我已经搜索过这个网站,但我无法确切地弄清楚我做错了什么 - 尽管我承认我的指针实现技能相当有限。
以下是我现在的代码;我一直在摆弄指针,但没有运气。这些错误还表明我需要使用 const *char,但我在其他示例中看到 *char 可以由用户输入。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
char args[128];
//User input
printf("> ");
fgets(args, 128, stdin);
execvp(args[0], *args[0]);
}
我得到的错误如下:
smallshellfile.c: In function ‘main’:
smallshellfile.c:13:21: error: invalid type argument of unary ‘*’ (have ‘int’)
smallshellfile.c:13:5: warning: passing argument 1 of ‘execvp’ makes pointer from integer without a cast [enabled by default]
/usr/include/unistd.h:575:12: note: expected ‘const char *’ but argument is of type ‘char’
有谁知道问题可能是什么?
I'm currently trying to have my C program read Unix arguments from the user. I've so far searched this site but I haven't been able to figure out exactly what I'm doing wrong - though admittedly my pointer implementation skills are rather limited.
The following is how I have the code now; I've been messing around with the pointers with no luck. The errors are also saying that I need to use const *char, but I've seen in other examples the *char can be input by the user.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
char args[128];
//User input
printf("> ");
fgets(args, 128, stdin);
execvp(args[0], *args[0]);
}
The error I get is as follows:
smallshellfile.c: In function ‘main’:
smallshellfile.c:13:21: error: invalid type argument of unary ‘*’ (have ‘int’)
smallshellfile.c:13:5: warning: passing argument 1 of ‘execvp’ makes pointer from integer without a cast [enabled by default]
/usr/include/unistd.h:575:12: note: expected ‘const char *’ but argument is of type ‘char’
Does anyone know what the problem may be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有几个问题:
*args[0]
毫无意义。args
是数组。args[0]
是字符。*args[0]
是什么?您必须创建一个以 NULL 结尾的
char*
数组,作为第二个参数传递。args[0]
是args
中的第一个字符。您应该传递整个字符串(仅args
),而不仅仅是其第一个字符。尝试类似的方法:
You have several problems:
*args[0]
is meaningless.args
is array.args[0]
is char. what is*args[0]
?You have to create a NULL-terminated array of
char*
, to pass as 2nd argument.args[0]
is the first char inargs
. you should pass the whole string (justargs
), not only its first char.Try something like:
这可能更适合您:
它具有最少的必要标头;它有一个正确声明的
main()
- C99 需要显式返回类型;它根据用户输入的信息运行 shell。除非用户在按回车键之前输入了超过 126 个字符,否则错误消息会正确地以换行符终止。如果execvp()
或任何exec*()
函数返回,则失败;您不需要测试其状态。我通过让 shell 做真正的工作来进行惊人的作弊。但您最终可能希望将用户输入的内容拆分为单词,以便命令位于第一个并且有多个参数。然后,您分配一个更大的 argv 数组,并解析字符串,将每个单独的参数放入 argv 中自己的条目中,然后使用 execvp() 开始有意义了。请注意,如果需要完成 I/O 重定向,则必须由您的 shell 来执行此操作(除非您运行真正的 shell 来为您执行此操作 - 就像我所做的那样)。
This might work better for you:
It has the minimum necessary headers; it has a correctly declared
main()
- C99 requires an explicit return type; it runs the shell on the information the user types in. The error message is correctly terminated with a newline unless the user typed more than 126 characters before hitting hitting return. Ifexecvp()
or any of theexec*()
functions returns, it failed; you don't need to test its status.I'm cheating phenomenally by making the shell do the real work. But you may end up wanting to split what the user typed into words, so that the command is first and there are multiple arguments. You'd then allocated a bigger
argv
array, and parse the string, putting each separate argument into its own entry inargv
and then usingexecvp()
begins to make sense. Note that if there is I/O redirection to be done, then it is your shell that will have to do it (unless you run the real shell to do it for you - like I did).