- 论坛徽章:
- 0
|
- #include "apue.h"
- #include "my_err.h"
- #include <sys/wait.h>
- int main()
- {
- char buf[MAXLINE];
- pid_t pid;
- int status;
- printf("%% ");
- while(fgets(buf,MAXLINE,stdin) != NULL)
- {
- if(buf[strlen(buf)-1]=='\n')
- buf[strlen(buf)-1]=0;
- if(pid=fork()<0)
- {err_sys("fork error");}
- else if(pid==0){
- execlp(buf,buf,(char *)0);
- err_ret("couldnot execute:%s",buf);
- exit(127);
- }
- if(pid=waitpid(pid,&status,0))
- err_sys("waitpid error");
- printf("%% ");
- }
- }
复制代码 [root@localhost redhat]# ./a.out
% date
三 3月 13 14:04:26 CST 2013
三 3月 13 14:04:26 CST 2013
|
|