1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <string.h> 5 #define MAXLINE 10 6 int main(void) 7 { 8 char buf[MAXLINE]; 9 pid_t pid; 10 int status; 11 printf("%%"); 12 while(fgets(buf,MAXLINE,stdin)!=NULL){ 13 if(buf[strlen(buf)-1] == ‘\n‘) 14 buf[strlen(buf)-1] = 0; 15 if (( pid = fork()) < 0 ) 16 printf("fork error\n"); 17 else if (pid == 0){ 18 execlp(buf,buf,(char *)0); 19 printf("could not execute : %s\n",buf); 20 exit(127); 21 } 22 if((pid = waitpid(pid,&status,0))<0) 23 printf("waitpid error\n"); 24 } 25 }
时间: 2024-10-18 21:57:55