-
Notifications
You must be signed in to change notification settings - Fork 6
/
2017-IN-01.c
242 lines (192 loc) · 5.01 KB
/
2017-IN-01.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <err.h>
/*
cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n
*/
int main(void) {
int a[2];
if (pipe(a) == -1) {
err(1, "pipe failed");
}
pid_t pid;
if ((pid = fork()) == -1) {
err(2, "first fork failed");
}
if (pid == 0) {
close(a[0]);
//close(1);
//dup(a[1]);
if (dup2(a[1], 1) == -1) {
err(3, "cut dup failed");
}
if (execlp("cut", "cut", "-d:", "-f7", "/etc/passwd", (char *) NULL) == -1) {
err(4, "cut exec failed");
}
} else {
close(a[1]);
}
int b[2];
if (pipe(b) == -1) {
err(4, "Second pipe failed");
}
if ((pid = fork()) == -1) {
err(5, "sort fork failed");
}
if (pid == 0) {
close(b[0]);
if (dup2(a[0], 0) == -1) {
err(6, "error dup read end sort");
}
if (dup2(b[1], 1) == -1) {
err(6, "error dup write end sort");
}
execlp("sort", "sort", (char *) NULL);
} else {
close(b[1]);
}
int c[2];
if (pipe(c) == -1) {
err(6, "error pipe for uniq");
}
if ((pid = fork()) == -1) {
err(7, "error forking uniq");
}
if (pid == 0) {
close(c[0]);
if (dup2(b[0], 0) == -1) {
err(9, "error dup read end uniq");
}
if (dup2(c[1], 1) == -1) {
err(10, "error dup read end uniq");
}
if (execlp("uniq", "uniq", "-c", (char *) NULL) == -1) {
err(11, "error exec uniq");
}
} else {
close(c[1]);
}
close(a[0]);
close(b[0]);
while (wait(NULL) > 0);
if (dup2(c[0], 0) == -1) {
err(11, "error dup read end sort -n");
}
if (execlp("sort", "sort", "-n", (char *) NULL) == -1) {
err(6, "error exec sort -n");
}
return 0;
}
#v2
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <sys/wait.h>
int ps[3][2];
void close_all(void);
void dup2_safe(int fd1, int fd2);
void pipe_safe(int fd_num);
pid_t fork_safe(void);
void wait_child(void);
void wait_child(void) {
int status;
if (wait(&status) == -1) {
err(4, "Could not wait for child process to finish");
}
if (!WIFEXITED(status)) {
errx(5, "Child process did not terminate normally");
}
if (WEXITSTATUS(status) != 0) {
errx(6, "Child process finished with exit code not 0");
}
}
pid_t fork_safe(void) {
pid_t child = fork();
if (child == -1) {
close_all();
err(2, "Could not fork");
}
return child;
}
void pipe_safe(int fd_num) {
if (pipe(ps[fd_num]) == -1) {
close_all();
err(1, "Could not pipe");
}
}
void close_all(void) {
int errno_ = errno;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (ps[i][j] >= 0) {
close(ps[i][j]);
}
}
}
errno = errno_;
}
void dup2_safe(int fd1, int fd2) {
if (dup2(fd1, fd2) == -1) {
close_all();
err(4, "Could not dup2");
}
}
//cut -d ':' -f7 /etc/passwd | sort | uniq -c | sort -n
int main(void) {
pipe_safe(0);
pid_t child = fork_safe();
if (child == 0) {
close(ps[0][0]);
dup2_safe(ps[0][1], 1);
if (execlp("cut", "cut", "-d", ":", "-f", "7", "/etc/passwd", (char *) NULL) == -1) {
close_all();
err(3, "Could not execlp cut");
}
}
wait_child();
close(ps[0][1]);
pipe_safe(1);
child = fork_safe();
if (child == 0) {
close(ps[1][0]);
dup2_safe(ps[0][0], 0);
dup2_safe(ps[1][1], 1);
if (execlp("sort", "sort", (char *) NULL) == -1) {
close_all();
err(3, "Could not execlp sort");
}
}
wait_child();
close(ps[1][1]);
pipe_safe(2);
child = fork_safe();
if (child == 0) {
close(ps[2][0]);
dup2_safe(ps[2][1], 1);
dup2_safe(ps[1][0], 0);
if (execlp("uniq", "uniq", "-c", (char *) NULL) == -1) {
close_all();
err(3, "Could not execlp uniq");
}
}
wait_child();
close(ps[2][1]);
dup2_safe(ps[2][0], 0);
if (execlp("sort", "sort", "-n", (char *) NULL) == -1) {
close_all();
err(3, "Could not sort");
}
close_all();
exit(0);
}
//Напишете програма на C, която използвайки външни shell команди през pipe() да
// извежда статистика за броя на използване на различните shell-ове от потребителите, дефинирани
// в системата. Изходът да бъде сортиран във възходящ ред според брой използвания на shell-овете.
//Примерно извикване и изход:
//$ ./main
//1 /bin/sync
//3 /bin/bash
//7 /bin/false
//17 /usr/sbin/nologin