-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexecCmd.c
615 lines (607 loc) · 15.5 KB
/
execCmd.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
#include "custom_header.h"
const char *SUPPORTED_CMD[]={
"pwd","cd","echo","exit","ls","pinfo","clock","remindme","setenv","unsetenv",
"jobs","kjob","fg","bg","overkill","quit","mkdir","mgdir"};
struct Process
{
int pid;
char cmd[1024];
int status; //status 1 shows running 0 shows stopped
};
struct Process backgroundProcess[1024];
int processPointer=0;
int findCmdNo(char *cmd)
{
int i,n;
n=strlen(cmd);
int count=0;
for(i=0;i<n;i++)
if(cmd[i]==' ')
break;
count=i;
char *command = (char *)malloc(sizeof(char)*count+2);
for(i=0;i<count;i++)
command[i]=cmd[i];
command[i]='\0';
int totalCommand = sizeof(SUPPORTED_CMD)/sizeof(char*);
for(i=0;i<totalCommand;i++)
if(!strcmp(command,SUPPORTED_CMD[i]))
{
free(command);
return i;
}
free(command);
return -1;
}
int exec_pk_ls(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
int aflag=0;
int lflag=0;
int numberOfParameter = 1;
if(argc==1)
return cmd_pk_ls(".");
else if(argc==2 && argv[1][0]!='-')
return cmd_pk_ls(argv[1]);
if(argc>=2 && argv[1][0]=='-')
{
numberOfParameter++;
if(!strcmp("-a",argv[1]))
aflag=1;
else if(!strcmp("-l",argv[1]))
lflag=1;
else if(!strcmp("-la",argv[1])||!(strcmp("-al",argv[1])))
aflag=lflag=1;
else
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Wrong flag used with ls");
return -1;
}
}
if(argc>=3 && argv[2][0]=='-')
{
numberOfParameter++;
if(!(strcmp("-a",argv[2])))
aflag=1;
else if(!(strcmp("-l",argv[2])))
lflag=1;
else if(!strcmp("-la",argv[2])||!(strcmp("-al",argv[2])))
aflag=lflag=1;
else
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Wrong flag used with ls");
return -1;
}
}
if(numberOfParameter==argc)
{
if(aflag && lflag)
return cmd_pk_ls_l_a(".");
else if(aflag)
return cmd_pk_ls_a(".");
else if(lflag)
return cmd_pk_ls_l(".");
else
return cmd_pk_ls(".");
}
int i;
for(i=numberOfParameter;i<argc;i++)
{
if(aflag && lflag)
return cmd_pk_ls_l_a(argv[i]);
else if(aflag)
return cmd_pk_ls_a(argv[i]);
else if(lflag)
return cmd_pk_ls_l(argv[i]);
else
return cmd_pk_ls(argv[i]);
}
return 0;
}
int exec_pk_mkdir(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if((argc!=2 && argc!=4) || (argc==4 && strcmp(argv[1],"-p")))
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as mkdir <relativePath> or mkdir -p <relativePath> <permissions>");
return -1;
}
int check =0;
if(argc==4)
check = mkdir(argv[2],atoi(argv[3]));
else
check = mkdir(argv[1],0700);
if(check<0)
perror("Its_PKS_Shell");
return check;
}
int exec_pk_pwd()
{
char *path = (char *)malloc(1000);
getcwd(path,1000);
printf("%s\n",path);
free(path);
return 0;
}
int exec_pk_cd(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc!=2 && argc!=1)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as cd <relativePath> or just cd");
return -1;
}
if(argc==1 || !strcmp(argv[1],"~"))
{
int check = chdir(getenv("PWD"));
if(check<0)
perror("Its_PKS_Shell");
}
else
{
int check = chdir(argv[1]);
if(check<0)
perror("Its_PKS_Shell");
}
return 0;
}
int exec_pk_echo(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
for(int i=1;i<argc;i++)
printf("%s ",argv[i]);
printf("\n");
return 0;
}
int exec_pk_pinfo(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc==1)
return exec_pinfo(0);
else
for(int i=1;i<argc;i++)
exec_pinfo(atoi(argv[i]));
return 0;
}
int exec_pk_remindme(char * cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc<3)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as remindme <interval> <msg>");
return -1;
}
return exec_reminder(cmd);
}
int exec_pk_clock(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc!=3 && argc!=5)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as clock -t <interval> or clock -t <interval> -n <times>");
return -1;
}
else if(strcmp(argv[1],"-t"))
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as clock -t <interval>");
return -1;
}
else if(argc==3)
return exec_clock(atoi(argv[2]),1000);
else if(strcmp(argv[3],"-n"))
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as clock -t <interval>");
return -1;
}
else
return exec_clock(atoi(argv[2]),atoi(argv[4]));
}
int exec_pk_setenv(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
int check = 0;
if(argc!=2 && argc!=3)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as setenv <variable> or setenv <variable> <value>");
return -1;
}
else if(argc==3)
check = setenv(argv[1],argv[2],1);
else
check = setenv(argv[1],"",1);
if(check<0){
perror("Its_PKS_Shell::Setenv:");
return -1;
}
return 0;
}
int exec_pk_unsetenv(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc!=2)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as unsetenv <variable>");
return -1;
}
int check = unsetenv(argv[1]);
if(check<0){
perror("Its_PKS_Shell::Setenv:");
return -1;
}
return 0;
}
int exec_pk_fg(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc!=2)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as fg <process index from jobs>");
return -1;
}
int pindex = atoi(argv[1]);
int i,count=0;
for(i=0;i<processPointer;i++)
{
if(backgroundProcess[i].pid)
count++;
if(count==pindex)
{
int status;
pid_t wpid = waitpid(backgroundProcess[i].pid,&status,WUNTRACED|WCONTINUED);
int check = kill(backgroundProcess[i].pid,SIGCONT);
//Remove from background process array
if(!check)
backgroundProcess[i].pid=0;
return check;
}
}
if(i==processPointer)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Not valid process index use jobs for index\n");
return -1;
}
return 0;
}
int exec_pk_bg(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
if(argc!=2)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as bg <process index from jobs>");
return -1;
}
int pindex = atoi(argv[1]);
int i,count=0;
for(i=0;i<processPointer;i++)
{
if(backgroundProcess[i].pid)
count++;
if(count==pindex)
{
int check = kill(backgroundProcess[i].pid,SIGCONT);
backgroundProcess[i].status = 1;
return check;
}
}
if(i==processPointer)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Not valid process index use jobs for index\n");
return -1;
}
return 0;
}
int exec_pk_overkill()
{
printf("Its_PKS_Shell:overkill will kill all background processes are you sure (y/n) ?");
char ch;
scanf("%c",&ch);
if(ch!='y' && ch!='Y')
return -1;
int i,count=0;
for(i=0;i<processPointer;i++)
if(backgroundProcess[i].pid)
kill(backgroundProcess[i].pid,9);
return 1;
}
int exec_pk_jobs(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
int check = 0;
if(argc!=1)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as jobs\n");
return -1;
}
int count=0;
for(int i=0;i<processPointer;i++)
{
if(backgroundProcess[i].pid)
{
count++;
if(backgroundProcess[i].status)
printf("\t[%d]\trunning\t%s\t[%d]\n",count,backgroundProcess[i].cmd,backgroundProcess[i].pid);
else
printf("\t[%d]\tstopped\t%s\t[%d]\n",count,backgroundProcess[i].cmd,backgroundProcess[i].pid);
}
}
return 0;
}
int exec_pk_kjob(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
int check = 0;
if(argc!=3)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Enter as kjob <process index> <signal number>\n");
return -1;
}
int count=0,i;
int pid = atoi(argv[1]);
int sig = atoi(argv[2]);
for(i=0;i<processPointer;i++)
{
if(backgroundProcess[i].pid)
count++;
if(count==pid)
{
int check = kill(backgroundProcess[i].pid,sig);
return check;
}
}
if(i==processPointer)
{
fprintf(stderr,"%s\n","Its_PKS_Shell:Not valid process index use jobs for index\n");
return -1;
}
return 0;
}
//function to ensure that shell itself doesn't get killed by signal
void CtrlCHandler(int sig_num)
{
if(CURR_FOREGROUND==-1)
return;
kill(CURR_FOREGROUND,SIGINT);
fflush(stdout);
}
void CtrlZHandler(int sig_num)
{
if(CURR_FOREGROUND==-1)
return;
kill(CURR_FOREGROUND,SIGTSTP);
backgroundProcess[processPointer].pid = CURR_FOREGROUND;
backgroundProcess[processPointer].status = 0;
strcpy(backgroundProcess[processPointer].cmd,CURR_FOREGROUND_NAME);
processPointer++;
printf("[+] %s [%d]\n",CURR_FOREGROUND_NAME,CURR_FOREGROUND);
fflush(stdout);
}
int launch_cmd(char *cmd)
{
char **argv = argumentize(cmd);
int argc = argCount(argv);
pid_t pid,wpid;
//background is defined when last argument is &
int background = 0;
int status=0;
if(!strcmp(argv[argc-1],"&"))
background = 1;
pid = fork();
if(pid<0)
{
//fork error
perror("It's PK's Shell");
_exit(1);
}
else if(!pid)
{
//child process should call execvp
if(background)
argv[argc - 1] = NULL;
int check = execvp(argv[0],argv);
if(check<0)
{
perror("It's PK's Shell");
//If not killed multiple copies of shell would open
_exit(1);
}
}
else if(!background)
{
//parent Process for forground Process
//Set global foreground process pid
CURR_FOREGROUND = pid;
CURR_FOREGROUND_NAME = argv[0];
wpid = waitpid(pid,&status,WUNTRACED);
if(wpid<0)
{
perror("It's PK's Shell");
}
//}while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
else
{
kill(pid,SIGCONT);
//parent process for background Process
backgroundProcess[processPointer].pid = pid;
backgroundProcess[processPointer].status = 1;
strcpy(backgroundProcess[processPointer].cmd,argv[0]);
processPointer++;
printf("[+] %s [%d]\n",argv[0],pid);
}
return status;
}
int checkBackgroud()
{
int wpid,status;
for(int i=0;i<processPointer;i++)
{
if(backgroundProcess[i].pid!=0)
{
wpid = waitpid(backgroundProcess[i].pid,&status,WNOHANG);
if(wpid==backgroundProcess[i].pid)
{
printf("Its_PKs_Shell: %s with pid %d exited with status %d\n",backgroundProcess[i].cmd,wpid,WIFEXITED(status));
backgroundProcess[i].pid=0;
}
}
}
return 0;
}
int execCmd(char *cmd)
{
int original_stdout = dup(1);
int original_stdin = dup(0);
char *str = checkInputRedirection(cmd);
if(str)
{
//printf("Input redirect %s\n",str);
int fd;
// open the file to replace stdout
fd = open(str, O_RDONLY);
if(fd == -1) {
perror("Failed to open file");
}
close(0);
// use dup2() to duplicate the fd
if(dup2(fd, 0) != 0) // 1 refers to stdout
perror("dup2 fail");
// close the original fd
close(fd);
}
str = checkOutputRedirection(cmd);
if(str)
{
int fd;
// open the file to replace stdout
fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(fd == -1) {
perror("Failed to open file");
}
close(1);
// use dup2() to duplicate the fd
if(dup2(fd, 1) != 1) // 1 refers to stdout
perror("dup2 fail");
// close the original fd
close(fd);
}
str = checkAppendRedirection(cmd);
if(str)
{
int fd;
// open the file to replace stdout
fd = open(str, O_WRONLY | O_CREAT | O_APPEND, 0644);
if(fd == -1) {
perror("Failed to open file");
}
close(1);
// use dup2() to duplicate the fd
if(dup2(fd, 1) != 1) // 1 refers to stdout
perror("dup2 fail");
// close the original fd
close(fd);
}
int status = 0;
int commandNumber = findCmdNo(cmd);
if(commandNumber==-1)
{
status = launch_cmd(cmd);
}
switch (commandNumber)
{
case 0:
//pwd command
status = exec_pk_pwd();
break;
case 1:
//cd command
status = exec_pk_cd(cmd);
break;
case 2:
//echo command
status = exec_pk_echo(cmd);
break;
case 3:
//exit command
_exit(0);
break;
case 4:
//ls command
status = exec_pk_ls(cmd);
break;
case 5:
//pinfo
status = exec_pk_pinfo(cmd);
break;
case 6:
//self implemented clock
status = exec_pk_clock(cmd);
break;
case 7:
//self implemented reminder
status = exec_pk_remindme(cmd);
break;
case 8:
//self implemented setenv
status = exec_pk_setenv(cmd);
break;
case 9:
//self implemented unsetenv
status = exec_pk_unsetenv(cmd);
break;
case 10:
//self implemented jobs background job handler
status = exec_pk_jobs(cmd);
break;
case 11:
//self implemented unsetenv
status = exec_pk_kjob(cmd);
break;
case 12:
//self implemented fg
status = exec_pk_fg(cmd);
break;
case 13:
//self implemented bg
status = exec_pk_bg(cmd);
break;
case 14:
//self implemented overkill
status = exec_pk_overkill();
break;
case 15:
//self implemented quit
status = exec_pk_overkill();
_exit(0);
break;
case 16:
//self implemented mkdir
status = exec_pk_mkdir(cmd);
break;
case 17:
//self implemented mkdir
status = exec_pk_mkdir(cmd);
if(!status)
status = exec_pk_cd(cmd);
break;
}
//Restore the original I\O field
dup2(original_stdout,1);
dup2(original_stdin,0);
close(original_stdout);
close(original_stdin);
checkBackgroud();
return status;
}