-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
81 lines (80 loc) · 2.15 KB
/
pipe.c
File metadata and controls
81 lines (80 loc) · 2.15 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
#include "header.h"
#include "redir.h"
int pip(char *tok,char *curadd,char *revcuradd){
char buff[10005];
strcpy(buff,tok);
char *tok2;
const char de[2]="|";
char* ref;
ref = buff;
tok2 = strtok_r(ref,de,&ref);
char *last_cmd;
int last_fd=-1,last_fdl=-1,sta=0;
while(tok2!=NULL ){
if(sta!=0)
break;
last_cmd= tok2;
tok2=strtok_r(ref,de,&ref);
if(tok2!=NULL){
int pipe_fd[2];
int fd = pipe (pipe_fd);
if(fd<0){
perror("Error at pipe");
return 0;
}
int pid = fork();
if(pid<0){
perror("Error at fork");
return 0;
}
if(pid==0){
close(pipe_fd[0]);
int fd2 = dup(STDOUT_FILENO);
if(fd2<0){
perror("Error at dup");
return 0;
}
int fd3 = dup2(pipe_fd[1],STDOUT_FILENO);
if(fd3<0){
perror("Error at dup2");
return 0;
}
sta= redir(last_cmd,curadd,revcuradd);
int re =dup2(fd2,STDOUT_FILENO);
if(re<0){
perror("Error at dup2");
return 0;
}
exit(0);
}
else{
wait(NULL);
close(pipe_fd[1]);
last_fd= dup(STDIN_FILENO);
if(last_fd<0){
perror("Error at dup");
return 0;
}
if(last_fdl=-1){
last_fdl=last_fd;
}
int fd3 = dup2(pipe_fd[0],STDIN_FILENO);
if(fd3<0){
perror("Error at dup2");
return 0;
}
}
}
}
if(sta==0){
sta = redir(last_cmd,curadd,revcuradd);
}
if(last_fdl!=-1){
int re= dup2(last_fdl,STDIN_FILENO);
if(re<0){
perror("Error at dup2");
return 0;
}
}
return sta;
}