forked from WesleyGlover/Linux-Shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
439 lines (379 loc) · 19.1 KB
/
pipe.c
File metadata and controls
439 lines (379 loc) · 19.1 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "major2.h"
enum { READ, WRITE };
int pipeCheck(char * command)
{
int i = 0;
int counter = 0;
// Looping through the command to find any '|'
while (command[i] != '\0')
{
// If '|' is found, increment the counter
if (command[i] == '|')
{
counter++;
}
// Leaving loop if there is > or < involved. NOT COMPATABLE
if ((command[i] == '>' || command[i] == '<') && counter > 0)
{
return -1;
}
i++;
}
// return the counter
return counter;
}
void pipeRun(char* input, int counter)
{
// Splitting the command up by the '|' into its own commands
char* argvs[500] = {NULL};
char* token1;
int i = 0;
token1 = strtok(input, "|");
while(token1 != NULL)
{
argvs[i] = token1;
token1 = strtok(NULL, "|");
i++;
}
int isOnlyWhitespace; // boolean flag to signal if the command is just whitespace
char* args[500] = { NULL };
char* command;
int status;
// If there are two pipes needed
if (counter == 2)
{
char* command1[500] = {NULL};
char* command2[500] = {NULL};
char* command3[500] = {NULL};
// Creating pipes
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
// Looping through commands to execute
for (i = 0; i < counter+1; i++)
{
// IF end of arguments, leave loop
if (argvs[i] == NULL)
break;
// Save the next argument into command
command = argvs[i];
// White space checking
for(int j = 0; j < strlen(command) - 1; j++) // Looop to check if the command entered is just whitespaces
{
if(command[j] != ' ') // If we encounter a character that is not whitespace, break from this loop
{
isOnlyWhitespace = 1;
break;
}
else // otherwise, signal the command is empty
{
isOnlyWhitespace = 0;
}
}
if(isOnlyWhitespace == 0) // if the command is empty, move to next loop iteration
{
return;
}
// Splitting up words to send the command properly to the console
if(!(strcmp(command, "") == 0))
{
char* token; // Token for splitting the command into its pieces
char character;
int hasMark = 0;
int k = 0; // iteration variable
int l = 0;
int temp1 = 0;
char* temp2;
for (int l = 0; l < sizeof(command); l++)
{
character = command[l];
if(character == '\'')
hasMark = 1;
}
if (hasMark == 1)
{
token = strtok(command, " "); // Get the first item of the command inside token
// printf("Printing Command from args: ");
while(token != NULL)
{
switch (i)
{
case 0:
command1[k] = token;
k++;
token = strtok(NULL, "\'\'");
command1[k] = token;
break;
case 1:
command2[k] = token;
k++;
token = strtok(NULL, "\'\'");
command2[k] = token;
break;
case 2:
command3[k] = token;
k++;
token = strtok(NULL, "\'\'");
command3[k] = token;
break;
default:
printf("Switch Not working\n");
break;
}
token = strtok(NULL, "\'");
k++;
}// END of while(token != NULL)
}
else
{
token = strtok(command, " "); // Get the first item of the command inside token
// printf("Printing Command from args: ");
while(token != NULL)
{
args[k] = token; // Set the index i of args to be the current token
switch (i)
{
case 0:
command1[k] = token;
break;
case 1:
command2[k] = token;
break;
case 2:
command3[k] = token;
break;
}
token = strtok(NULL, " "); // Go to the next token inside the command string
// printf("%s\n", args[k]);
k++;
} // END of while(token != NULL)
} // END OF else
} // END of if(!(strcmp(command, "") == 0)
} // END of for (i = 0; i < 500; i++)
// Forking into child to execute command
if (fork() == 0) // CHILD 1
{
if(tcsetpgrp(0, getpgid(0)) == -1) // Send the child program to the foreground
exit(0);
// Making WRITE end of fd1 the standard out
if(dup2(fd1[WRITE], WRITE) == -1)
{
printf("Error Opening pipe for writing\n");
exit(0);
}
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
close(fd2[READ]);
close(fd2[WRITE]);
// Executing command
execvp(command1[0], command1);
}
else // PARENT
{
if (fork() == 0) // CHILD 2
{
if(tcsetpgrp(0, getpgid(0)) == -1) // Send the child program to the foreground
exit(0);
// Making the READ end of fd1 standard in
if(dup2(fd1[READ], READ) == -1)
{
printf("Error Opening pipe for reading\n");
exit(0);
}
// Making WRITE end of fd2 the standard out
if(dup2(fd2[WRITE], WRITE) == -1)
{
printf("Error Opening pipe for writing\n");
exit(0);
}
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
close(fd2[READ]);
close(fd2[WRITE]);
// Executing command
execvp(command2[0], command2);
}
else // PARENT
{
if(fork() == 0) // CHILD 3
{
if(tcsetpgrp(0, getpgid(0)) == -1) // Send the child program to the foreground
exit(0);
// Making the READ end of fd1 standard in
if(dup2(fd2[READ], READ) == -1)
{
printf("Error Opening pipe for reading\n");
exit(0);
}
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
close(fd2[READ]);
close(fd2[WRITE]);
// Executing command
execvp(command3[0], command3);
} // END OF CHILD 3
} // PARENT FROM CHILD 2
} // PARENT FROM CHILD 1
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
close(fd2[READ]);
close(fd2[WRITE]);
// Having parent wait for children to finish
for (i = 0; i < counter+1; i++)
{
wait(&status);
}
} // END of if (counter == 2)
else if (counter == 1)
{
char* command1[500] = {NULL};
char* command2[500] = {NULL};
// Craeting pipes
int fd1[2];
pipe(fd1);
// Looping through commands to execute
for (i = 0; i < counter+1; i++)
{
// IF end of arguments, leave loop
if (argvs[i] == NULL)
break;
// Save the next argument into command
command = argvs[i];
// White space checking
for(int j = 0; j < strlen(command) - 1; j++) // Looop to check if the command entered is just whitespaces
{
if(command[j] != ' ') // If we encounter a character that is not whitespace, break from this loop
{
isOnlyWhitespace = 1;
break;
}
else // otherwise, signal the command is empty
{
isOnlyWhitespace = 0;
}
}
if(isOnlyWhitespace == 0) // if the command is empty, move to next loop iteration
{
return;
}
// Splitting up words to send the command properly to the console
if(!(strcmp(command, "") == 0))
{
char* token; // Token for splitting the command into its pieces
char character;
int hasMark = 0;
int k = 0; // iteration variable
int l = 0;
int temp1 = 0;
char* temp2;
for (int l = 0; l < sizeof(command); l++)
{
character = command[l];
if(character == '\'')
hasMark = 1;
}
if (hasMark == 1)
{
token = strtok(command, " "); // Get the first item of the command inside token
// printf("Printing Command from args: ");
while(token != NULL)
{
switch (i)
{
case 0:
command1[k] = token;
k++;
token = strtok(NULL, "\'\'");
command1[k] = token;
break;
case 1:
command2[k] = token;
k++;
token = strtok(NULL, "\'\'");
command2[k] = token;
break;
default:
printf("Switch Not working\n");
break;
}
token = strtok(NULL, "\'");
k++;
}// END of while(token != NULL)
}
else
{
token = strtok(command, " "); // Get the first item of the command inside token
// printf("Printing Command from args: ");
while(token != NULL)
{
args[k] = token; // Set the index i of args to be the current token
switch (i)
{
case 0:
command1[k] = token;
break;
case 1:
command2[k] = token;
break;
}
token = strtok(NULL, " "); // Go to the next token inside the command string
// printf("%s\n", args[k]);
k++;
} // END of while(token != NULL)
} // END OF else
} // END of if(!(strcmp(command, "") == 0)
} // END of for (i = 0; i < 500; i++)
// Forking into child to execute command
if (fork() == 0) // CHILD 1
{
if(tcsetpgrp(0, getpgid(0)) == -1) // Send the child program to the foreground
exit(0);
// Making WRITE end of fd1 the standard out
if(dup2(fd1[WRITE], WRITE) == -1)
{
printf("Error Opening pipe for writing\n");
exit(0);
}
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
// Executing command
execvp(command1[0], command1);
}
else // PARENT
{
if (fork() == 0) // CHILD 2
{
if(tcsetpgrp(0, getpgid(0)) == -1) // Send the child program to the foreground
exit(0);
// Making the READ end of fd1 standard in
if(dup2(fd1[READ], READ) == -1)
{
printf("Error Opening pipe for reading\n");
exit(0);
}
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
// Executing command
execvp(command2[0], command2);
}
} // PARENT FROM CHILD 1
// Closing pipes created
close(fd1[READ]);
close(fd1[WRITE]);
// Having parent wait for children to finish
for (i = 0; i < counter+1; i++)
{
wait(&status);
}
} // END of else if (counter == 1)
else // Error of too many pipes
{
fprintf(stderr, "Error: Too Many Pipes. Only Compatable Up to 2 Pipes.\n");
}
} // END of void pipeRun(char* input, int counter)