在C中编写shell我的程序无法退出()

在C中编写shell我的程序无法退出(),c,linux,bash,shell,cygwin,C,Linux,Bash,Shell,Cygwin,我正在编写一个程序,用C实现一个简单的shell 但当我在最后执行程序时,当我键入exit时,它不会退出 #include <stdio.h> #include <stdlib.h> #include <string.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #define BUFFER_LEN 1024 int main() {

我正在编写一个程序,用C实现一个简单的shell 但当我在最后执行程序时,当我键入exit时,它不会退出

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#define BUFFER_LEN 1024

int main()
{
        char line[BUFFER_LEN];  //get command line
        char *argv[100];        //user command
        char *path = "/bin/";   //set path at bin
        char progpath[20];      //full file path
        int argc;               //arg count

        while (1) {

                printf("My shell>> ");  //print shell prompt

                if (!fgets(line, BUFFER_LEN, stdin)) {  //get command and put it in line
                        size_t length = strlen(line);
                        if (line[length - 1] == '\n')
                        line[length - 1] = '\0';
                        break;  //if user hits CTRL+D break
                }
                if (strcmp(line, "exit") == 0) {        //check if command is exit
                        exit(0);
                        break;
                }

                char *token;    //split command into separate strings
                token = strtok(line, " ");
                int i = 0;
                while (token != NULL) {
                        argv[i] = token;
                        token = strtok(NULL, " ");
                        i++;
                }
                argv[i] = NULL; //set last value to NULL for execvp

                argc = i;       //get arg count
                for (i = 0; i < argc; i++) {
                        printf("%s\n", argv[i]);        //print command/args
                }
                strcpy(progpath, path); //copy /bin/ to file path
                strcat(progpath, argv[0]);      //add program to path

                for (i = 0; i < strlen(progpath); i++) {        //delete newline
 if (progpath[i] == '\n') {
                                progpath[i] = '\0';
                        }
                }
                int pid = fork();       //fork child

                if (pid == 0) { //Child
                        execvp(progpath, argv);
                        fprintf(stderr, "Child process could not do execvp\n");

                } else {        //Parent
                        wait(NULL);
                        printf("Child exited\n");
                }

        }
}
在这里,您可以在上面的输出中看到,当我在命令提示符下键入退出时,程序无法退出。我在上面的程序中犯了什么错误

strcmp(行“退出”\n“==0


如果不包含新行(\n),会发生什么情况?

您可能将删除新行与处理CTRL-D混合在一起。您的代码

if (!fgets(line, BUFFER_LEN, stdin)) {  //get command and put it in line
                    size_t length = strlen(line);
                    if (line[length - 1] == '\n')
                    line[length - 1] = '\0';
                    break;  //if user hits CTRL+D break
}
仅当
fgets
失败(例如,已到达文件末尾)时,才删除新行。 此外,如果
length==0,则
line[length-1]
将产生未定义的行为

我将替换逻辑:

if (!fgets(line, BUFFER_LEN, stdin)) {  //get command and put it in line
   break;  //if user hits CTRL+D break
} else {
   line[strcspn(line, "\n")] = '\0';
}

我已编辑了问题我正在尝试的代码中没有退出\n您从
fgets
中删除换行符的逻辑有缺陷。当
fgets
失败时,尝试删除换行符!如果
strlen
恰好返回
0
(不寻常但并非不可能),也要小心。好的,我刚刚做了一个
if(fgets(line,BUFFER_LEN,stdin)){//get命令,并将它放在第行//size_t length=strlen(line);//如果(line[length-1]='\n')//line[length-1]='\0';//break;//如果用户点击CTRL+D break}if(strcmp(第行,“exit”)==0){//检查命令是否为exit exit(0);break;}
但即使这样,程序也无法退出,甚至无法识别像ls-alYes这样的选项。感谢它的工作,它解决了我遇到的其他问题,但我在这里没有提到。
if (!fgets(line, BUFFER_LEN, stdin)) {  //get command and put it in line
   break;  //if user hits CTRL+D break
} else {
   line[strcspn(line, "\n")] = '\0';
}