-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsh.c
1097 lines (993 loc) · 28.4 KB
/
sh.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 <string.h>
#include <strings.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <glob.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/loadavg.h>
#include <utmpx.h>
#include "sh.h"
int sh( int argc, char **argv, char **envp ) {
signal(SIGINT, sig_handle);
signal(SIGTERM, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
char *prompt = calloc(PROMPTMAX, sizeof(char));
char *commandline = calloc(MAX_CANON, sizeof(char));
char *command, *arg, *aliashelp, *commandpath, *p, *pwd, *owd;
/* Use these two save chars for strtok_r on command and alias commands*/
char *command_save, *alias_save;
char **args = calloc(MAXARGS, sizeof(char*));
int uid, i, status, argsct, a, go = 1;
int background, bg_pid = 0;
struct passwd *password_entry;
char *homedir;
struct pathelement *pathlist, *history;
struct aliaselement *aliases;
const char sp[2] = " ";
extern char **environ;
glob_t globbuf;
size_t glc;
char **gl;
pthread_t pt_warnload, pt_watchuser;
/* for use with warnload */
float load = 0.0;
int load_thread = 1;
/* for use with watchuser */
static pthread_mutex_t user_lock = PTHREAD_MUTEX_INITIALIZER;
int user_thread = 1;
struct userarg *userargs;
/* for use with file redirection */
int fid;
int file_redirect, error_redirect, input_redirect = 0;
int noclobber = 0;
/* for use with pipe */
int ipc = 0;
int ipc_err = 0;
int pipefd[2];
char **pipeargs = calloc(5, sizeof(char*));
uid = getuid();
password_entry = getpwuid(uid); /* get passwd info */
homedir = getenv("HOME"); /* get homedir */
if ( (pwd = getcwd(NULL, PATH_MAX+1)) == NULL ) {
perror("getcwd");
exit(2);
}
owd = calloc(strlen(pwd) + 1, sizeof(char));
memcpy(owd, pwd, strlen(pwd));
prompt[0] = ' '; prompt[1] = '\0';
/* Put PATH into a linked list */
pathlist = get_path();
/* By default, we have no history or aliases or users */
history = NULL;
aliases = NULL;
userargs = NULL;
while ( go ) {
/* wait on background processes */
bg_pid = waitpid(-1, &status, WNOHANG);
if(bg_pid > 0) {
printf("Background child [%d] exited with status: %d\n", bg_pid, WEXITSTATUS(status));
}
/* print prompt */
printf("\n");
printf(prompt);
printf(" [");
printf(pwd);
printf("]> ");
i = 0;
/* get command line and process */
while (fgets(commandline, MAX_CANON, stdin) != NULL) {
/* wait on background processes */
bg_pid = waitpid(-1, &status, WNOHANG);
if(bg_pid > 0) {
printf("Background child [%d] exited with status: %d\n", bg_pid, WEXITSTATUS(status));
}
if (commandline[strlen(commandline) - 1] == '\n') {
commandline[strlen(commandline) - 1] = 0;
}
/* Add the command to history */
history = add_last(history, commandline);
/* Get the command */
command = strtok_r(commandline, sp, &command_save);
if(command == NULL) {
break;
}
/* Search for aliases */
a = 0;
struct aliaselement *alias = aliases;
while(alias != NULL) {
if(strcmp(command, alias->name) == 0) {
a = 1;
break;
}
alias = alias->next;
}
/* If we have an alias */
i = 0;
if(a) {
/* parse alias command */
arg = calloc(strlen(alias->command) + 1, sizeof(char));
strcpy(arg, alias->command);
aliashelp = strtok_r(arg, sp, &alias_save);
while(aliashelp != NULL) {
if(i == MAXARGS) {
strcpy(args[0], "maxargs");
break;
}
args[i] = calloc(strlen(aliashelp) + 1, sizeof(char));
strcpy(args[i], aliashelp);
aliashelp = strtok_r(NULL, sp, &alias_save);
i++;
}
command = strtok_r(NULL, sp, &command_save);
free(arg);
}
/* parse command line or remainder of command line if we have an alias */
while (command != NULL) {
if(i == MAXARGS) {
strcpy(args[0], "maxargs");
break;
}
args[i] = calloc(strlen(command) + 1, sizeof(char));
strcpy(args[i], command);
command = strtok_r(NULL, sp, &command_save);
i++;
}
/* SANITY CHECK: make sure the user passed in something */
if(args[0] == NULL) {
break;
}
/* Expand wildcard characters */
glob(args[0], GLOB_NOCHECK, NULL, &globbuf);
i = 1;
while(args[i] != NULL) {
glob(args[i], GLOB_APPEND | GLOB_NOCHECK, NULL, &globbuf);
i++;
}
/* gl becomes our arguments, it is the expanded version of args */
gl = globbuf.gl_pathv;
/* glc is the number of arguments. Use it for checking built in commands */
glc = globbuf.gl_pathc;
/* Check for background & at end of last argument */
char* last_arg = gl[glc - 1];
char last_char = last_arg[(strlen(last_arg) - 1)];
if(strcmp(&last_char, "&") == 0) {
last_arg[(strlen(last_arg) - 1)] = '\0';
background = 1;
}
/* Check for file output redirection (without appending) */
if(glc > 2 && (strcmp(gl[glc-2], ">") == 0 || strcmp(gl[glc-2], ">&") == 0)) {
/* Don't overwrite an existing file if noclobber is set */
if(noclobber) {
if(access(gl[glc-1], F_OK) == 0) {
fprintf(stderr, "%s: File exists.\n", gl[glc-1]);
free_args(args);
break;
}
}
/* Redirect the output to given file */
fid = open(gl[glc-1], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP);
if(fid == -1) {
perror(gl[glc-1]);
free_args(args);
break;
}
close(1);
dup(fid);
/* Redirect error output to the given file */
if(strcmp(gl[glc-2], ">&") == 0) {
close(2);
dup(fid);
error_redirect = 1;
}
close(fid);
file_redirect = 1;
/* Hide redirection character and filename */
gl[glc-2] = '\0';
gl[glc-1] = '\0';
glc = glc - 2;
}
/* Check for file output redirection (with appending) */
if(glc > 2 && (strcmp(gl[glc-2], ">>") == 0 || strcmp(gl[glc-2], ">>&") == 0)) {
/* Don't append to a file that doesn't exist */
if(noclobber) {
if(access(gl[glc-1], F_OK) != 0) {
fprintf(stderr, "%s: No such file or directory.\n", gl[glc-1]);
free_args(args);
break;
}
}
/* Redirect the output to given file */
fid = open(gl[glc-1], O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
if(fid == -1) {
perror(gl[glc-1]);
free_args(args);
break;
}
close(1);
dup(fid);
/* Redirect error output to the given file */
if(strcmp(gl[glc-2], ">>&") == 0) {
close(2);
dup(fid);
error_redirect = 1;
}
close(fid);
file_redirect = 1;
/* Hide redirection character and filename */
gl[glc-2] = '\0';
gl[glc-1] = '\0';
glc = glc - 2;
}
/* Check for file input redirection */
if(glc > 2 && strcmp(gl[glc-2], "<") == 0) {
/* Can't take input from a nonexisistent file */
if(access(gl[glc-1], F_OK) != 0) {
fprintf(stderr, "%s: No such file or directory.\n", gl[glc-1]);
free_args(args);
break;
}
/* Open the file to read from */
fid = open(gl[glc-1], O_RDONLY);
if(fid == -1) {
perror(gl[glc-1]);
free_args(args);
break;
}
close(0);
dup(fid);
close(fid);
input_redirect = 1;
/* Hide redirection character and filename */
gl[glc-2] = '\0';
gl[glc-1] = '\0';
glc = glc - 2;
}
/* Check for inter-process communication */
if(glc > 2 && (strcmp(gl[1], "|") == 0 || strcmp(gl[1], "|&") == 0)) {
ipc = 1;
if(strcmp(gl[1], "|&") == 0) {
ipc_err = 1;
}
/* Create our pipe */
if(pipe(pipefd) == -1) {
perror("pipe");
free_args(args);
break;
}
/* Put the command to the right of the pipe in a second arguments list */
i = 2;
int p = 0;
while(gl[i] != NULL) {
pipeargs[p] = malloc(strlen(gl[i]) + 1 * sizeof(char));
pipeargs[p] = gl[i];
i++; p++;
}
gl[1] = '\0';
glc = 1;
}
/* check for each built in command and implement */
/* Built in warnload */
if(strcmp(gl[0], "warnload") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
char *end;
if(glc != 2) {
printf("Usage: warnload LOAD\n");
} else if (strcmp(gl[1], "0.0") != 0 && (strtof(gl[1], &end) == 0 || strtof(gl[1], &end) < 0)) {
printf("LOAD must be a positive floating point number\n");
} else if (strcmp(gl[1], "0.0") == 0) {
load = 0.0;
load_thread = 1;
} else {
load = strtof(gl[1], &end);
if(load_thread != 0) {
load_thread = 0;
pthread_create(&pt_warnload, NULL, warnload, &load);
}
}
}
/* Built in watchuser */
else if(strcmp(gl[0], "watchuser") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc < 2) {
fprintf(stderr, "watchuser: Too few arguements.\n");
} else if(glc > 3) {
fprintf(stderr, "watchuser: Too many arguments.\n");
} else if (glc == 3 && strcmp(gl[2], "off") != 0) {
printf("Usage: watchuser USERNAME [off]\n");
} else if (glc == 3 && strcmp(gl[2], "off") == 0 ){
/* remove the given username from watched users list */
if(user_thread == 0) {
pthread_mutex_lock(&user_lock);
userargs->users = remove_user(userargs->users, gl[1]);
pthread_mutex_unlock(&user_lock);
} else {
printf("No watched users have been added yet\n");
}
} else {
/* Add the user to the list. Create the watchuser thread if it isn't already created */
if(user_thread == 1) {
user_thread = 0;
userargs = calloc(1, sizeof(struct userarg));
userargs->lock = user_lock;
userargs->users = add_user(NULL, gl[1]);
pthread_create(&pt_watchuser, NULL, watchuser, userargs);
} else {
pthread_mutex_lock(&user_lock);
userargs->users = add_user(userargs->users, gl[1]);
pthread_mutex_unlock(&user_lock);
}
}
}
/* WATCHMAIL NOT IMPLEMENTED */
else if(strcmp(gl[0], "watchmail") == 0) {
printf("We were not able to implement watchmail as we could not figure out\nhow to correctly use pthread_cancel(3) to cancel threads\n");
}
/* Build in fg */
else if(strcmp(gl[0], "fg") == 0) {
printf("Executing build-in [%s]\n", gl[0]);
/* No arguments, bring a default process into foreground */
if(glc == 1) {
kill(0, SIGCONT);
wait(NULL);
}
/* One argument, bring the process with pid into foreground */
else if(glc == 2) {
if(atoi(gl[1])) {
kill(atoi(gl[1]), SIGCONT);
waitpid(atoi(gl[1]), NULL, 0);
} else {
fprintf(stderr, "pid must be an integer\n");
}
} else {
fprintf(stderr, "Usage: fg [pid]\n");
}
}
/* Built in list */
else if(strcmp(gl[0], "list") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
i = 1;
/* No arguments, print the current working directory */
if(glc < 2) {
list(pwd);
} else {
/* list each of the arguments passed in */
while(i < glc) {
list(gl[i]);
printf("\n");
i++;
}
}
}
/* Built in exit */
else if (strcmp(gl[0], "exit") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
go = 0;
break;
}
/* Built in prompt */
else if (strcmp(gl[0], "prompt") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc < 2) {
/* user didn't enter a prompt so request one */
printf("input prompt prefix:");
fgets(prompt, PROMPTMAX, stdin);
if(prompt[strlen(prompt) - 1] == '\n') {
prompt[strlen(prompt) - 1] = 0;
}
} else {
/* set the prompt to be the first argument */
strncpy(prompt, gl[1], PROMPTMAX);
}
}
/* Built in pwd */
else if (strcmp(gl[0], "pwd") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
printf(pwd);
printf("\n");
}
/* Built in noclobber */
else if (strcmp(gl[0], "noclobber") == 0){
if(noclobber == 0) {
noclobber = 1;
} else {
noclobber = 0;
}
printf("noclobber is set to %d\n", noclobber);
}
/* Built in pid */
else if (strcmp(gl[0], "pid") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
printf("%d\n", getpid());
}
/* Built in which */
else if (strcmp(gl[0], "which") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc < 2) {
fprintf(stderr, "which: Too few arguments.\n");
} else {
i = 1;
while(i < glc) {
char *wh;
/* call the which function which will check for the command in the path */
wh = which(gl[i], pathlist);
if(wh == NULL) {
fprintf(stderr, "%s: Command not found.\n", gl[i]);
} else {
printf("%s\n", wh);
}
free(wh);
i++;
}
}
}
/* Built in where */
else if (strcmp(gl[0], "where") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc < 2) {
fprintf(stderr, "where: Too few arguments.\n");
} else {
i = 1;
while(i < glc) {
where(gl[i], pathlist);
i++;
}
}
}
/* Built in cd */
else if (strcmp(gl[0], "cd") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc > 2) {
printf("cd: Too many arguments.\n");
} else {
char *tmp;
/* tmp will be the new directory or NULL on failure */
tmp = cd(gl[1], homedir, owd);
if(tmp == NULL) {
break;
} else {
free(owd);
/* set owd to be the old (previous) working directory */
owd = pwd;
/* set the new working directory */
pwd = tmp;
}
tmp = NULL;
}
}
/* Built in printenv */
else if (strcmp(gl[0], "printenv") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc > 2) {
fprintf(stderr, "%s: Too many arguments.\n", gl[0]);
} else if (glc > 1) {
/* print a particular environmental variable */
char *tmp;
tmp = getenv(gl[1]);
if(tmp == NULL) {
fprintf(stderr, "%s: Environmental variable not found.\n", gl[1]);
} else {
printf("%s\n", tmp);
}
tmp = NULL;
} else {
/* print all environmental variables */
i = 0;
while(environ[i] != NULL) {
printf("%s\n", environ[i]);
i++;
}
}
}
/* Built in setenv */
else if (strcmp(gl[0], "setenv") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc > 3) {
fprintf(stderr, "%s: Too many arguments.\n", gl[0]);
} else if (glc > 2) {
/* set an environmental variable to the given value */
setenv(gl[1], gl[2], 1);
} else if (glc > 1) {
/* set an environmental variable to an empty value */
setenv(gl[1], "", 1);
} else {
/* print all environmental variables */
i = 0;
while(environ[i] != NULL) {
printf("%s\n", environ[i]);
i++;
}
}
/* in case we update the home directory */
homedir = getenv("HOME");
}
/* Built in history */
else if (strcmp(gl[0], "history") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
int n = 10;
/* set how many history records to print */
if(glc > 1 && atoi(gl[1]) != 0) {
n = atoi(gl[1]);
}
struct pathelement *curr = history;
/* loop and print past commands */
while(curr != NULL && n > 0) {
printf("%s\n", curr->element);
curr = curr->next;
n--;
}
}
/* Built in alias */
else if (strcmp(gl[0], "alias") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc < 2) {
/* list all aliases */
struct aliaselement *curr = aliases;
while(curr != NULL) {
printf("%s %s\n", curr->name, curr->command);
curr = curr->next;
}
} else if(glc < 3) {
/* list a specific alias */
struct aliaselement *curr = aliases;
while(curr != NULL) {
if(strcmp(gl[1], curr->name) == 0) {
printf("%s\n", curr->command);
}
curr = curr->next;
}
} else {
/* add an alias */
char buf[MAX_CANON];
snprintf(buf, MAX_CANON, "%s", gl[2]);
i = 3;
while(gl[i] != NULL && i < MAXARGS) {
strcat(buf, " ");
strcat(buf, gl[i]);
i++;
}
aliases = add_alias(aliases, gl[1], buf);
}
}
/* Built in kill */
else if (strcmp(gl[0], "kill") == 0) {
printf("Executing built-in [%s]\n", gl[0]);
if(glc < 2) {
fprintf(stderr, "kill: Too few arguments.\n");
} else if(glc < 3 || strchr(gl[1], '-') == NULL) {
/* default kill with SIGINT */
i = 1;
while(gl[i] != NULL) {
kill_process(gl[i]);
i++;
}
} else {
/* kill with the given signal number */
char *signal;
signal = strtok(gl[1], "-");
i = 2;
while(gl[i] != NULL) {
kill_process_signal(gl[i], signal);
i++;
}
}
}
/* MAXARGS handler */
else if (strcmp(gl[0], "maxargs") == 0) {
fprintf(stderr, "Error: Too many arguments.\n");
}
/* Absolute/Relative paths */
else if (strncmp(gl[0], "/", 1) == 0 || strncmp(gl[0], "./", 2) == 0 || strncmp(gl[0], "../", 3) == 0) {
if(access(gl[0], X_OK) == 0) {
pid_t pid = fork();
if(pid == -1) {
perror("fork");
} else if(pid == 0) {
/* print what child is executing and execve it */
printf("Executing [%s]\n", gl[0]);
/* If we are piping this is the command on the left, set standard output to the pipe output */
if(ipc) {
close(1);
dup(pipefd[1]);
if(ipc_err) {
close(2);
dup(pipefd[1]);
}
close(pipefd[0]);
close(pipefd[1]);
}
execve(gl[0], gl, environ);
/* on exec error */
perror(gl[0]);
exit(127);
} else {
/* if not a background process, wait */
if(!background) {
/* If we are piping this is the command on the right. Wait for the first command to finish */
if(ipc) {
waitpid(pid, &status, 0);
pid_t pid2 = fork();
if(pid2 == -1) {
perror("fork");
} else if (pid2 == 0) {
/* Set stdin to be the pipe input */
close(0);
dup(pipefd[0]);
close(pipefd[1]);
close(pipefd[0]);
/* Which command should we run */
char* wh;
wh = which(pipeargs[0], pathlist);
if(wh == NULL) {
fprintf(stderr, "%s: Command not found", pipeargs[0]);
} else {
execve(wh, pipeargs, environ);
}
} else {
close(pipefd[0]);
close(pipefd[1]);
}
} else {
/* wait for chil process */
waitpid(pid, &status, 0);
/* if child exits with non-zero status print it */
if(WEXITSTATUS(status) != 0) {
printf("Exit: %d\n", WEXITSTATUS(status));
}
}
}
}
} else {
/* path doens't exist */
fprintf(stderr,"%s: Command not found.\n", gl[0]);
}
}
/* Executable */
else {
char* wh;
char* wh2;
/* figure out which executable to execute */
wh = which(gl[0], pathlist);
if(wh == NULL) {
fprintf(stderr, "%s: Command not found.\n", gl[0]);
} else {
pid_t pid = fork();
if(pid == -1) {
perror("fork");
} else if (pid == 0) {
/* what we are executing */
printf("Executing [%s]\n", wh);
/* If we are piping this is the command on the left, set standard output to be the pipe output*/
if(ipc) {
close(1);
dup(pipefd[1]);
if(ipc_err) {
close(2);
dup(pipefd[1]);
}
close(pipefd[0]);
close(pipefd[1]);
}
execve(wh, gl, environ);
/* on execve error */
perror(wh);
exit(127);
} else {
/* if not a background process, wait */
if(!background) {
/* If we are piping this is the command on the right, wait for command on left to finish */
if(ipc) {
waitpid(pid, &status, 0);
pid_t pid2 = fork();
if(pid2 == -1) {
perror("fork");
} else if (pid2 == 0) {
/* Set standard input to be pipe input */
close(0);
dup(pipefd[0]);
close(pipefd[1]);
close(pipefd[0]);
/* Which command should we run */
wh2 = which(pipeargs[0], pathlist);
if(wh2 == NULL) {
fprintf(stderr, "%s: Command not found", pipeargs[0]);
} else {
execve(wh2, pipeargs, environ);
}
} else {
close(pipefd[0]);
close(pipefd[1]);
}
} else {
/* wait for child */
waitpid(pid, &status, 0);
/* if child exits with nonzero value print it */
if(WEXITSTATUS(status) != 0){
printf("Exit: %d\n", WEXITSTATUS(status));
}
}
}
}
free(wh);
}
}
/* reset background */
background = 0;
/* reset glob */
globfree(&globbuf);
/* reset args */
free_args(args);
free_args(pipeargs);
/* if we redirected file output, reset it to the screen */
if(file_redirect == 1) {
file_redirect = 0;
fid = open("/dev/tty", O_WRONLY);
close(1);
dup(fid);
if(error_redirect == 1) {
error_redirect = 0;
close(2);
dup(fid);
}
close(fid);
}
/* if we redirected file input, reset it to the keyboard */
if(input_redirect == 1) {
input_redirect = 0;
fid = open("/dev/tty", O_RDONLY);
close(0);
dup(fid);
close(fid);
}
ipc = 0;
ipc_err = 0;
sleep(1);
/* Print prompt again */
printf(prompt);
printf(" [");
printf(pwd);
printf("]> ");
}
}
/* free allocated memory */
free(prompt);
free(commandline);
free_args(args);
free_args(pipeargs);
free(args);
free(pipeargs);
free(owd);
free(pwd);
struct pathelement *tmp;
while(pathlist != NULL) {
tmp = pathlist->next;
free(pathlist);
pathlist = tmp;
}
while(history != NULL) {
tmp = history->next;
free(history->element);
free(history);
history = tmp;
}
struct aliaselement *temp;
while(aliases != NULL) {
temp = aliases->next;
free(aliases->name);
free(aliases->command);
free(aliases);
aliases = temp;
}
struct userelement *tem;
while(userargs != NULL && userargs->users != NULL) {
tem = userargs->users->next;
free(userargs->users->username);
free(userargs->users);
userargs->users = tem;
}
if(userargs != NULL) {
pthread_mutex_destroy(&userargs->lock);
free(userargs);
}
globfree(&globbuf);
return 0;
} /* sh() */
char *which(char *command, struct pathelement *pathlist ) {
/* loop through pathlist until finding command and return it. Return
NULL when not found. */
struct pathelement *current = pathlist;
char *buf;
buf = malloc(MAX_CANON * sizeof(char));
while(current != NULL) {
snprintf(buf, MAX_CANON, "%s/%s", current->element, command);
/* we've found the command, so return it and exit */
if(access(buf, F_OK) == 0) {
return buf;
break;
}
current = current->next;
}
free(buf);
buf = NULL;
return buf;
} /* which() */
void where(char *command, struct pathelement *pathlist ) {
/* similarly loop through finding all locations of command */
struct pathelement *current = pathlist;
char buf[MAX_CANON];
while(current != NULL) {
snprintf(buf, MAX_CANON, "%s/%s", current->element, command);
/* we've found the command, so print it and continue */
if(access(buf, F_OK) == 0) {
printf("%s\n", buf);
}
current = current->next;
}
} /* where() */
void list ( char *dir ) {
/* see man page for opendir() and readdir() and print out filenames for
the directory passed */
DIR* dirstream;
struct dirent* files;
/*try to open the given direcotry */
dirstream = opendir(dir);
/* failed to open */
if(dirstream == NULL) {
perror(dir);
} else {
/* print what directory we're listing */
printf("%s:\n", dir);
/* print the files in the directory */
while((files = readdir(dirstream)) != NULL){
printf("%s\n", files->d_name);
}
}
free(dirstream);
} /* list() */
char *cd( char *dir, char *homedir, char *prevdir ) {
if(dir == NULL) {
/* with no dir, we want to change to the homedir */
dir = homedir;
} else if(strcmp(dir, "-") == 0) {
/* change to the previous directory capability */
dir = prevdir;
}
if(chdir(dir) == -1) {
/* on chdir error */
perror(dir);
return NULL;
} else {
/* return the new directory after we change directories */
return getcwd(NULL, PATH_MAX+1);
}
}
/* try to kill the given process with the given signal */
void kill_process_signal(char* process, char *signal) {
int i = 0;
if(atoi(process) && atoi(signal)) {
/* if we can convert process and signal to numbers, try to kill the process with the signal */
i = kill(atoi(process), atoi(signal));
} else {
/* something was wrong with process or signal */
fprintf(stderr, "kill: Arguments should be jobs or process id's.\n");
}
/* kill didn't work */
if(i == -1)
perror(process);
}
/* try to terminate the given process */
void kill_process(char *process) {
int i = 0;
if(atoi(process)) {
/* if we can convert process to number, try to kill the process with SIGTERM */
i = kill(atoi(process),SIGTERM);
} else {
/* something was wrong with process */
fprintf(stderr, "kill: Arguments should be jobs or process id's.\n");
}
/* kill didn't work */
if(i==-1)
perror(process);
}
/* warn the user if the load is above the set warning value */
void *warnload(void *args) {
float l;
while(1) {
/* get the warning value */
l = *(float *)args;
/* should we exit? */
if(l == 0.0) {
printf("exiting");