Thursday, May 1, 2008

Read commands from standard input and execute them

# include

# include

# include “ourhdr.h”

int

main(void)

{

char buf [MAXLINE];

pid_t pid;

int status;

printf(“ %%”);

while(fgets(buf, MAXLINE, stdin) ! = NULL)

{

buf[strlen(buf) -1] = 0;

if( (pid=ford( ) ) <>

err_sys (“fork error”);

else if(pid == 0)

{

execlp(buf, buf, (char *) 0);

err_ret (“couldn’t execute: #s”, buf);

exit(127);

}

if( (pid=waitpid(pid, &status, 0) ) <>

err_sys(“waitpid error”);

printf(“%%”);

}

exit (0);

}

No comments: