-1
當前當我點擊ctrl+d
時,它會一直重複打印> ERROR
,直到我暫停程序(ctrl+z
)。我嘗試過各種方法來解決這個問題,但它以其他方式破壞了程序。程序未在EOF上停止
int main()
{
char *command;
char **parameters;
int status;
size_t buffsize = 0;
while(1)
{
command = NULL;
printf("> ");
getline(&command, &buffsize, stdin);
command[strlen(command)-1] = '\0';
parameters = tokenize(command);
if (!strcmp(command, "exit"))
{
exit(1);
}
if (fork() != 0)
{
waitpid(-1, &status, 0);
}
else
{
status = execvp(command, parameters);
if (status == -1)
{
printf("ERROR\n");
exit(1);
}
}
free(command);
}
return 0;
}
編輯:這是修復。謝謝你jil
if(getline(&command, &buffsize, stdin)) == -1) {
return 0;
}
在什麼時候你的程序檢查EOF? – user3386109
我曾嘗試添加if(command == EOF)'或'command == 0'''返回0',但它仍然不起作用。 – noname
tokenize()函數有什麼作用?除非調用它來修改字符串命令,否則最終會將完整命令作爲execvp的第一個參數傳遞,您應該將路徑傳遞給可執行文件。 – odelande