-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy path1.6.3.c
More file actions
28 lines (23 loc) · 680 Bytes
/
1.6.3.c
File metadata and controls
28 lines (23 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "apue.h"
#include <sys/wait.h>
int main(int argc, char *argv[]) {
char buf[MAXLINE]; // apue.h 中定义 #define MAXLINE 4096
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("couldn't execute: %s", buf);
exit(127);
}
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}