Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在Linux中创建简单Shell_C_Arrays_Linux_Shell_Strtok - Fatal编程技术网

C 在Linux中创建简单Shell

C 在Linux中创建简单Shell,c,arrays,linux,shell,strtok,C,Arrays,Linux,Shell,Strtok,我正在做一项任务,用C语言创建一个非常简单的Linux shell,它几乎完全按照我所希望的方式工作 如果用户输入一个简单的Linux命令,程序将运行它并循环以允许另一个命令。如果用户输入“退出”,程序将退出 我的问题是命令只在第一次起作用。之后,它们似乎以某种方式变得格式不正确。有没有办法重新初始化我的args数组,以便它能够正确地接收新的输入 int main() { char* args[50]; // Argument array. char userI

我正在做一项任务,用C语言创建一个非常简单的Linux shell,它几乎完全按照我所希望的方式工作

如果用户输入一个简单的Linux命令,程序将运行它并循环以允许另一个命令。如果用户输入“退出”,程序将退出

我的问题是命令只在第一次起作用。之后,它们似乎以某种方式变得格式不正确。有没有办法重新初始化我的args数组,以便它能够正确地接收新的输入

int main() {
    char* args[50];          // Argument array.
    char userInput[200];     // User input.
    char* userQuit = "quit"; // String to be compared to user input to quit program.
    int pid;                 // Process ID for fork().
    int i = 0;               // Counter.

    while(1) {
        // Promt and get input from user.
        printf("minor5> ");
        fgets(userInput, sizeof(userInput), stdin);

        // Pass userInput into args array.
        args[0] = strtok(userInput, " \n\0");

        // Loop to separate args into individual arguments, delimited by either space, newline, or NULL.
        while(args[i] != NULL) {
            i++;
            args[i] = strtok(NULL, " \n\0");
        }

        // If the first argument is "quit", exit the program.
        if(strcmp(args[0], userQuit) == 0) {
            printf("Exiting Minor5 Shell...\n");
            exit(EXIT_SUCCESS);
        }

        // Create child process.
        pid = fork();

        // Parent process will wait for child to execute.
        // Child process will execute the command given in userInput.
        if(pid > 0) {
            // Parent //
            wait( (int *) 0 );
        } else {
            // Child //
            int errChk;
            errChk = execvp(args[0], args);

            if(errChk == -1) {
                printf("%s: Command not found\n", userInput);
            }
        }
    }

    return 0;
}
您需要确保
args
的最后一个值为空。它可能有一个在第一个命令,偶然,但没有保证

这是一个经过修改的解析循环片段[请原谅这种不必要的风格清理]:

// Pass userInput into args array.
char *uptr = userInput;

i = 0;
while (1) {
    char *token = strtok(uptr, " \n");
    uptr = NULL;

    if (token == NULL)
        break;

    args[i++] = token;
}

// NOTE: this is the key missing ingredient from your code
args[i] = NULL;

抱歉,但是为什么您认为他的代码中的
args
不能保证有
NULL
最后一个值?这个条件
while(args[i]!=NULL)
保证了这一点,不是吗?另一件事是他没有将自己的
i
重置为0。这段代码在第二次尝试输入命令时导致了分段错误。@mangusta谢谢你。我没有注意到我忘了重置计数器。这就是问题的症结所在。它现在工作得很好!谢谢。@mangusta不,没有。提取OP的代码并运行它。Do:
ls/etc/passwd
。然后执行:
vi
[不带任何参数]。您将看到它被传递:
vi”“
,而不是无参数。请尝试使用手册
vi
作为参考