forked from sisd/Laal_Shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaal_shell.c
682 lines (620 loc) · 15.8 KB
/
laal_shell.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> //for getXXX() func
#include <sys/types.h> //For wait
#include <sys/wait.h> //for wait
#include <signal.h>
#include <setjmp.h>
#include <fcntl.h> //For file control
#include <limits.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/dir.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
char ipfile[100], opfile[100];
char *input; size_t inputsize = 500;
int mflag, njobs=0, jobnumber[1000000], ctrZ_flag, bg_flag;
sigjmp_buf ctrlz_buf;
struct stat bufferc;
int stdout1, stdin1;
char ta = 't';
typedef struct jobs
{
pid_t pid;
char name[50];
}jobs;
jobs job[1000000];
// function to remove any extra white spaces from the command
char* whitespaces(char str[]) {
char *arr = str;
int i,j=0;
for(i=0; i<strlen(arr); i++) {
if(arr[i]=='\t') arr[i]=' ';
}
for (i=0;i<strlen(str);i++) {
if (str[i] == ' ') {
for (; str[i] == ' '; i++);
i--;
}
arr[j] = str[i];
j++;
if (arr[0] == ' ') j--;
}
if (arr[j-1] == ' ') arr[j-1] = '\0';
else arr[j] = '\0';
return arr;
}
int tokeniser(char input3[], char list[][30], char* t) {
char *token;
token=strtok(input3,t);
int i=0;
while(token)
{
sprintf(list[i++], "%s", token);
token=strtok(NULL, t);
}
return i;
}
int custom_pwd(char* pwd) {
char *token;
char dir[30][30], temp[30];
sprintf(temp, "%s", pwd);
int i=0,j,err;
for (token=strtok(temp,"/"); token != NULL; token=strtok(NULL, "/"), i++)
sprintf(dir[i], "%s",token);
sprintf(pwd, "/%s", dir[i-1]);
return 0;
}
int getPS(char mwd[]) {
char *username = (char *) malloc(30*sizeof(char));
username = getenv("USER");
struct utsname system_info;
uname(&system_info);
char *pwd=malloc(1000*sizeof(char));
getcwd(pwd, 100);
if(strcmp(mwd,pwd)!=0) custom_pwd(pwd);
else pwd[0] = '\0';
printf("%s @ %s :~%s $ ", username, system_info.sysname, pwd);
return 0;
}
pid_t multipleCommands(char input[]) {
char list[10][30];
int status;
pid_t temp_pid,temp2_pid;
int n_args=tokeniser(input, list, ";"); //To check for multiple commands
mflag=0;
if(n_args>1) {
int k;
mflag=1;
for(k=0; k<n_args; k++) {
sprintf(input, "%s", list[k]);
temp_pid=fork();
if(temp_pid==0) //child process in multiple command
return temp_pid;
if(temp2_pid=wait(&status) < 0) _exit(1);
else if (temp_pid > 0) {
temp2_pid=wait(&status);
continue;
}
}
return -1;
}
return -2;
}
void remove_job(pid_t pid) {
int jno = jobnumber[pid]; //To get jobnumber of process which got ctr+Z
int ptr;
for(ptr=jno; ptr<njobs-1; ptr++) {
job[ptr]=job[ptr+1];
jobnumber[job[ptr].pid]--;
}
njobs--;
}
int getiofile(char input[], int in) {
int i = 0;
if (in == 1) {
for(i=0; i < strlen(input); i++) {
if(input[i]=='<') {
for (; input[i+1] == ' '; i++);
break;
}
}
int x = 0;
for (i++; input[i] != '>' && input[i] != ' ' && input[i] != '\n'; i++, x++)
ipfile[x] = input[i];
ipfile[x+1] = '\0';
}
else {
for(i=0; i < strlen(input); i++) {
if(input[i]=='>') {
if (input[i+1] == '>') {ta = 'a'; i++;}
for (; input[i+1] == ' '; i++);
break;
}
}
int x = 0;
for (i++; input[i] != '<' && input[i] != ' ' && input[i] != '\n'; i++, x++)
opfile[x] = input[i];
opfile[x+1] = '\0';
}
printf("..in=%s..out=%s..\n", ipfile, opfile);
return 0;
}
int outputdup(char *fileout) {
printf("ta=%c..\n", ta);
int fout;
//printf("%s\n", fileout);
if (stat(fileout, &bufferc) == 0 && access(fileout,W_OK)!=0) {
printf("You dont have permission to access %s\n",fileout);
return 1;
}
else {
if(stat(fileout, &bufferc)) //write permissions only for user
creat(fileout,0644);
if (ta == 't')
fout = open(fileout, O_WRONLY | O_TRUNC, 0644);
else fout = open(fileout, O_WRONLY | O_APPEND, 0644);
stdout1 = dup(1);
dup2(fout, 1);
close(fout);
}
return 0;
}
int inputdup(char *filein) {
int fin;
if(stat(filein, &bufferc)!=0) {
printf("File %s doesn't exist\n",filein);
return 1;
}
else if(access(filein,R_OK)!=0) {
printf("You dont have permission to access %s\n",filein);
return 1;
}
else {
fin=open(filein,O_RDONLY,0644); //opening file in RDONLY with permissions
stdin1 = dup(0);
dup2(fin, 0);
close(fin);
}
return 0;
}
void iowork(char input[]) {
int k = 0;
ipfile[0]='\0';
opfile[0]='\0';
for(k=0; k<strlen(input);k++) {
if(input[k]=='>') {
getiofile(input, 1);
break;
}
}
for(k=0; k<strlen(input);k++) {
if(input[k]=='<') {
getiofile(input, 0);
break;
}
}
}
//To execute piped commands
void pipe_commands(char input[]) {
char list[10][30],parse[100], *token, ptr, temp[10][30];
//char *input2=malloc(1000*sizeof(char));
int i, j,k, num=0, fd[2], argc=0, status, fin,fout, in=0;
pid_t pid;
//To tokenise wrt "|"
num=tokeniser(input, list, "|");
for(i=0; i<num; i++) {
bg_flag=0; //For each subpart, bg_flag=0
//Checking for ip and op file
iowork(list[i]);
//sscanf(list[i],"\n%[^<>]", input2);
for (k = 0; k < strlen(list[i]); k++) {
if (k == '>' || k == '<' || k == '\n' || k == '\0') break;
input[k] = list[i][k];
}
input[k] = '\0';
input=whitespaces(input);
sprintf(parse, "%s",input); //Keep extra copy of input in input2
//Tokenise each subpart of pipe into temp, parse is destroyed by strtok
argc=tokeniser(parse,temp," ");
char **argv=malloc(1000*sizeof(char*));
for (j = 0; j < argc; j++) {
argv[j] = temp[j];
}
if(strcmp(temp[argc-1], "&") == 0) {
bg_flag=1; //Implies command is a bg
temp[argc-1][0] = '\0';
argc--;
}
pipe(fd); //Make a pipe
pid=fork();
if(pid==0) {
if(fd<0)
printf("Pipe error\n");
else {
if(fd[0]!=0) //As long as input is not stdin1, duplicate it as read end of the pipe
{
dup2(in, 0);
close(fd[0]);
}
if(i!=num-1)
{
dup2(fd[1], 1); //Except for last one, output is to write end of the pipe
close(fd[1]);
}
int ret = 0;
//If input redirection is there in subpart of pipe
if(ipfile[0]!='\0') {
ret = 0;
ret = inputdup(ipfile);
if (ret == 1) { continue; }
}
//If op redirection is there in subpart of pipe
if(opfile[0]!='\0') {
ret = 0;
ret = outputdup(opfile);
if (ret == 1) { continue; }
}
//If subpart of pipe is echo
if(strcmp(temp[0],"echo")==0) {
int stdout1;
if(opfile[0]!='\0') {
ret = 0;
ret = outputdup(opfile);
if (ret == 1) { continue; }
}
for(k=1; k<argc; k++)
for(j=0; j<strlen(temp[k]); j++)
printf("%c", temp[k][j]);
printf("\n");
dup2(stdout1,1); //After redirecting output to file, control regained by stdout1
_exit(1);
}
else {
printf("..%s..\n", argv[0]);
execvp(argv[0], argv); //execute normal subparts in pipe
_exit(1);
}
} //else of (fd<0)
} //end of pid==0
else if(pid>0) {
if(fd<0)
printf("Pipe error\n");
else {
job[njobs].pid=pid; //current process pid
sprintf(job[njobs].name, "%s", temp[0]);
jobnumber[pid] = njobs;
njobs++;
if(bg_flag!=1) {
ctrZ_flag=0;
waitpid(pid,&status,WUNTRACED);
if(pid < 0) {
_exit(1);
}
if (pid==0);
if(ctrZ_flag==0)
remove_job(pid); //no ctrl Z pressed, job done
}
else
printf("[%d] %s \n",job[njobs-1].pid, job[njobs-1].name);
close(fd[1]);
in=fd[0];
}
}
} //End of for oop
} //End of function
int pinfo_fn(char *process) {
int i = 0;
FILE *fd;
char add[100];
char text[100],exe[100],state,size[10],extra_arr[100] = "a";
for (i = 0; i < 100; i++)
add[i] = '\0';
add[0] = '/'; add[1] = 'p'; add[2] = 'r'; add[3] = 'o'; add[4] = 'c'; add[5] = '/'; add[6] = '\0';
//printf("Process==%s...\n", process);
if (process[0] == '\0') {
sprintf(process, "%d", getpid());
}
//copy
printf("Pid -- %s\n",process);
for (i = 0; process[i] != '\0'; i++)
add[6+i] = process[i];
add[6+i] = '\0';
strcpy(exe,add);
for (i = 0; add[i] != '\0'; i++);
exe[i] = '/'; exe[i+1] = 'e'; exe[i+2] = 'x'; exe[i+3] = 'e'; exe[i+4] = '\0';
for (i = 0; add[i] != '\0'; i++);
add[i] = '/'; add[i+1] = 's'; add[i+2] = 't'; add[i+3] = 'a'; add[i+4] = 't'; add[i+5] == '\0';//add[i+5] = 'u'; add[i+6] = 's'; add[i+7] = '/'; add[i+8] = '\0';
fd = fopen(add, "r");
if (fd == NULL) {
printf("Process does not exist.\n"); return 0;
}
//printf("%s...\n", add);
fscanf(fd, "%s", text);
fscanf(fd, "%s", text);
fscanf(fd, "%s", text);
printf("Process Status -- %s\n", text);
for (i = 0; add[i] != '\0'; i++);
add[i] = 'm'; add[i+1] = '\0';
fd = fopen(add, "r");
if (fd == NULL) {
printf("Process does not exist.\n"); return 0;
}
fscanf(fd, "%s", text);
printf("Memory -- %s\n",text);
//printf("exe==%s\n", exe);
printf("Executable -- ");
int ret = readlink(exe,extra_arr,100);
printf("%s\n",extra_arr);
return 0;
}
void signalHandlerc(int sig_num) {
signal(SIGINT, signalHandlerc);
printf("ctrl c pressed \n");
fflush(stdout);
}
void signalHandlerz(int sig_num) {
signal(SIGTSTP, signalHandlerz);
printf("ctrl z pressed \n");
fflush(stdout);
}
int main() {
pid_t Mpid = getpid();
char *input = malloc(1000*sizeof(char));
char *input2=malloc(1000*sizeof(char));
char mwd[100], pwd[100], p[10][30];
pid_t pid, cpid,err;
int i,len,j,status,k,a,argc, fin,fout;
getcwd(mwd, 100);
signal(SIGINT, signalHandlerc);
signal(SIGTSTP, signalHandlerz);
while(1) {
getPS(mwd); //Prints prompt
input[0]='\0';
ipfile[0]='\0';
opfile[0]='\0';
ta = 't';
getline(&input, &inputsize , stdin);
input[strlen(input)-1] = '\0';
//scanf(" %[^\n]s",input);
sprintf(input2, "%s", input);
pid_t mpid = multipleCommands(input);
if ( mpid == -1) continue;
//Piper check comes here
int piper=0, pipflag=0;
for(i=0;i<strlen(input); i++) {
if (pipflag == 1) {break;}
if(input[i]=='|') {
piper=1;
pipe_commands(input);
pipflag=1;
}
}
if(piper==1 && mpid==0 && mflag==1) //If pipe in multiple commands
exit(10);
else if(piper==1 && mflag==0) //0 Multiple commands and piping involved
continue;
ipfile[0]='\0';
opfile[0]='\0';
for(i=0; i<strlen(input);i++) {
if(input[i]=='<') getiofile(input, 1); //atmax one same type char ""<>""
else if (input[i] == '>') getiofile(input, 0);
}
for (int ite = 0; ite < strlen(input) && input[ite] != '<' && input[ite] != '>'; ite++)
input2[ite] = input[ite];
input2 = whitespaces(input2);
sprintf(input, "%s", input2);
//printf("..%s..\n", input);
if(strcmp(input,"exit")==0 || strcmp(input,"quit")==0) {
printf("Exiting shell BYE BYE\n\n");
kill(Mpid, 9);
}
if(input[0]=='\0' || input[0]=='\n'){
printf("\n");
continue;
}
if(strcmp(input, "pwd")==0) {
if(opfile[0]!='\0') {
if (outputdup(opfile) == 1) continue;
}
getcwd(pwd,100);
printf("%s\n", pwd);
dup2(stdout1,1);
//printf("kmkdvkmskdfmmkf fkm km\n");
}
else {
bg_flag=0;
if(input[strlen(input)-1] == '&') {
bg_flag=1; //Implies command is a bg
input[strlen(input)-2] = '\0';
}
//printf("input = %s..\n", input);
argc=tokeniser(input,p, " "); //For argument list
if (strcmp(p[0], "pinfo") == 0) {
pinfo_fn(p[1]);
}
//cd
if(strcmp(p[0], "cd")==0) {
if (argc == 1)
chdir(mwd);
else if(argc > 1) {
if (p[1][0] == '~' && p[1][1] == '\0') { chdir(mwd); }
else {
DIR *dir = opendir(p[1]);
if (dir) { closedir(dir); chdir(p[1]); }
else printf("No such directory exists\n");
}
}
else
printf("No such directory exists\n");
getcwd(pwd, 100);
}
//echo
else if(strcmp(p[0], "echo")==0) {
if(opfile[0] != '\0') {
if (outputdup(opfile) == 1) continue;
}
for(k=1; k<argc && p[k][0] != '>' && p[k][0] != '<'; k++) {
for(j=0; j<strlen(p[k]); j++)
printf("%c", p[k][j]);
printf(" ");
}
printf("\n");
dup2(stdout1,1);
}
//jobs
else if (strcmp(p[0], "jobs")==0) {
//printf("njobs = %d\n", njobs);
if (njobs == 0)
printf("No jobs to show\n");
if(njobs > 0) {
for(i = 0; i < njobs; i++) {
char addr[100] = "/proc/";
char arrtemp[100];
sprintf(arrtemp, "%d" ,job[i].pid);
strcat(addr, arrtemp);
strcat(addr, "/stat");
char text[100];
FILE *fd;
fd = fopen(addr, "r");
fscanf(fd, "%s", text);
fscanf(fd, "%s", text);
fscanf(fd, "%s", text);
if (text[0] == 'R') {sprintf(text, "Running");}
else if (text[0] == 'S') {sprintf(text, "Stopped");}
printf("[%d] %s %s [%d]\n",i+1, job[i].name, text, job[i].pid);
free(fd);
}
}
}
//kjob
else if (strcmp(p[0],"kjob")==0) {
//printf("argc=%d\n",argc);
if (argc == 3) {
int sig = atoi(p[2]);
int j_no = atoi(p[1])-1;
if (j_no < njobs) {
kill(job[j_no].pid, sig);
remove_job(job[j_no].pid);
}
else printf("No such job exists such as %d\n", j_no);
}
else printf("Wrong usage of kjob\n");
}
//overkill
else if (strcmp(p[0], "overkill")==0) {
stdout1 = dup(1);
if(opfile[0] != '\0') {
if (outputdup(opfile) == 1) continue;
}
if (njobs == 0) printf("No jobs to kill\n");
else {
for(i=0; i<njobs; i++) {
printf("[%d] %s [%d] killed \n", i+1, job[i].name, job[i].pid);
kill(job[i].pid, 9);
remove_job(job[i].pid);
}
sleep(10);
}
dup2(stdout1,1);
printf("Done\n");
}
//fg
else if (strcmp(p[0], "fg")==0) {
if (argc == 2) {
int j_no= atoi(p[1]) - 1;
if (j_no < njobs) {
int status;
ctrZ_flag=0;
kill(job[atoi(p[1])-1].pid , SIGCONT);
pid_t pid = waitpid(job[atoi(p[1])-1].pid, &status, WUNTRACED);
if(ctrZ_flag==1) {
printf("%s with pid:%d killed\n",job[j_no].name,pid);
remove_job(job[i].pid);
sleep(0);
}
}
else printf("No such job exists\n");
}
else printf("Wrong usage of fg\n");
}
//bg
else if (strcmp(p[0], "bg")==0) {
if (argc == 2) {
int j_no=atoi(p[1])-1;
if (j_no < njobs) {
kill(job[atoi(p[1])-1].pid , SIGCONT);
}
}
else printf("No shuch job exists\n");
}
//setenv
else if(strcmp(p[0], "setenv")==0) {
if (argc == 3) {
setenv(p[1], p[2], 1);
printf("Environment variable set:: %s : %s\n", p[1], p[2]);
}
else printf("Wrong usage of setenv\n");
}
//unsetenv
else if (strcmp(p[0], "unsetenv")==0) {
if (argc == 2) {
unsetenv(p[1]);
printf("Environment variable unset:: %s\n", p[1]);
}
else printf("Wrong usage of unsetenv\n");
}
//Need to use exec
else {
//pid=fork();
char **argv=malloc(265*sizeof(char*));
for (j = 0; j < argc && p[j][0] != '<' && p[j][0] != '>'; j++) { argv[j]=p[j]; }
//for (int ite = 0; ite < argc; ite++) printf("argv[%d] = %s\n", ite, argv[ite]);
pid = fork();
if(pid > 0) {
job[njobs].pid = pid; //current process pid
sprintf(job[njobs].name, "%s", p[0]);
jobnumber[pid] = njobs;
njobs++;
if(bg_flag != 1) {
//printf("not background\n");
ctrZ_flag = 0;
waitpid(pid,&status,WUNTRACED);
if(pid < 0) {
perror("Error!");
_exit(1);
}
if(ctrZ_flag==0) {
remove_job(pid); //no ctrl Z presses, job done
kill(pid,9);
}
else if (ctrZ_flag==1);
}
else
printf("[%d] %s \n",job[njobs-1].pid, job[njobs-1].name);
}
else if (pid==0) {
if(ipfile[0]!='\0') {
if (inputdup(ipfile) == 1) continue;
}
if(opfile[0]!='\0') {
if (outputdup(opfile) == 1) continue;
}
execvp(argv[0], argv); //removed err
dup2(stdin1, 0);
dup2(stdout1, 1);
_exit(1);
}
}
}
if(mpid==0 && mflag==1) { //in multiple command, if child is done, return to its parent for loop
exit(16);
}
}
return 0;
}