/*
* DESCRIPTION: process control that simulate a simple shell;
* three function : fork(), exec(), waitpid();
* author:liy
* DATE:8-23-2006
*/
#include "apue.h"
#include <sys/wait.h>
int main(void)
{
char buf[MAXLINE]; /* MAXLINE from apue.h */
pid_t pid;
int status;
puts("###########A simple shell##########");
printf("%%"); /* simulate a dos prompt */
while(fgets(buf, MAXLINE, stdin) != NULL){
if(buf[strlen(buf) - 1] == '/n'){
buf[strlen(buf) - 1] = 0; /* replace new line null */
}
if((pid = fork()) < 0){
err_sys("fork error");
}
else if(pid == 0){ /* child */
execlp(buf, buf, (char *)0);
err_ret("Could'n execute %s", buf);
exit(127);
}
/* parent */
if((pid = waitpid(pid, &status, 0)) < 0){
err_sys("watipid error");
}
printf("%%");
}
puts("bye ^_^");
exit(0);
}