-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
1190 lines (1136 loc) · 34.2 KB
/
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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/resource.h>
#include <setjmp.h>
//#include <errorno.h>
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
pid_t curpid;
jmp_buf env;
int infile,outfile;
char inputfile[10];
char output[10];
typedef struct process
{
pid_t pid;
int bg_flag;
char proc_cmd[100];
}p;
p *jobs[1000];
typedef struct pipecommand
{
char *argv;
int redirectFlag;
}pc;
pid_t shell_pgid;
struct termios shell_tmodes;
int shell_terminal;
int shell_is_interactive;
int job_no;
void removeJob(pid_t pid)
{
//printf("Came to remove job with pid: %d, the total jobs are %d\n",pid,job_no );
int i=0,ptr=0;
for(i=1;i<=job_no;i++)
{
if(jobs[i]->pid==pid)
{
jobs[i]->pid=0;
//strcpy(jobs[i]->proc_cmd,"NULL");
jobs[i]->bg_flag=0;
}
}
}
p *addJob(pid_t pid, char *proc_cmd, int bg_flag, int job_no)
{
jobs[job_no]=malloc(sizeof(p));
jobs[job_no]->pid=pid;
strcpy(jobs[job_no]->proc_cmd,proc_cmd);
jobs[job_no]->bg_flag=bg_flag;
}
char *findJobCmd(pid_t pid)
{
int i=1;
for(i=1;i<=job_no;i++)
{
if(jobs[i]->pid==pid)
{
return (jobs[i]->proc_cmd );
}
}
return;
}
int findJobNo(pid_t pid)
{
int i=1;
for(i=1;i<=job_no;i++)
{
if(jobs[i]!=NULL && jobs[i]->pid==pid)
{
return (i);
}
}
return -1;
}
int findJobPid(int job_index)
{
int i=1,counter=1;
pid_t pid;
/* while(counter<job_index && i<=job_no)
{
i++;
if(jobs[i]!=NULL && jobs[i]->pid!=0)
{
counter++;
}
} */
if(jobs[job_index]!=NULL && jobs[job_index]->pid!=0)
{
return jobs[job_index]->pid;
}
return -1;
}
void handler(int signo)
{
//Will check if any of the childs have exited
pid_t pid;
int status;
while((pid = waitpid(-1, &status, WNOHANG)) > 0)
{ //will enter as long as there are children that have just exited, if pid = 0 that means theres a child in background.
char *proc_cmd=malloc(1025*1024*sizeof(char));
strcpy(proc_cmd,findJobCmd(pid));
WIFEXITED(status);
if (status==0)
{
fprintf(stderr,"[proc %s with pid %d exited normally]\n",findJobCmd(pid),pid);
}
else
{
fprintf(stderr,"[proc %d exited with code %d]\n",pid, WEXITSTATUS(status));
}
/*remove the pid from jobs list */
removeJob(pid);
}
}
void sig_tstp(int signno)
{
if(findJobNo(curpid)==-1)
{
fprintf(stderr, "\nNo foreground job running!\n");
longjmp(env,2);
return;
}
else
{
int i=findJobNo(curpid);
if(jobs[i]->bg_flag==0) //fg process
{
jobs[i]->bg_flag=1;
tcsetpgrp (STDIN_FILENO, getpgid(curpid));
tcsetpgrp (STDOUT_FILENO, getpgid(curpid));
printf("\n");
longjmp(env,2);
}
}
signal(SIGTSTP, sig_tstp); /* reestablish signal handler */
return;
}
void initShell()
{
/* See if we are running interactively. */
shell_terminal = STDIN_FILENO;
shell_is_interactive = isatty (shell_terminal);
if (shell_is_interactive)
{
/* Loop until we are in the foreground. */
while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
kill (- shell_pgid, SIGTTIN);
/* Ignore interactive and job-control signals. */
signal (SIGINT, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
// signal (SIGTSTP, SIG_IGN);
signal(SIGTSTP, sig_tstp);
signal (SIGTTIN, SIG_IGN);
signal (SIGTTOU, SIG_IGN);
//signal (SIGCHLD, SIG_IGN);
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = &handler;
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sa, 0) == -1)
{
perror(0);
//exit(1);
}
/* Put ourselves in our own process group. */
shell_pgid = getpid ();
if (setpgid (shell_pgid, shell_pgid) < 0)
{
perror ("Couldn't put the shell in its own process group");
exit (1);
}
/* Grab control of the terminal. */
tcsetpgrp (shell_terminal, shell_pgid);
/* Save default terminal attributes for shell. */
tcgetattr (shell_terminal, &shell_tmodes);
}
}
void getusername(char uname[]) //Function to get the username of the machine
{
register struct passwd *pw;
register uid_t uid;
int c;
uid = geteuid ();
pw = getpwuid (uid);
if (pw)
{
//char uname[100];
strcpy ( uname,(pw->pw_name) );
//printf("%s\n", uname);
//return uname;
}
//fprintf (stderr,"%s: cannot find username for UID %u\n","whoami", (unsigned) uid);
return;
//exit (EXIT_FAILURE);
}
int isBackgroundJob( char *com)
{
int len=strlen(com);
if(com[len-1]=='&')
{
return 1;
}
return 0;
}
int isPipeJob( char *com)
{
int len=strlen(com),pipes=0,i;
for(i=0;i<len;i++)
{
if(com[i]=='|')
{
pipes++;
}
}
return pipes;
}
int isRedirect( char *com)
{
//printf("came to check for redirectFlag\n" );
int len=strlen(com),counter=0,i;
infile=0;
outfile=0;
int flag=0;
for(i=0;i<len;i++)
{
if(com[i]=='>' && com[i+1]=='>') //append output
{
outfile=2;
flag=1;
}
else if(com[i]=='>') //output
{
outfile=1;
flag=1;
}
if(com[i]=='<') //input
{
infile=3;
flag=1;
}
}
return(flag);
}
int executeRedirect(char *cmd,char *com,int flag)
{
char *token=malloc(1024*sizeof(char));
if(outfile==1)
{
printf("Redirecting to output file!\n");
sscanf(com, "%*[^>]> %[^\n]", output);
outfile=1;
// strcpy(output,token);
}
if (outfile==2)
{
printf("Redirecting to output file!\n");
sscanf(com, "%*[^>]>> %[^\n]", output);
outfile=2;
}
if(infile==3)
{
printf("Redirecting from input file!\n");
sscanf(com, "%*[^<]< %[^>\n]", inputfile);
infile=3;
//strcpy(inputfile,token);
}
}
int isBuiltInCommand(char *cmd)
{
//printf("HERE 5\n");
if (strcmp(cmd,"cd")==0)
{
return 1;
}
else if (strcmp(cmd,"pwd")==0)
{
return 1;
}
else if (strcmp(cmd,"echo")==0)
{
return 1;
}
else if (strcmp(cmd,"pinfo")==0)
{
return 1;
}
else if (strcmp(cmd,"jobs")==0)
{
return 1;
}
else if (strcmp(cmd,"kjob")==0)
{
return 1;
}
else if ( strcmp(cmd,"overkill")==0)
{
return 1;
}
else if( strcmp(cmd,"fg")==0)
{
return 1;
}
else if( strcmp(cmd,"history")==0)
{
return 1;
}
return 0;
}
void executeBuiltInCommand(char *cmd, char *savedTokens[],int no_of_tokens, char *homedir)
{
if (strcmp("cd" , cmd)==0)
{
char *p;
if( savedTokens[1]==NULL ) //implementing default behaviour of cd. If no argument is provided cd goes to home dir of shell
{
p=malloc( (strlen(homedir))* sizeof(char) );
strcpy(p,homedir);
}
else
{
p=malloc( (strlen(savedTokens[1]))* sizeof(char) );
char *oldstr=savedTokens[1];
p=oldstr;
int i=2,len,lenp=0,lenold=0;
while(i<=no_of_tokens)
{
lenold=strlen(oldstr);
len=strlen(savedTokens[i]);
p=malloc( (len+lenp+1)* sizeof(char) );
strcpy(p,oldstr);
strcat(p,savedTokens[i]);
strcat(p," ");
oldstr=p;
i++;
}
}
if ( strcmp(p,"~") == 0)
{
strcpy(p,homedir);
}
if( chdir(p)!=0 ) //On success chdir returns 0
{
perror("Error! ");
}
}
else if(strcmp("pwd", cmd)==0)
{
char pwd_addr[256];
getcwd(pwd_addr , sizeof(pwd_addr) );
printf("%s\n",pwd_addr);
}
else if(strcmp("echo", cmd)==0) //has been implemented considering the " " should not be printed.
{
char *p=malloc( (strlen(savedTokens[1]))* sizeof(char) );
char *oldstr=savedTokens[1];
p=oldstr;
int i=2,len,lenp=0,lenold=0;
while(i<=no_of_tokens)
{
lenold=strlen(oldstr);
len=strlen(savedTokens[i]);
p=malloc( (len+lenp+1)* sizeof(char) );
strcpy(p,oldstr);
strcat(p," ");
strcat(p,savedTokens[i]);
oldstr=p;
i++;
}
len=strlen(p);
if(strcmp(p,"~")==0)
{
printf("%s\n",homedir );
return;
}
//printf("Final input for echo: %s\n",p );
int j=0,k=0,flag=0;
while(j<=len)
{
if(p[j]==34) //If P[J]== "
{
flag=0;
for(k=j+1;k<=len&&p[k]!=34;k++);
if(k!=len+1)
{
//printf("Entered the if cuz k is %d\n",k );
for (k = j+1 ;k <= len && p[k]!=34; k++)
{
printf("%c",p[k]);
}
} //End only after matching " has been found of nothing has been found at all.
else
{
printf("\nError! Terminating \" not found!\n");
break;
}
j=k;
}
else
{
printf("%c",p[j]);
flag=2;
}
j++;
}
printf("\n");
}
else if( strcmp("pinfo", cmd)==0 )
{
FILE *fp,*file;
char *line = NULL;
char *pidline = malloc(100 * sizeof(char));
char *statusline = malloc(100 * sizeof(char));
char *memline = malloc(100 * sizeof(char));
size_t len;
char read;
if( savedTokens[1]==NULL )
{
fp=fopen("/proc/self/status", "r");
file=fopen("/proc/self/smaps", "r");
}
else
{
//printf("%s\n",cmd );
//printf("%s\n",savedTokens[1] );
char *addline=malloc(50 * sizeof(char));
char *addlinecopy=malloc(50 * sizeof(char));
strcpy(addline,"/proc/");
strcat(addline,savedTokens[1]);
strcpy(addlinecopy,addline);
strcat(addline,"/status");
strcat(addlinecopy,"/smaps");
file=fopen(addlinecopy, "r");
fp=fopen(addline, "r");
if (!fp)
{
fprintf(stderr, "Error! PID doesnt exist!\n" );
return;
}
}
int *flag=calloc(2,sizeof(int));
while ((read = getline(&line, &len, fp)) != -1)
{
if ( strncmp("Pid:",line,4)==0 )
{
flag[0]=1;
strcpy(pidline,line);
}
if ( strncmp("State:",line,6)==0 )
{
flag[1]=1;
strcpy(statusline,line);
}
if ( strncmp("VmSize:",line,7)==0 )
{
flag[2]=1;
strcpy(memline,line);
}
}
if(flag[0]==1)
{
printf("%s\n",pidline );
}
else
{
fprintf(stderr,"PID not found!\n");
}
if(flag[1]==1)
{
printf("%s\n",statusline );
}
else
{
fprintf(stderr,"Status not found!\n");
}
if(flag[2]==1)
{
printf("%s\n",memline );
}
else
{
fprintf(stderr,"Virtual memory size not found!\n");
}
if (!file)
{
fprintf(stderr, "Executable Path could not be found!\n" );
return;
}
while ((read = getline(&line, &len, file)) != -1)
{
char *lcopy=malloc(strlen(line) * sizeof(char));
strcpy(lcopy,line);
char *token=strtok(lcopy, "/");
int i=0;
for(i=0;token[i]==line[i];i++);
printf("Executable Path: %s\n",line+i );
break;
/* code */
}
}
else if(strcmp("jobs",cmd)==0)
{
int i=1,j=1;
for(i=1;i<=job_no;i++)
{
if(jobs[i]->pid!=0)
{
printf("[%d] %s [%d]\n",i,jobs[i]->proc_cmd,jobs[i]->pid );
}
}
}
else if(strcmp("kjob",cmd)==0)
{
if(no_of_tokens!=2)
{
fprintf(stderr, "Insufficient number of arguments!\n" );
return;
}
int jobno=atoi(savedTokens[1]);
int signo=atoi(savedTokens[2]);
pid_t pid=findJobPid(jobno);
if(pid!=-1)
{
if(signo==9)
{
removeJob(pid);
}
if(kill(pid,signo)!=0)
{
fprintf(stderr, "Error in sending signal! \n");
}
}
else
{
perror("Process not found!");
}
}
else if(strcmp("overkill",cmd)==0)
{
int i;
pid_t pid;
for(i=1;i<=job_no;i++)
{
pid=findJobPid(i);
if(pid!=-1)
{
jobs[i]->pid=0;
//strcpy(jobs[i]->proc_cmd,"NULL");
jobs[i]->bg_flag=0;
}
if(pid!=-1 && pid!=0)
kill(pid,9);
}
}
else if(strcmp("fg",cmd)==0)
{
if(no_of_tokens!=1)
{
fprintf(stderr, "Insufficient number of arguments!\n" );
return;
}
int jobno=0;
if(savedTokens[1]!=NULL)
jobno=atoi(savedTokens[1]);
int pid=findJobPid(jobno);
if(pid==-1)
{
fprintf(stderr, "PID not found!\n" );
}
else
{
int status;
kill(getpgid(pid),SIGTSTP);
//do
//{
/* transfer controlling terminal */
//removeJob(pid);
int i=findJobNo(pid);
jobs[i]->bg_flag=0;
if ( tcsetpgrp (STDIN_FILENO, getpgid(pid)) < 0)
{
perror("tcsetpgrp 2");
}
if ( kill(pid, SIGCONT) < 0) {
perror("kill:sending SIGCONT");
}
waitpid(pid,&status,WUNTRACED);
// } while (!WIFEXITED(status) && !WIFSIGNALED(status));
/* Children completed: put the shell back in the foreground. */
if( tcsetpgrp (STDIN_FILENO, getpgrp()) < 0) {
perror("tcsetpgrp 2");
}
}
}
else if(strcmp(cmd,"history")==0)
{
int i=1;
for(i=1;i<=job_no && jobs[i]!=NULL;i++)
{
printf("[%d] %s\n",i,jobs[i]->proc_cmd);
}
}
}
void executeCommand(char *cmd,char**savedTokens,int no_of_tokens)
{
//execvp("name of command",argvector)
int i=0;
savedTokens[no_of_tokens+1]=NULL;
execvp(savedTokens[0],savedTokens);
//perror(savedTokens[0]);
}
int parseCommand(char *command, char **argv,int redirectFlag)
{
int i=0,no_of_tokens=0;
char *token=malloc(1024*sizeof(char));
if(redirectFlag!=0)
{
executeRedirect(argv[0],command,redirectFlag);
}
token = strtok (command,">");
token = strtok (command,"<");
token = strtok (command," \t\n" );
if(token==NULL)
return;
while (token!=NULL)
{
argv[i]=token;
i++;
token=strtok(NULL," \t\n");
}
argv[i]=NULL;
no_of_tokens=i-1;
char *cmd=argv[0];
return(no_of_tokens);
}
int spawn_proc(int in,int out,pc node,int redirectFlag,char *homedir)
{
int no_of_tokens=0,std_out;
pid_t pid;
char **argv=malloc ( 100 * sizeof(char)); //Number of tokens there can be
pid=fork();
if(pid<0)
{
fprintf(stderr, "Error in Piping!\n" );
}
else if(pid==0) /* Child */
{
infile=0;
outfile=0;
int random=isRedirect(node.argv);
no_of_tokens=parseCommand(node.argv,argv,redirectFlag);
if(in!=0)
{
dup2(in,0);
close(in);
}
if(out!=1)
{
dup2(out,1);
close(out);
}
if(outfile!=0)
{
int fd1;
if(outfile==1)
{
fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666);
lseek(fd1, 0, SEEK_SET);
}
else if(outfile==2)
{
fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666);
}
if(fd1==-1)
{
fprintf(stderr, "Can't open file for output!\n");
memset(output, 0, 10);
outfile=0;
return;
}
int std_out=dup(1);
dup2(fd1,STDOUT_FILENO);
close(fd1);
memset(output, 0, 10);
outfile=0;
//redirectFlag=0;
}
if(infile==3)
{
int fd2;
fd2=open(inputfile,O_RDWR,00666);
if(fd2==-1)
{
fprintf(stderr, "Can't open file for input! 2\n");
memset(inputfile, 0, 10);
infile=0;
return;
}
dup2(fd2,STDIN_FILENO);
close(fd2);
memset(inputfile, 0, 10);
infile=0;
//redirectFlag=0;
}
if(isBuiltInCommand(argv[0])==1)
{
job_no++;
addJob(0,argv[0],0,job_no);
executeBuiltInCommand(argv[0],argv,no_of_tokens,homedir);
dup2(std_out,1);
_exit(1);
}
else
return (execvp(argv[0],argv));
}
return pid;
}
int executePipe( int no_of_pipes, char *com, int redirectFlag,char *homedir)
{
pc *pcmd=malloc(100 * sizeof(pc));
int fd[2];
int isFirstPipe=1;
int count = 0;
char *commandline=malloc(1024*sizeof(char));
strcpy(commandline,com);
char *command=malloc(1024*sizeof(char));
char **argv=malloc ( 100 * sizeof(char)); //Number of tokens there can be
char *token=malloc(1024*sizeof(char));
int i=0,j=0;
command = strtok ( com,"|"); //first command
while( command!=NULL)
{
pcmd[i].argv=command;
pcmd[i].redirectFlag=isRedirect(command);
command = strtok (NULL, "|");
i++;
}
//Tokenise command for execution
//parseCommand(pcmd[0].argv,argv);
int in=0;
for(i=0;i<no_of_pipes;i++)
{
pipe(fd);
spawn_proc(in,fd[1],pcmd[i],pcmd[i].redirectFlag,homedir);
close(fd[1]);
in=fd[0];
}
if(in!=0)
dup2(in,0);
//last command
infile=0;
outfile=0;
int random=isRedirect(pcmd[i].argv);
int no_of_tokens=parseCommand(pcmd[i].argv,argv,pcmd[i].redirectFlag);
int std_out=dup(1);
if(outfile!=0)
{
int fd1;
if(outfile==1)
{
fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666);
lseek(fd1, 0, SEEK_SET);
}
else if(outfile==2)
{
fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666);
}
if(fd1==-1)
{
fprintf(stderr, "Can't open file for output 1!\n");
memset(output, 0, 10);
outfile=0;
return;
}
dup2(fd1,STDOUT_FILENO);
close(fd1);
memset(output, 0, 10);
outfile=0;
//redirectFlag=0;
}
if(infile==3)
{
int fd2;
fd2=open(inputfile,O_RDWR,00666);
if(fd2==-1)
{
fprintf(stderr, "Can't open file for input! 3\n");
memset(inputfile, 0, 10);
infile=0;
return;
}
dup2(fd2,STDIN_FILENO);
close(fd2);
memset(inputfile, 0, 10);
infile=0;
//redirectFlag=0;
}
if(isBuiltInCommand(argv[0])==1)
{
job_no++;
addJob(0,argv[0],0,job_no);
executeBuiltInCommand(argv[0],argv,no_of_tokens,homedir);
_exit(1);
dup2(std_out,1);
return(0);
}
else
return(execvp(argv[0],argv));
}
int main(int argc, char *argv[])
{
initShell();
//signal(SIGCHLD, SIG_IGN);
//Declarations
int bytes_read,no_of_tokens,no_of_commands=0,redirectFlag=0;
char uname[80],homedir[256],input[1024],hostname[80],tempstr[256],cwd[256];
char *cmdline, *sentence, *line, *token,**savedTokens, **command, *cmd, *mcptr, *com;
size_t length,homedirlen;
pid_t childPid;
//Change Shell home dir to ~
getcwd(homedir , sizeof(homedir) );
getusername(uname);
homedirlen=strlen(homedir);
strcpy(cwd,homedir);
/* int ptr=0;
for(ptr=0;ptr<=1000;ptr++)
{
jobs[ptr]=NULL;
}*/
int exit_flag=0;
while (1)
{
sigset_t mask, prevmask;
//Initialize mask with just the SIGCHLD signal
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &prevmask); /*block SIGCHLD, get previous mask*/
no_of_tokens=0;
command=malloc ( 200 * sizeof(char)); //Number of commands there can be will be stored in 2D Array
cmdline = (char *) malloc (1025 * sizeof(char));
line = (char *) malloc (1025 * sizeof(char));
cmd = (char *) malloc (1025 * sizeof(char));
savedTokens=malloc ( 100 * sizeof(char)); //Number of tokens there can be
strcpy(line,"\n");
gethostname(hostname, sizeof(hostname));
getcwd(cwd , sizeof(cwd) );
//printf("PRINT THIS : %s\n",cwd+homedirlen );
//printf("CWD: %d HOMEDIR: %d\n",strlen(cwd),strlen(homedir) );
if( strncmp( cwd, homedir, homedirlen-1) == 0) // && strncmp( cwd, homedir, homedirlen-1)!=0) //If the current working directory is not ~
{
strcpy(tempstr,"~");
//printf("HOME DIR IS: %s\n",tempstr );
strcat(tempstr,cwd+homedirlen);
strcpy(cwd, tempstr);
}
int jumper=setjmp(env);
printf("<%s@%s:%s>",uname,hostname,cwd ); //PROMPT
getline (&line, &length+1, stdin);
//PARSING:
//Stage 1: Handling multiple commands:
int k=0;
token = strtok (line, ";");
command[k]=token;
while ( token!=NULL )
{
command[k]=token;
token = strtok (NULL,";");
k++;
}
no_of_commands=k-1;
if(no_of_commands==-1)
{
printf("Exiting main shell!\n");
printf("\n");
return 0;
}
else if(command[no_of_commands]!=NULL)
{
int len=strlen(command[no_of_commands]);
command[no_of_commands][len-1]=0; //Last token gets an extra \n .. therefore removed here.
}
//STAGE 2:
for(k=0;k<=no_of_commands;k++)
{
/* sigset_t mask, prevmask;
//Initialize mask with just the SIGCHLD signal
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &prevmask); /*block SIGCHLD, get previous mask*/
cmdline = command[k];
com = (char *) malloc (1025 * sizeof(char));
if(command[k]!=NULL)
strcpy(com,command[k]); //com stores the whole command to be executed
else
com=NULL;
//Stage 3: Piping
int no_of_pipes=0;
if(com!=NULL)
{
redirectFlag=isRedirect(com);
no_of_pipes=isPipeJob(com);
}
if(no_of_pipes!=0)
{
int status;
pid_t procid=fork();
if(procid==0)
{
executePipe(no_of_pipes,com,redirectFlag,homedir);
}
else
{
sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking
wait(&status);
}
}
else
{
int i=0;
token = strtok(cmdline,">");
token = strtok(cmdline,"<");
token = strtok(cmdline," \t\n");
if(token==NULL)
{
no_of_commands=-1;
}
while(token != NULL)
{
savedTokens[i]=token;
i++;
token = strtok (NULL, " \t\n");
}
if(i!=0)
{
no_of_tokens=i-1;
cmd=savedTokens[0];
}
else
{
no_of_tokens=0;
cmd=NULL;
}