我正在編寫一個迷你shell來更熟悉C中的Unix進程管理。它從命令行讀取內容並通過execlp將這些參數傳遞給系統。strtok和execlp在迷你shell中
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
#define MAXSIZE 100
char prompt[MAXSIZE];
int main(void)
{
pid_t pid;
printf("> ");
// read stuff
if (fgets(prompt, MAXSIZE, stdin) == NULL){
printf("Input validation error!");
abort();
}
// printf("DEBUG: %s" , prompt);
if (strcmp(prompt, "exit")==0) abort();
if ((pid=fork())<0){ // copy process
printf("Process error!");
abort();
}
if (pid==0){ // exec in son-prcess
char *command=(char*)strtok(prompt, " ");
execlp(command, command, 0); // overwrite memory
printf("Error, command not found!");
abort();
} else {
waitpid(pid, 0, 0);
}
}
其實這就是它,但我沒有從execlp()
得到任何輸出。 有人知道這是爲什麼嗎?
是否顯示「Error,command not found!」打印或不打印? – atomice 2009-10-30 17:00:59