-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiping.c
More file actions
55 lines (44 loc) · 1.21 KB
/
piping.c
File metadata and controls
55 lines (44 loc) · 1.21 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
#include "headers.h"
void piping(char *command){
char *sub_command[1000];
ll n_command = 0;
n_command = split_by(sub_command, command, "|");
// for(ll k = 0; k < n_command; k++){
// printf("sub-command %lld: \"%s\"\n", k, sub_command[k]);
// }
int fds[2], pid;
int input = 0;
for(ll k = 0; k < n_command; k++){
pipe(fds);
pid = fork();
if(pid == -1){
printf("\033[0;31mError: Unable to fork\033[0m\n");
perror("fork");
return;
}
else if(pid == 0){
dup2(input, 0);
if(k+1 != n_command){
dup2(fds[1], 1);
}
close(fds[0]);
//*** Check Redirection ***
int redirect;
if(strstr(sub_command[k], ">") != NULL || strstr(sub_command[k], "<") != NULL ){
redirect = 1;
}
if(redirect == 1){
redirection(sub_command[k]);
}
else{
execute_command(sub_command[k]);
}
exit(1);
}
else{
wait(NULL);
close(fds[1]);
input = fds[0];
}
}
}