-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipeHandler.c
282 lines (260 loc) · 7.97 KB
/
pipeHandler.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "headers.h"
#include "commandHandler.h"
int BACKUP_STDOUT_FILENO_p, BACKUP_STDIN_FILENO_p;
int numPipes;
int parsePipes(char* inp, char** parsed){
int i=0;
parsed[i] = strtok(inp, "|");
i+=1;
while(1){
parsed[i] = strtok(NULL, "|");
if(parsed[i]==NULL){
numPipes=i;
return 0;
}
i+=1;
}
return -1;
}
int parseCommands(char* inp, char** parsed){
int i=0;
parsed[i] = strtok(inp, " ");
i+=1;
while(1){
parsed[i] = strtok(NULL, " ");
if(parsed[i]==NULL){
return 0;
}
i+=1;
}
return -1;
}
int checkPipe(char *input){
int i=0;
for(i=0;input[i]!='\0';i++){
if(input[i]=='|')
return 1;
}
return 0;
}
int runPipe(char *input){
int fo=fork();
if(fo==0){
char *commands[1000];
parsePipes(input, commands);
int i=0, pipe_des[2];
start:
// create a pipe
if (pipe(pipe_des) < 0) {
perror("Could not create pipe.");
exit(1);
}
int f=fork();
if(f==0){
//printf("%d", EOF);
close(pipe_des[1]);
//Setting stdin to read side of pipe
if (dup2(pipe_des[0], STDIN_FILENO) < 0) {
perror("Unable to duplicate file descriptor.");
return -1;
}
if(commands[i]){
i++;
if(commands[i]){
goto start;
}
}
close(pipe_des[0]);
exit(0);
}else{
int isfinal=0;
close(pipe_des[0]);
// Setting stdout to pipe
if (dup2(pipe_des[1], STDOUT_FILENO) < 0) {
perror("Unable to duplicate file descriptor.");
return -1;
}
if(commands[i+1]==NULL){
//printf("%d", EOF);
close(pipe_des[1]);
if (dup2(BACKUP_STDOUT_FILENO_p, STDOUT_FILENO) < 0) {
perror("Could Not restore to original output stream");
exit(1);
}
isfinal=1;
}
char *parsed[1000];
char temp[PATH_MAX];
strcpy(temp, commands[i]);
int j=0;
while(temp[j]==' ') j++;
parseCommands(&temp[j], parsed);
runCommand(parsed, &commands[i][j]);
if(isfinal){
exit(0);
}
//FILE *tF;
//tF=fopen("test", "a");
//fprintf(tF, "STARTPIPE:Child id=%d command=%s\n", f, parsed[0]);
//fclose(tF);
// Waiting for child
int status;
if(!isfinal){
do{
waitpid(f, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status) && !WIFSTOPPED(status));
}
if(f!=0){
//FILE *tF;
//tF=fopen("test", "a");
//fprintf(tF, "ENDPIPE:Child id=%d command=%s\n", f, parsed[0]);
//fclose(tF);
kill(f, SIGKILL);
}
exit(0);
// close(pipe_des[0]);
// close(pipe_des[1]);
}
}else{
int status;
do{
waitpid(fo, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status) && !WIFSTOPPED(status));
kill(fo, SIGKILL);
}
}
/* Code for Pipes (One with multiple childs and one which works for 2 pipes)
*
int runPipe(char *input){
char *commands[1000];
parsePipes(input, commands);
int i=0, pipe_des[2];
start:
// create a pipe
if (pipe(pipe_des) < 0) {
perror("Could not create pipe.");
exit(1);
}
int f=fork();
if(f==0){
close(pipe_des[1]);
//Setting stdin to read side of pipe
if (dup2(pipe_des[0], STDIN_FILENO) < 0) {
perror("Unable to duplicate file descriptor.");
return -1;
}
if(commands[i]){
i++;
if(commands[i]){
goto start;
}
}
close(pipe_des[0]);
exit(0);
}else{
int isfinal=0;
close(pipe_des[0]);
// Setting stdout to pipe
if (dup2(pipe_des[1], STDOUT_FILENO) < 0) {
perror("Unable to duplicate file descriptor.");
return -1;
}
if(commands[i+1]==NULL){
if (dup2(BACKUP_STDOUT_FILENO_p, STDOUT_FILENO) < 0) {
perror("Could Not restore to original output stream");
exit(1);
}
close(pipe_des[1]);
isfinal=1;
printf("Restored stdout for %s and final=%d\n", commands[i], isfinal);
}
char *parsed[1000];
char temp[PATH_MAX];
strcpy(temp, commands[i]);
int j=0;
while(temp[j]==' ') j++;
parseCommands(&temp[j], parsed);
runCommand(parsed, &commands[i][j]);
if(isfinal){
exit(0);
fflush(stdout);
}
close(pipe_des[0]);
close(pipe_des[1]);
// Waiting for child
int status;
if(!isfinal){
do{
waitpid(f, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status) && !WIFSTOPPED(status));
}
}
int i=0;
char temp[PATH_MAX];
while(commands[i]){
char *parsed[1000];
strcpy(temp, commands[i]);
printf("%s\n", commands[i++]);
int j=0;
while(temp[j]==' ') j++;
printf("%s and %d spaces\nparsed into: ", &temp[j], j);
parseCommands(&temp[j], parsed);
int k=0;
while(parsed[k]){
printf("%s ", parsed[k++]);
}
printf("\n\n");
}
fflush(stdout);
}
int runPipeTwo(char *input){
char *commands[1000], *parsed[1000], temp[PATH_MAX];
parsePipes(input, commands);
int i=0, pipe_des[2];
pipe(pipe_des);
int f=fork();
if(f==0){
close(pipe_des[0]);
dup2(pipe_des[1], STDOUT_FILENO);
strcpy(temp, commands[0]);
int j=0;
while(temp[j]==' ') j++;
parseCommands(&temp[j], parsed);
runCommand(parsed, &commands[0][j]);
close(pipe_des[1]);
exit(0);
}else{
close(pipe_des[1]);
dup2(pipe_des[0], STDIN_FILENO);
strcpy(temp, commands[1]);
int j=0;
while(temp[j]==' ') j++;
parseCommands(&temp[j], parsed);
// Waiting for child
int status;
do{
waitpid(f, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status) && !WIFSTOPPED(status));
runCommand(parsed, &commands[1][j]);
close(pipe_des[0]);
}
}
*/
int checkPipeandrunCommand(char **parsed, char *input){
BACKUP_STDIN_FILENO_p=dup(STDIN_FILENO);
BACKUP_STDOUT_FILENO_p=dup(STDOUT_FILENO);
if(checkPipe(input)==0)
runCommand(parsed, input);
else{
runPipe(input);
}
//Restoring back to original
if (dup2(BACKUP_STDOUT_FILENO_p, STDOUT_FILENO) < 0) {
perror("Could Not restore to original output stream");
exit(1);
}
if (dup2(BACKUP_STDIN_FILENO_p, STDIN_FILENO) < 0) {
perror("Could Not restore to original input stream");
exit(1);
}
}