-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
77 lines (73 loc) · 1.35 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
#include "headers.h"
#include "command.h"
int pip(char *comm){
char comCopy[2000];
int x;
strcpy(comCopy,comm);
char *pipToks[100];
for(x=0;x<100;x++){
pipToks[x] = (char *)calloc(2000, sizeof(char));
}
int pipCnt=0;
char *pipTok= strtok(comm,"|");
while(pipTok!=NULL){
strcpy(pipToks[pipCnt],pipTok);
pipCnt++;
pipTok = strtok(NULL,"|");
}
int fd[2];
pid_t childPid;
int s;
x=1;
for(int i=0;i<pipCnt;i++){
if(pipe(fd)<0){
perror("Pipe");
exitCode = -1;
break;
}
else{
exitCode = 5;
}
int forkRet = fork();
if(forkRet<0){
exitCode = -1;
perror("Pipe fork");
break;
}
else if(forkRet>0){
//parent - write to pipe
wait(NULL);
close(fd[1]); //close write end
s=fd[0];
}
else if(forkRet == 0){
//child - read from pipe
if(i!=0)
if(dup2(s,0)<0){ //replace stdin of pipe rhs with read end of lhs
perror("dup2 stdin");
exitCode = -1;
close(fd[1]);
close(fd[0]);
exit(1);
}
if(i<pipCnt-1){
if(dup2(fd[1],1)<0){ //replace stdout of lhs with write end of rhs
perror("dup2 stdin");
exitCode = -1;
close(fd[1]);
close(fd[0]);
exit(1);
}
}
if(!chkRedir(pipToks[i]))//input from fd[0]
x=0;
close(fd[0]); //close read end
close(fd[1]); //close write end
exit(1);
}
}
for(s=0;s<100;s++){
free(pipToks[s]);
}
return x;
}