C - 将 execvp 与用户输入一起使用

发布于 2025-01-05 01:17:48 字数 837 浏览 2 评论 0原文

我目前正在尝试让我的 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 技术交流群。

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

发布评论

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

评论(2

向地狱狂奔 2025-01-12 01:17:48

您有几个问题:

  1. *args[0] 毫无意义。 args 是数组。 args[0] 是字符。 *args[0] 是什么?

  2. 您必须创建一个以 NULL 结尾的 char* 数组,作为第二个参数传递。

  3. args[0]args 中的第一个字符。您应该传递整个字符串(仅 args),而不仅仅是其第一个字符。

尝试类似的方法:

char *argv[]={args,NULL};
execvp(args,argv);

You have several problems:

  1. *args[0] is meaningless. args is array. args[0] is char. what is *args[0]?

  2. You have to create a NULL-terminated array of char*, to pass as 2nd argument.

  3. args[0] is the first char in args. you should pass the whole string (just args), not only its first char.

Try something like:

char *argv[]={args,NULL};
execvp(args,argv);
唠甜嗑 2025-01-12 01:17:48

这可能更适合您:

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    char args[128];
    char *argv[] = { "sh", "-c", args, 0 };
    printf("> ");
    if (fgets(args, 128, stdin) != 0)
    {
        execvp(argv[0], argv);
        fprintf(stderr, "Failed to exec shell on %s", args);
        return 1;
    }
    return 0;
}

它具有最少的必要标头;它有一个正确声明的 main() - C99 需要显式返回类型;它根据用户输入的信息运行 shell。除非用户在按回车键之前输入了超过 126 个字符,否则错误消息会正确地以换行符终止。如果 execvp() 或任何 exec*() 函数返回,则失败;您不需要测试其状态。

我通过让 shell 做真正的工作来进行惊人的作弊。但您最终可能希望将用户输入的内容拆分为单词,以便命令位于第一个并且有多个参数。然后,您分配一个更大的 argv 数组,并解析字符串,将每个单独的参数放入 argv 中自己的条目中,然后使用 execvp() 开始有意义了。请注意,如果需要完成 I/O 重定向,则必须由您的 shell 来执行此操作(除非您运行真正的 shell 来为您执行此操作 - 就像我所做的那样)。

This might work better for you:

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    char args[128];
    char *argv[] = { "sh", "-c", args, 0 };
    printf("> ");
    if (fgets(args, 128, stdin) != 0)
    {
        execvp(argv[0], argv);
        fprintf(stderr, "Failed to exec shell on %s", args);
        return 1;
    }
    return 0;
}

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. If execvp() or any of the exec*() 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 in argv and then using execvp() 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).

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