-
Notifications
You must be signed in to change notification settings - Fork 307
/
15.9.c
101 lines (80 loc) · 2.07 KB
/
15.9.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 1024
static void sig_pipe(int);
int main(void)
{
int n, fd1[2], fd2[2];
pid_t pid;
char line[MAXLINE];
if (signal(SIGPIPE, sig_pipe) == SIG_ERR) {
printf("signal error\n");
exit(-1);
}
if (pipe(fd1) < 0 || pipe(fd2) < 0) {
printf("pipe error\n");
exit(-1);
}
if ((pid = fork()) < 0) {
printf("fork error\n");
exit(-1);
} else if (pid > 0) {
close(fd1[0]);
close(fd2[1]);
while (fgets(line, MAXLINE, stdin) != NULL) {
n = strlen(line);
if (write(fd1[1], line, n) != n) {
printf("write error to pipe\n");
exit(-1);
}
if ((n = read(fd2[0], line, MAXLINE)) < 0) {
printf("read error to pipe\n");
exit(-1);
}
if (0 == n) {
printf("child closed pipe\n");
break;
}
line[0] = 0;
if (fputs(line, stdout) == EOF) {
printf("fputs error\n");
exit(-1);
}
}
if (ferror(stdin)) {
printf("fgets error to stdin\n");
exit(-1);
}
return 0;
} else {
close(fd1[1]);
close(fd2[0]);
if (fd1[0] != STDIN_FILENO) {
if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO) {
printf("dup2 error to stdin");
exit(-1);
}
close(fd1[0]);
}
if (fd2[1] != STDOUT_FILENO) {
if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO) {
printf("dup2 error to stdin");
exit(-1);
}
close(fd2[1]);
}
if (execl("./15.8" "15.8", (char *)0) < 0) {
printf("execl error\n");
exit(-1);
}
return 0;
}
}
static void sig_pipe(int signo)
{
printf("SIGPIPE caught\n");
exit(1);
}