繁体   English   中英

简单的Linux Shell-execvp()失败

[英]Simple Linux Shell - execvp() failing

我需要一个用于类的简单shell的帮助,而且我担心我不太了解execvp()函数的工作方式。

Shell并没有做很多事情,不支持管道,重定向,脚本或类似的东西。 它仅读取命令,读取选项(命令为option [0]),然后派生。

它工作了几次,然后开始给我有关无法找到命令的错误。 此处发布的其他类似问题与管道或重定向有关。

请原谅noobcode,它虽然不漂亮,但我希望它清晰易读:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

#define OPT_AMT 10

const size_t SIZE = 256;
int i = 0;
int o = 0;

int main(void) {

    // initializing data

    int exit = 0;
    char cwd[SIZE];
    char cmd[SIZE];
    char input[SIZE * OPT_AMT];
    char *opt[OPT_AMT];
    getcwd(cwd, SIZE);

    // main loop

    while (exit == 0) {
        // reset everything
        o = 1;
        i = 0;
        cmd[0] = "\0";
        while (i < OPT_AMT) {
            opt[i++] = "\0";
        }

        // get input
        printf("%s $ ", cwd);
        scanf("%s", cmd);
        gets(input);
        opt[0] = cmd;

        char *t = strtok(input, " ");
        while (t != NULL) {
            opt[o++] = t;
            t = strtok(NULL, " ");
        }

        // if exit, exit
        if (strcmp(cmd, "exit") == 0) {
            exit = 1;
        }
        // else fork and execute
        else {
            pid_t pID = fork();

            if (pID == 0) { // child process
                execvp(cmd, opt);
            } else if (pID < 0) { // failed to fork
                printf("\nFailed to fork\n");
            } else { // parent process
                wait(0);
            }
        }
    }

    // cleanup

    printf("\nFinished!  Exiting...\n");
    return 0;
}

有什么公然的错误吗? 我最近添加了退出条件和重置options数组。

另外,这是我的第一个问题,因此请提醒我可能违反的任何规则。

对于初学者,这

cmd[0] = "\0";

必定是

cmd[0] = '\0';

听您的编译器的警告。

要启用它们,请使用-Wall -Wextra -pedantic选项(对于gcc)。


另外,您可能最好将opt的元素初始化为指向“ nothing”,即NULL但指向文字"\\0"

    while (i < OPT_AMT) {
        opt[i++] = NULL;
    }

因为execvp()要求optNULL终止的C-“字符串”数组(感谢Paul提及/措辞相关背景)。


另外^ 2:不要使用gets() ,因为它很邪恶,甚至不再是C标准的一部分。 代替

gets(input);

采用

fgets(input, sizeof input, stdin);

gets()容易让用户使传递的(输入)缓冲区溢出。 (想到这一点,我没有保罗,顺便说一句; ;-))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM