Tuesday, August 11, 2009

a simple shell in c

#include
#include
#include
#include

#define BUFSIZ 1024


int main(void) {
char command[BUFSIZ];
int status;
pid_t pid;

for(;;) {
printf("simpsh: ");

if(fgets(command, sizeof(command), stdin) == NULL) {
printf("\n");
return 0;
}

command[strlen(command) - 1] = '\0';

if((pid = fork()) == 0)
execlp(command, command, 0);

// parent do signal(SIGCHLD, SIGIGN); ignore the children exits
// or call waitpid()
// or use daemon: parent call : exit(0); --> background & instead
while(wait(&status) != pid)
continue;

printf("\n");
}
}

No comments:

Post a Comment