-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
96 lines (72 loc) · 1.7 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
#include "functions.h"
void piping(char *line)
{
// what about || and |||
char *delim = "|";
char *commands[150]; //set to null?
int i = 0;
char *sptr;
char *token = mallify; forn(i) token[i] = '\0';
token = strtok_r(line,delim,&sptr);
line = NULL;
commands[i] = token;
for(int j=0; ;j++)
{
token = strtok_r(line,delim,&sptr);
if(token == NULL) break;
++i;
commands[i] = token;
}
int saved_stdout = dup(STDOUT_FILENO);
int saved_stdin = dup(STDIN_FILENO);
int fd[2];
// int out = 1;
// int in = 0;
for(int k=0;k<=i;k++)
{
if( pipe(fd) < 0)
{
perror("Pipe Error");
forn(i) smiley[i] = '\0';
strcat(smiley,":'(");
}
int pid = fork();
if(pid < 0)
{
perror("Fork error");
forn(i) smiley[i] = '\0';
strcat(smiley,":'(");
}
if(pid == 0)
{
close(fd[0]);
if(k == i) dup2(saved_stdout,1);
else dup2(fd[1],1);
//dup2(in,0);
command(commands[k]);
close(fd[1]);
exit(0);
}
else if(pid > 0)
{
close(fd[1]);
wait(NULL);
//in = fd[0];
dup2(fd[0],0);
close(fd[0]);
}
}
if( dup2(saved_stdout,1) < 0)
{
perror("dup2 error");
forn(i) smiley[i] = '\0';
strcat(smiley,":'(");
}
if (dup2(saved_stdin,0) < 0)
{
perror("dup2 error stdin");
forn(i) smiley[i] = '\0';
strcat(smiley,":'(");
}
return;
}