-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipe.c
279 lines (242 loc) · 7.4 KB
/
pipe.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "shell.h"
#include "utils.h"
#include "parse.h"
#include "redirection.h"
#include "pipe.h"
size_t count_pipes(const char* command) {
size_t len = strlen(command);
size_t count = 0;
for (size_t i = 0; i < len; i++) {
if (command[i] == '|') {
count++;
}
}
return count;
}
char** break_pipes(char* str) {
// breaks the command by '|' and returns them as an array
// does not check for whitespace only, or empty commands
// does not strip whitespace
// dynamically allocates new memory, must be freed by caller
// uses strtok on original pointer, renders it unusable, but can be freed
size_t num_pipes = count_pipes(str);
size_t num_commands = num_pipes + 1;
char** commands = (char**)malloc(sizeof(char*) * (num_commands + 1));
commands[num_commands] = NULL;
size_t i = 0;
char* next = strtok(str, "|");
while (next != NULL) {
commands[i] = (char*)malloc(sizeof(char) * (strlen(next) + 1));
strcpy(commands[i], next);
i++;
next = strtok(NULL, "|");
}
return commands;
}
void pied_piper_fork(char* str) {
// runs each command from the pipeline in a new fork
if (str == NULL) {
return;
}
size_t num_pipes = count_pipes(str);
size_t num_commands = num_pipes + 1;
if (num_commands == 1) {
parse_command(str);
return;
}
char** commands = break_pipes(str); // DONOT USE STR AFTER THIS
int pipe_a[2];
int pipe_b[2];
for (int i = 0; i < num_commands; i++) {
if (i & 1) {
if (i < num_commands - 1) {
pipe(pipe_b);
}
pid_t pid = fork();
if (pid < 0) {
// IN PARENT, FORK FAILED
perror("Could not create pipeline");
for (int j = 0; j < num_commands; j++) {
free(commands[j]);
}
free(commands);
return;
} else if (pid > 0) {
// IN PARENT
if (i > 0) {
close(pipe_a[0]);
}
if (i < num_commands - 1) {
close(pipe_b[1]);
}
waitpid(pid, NULL, 0);
} else {
// IN CHILD
if (i > 0) {
// close(pipe_a[1]);
dup2(pipe_a[0], STDIN_FILENO);
}
if (i < num_commands - 1) {
// close(pipe_b[0]);
dup2(pipe_b[1], STDOUT_FILENO);
}
parse_command(commands[i]);
exit(0);
}
} else {
if (i < num_commands - 1) {
pipe(pipe_a);
}
pid_t pid = fork();
if (pid < 0) {
// IN PARENT, FORK FAILED
perror("Could not create pipeline");
for (int j = 0; j < num_commands; j++) {
free(commands[j]);
}
free(commands);
return;
} else if (pid > 0) {
// IN PARENT
if (i > 0) {
close(pipe_b[0]);
}
if (i < num_commands - 1) {
close(pipe_a[1]);
}
waitpid(pid, 0, WUNTRACED);
} else {
// IN CHILD
if (i > 0) {
// close(pipe_b[1]);
dup2(pipe_b[0], STDIN_FILENO);
}
if (i < num_commands - 1) {
// close(pipe_a[0]);
dup2(pipe_a[1], STDOUT_FILENO);
}
parse_command(commands[i]);
exit(0);
}
}
}
for (int j = 0; j < num_commands; j++) {
free(commands[j]);
}
free(commands);
return;
}
void pied_piper(char* str) {
// does not fork a child for each command in the pipeline
// The pipe does not stop if a command in between fails.
if (str == NULL) {
return;
}
size_t num_pipes = count_pipes(str);
size_t num_commands = num_pipes + 1;
if (num_commands == 1) {
parse_command(str);
return;
}
char* temp;
char* next = strtok_r(str, "|", &temp);
if (next == NULL)
return;
char* command = (char*)malloc(sizeof(char) * (strlen(next) + 1));
strcpy(command, next);
int** pipes = (int**)malloc(sizeof(int*) * num_pipes);
for (size_t j = 0; j < num_pipes; j++) {
pipes[j] = (int*)malloc(sizeof(int) * 2);
}
int stdout_copy = dup(STDOUT_FILENO);
if (stdout_copy < 0) {
perror("Could not create pipeline");
free(command);
for (size_t j = 0; j < num_pipes; j++) {
free(pipes[j]);
}
free(pipes);
return;
}
if (pipe(pipes[0]) < 0) {
perror("Could not create pipeline");
free(command);
for (size_t j = 0; j < num_pipes; j++) {
free(pipes[j]);
}
free(pipes);
close(stdout_copy);
return;
}
if (dup2(pipes[0][1], STDOUT_FILENO) < 0) {
perror("Could not create pipeline");
free(command);
close(pipes[0][0]);
close(pipes[0][1]);
for (size_t j = 0; j < num_pipes; j++) {
free(pipes[j]);
}
free(pipes);
close(stdout_copy);
return;
}
parse_command(command);
close(pipes[0][1]);
int stdin_copy = dup(STDIN_FILENO);
if (stdin_copy < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
size_t i = 0;
while ((next = strtok_r(NULL, "|", &temp)) != NULL) {
command = (char*)realloc(command, sizeof(char) * (strlen(next) + 1));
strcpy(command, next);
i++; // i refers to 0 indexed command number
if (i == num_commands - 1) {
break; // command already copied here
}
if (pipe(pipes[i]) < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
if (dup2(pipes[i - 1][0], STDIN_FILENO) < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
if (dup2(pipes[i][1], STDOUT_FILENO) < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
parse_command(command);
close(pipes[i - 1][0]);
close(pipes[i][1]);
}
if (dup2(pipes[i - 1][0], STDIN_FILENO) < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
if (dup2(stdout_copy, STDOUT_FILENO) < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
parse_command(command);
if (dup2(stdin_copy, STDIN_FILENO) < 0) {
perror("Could not create pipeline");
fprintf(stderr, "Fatal I/O error. Exiting...");
goodbye();
}
close(pipes[i - 1][0]);
close(stdin_copy);
close(stdout_copy);
free(command);
for (size_t j = 0; j < num_pipes; j++) {
free(pipes[j]);
}
free(pipes);
}