-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_shell.cpp
467 lines (438 loc) · 20.9 KB
/
my_shell.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
char *line = NULL;
char *token;
char **firstArg = NULL;
size_t n = 0;
int status;
std::vector <char *> commandArguments;
std::vector <char *> command;
int numPipes;
FILE *fileToBeDuped1;
int fileDescriptor1;
FILE *fileToBeDuped2;
int fileDescriptor2;
FILE *fileToBeDuped3;
int fileDescriptor3;
bool fileRedirection = false;
//if there is no command line argument use default ./my_shell prompt
if (argc == 1) {
fprintf(stdout, "my_shell> ");
//if there is command line argument then use that argument as prompt
} else if (argc == 2) {
fprintf(stdout, "%s> ", argv[1]);
//else print usage error
} else {
fprintf(stderr, "Usage: ./my_shell or ./my_shell PROMPT\n");
exit(1);
}
while(getline(&line, &n, stdin) != -1) {
//exit on success if user enters exit
if (strcmp(line, "exit\n") == 0) {
exit(0);
}
//parse the arguments and push into vector
token = strtok(line, " \n");
//error check strtok
if (token == NULL) {
perror("Strtok failure");
exit(1);
}
while (token != NULL) {
commandArguments.push_back(token);
token = strtok(NULL, " \n");
}
//count number of pipes
numPipes = 0;
for(int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "|") == 0) {
++numPipes;
}
}
//number of processes is pipes + 1
int processCount = numPipes + 1;
//create vector to hold file descriptors for the pipes
std::vector<int *>FDs;
//create new file descriptors for each pipe and push back into vector
for (int i = 0; i < numPipes; ++i) {
int *fileDescriptors = new int[2];
fileDescriptors[0] = 0;
fileDescriptors[1] = 1;
FDs.push_back(fileDescriptors);
}
//call pipe on each pair of file descriptors
for (int i = 0; i < FDs.size(); ++i) {
pipe(FDs[i]);
}
//if there are no pipes
if (numPipes == 0) {
//fork
switch(fork()) {
//error check fork
case -1:
perror("Fork failure");
exit(1);
case 0:
//iterate through to see if there is file redirection
for (int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "<") == 0 || strcmp(commandArguments[i], ">") == 0 ||
strcmp(commandArguments[i], ">>") == 0 || strcmp(commandArguments[i], "2>") == 0 ) {
fileRedirection = true;
}
}
//grab the command before the file redirection symbol
for (int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "<") != 0 && strcmp(commandArguments[i], ">") != 0 &&
strcmp(commandArguments[i], ">>") != 0 && strcmp(commandArguments[i], "2>") != 0 ) {
command.push_back(commandArguments[i]);
} else {
break;
}
}
//convert vector of char* to char** to pass to exec
firstArg = (char**)malloc(sizeof(char*) * (command.size() + 1));
if (firstArg == NULL) {
perror("Malloc failure\n");
exit(1);
}
for(int i = 0; i < command.size(); ++i) {
firstArg[i] = command[i];
}
firstArg[command.size()] = NULL;
//if there is file redirection
//take the command after the file redirection symbol
//open the file
//dup it
//close it
if (fileRedirection == true) {
for (int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "<") == 0) {
fileToBeDuped1 = fopen(commandArguments[i + 1], "r");
if (fileToBeDuped1 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor1 = fileno(fileToBeDuped1);
if (fileDescriptor1 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor1, 0) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped1);
} else if (strcmp(commandArguments[i], ">") == 0) {
fileToBeDuped2 = fopen(commandArguments[i + 1], "w");
if (fileToBeDuped2 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor2 = fileno(fileToBeDuped2);
if (fileDescriptor2 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor2, 1) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped2);
} else if (strcmp(commandArguments[i], ">>") == 0) {
fileToBeDuped2 = fopen(commandArguments[i + 1], "a");
if (fileToBeDuped2 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor2 = fileno(fileToBeDuped2);
if (fileDescriptor2 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor2, 1) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped2);
} else if (strcmp(commandArguments[i], "2>") == 0) {
fileToBeDuped3 = fopen(commandArguments[i + 1], "w");
if (fileToBeDuped3 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor3 = fileno(fileToBeDuped3);
if (fileDescriptor3 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor3, 2) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped3);
}
}
}
//execvp the command
//even if there is not file redirection it will hit this
execvp(firstArg[0], firstArg);
//error check
perror("Execvp failure");
exit(1);
default:
//parent should wait
//free memory
wait(&status);
free(firstArg);
}
//else if there are pipes
} else {
//for each process
for (int i = 0; i < processCount; ++i) {
std::vector<char *>vectorWhat;
//push back all arguments before the pipe into vector
//this will be our process
for(int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "|") != 0) {
vectorWhat.push_back(commandArguments[i]);
} else {
break;
}
}
//if there is file redirection set fileRedirection to true
for (int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "<") == 0 || strcmp(commandArguments[i], ">") == 0 ||
strcmp(commandArguments[i], ">>") == 0 || strcmp(commandArguments[i], "2>") == 0 ) {
fileRedirection = true;
}
}
std::vector<char *>noFileRedirection;
//grab the commands before the file redirection symbol
for (int i = 0; i < vectorWhat.size(); ++i) {
if (strcmp(commandArguments[i], "<") != 0 && strcmp(commandArguments[i], ">") != 0 &&
strcmp(commandArguments[i], ">>") != 0 && strcmp(commandArguments[i], "2>") != 0 ) {
noFileRedirection.push_back(commandArguments[i]);
} else {
break;
}
}
//convert to char** to be able to pass to exec
char **whatArgs = (char**)malloc(sizeof(char*) * (noFileRedirection.size() + 1));
if (whatArgs == NULL) {
perror("Malloc failure\n");
exit(1);
}
for(int i = 0; i < noFileRedirection.size(); ++i) {
whatArgs[i] = noFileRedirection[i];
}
whatArgs[noFileRedirection.size()] = NULL;
//fork
switch(fork()) {
//error check fork
case -1:
perror("Bad Fork");
exit(1);
case 0:
//if there is file redirection do the same as you did before
if (fileRedirection == true) {
for (int i = 0; i < commandArguments.size(); ++i) {
if (strcmp(commandArguments[i], "<") == 0) {
fileToBeDuped1 = fopen(commandArguments[i + 1], "r");
if (fileToBeDuped1 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor1 = fileno(fileToBeDuped1);
if (fileDescriptor1 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor1, 0) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped1);
} else if (strcmp(commandArguments[i], ">") == 0) {
fileToBeDuped2 = fopen(commandArguments[i + 1], "w");
if (fileToBeDuped2 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor2 = fileno(fileToBeDuped2);
if (fileDescriptor2 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor2, 1) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped2);
} else if (strcmp(commandArguments[i], ">>") == 0) {
fileToBeDuped2 = fopen(commandArguments[i + 1], "a");
if (fileToBeDuped2 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor2 = fileno(fileToBeDuped2);
if (fileDescriptor2 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor2, 1) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped2);
} else if (strcmp(commandArguments[i], "2>") == 0) {
fileToBeDuped3 = fopen(commandArguments[i + 1], "w");
if (fileToBeDuped3 == NULL) {
perror("Fopen failure");
exit(1);
}
fileDescriptor3 = fileno(fileToBeDuped3);
if (fileDescriptor3 == -1) {
perror("Fileno failure");
exit(1);
}
if (dup2(fileDescriptor3, 2) == -1) {
perror("Dup failure");
exit(1);
}
fclose(fileToBeDuped3);
}
}
}
//if it is the first process
if (i == 0) {
for (int j = 0; j < FDs.size(); ++j) {
//close the read end
if (close(FDs[j][0]) == -1) {
perror("Close read end failure");
exit(1);
}
//if it is not the first pipe
if (j != 0) {
//close the write end
if (close(FDs[j][1]) == -1) {
perror("Close write end failure");
exit(1);
}
}
}
//dup the write
if (dup2(FDs[i][1], 1) == -1) {
perror("Dup failure");
exit(1);
}
//exec the arguments
execvp(whatArgs[0], whatArgs);
perror("Execvp failure");
exit(1);
}
//if it is one of the middle processes
if (i > 0 && i < numPipes) {
for (int j =0; j < FDs.size(); ++j) {
//close the read ends except for the pipe previous
if(j != (i - 1)) {
if (close(FDs[j][0]) == -1) {
perror("Close read end failure");
exit(1);
}
}
//close write ends except for current pipe
if (j != i) {
if (close(FDs[j][1]) == -1) {
perror("Close write end failure");
exit(1);
}
}
}
//dup write end
if (dup2(FDs[i][1], 1) == -1) {
perror("Dup failure");
exit(1);
}
//dup read end
if (dup2(FDs[i-1][0], 0) == -1) {
perror("Dup failure");
exit(1);
}
//exec the arguments
execvp(whatArgs[0], whatArgs);
perror("Execvp failure");
exit(1);
}
//if it is the last process
if (i == processCount - 1) {
for (int j = 0; j < FDs.size(); ++j) {
//close all read ends except for current
if (j != (i - 1)) {
if (close(FDs[j][0]) == -1) {
perror("Close read end failure");
exit(1);
}
}
//close all write ends
if (close(FDs[j][1]) == -1) {
perror("Close write end failure");
exit(1);
}
}
//dup read end
if (dup2(FDs[i-1][0], 0) == -1) {
perror("Dup failure");
exit(1);
}
//exec arguments
execvp(whatArgs[0], whatArgs);
perror("Execvp failure");
exit(1);
}
}
//if it is not the last process erase the process + the pipe from the command arguments
if (i != numPipes) {
commandArguments.erase(commandArguments.begin(), (commandArguments.begin() + vectorWhat.size() + 1));
} else {
//else just erase the process because there is no pipe following
commandArguments.erase(commandArguments.begin(), (commandArguments.begin() + vectorWhat.size()));
}
//clear vector and free args
vectorWhat.clear();
free(whatArgs);
}
//parent close all read and write ends of all pipes
for (int i = 0; i < numPipes; ++i) {
if (close(FDs[i][0]) == -1) {
perror("Close read end failure");
exit(1);
}
if (close(FDs[i][1]) == -1) {
perror("Close write end failure");
exit(1);
}
}
//wait for each process
for (int i = 0; i < processCount; ++i) {
wait(&status);
}
}
//clear command arguments
commandArguments.clear();
//delete all FDs
for (int i = 0; i < FDs.size(); ++i) {
delete(FDs[i]);
}
//return to command prompt
if (argc == 1) {
fprintf(stdout, "my_shell> ");
} else if (argc == 2) {
fprintf(stdout, "%s> ", argv[1]);
}
}
}