-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiping.c
More file actions
executable file
·185 lines (167 loc) · 4.97 KB
/
piping.c
File metadata and controls
executable file
·185 lines (167 loc) · 4.97 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
#include "headers.h"
int pipecheck(char com[])
{
ll flag = 0;
for (ll i = 0; com[i]; i++)
{
if (com[i] == '|')
{
flag = 1;
break;
}
}
return flag;
}
//***************************************************************************************************************
int redirection_check(char ogcom[])
{
char *token[100000];
token[0] = strtok(ogcom, " \t\r\n");
ll k = 0, r;
while (token[k] != NULL) //Separating tokens within the command
{
k++;
token[k] = strtok(NULL, " \t\r\n");
}
for (ll j = 0; j < k; j++)
{
for (ll i = 0; token[j][i]; i++)
if ((token[j][i] == '>') || token[j][i] == '<' || (token[j][i] == '>' && token[j][i + 1] == '>') && r == 0)
{
r = 1;
if (token[j][i] == '>' && token[j][i + 1] == '>')
i++;
}
else if ((token[j][i] == '>') || token[j][i] == '<' || (token[j][i] == '>' && token[j][i + 1] == '>') && r == 1)
{
r = 2;
if (token[j][i] == '>' && token[j][i + 1] == '>')
i++;
}
}
return r;
}
//**********************************************************************************************************
void piping(char *commands[], ll k)
{
pipingflag = 1;
pid_t pid;
int mypipe[2], mypipe2[2], inp = 0, outp = 0;
char *part[10000], buffer[100000], ogcom[10000], temp[10000];
for (ll i = 0; i < k; i++)
{
char *part[10000], *part2[10000];
strcpy(ogcom, commands[i]);
int oldout, oldin, ofd, ifd, r = 0;
part[0] = strtok(ogcom, " \t\n\r"); //separating the commands
ll ii = 0;
while (part[ii] != NULL)
{
ii++;
part[ii] = strtok(NULL, " \t\r\n");
}
// printf("*%lld. -%s-\n", i, commands[i]);
// printf("tokens = \n");
// for (ll j = 0; j < ii; j++)
// printf("%lld. -%s-\n", j, part[j]);
if (i % 2 != 0)
{
int z = pipe(mypipe2);
if (z < 0)
perror("Error: pipe could not be created\n");
}
else
{
int z = pipe(mypipe);
if (z < 0)
perror("Error: pipe could not be created\n");
}
pid_t pid = fork();
if (pid == 0)
{
if (i == 0)
{
dup2(mypipe[1], 1); // output to pipe
close(mypipe[0]); //closing input end of pipe
strcpy(temp, commands[i]);
}
else if (i == k - 1)
{
if (i % 2 == 1)
{
// printf("came into odd end\n");
dup2(mypipe[0], 0); //Input from pipe
}
else
{
// printf("came into even end\n");
dup2(mypipe2[0], 0); //input from transit
}
}
else if (i % 2 == 0)
{
// printf("came into even middle\n");
dup2(mypipe2[0], 0); //input from transit
close(mypipe[0]); // close input end of pipe
dup2(mypipe[1], 1); // output to pipe
}
else if (i % 2 == 1)
{
// printf("came into odd middle\n");
dup2(mypipe[0], 0); //input from pipe
close(mypipe[1]); // close output end of pipe
dup2(mypipe2[1], 1); // output to transit
}
// printf("VALUEEE OFFF RR = %d\n", r);
strcpy(temp, commands[i]);
r = redirection_check(temp);
if (r == 1 || r == 2)
redirection(part, ii, commands[i], r);
// printf("now from exec\n");
else
{
int z = execvp(part[0], part); // exec
if (z < 0)
perror("Error: command not found\n");
if(r==0)
exit(0);
}
exit(0);
}
else
{
wait(NULL);
if (i == 0)
{
close(mypipe[1]);
if (inp == 1)
{
dup2(oldin, 0);
close(ifd);
}
}
else if (i == k - 1)
{
if (i % 2 == 0)
close(mypipe2[0]);
else
close(mypipe[0]);
if (outp == 1)
{
dup2(oldout, 1);
close(ofd);
}
}
else if (i % 2 == 0)
{
close(mypipe2[0]);
close(mypipe[1]);
}
else if (i % 2 == 1)
{
close(mypipe[0]);
close(mypipe2[1]);
}
}
}
}