- 论坛徽章:
- 0
|
#include <sys/wait.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define MAXLINE 80
int main(void)
{
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) {
printf("fork error!");
} else if(pid == 0){ /*程序愿意应该是在子进程执行该命令吧*/
execlp(buf, buf, (char*) (0));
printf("%s could not exec! ", buf); /*加个%s*/
// exit(127); /*这条命令让程序退出*/
}
if ((pid = waitpid(pid, &status, 0)) < 0) {
printf("waitpid error!");
}
printf(" %%");
}
exit(0);
}
|
|