gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:04 25 4
gpt4 key购买 nike

我正在编写一个程序来用 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");
}

}
}

我在 cygwin 上执行,因此生成了一个 .exe。

程序的执行

$ ./a.exe
My shell>> ls
ls

a.exe p3.4.cpp p3.6.cpp p3.6.cpp~ p3.7.cpp p3.7.cpp~ p3.cpp shell.c shell.c~ test.txt
Child exited
My shell>> cat test.txt
cat.txt

Child process could not do execvp
My shell>> exit
exit

Child process could not do execvp
My shell>> exit()
exit()

Child process could not do execvp
My shell>> exit
exit

Child process could not do execvp
My shell>> exit
exit

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

最佳答案

您可能将删除新行与处理 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==0line[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';
}

关于c - 在 C 中编写一个 shell 我的程序无法退出(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52025177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com