-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
711 lines (643 loc) · 13.6 KB
/
shell.c
File metadata and controls
711 lines (643 loc) · 13.6 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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
PROJECT - SHELL
GROUP ID: 24
GAURAV AGGARWAL - 2017288
NIRAV DIWAN - 2017072
*/
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<limits.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
char cmdhistory[1000][1000];//stores the commands used
int ncmd=0;//keeps a count of the number of commands executed
char executable[1000], attribute[1000];//arguments to store all the required info of a command
char output[1000], error[1000], input[1000];
char arguments[100][1000];
int isappend = 0, numArguments = 0, inputind = -1, outputind = -1, errorind = -1;
/*
Splits the array with Type = {">", "<", " "}.
Returns the final size of the splited array
*/
int splitByType(char *line, char *array[], char *type)
{
char *splitPipe = strtok(line,type);
int i = 0;
while( splitPipe != NULL )
{
array[i++] = splitPipe;
splitPipe = strtok(NULL, type);
}
return i;
}
/*
Checks if thepath is valid or not
Gets the Absolute of the Path taken as Input
Returns the absolute Path
*/
char* getPath(char* string)
{
char *actualPath;
char *path[1000];
char* currentPath = getcwd(NULL,0);
int elements = splitByType(string,path,"/");
if(elements > 1)
{
char nowPath[1000];
memset(nowPath,'\0', 1000*sizeof(char));
for(int h = 0; h < elements-1; h++)
{
strcat(nowPath,path[h]);
strcat(nowPath,"/");
}
int checkDirectory=chdir(nowPath);
if(checkDirectory<0)
{
printf("Invalid Path");
return "";
}
actualPath = getcwd(NULL,0);
strcat(actualPath,"/");
strcat(actualPath, path[elements-1]);
}
else {
actualPath = getcwd(NULL,0);
strcat(actualPath,"/");
strcat(actualPath, path[elements-1]);
}
chdir(currentPath);
strcat(actualPath,"\0");
return actualPath;
}
/*
Checks if the File exists at the argument path
If file does not exist the mentioned file is created
*/
void checkfile(int fileno,char *path)
{
if( fileno == -1)
{
FILE * fPtr;
fPtr = fopen(path,"w");
if(fPtr == NULL)
{
/* File not created hence exit */
printf("Unable to create file.\n");
}
fclose(fPtr);
}
}
/*
Handles all the '<' redirection and adjusts the file descriptor accordingly
*/
int inputfd(char *input)
{
if(strlen(input) > 0)
{
if(input[0] == '&')
{
if(strlen(input)==2 && (input[1] == '1' || input[1] == '2' || input[1] == '0'))
{
if(input[1] == '1') {
close(0);
dup(1);
}
else if(input[1] == '2') {
close(0);
dup(2);
}
return 1;
}
else
{
printf("%s\n", "Bad File Descriptor.");
return 0;
}
}
char *inputPath;
inputPath = getPath(input);
if(strcmp(inputPath,"\0") == 0){
printf("%s\n","Path is Invalid " );
return 0;
}
else{
int file = open(inputPath, O_RDONLY);
if(file != -1) {
close(0);
dup(file);
}
else {
printf("%s\n", "File Doesn't Exist");
return 0;
}
}
}
return 1;
}
/*
Handles all the '>' redirection and adjusts the file descriptor accordingly
*/
int outputfd(char *output, int isappend)
{
if(strlen(output) > 0)
{
if(output[0] == '&')
{
if(strlen(output)==2 && (output[1] == '1' || output[1] == '2' || output[1] == '0'))
{
if(output[1] == '0') {
close(1);
dup(0);
}
else if(output[1] == '2') {
close(1);
dup(2);
}
return 1;
}
else
{
printf("%s\n", "Bad File Descriptor.");
return 0;
}
}
char* outputPath;
outputPath = getPath(output);
if(strcmp(outputPath,"\0") == 0){
printf("%s\n","Path is Invalid " );
return 0;
}
else{
int file;
if(isappend == 1) {
file = open(outputPath,O_WRONLY | O_APPEND);
checkfile(file, outputPath);
file = open(outputPath,O_WRONLY | O_APPEND);
}
else {
remove(outputPath);
file = open(outputPath,O_WRONLY);
checkfile(file, outputPath);
file = open(outputPath,O_WRONLY);
}
close(1);
dup(file);
}
}
return 1;
}
/*
Adjusts the Error File Descriptor
*/
int errorfd(char *error)
{
if(strlen(error) > 0)
{
if(error[0] == '&')
{
if(strlen(error)==2 && (error[1] == '1' || error[1] == '2' || error[1] == '0'))
{
if(error[1] == '0') {
close(2);
dup(0);
}
else if(error[1] == '1') {
close(2);
dup(1);
}
return 1;
}
else
{
printf("%s\n", "Bad File Descriptor.");
return 0;
}
}
char* errorPath;
errorPath = getPath(error);
if(strcmp(errorPath,"\0") == 0){
return 0;
}
else{
int file = open(errorPath,O_WRONLY);
checkfile(file, errorPath);
file = open(errorPath,O_WRONLY);
close(2);
dup(file);
}
}
return 1;
}
/*
Handles all the specialcommands i.e commands not in /bin,/usr/bin
Eg- cd ,clear,history
*/
int specialCommand()
{
if(strcmp(executable,"cd") == 0)
{
int len = strlen(arguments[0]);
int ch = 0;
if(len == 0 || strcmp(arguments[0], "$HOME") == 0) {
ch = chdir(getenv("HOME"));
}
else
ch = chdir(arguments[0]);
if(ch < 0)
{
printf("Wrong path specified.Check the Path again\n");
}
return 1;
}
if(strcmp(executable, "clear") == 0)
{
printf("\e[1;1H\e[2J");
return 1;
}
if(strcmp(executable, "history") == 0) {
if(ncmd == 0) {
printf("%s\n", "No Commands Found");
}
else {
for(int i=0; i<ncmd; ++i) {
printf("%d %s\n",i+1,cmdhistory[i]);
}
}
return 1;
}
return 0;
}
/*
Main Execute function which calls execvp to implement commands entered into shell
Uses global variables (Above) to implement based on User commands
*/
void execute()
{
if(inputind < outputind && inputind < errorind){
int val = inputfd(input);
if(val == 0) return;
if(outputind<errorind){
val = outputfd(output, isappend);
if(val == 0) return;
val = errorfd(error);
if(val == 0) return;
}
else{
val = errorfd(error);
if(val == 0) return;
val = outputfd(output, isappend);
if(val == 0) return;
}
}
else if(outputind < inputind && outputind < errorind){
int val = outputfd(output,isappend);
if(val == 0) return;
if(inputind<errorind){
val = inputfd(input);
if(val == 0) return;
val = errorfd(error);
if(val == 0) return;
}
else{
val = errorfd(error);
if(val == 0) return;
val = inputfd(input);
if(val == 0) return;
}
}
else
{
int val = errorfd(error);
if(val == 0) return;
if(inputind < outputind){
val = inputfd(input);
if(val == 0) return;
val = outputfd(output, isappend);
if(val == 0) return;
}
else{
val=outputfd(output,isappend);
if(val == 0) return;
val = inputfd(input);
if(val == 0) return;
}
}
if(strcmp(executable,"") == 0) exit(0);
char *args[1000];
char command[1000] = "/bin/";
strcat(command, executable);
int i = 0;
args[i] = command;
++i;
if(strlen(attribute) > 0){
char arg1[1000] = "-";
strcat(arg1,attribute);
args[i] = arg1;
++i;
}
if(numArguments > 0){
for(int j=0; j<numArguments; j++) {
args[i] = arguments[j];
++i;
}
}
int check = execvp(args[0],args);
memset(args[0],'\0', 1000*sizeof(char));
strcat(args[0], "/usr/bin/");
strcat(args[0], executable);
int check2 = execvp(args[0],args);
if(check < 0 || check2 < 0) {
printf("%s\n", "Exec command failed.");
}
exit(1);
}
/*
Function to jump over spaces.
Returns the index of last space until non-space character is found.
*/
int removeSpacing(char *string, int i, int lengthString)
{
int j = i+1;
for(; j< lengthString; ++j) {
if(string[j] != ' ' && string[j] != '\t') {
break;
}
}
return (j-1);
}
/*
Stores all the attributes in the string array
Returns the index from which the attribute starts
*/
int findAttribute(int low, char *command, char *string)
{
int lengthString = strlen(command);
int n = strlen(string);
int i = low;
for(; i<lengthString; ++i)
{
if (command[i] == ' ' || command[i] == '>' || command[i] == '<' || command[i] == '\t')
{
for(int j=low; j<i; j++)
{
string[n+j-low] = command[j];
}
break;
}
else if(i == lengthString - 1) {
for(int j=low; j<i+1; j++)
{
string[n+j-low] = command[j];
}
i += 1;
break;
}
}
return i;
}
/*
Determines the command that has to be executed.
*/
void determineExec(char command[])
{
memset(executable,'\0', 1000*sizeof(char));
memset(attribute,'\0', 1000*sizeof(char));
memset(input,'\0', 1000*sizeof(char));
memset(output,'\0', 1000*sizeof(char));
memset(error,'\0', 1000*sizeof(char));
for(int i=0;i<100;++i) {
memset(arguments[i],'\0', 1000*sizeof(char));
}
isappend = 0;
numArguments = 0;
inputind = -1;
outputind = -1;
errorind = -1;
int lengthString = strlen(command);
for(int g=1;g<lengthString;g++)
{
if(command[g] == '>' && command[g-1] == '1' && (g-2 < 0 || command[g-2] == ' '))
{
memset(output,'\0', 1000*sizeof(char));
int fstart;
if(command[g+1] == '>') {
isappend = 1;
fstart = removeSpacing(command,g+1,lengthString);
}
else {
fstart = removeSpacing(command,g,lengthString);
}
int fend=findAttribute(fstart+1,command,output);
outputind = g;
for(int y=g-1; y<fend; ++y)
{
command[y] = ' ';
}
}
else if(command[g] == '>' && command[g-1] == '2' && (g-2 < 0 || command[g-2] == ' '))
{
memset(error,'\0', 1000*sizeof(char));
int fstart;
if(command[g+1] == '>') {
isappend = 1;
fstart = removeSpacing(command,g+1,lengthString);
}
else {
fstart = removeSpacing(command,g,lengthString);
}
int fend=findAttribute(fstart+1,command,error);
errorind = g;
for(int y=g-1; y<fend; ++y)
{
command[y] = ' ';
}
}
else if(command[g]=='>' && command[g-1] == '0' && (g-2 < 0 || command[g-2] == ' '))
{
memset(input,'\0', 1000*sizeof(char));
int fstart=removeSpacing(command,g,lengthString);
int fend=findAttribute(fstart+1,command,input);
inputind = g;
for(int y=g-1; y<fend; ++y)
{
command[y] = ' ';
}
}
else if(command[g] == '<' && (command[g-1] == '2' || command[g-1] == '1' || command[g-1] == '0'))
{
printf("%s\n", "Invalid Command wrong redirection");
return;
}
else if(command[g] == '>' && (command[g-2] == '2' || command[g-2] == '1' || command[g-2] == '0') && (g-3)>=0 && command[g-1] == ' ' && command[g-3] == ' ')
{
printf("%s\n", "Invalid Command wrong redirection");
return;
}
}
int i = -1;
i = removeSpacing(command, i, lengthString);
i = findAttribute(i+1, command, executable);
while(i<lengthString)
{
if(command[i] == '-'){
if(i+1 >= lengthString || command[i+1] == '-')
{
printf("%s\n", "Invalid Command");
return;
}
i = findAttribute(i+1, command, attribute);
}
else if(command[i] == '>')
{
i = removeSpacing(command, i, lengthString);
if(i+1 >= lengthString || (command[i-1] == '>' && command[i+1] == '>')) {
printf("%s\n", "Invalid Command");
return;
}
if(command[i+1] == '>')
{
isappend = 1;
i+=1;
continue;
}
if(i < outputind) {
char temp[1000] = "";
memset(temp,'\0', 1000*sizeof(char));
i = findAttribute(i+1, command, temp);
}
else {
memset(output,'\0', 1000*sizeof(char));
outputind = i;
i = findAttribute(i+1, command, output);
}
}
else if(command[i] == '<')
{
i = removeSpacing(command, i, lengthString);
if(i+1 >= lengthString || command[i+1] == '<') {
printf("%s\n", "Invalid Command");
return;
}
if(i < inputind) {
char temp[1000] = "";
memset(temp,'\0', 1000*sizeof(char));
i = findAttribute(i+1, command, temp);
}
else {
memset(input,'\0', 1000*sizeof(char));
inputind = i;
i = findAttribute(i+1, command, input);
}
}
else if(command[i] == ' ' || command[i] == '\t')
{
i+=1;
}
else {
i = findAttribute(i, command, arguments[numArguments]);
numArguments++;
}
}
}
/*
Handles commands executed through pipes
Changes Pipe size to 1MB
*/
void multiplePipe(char **cmd)
{
int p[2];
pid_t pid;
int fdIn = 0;
while (*cmd != NULL)
{
// printf("%s\n", *cmd);
pipe(p);
int pipe_sz = fcntl(p[1], F_SETPIPE_SZ, 1000000);
if ((pid = fork()) == -1)
{
printf("%s\n", "Cannot fork child process!!");
exit(0);
}
else if (pid == 0)
{
dup2(fdIn, 0); //change the input according to the old one
if (*(cmd + 1) != NULL)
dup2(p[1], 1);
close(p[0]);
determineExec(*cmd);
execute();
}
else
{
close(p[1]);
fdIn = p[0]; //save the input for the next command
wait(NULL);
cmd++;
}
}
}
/*
Splits the array with "|".
*/
void splitByPipe(char *line)
{
char *array[1000];
for(int i=0;i<100;++i) {
array[i] = '\0';
}
int elements = splitByType(line, array, "|");
determineExec(array[0]);
int check = specialCommand();
if(elements != 1 || check == 0) {
multiplePipe(array);
}
}
/*
Function to take input from stdin
*/
void Input()
{
// taking input
printf("\e[36;1m~%s>$ \e[0m", getcwd(NULL,0));
char* command=malloc(sizeof(char)*1000);
size_t n = 1000;
getline(&command, &n, stdin);
if(strcmp(command,"\n")==0)
return;
command[strlen(command)-1]='\0';
strcpy(cmdhistory[ncmd],command);
ncmd++;
if(strcmp("exit", command) == 0) {
printf("%s","\e[1;1H\e[2J");
exit(0);
}
for(int i=0; i<strlen(command); ++i) {
if(command[i] == (char)34 || command[i] == (char)39)
command[i] = ' ';
}
splitByPipe(command);
}
/*
Function to handle ^C signal
*/
void sig(int sigi)
{
return;
}
/*
Main Function
*/
void main()
{
signal(SIGINT, sig);
printf("%s","\e[1;1H\e[2J");
while(1) {
Input();
}
}