-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
267 lines (237 loc) · 7.1 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
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
#include "headers.h"
#include "functions.h"
void A(char *command, char *path)
{
char **x = AmpersandHandler(command);
if (debug)
printf("a1\n");
for (int i = 0;; i++)
{
if (x[i] == NULL)
break;
if (debug)
printf("ai = %d\n", i);
if (debug)
printf("in A, x[i] = %s\n", x[i]);
executeCommand(x[i], DeTildify(path));
}
}
char **pipeLine(char *command, char *path)
{
int exitcode = 1;
int pipes = 1;
int l = strlen(command);
for (int i = 0; i < l; i++)
if (command[i] == '|')
pipes++;
if (pipes == 1)
{
A(command, path);
return NULL;
}
char **y = (char **)malloc(MY_LEN * sizeof(char *));
for (int i = 0; i < MY_LEN; i++)
{
y[i] = (char*) malloc(MY_LEN* sizeof(char));
strcpy(y[i],"");
}
int yindex = 0;
y[0] = strtok(command, "|");
while (y[yindex] != NULL)
{
yindex++;
y[yindex] = strtok(NULL, "|");
}
int totalpipefiles = 2 * (yindex - 1);
int pipefiles[totalpipefiles];
for (int i = 0; i < totalpipefiles; i += 2)
{
int s = pipe(pipefiles + i);
if (s == -1)
{
fprintf(stderr, "Unable to create pipe at %d\n", i);
exitcode = -1;
return NULL;
}
}
for (int i = 0; i < yindex; i++)
{
int pid = fork();
if (pid == 0)
{
if (i < (yindex - 1))
dup2(pipefiles[2 * i + 1], STDOUT_FILENO);
if (i > 0)
dup2(pipefiles[2 * i - 2], STDIN_FILENO);
for (int j = 0; j < totalpipefiles; j++)
close(pipefiles[j]);
A(y[i], path);
exit(exitcode);
}
}
for (int i = 0; i < totalpipefiles; i++)
close(pipefiles[i]);
// Reap all the dead child processes
for (int i = 0; i < yindex; i++)
{
if (i == yindex - 1)
{
wait(&exitcode);
exitcode = WEXITSTATUS(exitcode);
}
else
wait(NULL);
}
}
// typedef struct file
// {
// int* fd;
// }file;
// char **pipeLine(char *command, char *path)
// {
// char **y = (char **)malloc(MY_LEN * sizeof(char *));
// for (int i = 0; i < MY_LEN; i++)
// {
// y[i] = NULL;
// }
// int exitcode = 1;
// int pipes = 1;
// for (int i = 0; i < strlen(command); i++)
// if (command[i] == '|')
// pipes++;
// if (pipes == 1)
// {
// A(command, path);
// return NULL;
// }
// int yindex = 0;
// y[0] = strtok(command, "|");
// while (y[yindex] != NULL)
// {
// yindex++;
// y[yindex] = strtok(NULL, "|");
// }
// int totalcommands = yindex - 1;
// file** pipefiles = (file**) malloc(totalcommands*sizeof(file*));
// for(int i =0 ; i < totalcommands; i++)
// {
// pipefiles[i]->fd = (int*) malloc(2*sizeof(int));
// }
// for (int i = 0; i < totalcommands; i ++)
// {
// file* temp = pipefiles[i];
// int s = pipe(temp->fd);
// if (s == -1)
// {
// fprintf(stderr, "Unable to create pipe at %d\n", i);
// return NULL;
// }
// }
// for (int i = 0; i < totalcommands; i++)
// {
// int pid = fork();
// if (pid == 0)
// {
// if(i != (totalcommands-1)) dup2(pipefiles[i]->fd[1], STDOUT_FILENO);
// if(i != 0) dup2(pipefiles[i-1]->fd[0], STDIN_FILENO);
// for (int j = 0; j < totalcommands; j++)
// {
// close(pipefiles[j]->fd[0]);
// close(pipefiles[i]->fd[0]);
// }
// A(y[i], path);
// exit(exitcode);
// }
// }
// for (int i = 0; i < totalcommands; i++)
// {
// close(pipefiles[i]->fd[0]);
// close(pipefiles[i]->fd[0]);
// }
// }
// char** pipeLine(char *inputString, char* path)
// {
// // Count the number of pipe separated commands
// int comCount = 1;
// int exitCode = 1;
// int len = strlen(inputString);
// for (int i = 0; i < len; i++)
// if (inputString[i] == '|')
// comCount++;
// // If there is just one, no pipes, just run
// if (comCount == 1)
// executeCommand(inputString,path);
// // Otherwise
// else
// {
// // Assign one pointer to each command
// char *commands[comCount];
// for (int i = 0; i <= comCount; i++)
// commands[i] = NULL;
// comCount = 0;
// commands[0] = strtok(inputString, "|");
// while (commands[comCount] != NULL)
// {
// comCount++;
// commands[comCount] = strtok(NULL, "|");
// }
// // Remove leading and trailing spaces from each command
// for (int i = 0; i < comCount; i++)
// {
// if (commands[i][0] == ' ')
// commands[i]++;
// if (commands[i][strlen(commands[i]) - 1] == ' ')
// commands[i][strlen(commands[i]) - 1] = '\0';
// }
// // Count number of pipes and create required file descriptor pairs
// int pipeCount = comCount - 1;
// int fds[2 * pipeCount];
// // Initialize the pipe file descriptors
// for (int i = 0; i < 2 * pipeCount; i += 2)
// if (pipe(fds + i))
// {
// fprintf(stderr, "Unable to create pipe\n");
// exitCode = -1;
// return;
// }
// int pid;
// // For each command
// for (int i = 0; i < comCount; i++)
// {
// // Make a new child process for the shell
// pid = fork();
// // In the child process
// if (pid == 0)
// {
// // If not the last command, link the block's write fd to stdout
// if (i < pipeCount)
// dup2(fds[2 * i + 1], STDOUT_FILENO);
// // If not the first command, link the previous block's read fd to stdin to read what the previous command wrote
// if (i > 0)
// dup2(fds[2 * i - 2], STDIN_FILENO);
// // Close the pipes in the current child (just clean up)
// for (int j = 0; j < 2 * pipeCount; j++)
// close(fds[j]);
// // Execute the command
// char *command = commands[i];
// executeCommand(command,path);
// // Exit the process on completion
// exit(exitCode);
// }
// }
// // Close all the pipes in the parent
// for (int i = 0; i < 2 * pipeCount; i++)
// close(fds[i]);
// // Reap all the dead child processes
// for (int i = 0; i < comCount; i++)
// {
// if(i == comCount - 1)
// {
// wait(&exitCode);
// exitCode = WEXITSTATUS(exitCode);
// }
// else
// wait(NULL);
// }
// }
// }