#include #include #include #include #include #include #include #include int main(){ char *token; char command[80];; char **arglist, *str, *strl; int i,j,n,val,status; printf( "At the prompt enter a command; Enter an empty line to quit\n"); while(1){ printf( "--->"); fgets(command, 80,stdin); //printf("input string is: %s\n",command); //Determine number of command line entries if (strlen(command) < 2) break; if (command[strlen(command)-2] == ' ') n=0; else n=1; strl=command-1; str=command; while ((str=index(str,' '))!=NULL){ //printf("str=%s,strl=%s\n",str,strl); if ((strl+1 < str) && (*str != '\n')){ n++; //printf("------%d\n",n); } strl=str; str=str+1; } n++; //add space for terminating 0 //printf("n=%d\n",n); if (!(val=fork())){ //parse string token = strtok(command, " "); i=0; arglist=(char **)malloc(n*sizeof(char*)); while(token!=NULL){ if ('\n' != *token){ arglist[i++]=token; //printf("token=%s\n",token); token=strtok(NULL," "); } else break; } arglist[i]=0; assert(i<=n); //printf("n=%d,i=%d\n",n); //printf("The parsed string is\n"); //for (j=0;j0)){ printf("child process %d exited with status = %d\n",val,status); fflush(stdout); } usleep(50000); } while((val=waitpid(0,&status,WNOHANG)>0)){ printf("child process %d exited with status = %d\n",val,status); fflush(stdout); } }