#include main() { char buf[1024]; char *args[64]; for (;;) { /* Prompt for and read a command */ printf("Command: "); if (gets(buf) == NULL) { printf("\n"); exit(0); } /* Split the string into arguments */ parse(buf, args); /* Execute the command */ execute(args); } } parse(char *buf, char **args) { while(*buf != NULL) { /* Strip whitespaces. Use nulls, so that the previous argument is terminated automaticaly. */ while((*buf == ' ') || (*buf == '\t')) *buf++ = NULL; /* Save the argument */ *args++ = buf; /* Skip over the argument */ while((*buf != NULL) && (*buf != ' ') && (*buf != '\t')) buf++; } *args = NULL; } execute(char **args) { int pid, status; /* Get a child process */ if((pid = fork()) < 0) { perror("fork"); exit(1); } /* The child executes the code inside the if */ if(pid == 0) { execvp(*args, args); perror(*args); exit(1); } /* The parrent executes the wait */ while(wait(&status) != pid) /* empty */; }