-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallsh.c
More file actions
1519 lines (1323 loc) · 63.8 KB
/
smallsh.c
File metadata and controls
1519 lines (1323 loc) · 63.8 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
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
/*
Name: Molly Johnson
ONID: johnsmol
CS 344 Winter 2019
Due: 3/3/19
All information used to create this code is adapted from the OSU CS 344 Winter 2019
lectures and assignment instructions/hints unless otherwise specifically indicated.
Note: Also adapted from my own work from 11/14/18 (took the class in the Fall 2018 term but
am retaking this term for a better grade).
note: fflush(stdout) used after every printf() in this assignment at the advice of the instructor
in the assignment instructions to make sure the output buffers get flushed every time I try to print.
*/
//added #define _GNU_SOURCE before #include <stdio.h> to prevent "implicit function declaration"
//warnings with getline. Adapted from:
//https://stackoverflow.com/questions/8480929/scratchbox2-returns-implicit-declaration-of-function-getline-among-other-weir
#define _GNU_SOURCE
//include all header files:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <dirent.h>
#include <signal.h>
#include <errno.h>
//constant macro definitions:
//max values for chars and args were determined by the CS344 assignment3 instructions.
//max chars is actually 2048 but i added one additional char for the null terminator.
//max args is actually 512 but i added one additional arg for the NULL arg req'd to null-terminate
//an array sent to execvp() so execvp() knows when the end of the array has been reached.
//max forks an arbitrary value i created to prevent fork-bombing the server.
#define MAX_CHARS 2049
#define MAX_ARGS 513
#define MAX_FORKS 500
#define EXIT "exit"
#define CD "cd"
#define STATUS "status"
#define FALSE 0
#define TRUE 1
#define NO_ACTION "NO_ACTION"
//global variables:
//flag for if background is possible (if SIGTSTP command given, should ignore "&" and
//just run it as a foreground command). set to true by default so initially background
//mode is possible until the user enters ctrl-Z. used as a global variable since can't
//pass in additional parameters into the signal handler function. (and was the recommended
//method on the piazza discussion board).
int backgroundPossibleGlobal = TRUE;
//function declarations:
int StringMatch(char *string1, char *string2);
void GetInputString(char *userInputString);
char *GetPID();
char *ReplaceString(char *str, char *orig, char *rep);
void VariableExpand(char *varIn);
int GetArgs(char **parsedInput, char *userInputString, char *inputFileIn, char *outputFileIn, int *isBackgroundBool);
int IsBlank(char *userInputIn);
int IsComment(char *userInputIn);
int IsNewline(char *userInputIn);
int IsExit(char *userInputIn);
int IsStatus(char *userInputIn);
int IsChangeDir(char *userInputIn);
int IsNoAction(char *userInputIn);
void ChangeDirBuiltInNoArgs();
void ChangeDirBuiltInOneArg(char *directoryArg);
void Execute(char **parsedInput, int *childExitStatusIn);
void StatusBuiltIn(int childExitStatusIn);
void StatusSIGINT(int childExitStatusIn);
void StatusBuiltInBackground(int backgroundChildExitStatusIn);
int RedirectInputFile(char *inputFileIn);
int RedirectOutputFile(char *outputFileIn);
void ExitBuiltIn(int foregroundProcessCountIn, int backgroundProcessCountIn, int backgroundPidArrayIn[], int foregroundPidArrayIn[], int childExitStatusIn);
int NeedsOutputRedirect(char *outputFileIn);
int NeedsInputRedirect(char *inputFileIn);
void CheckBackgroundProcesses(int *backgroundProcessCountIn, int backgroundPidArrayIn[], int *childExitStatusBckd);
void CatchSIGTSTP(int signo);
/*
NAME
statusSIGINT
SYNOPSIS
checks if the process was terminated (does not check if exited normally)
DESCRIPTION
will use SIFSIGNALED and WTERMSIG to see if the process was terminated by a signal
(and if so, which one). added because the parent process needs to check for terminated child
signals after every waitpid() without also checking for an exit status.
*/
void StatusSIGINT(int childExitStatusIn){
if(WIFSIGNALED(childExitStatusIn) != 0){
//if process was terminated by a signal, use WTERMSIG to get the terminating signal number
//(WTERMSIG will return this number)
int termSignal = WTERMSIG(childExitStatusIn);
printf("terminated by signal %d\n", termSignal); fflush(stdout);
}
}
/*
NAME
catchsigtstp
SYNOPSIS
enters or exits foreground-only mode
DESCRIPTION
signal handler the parent process uses to catch/handle a SIGTSTP signal. receives the signal number (an int) as a parameter.
checks if the background mode is currently possible (i.e. if the boolean for background mode possible is true).
if it is, prints message saying foreground-only mode is being entered. prints message to the user. uses write instead of
printf() to make sure signal handler is re-entrant. sets the background possible global boolean to false. if the background
mode possible boolean was already false, prints a message to the user that they're exiting the foreground-only mode. also
uses write instead of printf to prevent re-entrancy problems (printf isn't reentrant whereas write is). sets the background
possible global boolean to true. returns void.
*/
void CatchSIGTSTP(int signo){
//check if the background mode is currently possible (i.e. if backgroundPossibleGlobal is true)
if(backgroundPossibleGlobal == TRUE ){
//if was true, print message to user that they're entering the foreground-only mode
char *message = "\nEntering foreground-only mode (& is now ignored)\n: ";
//use write() instead of printf() to print the message since write() is re-entrant.
//lectures state strlen() isn't re-entrant, but students/instructors on the piazza
//discussion board found that recently it became a re-entrant function and could thus
//be used for this assignment.
write(STDOUT_FILENO, message, strlen(message)); fflush(stdout);
//set the background possible global variable to false to enter foreground-only mode
backgroundPossibleGlobal = FALSE;
}
else{ //backgroundPossibleGlobal is false
//if was false, print message to the user that they're exiting the foreground-only mode
char *message2 = "\nExiting foreground-only mode\n: ";
//again use write instead of printf due to re-entrancy
write(STDOUT_FILENO, message2, strlen(message2)); fflush(stdout);
//set the background possible global variable to true to exit foreground-only mode
backgroundPossibleGlobal = TRUE;
}
}
/*
NAME
statusbuiltinbackground
SYNOPSIS
gives the status of the last background process
DESCRIPTION
takes the background status integer variable in as a parameter. uses WIFEXITED and WIFSIGNALED
macros to determine if the background process exited normally (and if so what was the exit
value) or if it was terminated by a signal (and if so what was the terminating signal). if
neither one is true, prints an error and exits w a non-zero exit value. returns void. note:
a process can have either an exit status or a terminating signal but not both!
*/
void StatusBuiltInBackground(int backgroundChildExitStatusIn){
//additional information on how to obtain exit status and terminating signal information using WIFEXITED
//and WEXITSTATUS and WIFSIGNALED and WTERMSIG adapted from:
//https://stackoverflow.com/questions/27306764/capturing-exit-status-code-of-child-process and
//https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.15/gtpc2/cpp_wifexited.html and
//https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.15/gtpc2/cpp_wtermsig.html and
//https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.15/gtpc2/cpp_wexitstatus.html
//if WIFEXITED returns non-zero, means process terminated normally
if(WIFEXITED(backgroundChildExitStatusIn) != 0){
//if process terminated normally, use WEXITSTATUS to get the actual exit value
//(WEXITSTATUS will return this number)
int backgroundExitStatus = WEXITSTATUS(backgroundChildExitStatusIn);
//print the exit value of the background process that terminated normally
printf("background exit value %d\n", backgroundExitStatus); fflush(stdout);
}
//if WIFSIGNALED returns non-zero, means process was terminated by a signal.
else if(WIFSIGNALED(backgroundChildExitStatusIn) != 0){
//if process was terminated by a signal, use WTERMSIG to get the terminating signal number
//(WTERMSIG will return this number)
int backgroundTermSignal = WTERMSIG(backgroundChildExitStatusIn);
//print the signal number of the background process that was terminated by the signal
printf("terminated by signal %d\n", backgroundTermSignal); fflush(stdout);
}
//if neither one returns non-zero, is a major problem (one and only one of them should be returning non-zero)
else{
//if somehow neither WIFEXITED nor WIFSIGNALED returned non-zero, print the error and exit w a non-zero exit value.
perror("neither WIFEXITED nor WIFSIGNALED returned a non-zero value, major error in your status checking!\n");
exit(1);
}
}
/*
NAME
checkbackgroundprocesses
SYNOPSIS
checks background processes to see if any have exited or been terminated
DESCRIPTION
takes as parameters the number of background process counts, the array of background PIDs that are still
processing (i.e. not exited or terminated by a signal), and the background child exit status. if the process
count is 1 or more, will go through each background process and use waitpid with the WNOHANG option to check if
the status for each pid is available (0 = process terminated) or if the status is not yet available. using the
WNOHANG option will allow the waitpid function to return immediately whether the process being checked has
terminated yet or not. if a background process has completed (terminated or exited), will print a message to
the user stating which background pid is done and then call the status built in function to get either the
exit value or terminating signal for that background process pid. returns void.
*/
void CheckBackgroundProcesses(int *backgroundProcessCountIn, int backgroundPidArrayIn[], int *childExitStatusBckd){
//check that there are 1 or more background processes that haven't yet exited/been terminated
if(*backgroundProcessCountIn > 0){
//had trouble with waitpid expecting an int not a pointer to an int, so set a temporary
//background status int to the background child exit status that was passed in
int backgroundStatTemp = *childExitStatusBckd;
//loop through all background processes not yet exited/terminated
for(int k = 0; k < *backgroundProcessCountIn; k++){
//also had trouble passing in the array element to waitpid so set a temporary background spawnpid
//to the pid in the current array position.
pid_t backgroundSpawnPid = backgroundPidArrayIn[k];
//use waitpid to get the status of the current background process pid. pass in the temp background
//spawnpid, temp background exit status(so it'll be changed to the correct status by the waitpid
//function), and the WNOHANG option (which will cause waitpid to return immediately whether the process
//has exited/been terminated or not, as we don't want to be waiting on background processes if they're
//not done, we should be just returning control to the command line and checking periodically for
//if they're done).
pid_t actualBackgroundPID = waitpid(backgroundSpawnPid, &backgroundStatTemp, WNOHANG);
//set the background status that was passed in to the value of the temp background status variable
*childExitStatusBckd = backgroundStatTemp;
//if waitpid returned -1, an error occurred. print error message and exit w non-zero value.
if(actualBackgroundPID == -1){
perror("waitpid for background process error!\n"); exit(1);
}
//if waitpid returned a non-zero value (but not -1), means child process terminated. (return of 0 means
//the status for the pid is not yet available. in that case, will do nothing).
else if(actualBackgroundPID != 0){
//if child process terminated, print message to the user that that background pid is done.
printf("background pid %d is done: ", actualBackgroundPID); fflush(stdout);
//loop through all but the last background pid (starting from the position of the pid that was found
//to be terminated/exited)
for(int i = k; i < *backgroundProcessCountIn - 1; i++){
//shift all background pids over to the left one from that point to prevent pids that have already
//been terminated/exited from being in the array
backgroundPidArrayIn[i] = backgroundPidArrayIn[i + 1];
}
//use the status background built in function to get either the exit value or the terminating signal for
//the background process that was just terminated or exited.
StatusBuiltInBackground(backgroundStatTemp);
//decrease the background process count by one (since shifted all remaining pids so that the array length
//is one less than before and the exited/terminated pid has been removed)
(*backgroundProcessCountIn) = ((*backgroundProcessCountIn) - 1);
}
}
}
}
/*
NAME
redirectinputfile
SYNOPSIS
redirects stdin to a specified input file
DESCRIPTION
takes in the input file name as a parameter. opens the file. if not able to open,
sets exit status to a non-zero number and prints a message to the user.
if able to be opened, uses dup2 to redirect stdin(0) to the source file descriptor.
checks for dup2 error and if there's an error also sets the exit value to a non-zero
number. returns the child exit status integer (0 if successful, non-zero if are errors).
*/
int RedirectInputFile(char *inputFileIn){
//create temporary child exit status variable, set to 0 by default
int childExitStat = 0;
//set source file descriptor to value returned by open, open the input file (read only)
int sourceFD = open(inputFileIn, O_RDONLY);
//if open returned -1, was an error opening the file. set exit status to 1 and print
//error message to the user.
if(sourceFD == -1){
childExitStat = 1;
printf("cannot open %s for input\n", inputFileIn); fflush(stdout);
}
//set dupresult to the value returned by dup2. pass in the source file descriptor and the
//stdin value (which is 0) to redirect input from stdin to the source file descriptor.
int dupResult = dup2(sourceFD, 0);
//if dup2 returns -1, the redirect had an error. set exit status to 1.
if(dupResult == -1){
childExitStat = 1;
}
//return the child exit status
return childExitStat;
}
/*
NAME
redirectoutputfile
SYNOPSIS
redirects stdout to a specified output file
DESCRIPTION
takes in the output file name as a paremeter. opens the file (if created already) or creates
one if not already created. if not able to open/create, set exit status to non-zero and print
error message to the user. if able to be opened/created, uses dup2 to redirect stdout (1) to the
target file descriptor. checks dup2 for error and if there's an error and if there's an error also
sets the exit value to a non-zero number. returns the child exit status integer (0 if successful,
non-zero if there are errors).
*/
int RedirectOutputFile(char *outputFileIn){
//create temporary child exit status variable, set to 0 by default
int childExitStat = 0;
//set target file descriptor to value returned by open, open or create or truncate(i.e.
//overwrite previous file contents if was created)
int targetFD = open(outputFileIn, O_WRONLY | O_CREAT | O_TRUNC, 0644);
//if open returned -1, was an error opening/creating/truncating the file. set exit
//status to 1 and print error message to the user.
if(targetFD == -1){
printf("cannot open %s for output\n", outputFileIn); fflush(stdout);
childExitStat = 1;
}
//set dupresult to the value returned by dup2. pass in the target file descriptor and the
//stdout value (which is 1) to redirect output from stdout to the target file descriptor
int dupResult = dup2(targetFD, 1);
//if dup2 returns -1, the redirect had an error. set exit status to 1.
if(dupResult == -1){
childExitStat = 1;
}
//return the child exit status
return childExitStat;
}
/*
NAME
needsinputredirect
SYNOPSIS
checks if the current process needs its input redirected from stdin to a file or not
DESCRIPTION
takes in an input file name (or NO_ACTION if no input file was given by the user).
Calls the StringMatch function to see if the input file name matches NO_ACTION. if
it does, no input redirect is needed, function returns false. otherwise the user
entered an input file name and the process' input will need to be redirected to the
file so the function returns true.
*/
int NeedsInputRedirect(char *inputFileIn){
//call StringMatch to check if the input file matches NO_ACTION (i.e. doesn't need
//input redirect) or if it doesn't match that (and thus the user entered an actual
//file name for redirection)
if(StringMatch(inputFileIn, NO_ACTION) == TRUE){
//if the input file name matches NO_ACTION, no redirect will be needed. return false.
return FALSE;
}
//if the input file name did not match NO_ACTION, redirection will be needed. return true.
return TRUE;
}
/*
NAME
needsoutputredirect
SYNOPSIS
checks if the current process needs its output redirected from stdout to a file or not
DESCRIPTION
takes an output file name (or NO_ACTION if no output file was given by the user). calls
the StringMatch function to see if the output file name matches NO_ACTION. if it does,
no output redirection is needed, function returns false. otherwise the user entered an
output file name and the process' output will need to be redirected to the file so the
function returns true.
*/
int NeedsOutputRedirect(char *outputFileIn){
//call StringMatch to check if the output file matches NO_ACTION (i.e. doesn't need
//output redirect) or if it doesn't match that (and thus the user entered an actual
//file name for redirection)
if(StringMatch(outputFileIn, NO_ACTION) == TRUE){
//if output file name matches NO_ACTION, no redirect will be needed. return false.
return FALSE;
}
//if the output file name did not match NO_ACTION, redirection will be needed. return true.
return TRUE;
}
/*
NAME
exitbuiltin
SYNOPSIS
cleans up any remaining processes running (kills them) and exits with the correct exit status
DESCRIPTION
checks each foreground process and any remaining background processes and kills them with SIGKILL
*/
void ExitBuiltIn(int foregroundProcessCountIn, int backgroundProcessCountIn, int backgroundPidArrayIn[], int foregroundPidArrayIn[], int childExitStatusIn){
//check if there's 1 or more foreground processes
if(foregroundProcessCountIn > 0){
//loop through each foreground process and try to kill it (didn't appear to have
//any harmful effects from trying to kill processes that had already terminated or exited,
//just did nothing, so didn't go through array each time a process ended or was terminated
//to remove it from the array). instructor brewster said a static fixed array of 128 args would
//be sufficient for storing process pids and that it didn't have to be dynamic.
for(int k = 0; k < foregroundProcessCountIn; k++){
kill(foregroundPidArrayIn[k], SIGKILL);
}
}
//check if there's 1 or more background processes
if(backgroundProcessCountIn > 0){
//loop through each background process and try to kill it (this array was already created dynamically
//to account for the periodic checking with waitpid() and WNOHANG).
for(int m = 0; m < backgroundProcessCountIn; m++){
kill(backgroundPidArrayIn[m], SIGKILL);
}
}
//exit w the exit status
exit(childExitStatusIn);
}
/*
NAME
statusbuiltin
SYNOPSIS
gives the status of the last process
DESCRIPTION
takes the status integer of a process in as a parameter. uses WIFEXITED and WIFSIGNALED macros
to determine if the process exited normally (and if so what was the exit value) or if it was
terminated by a signal (and if so what was the terminating signal). if neither one is true,
prints an error and exits w a non-zero value. returns void. note: a process can have either
an exit status or a terminating signal but not both!
*/
void StatusBuiltIn(int childExitStatusIn){
//additional information on how to obtain exit status and terminating signal information using WIFEXITED
//and WEXITSTATUS and WIFSIGNALED and WTERMSIG adapted from:
//https://stackoverflow.com/questions/27306764/capturing-exit-status-code-of-child-process and
//https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.15/gtpc2/cpp_wifexited.html and
//https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.15/gtpc2/cpp_wtermsig.html and
//https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.15/gtpc2/cpp_wexitstatus.html
//if WIFEXITED returns non-zero, means process terminated normally
if(WIFEXITED(childExitStatusIn) != 0){
//if process terminated normally, use WEXITSTATUS to get the actual exit value
//(WEXITSTATUS will return this number)
int exitStatus = WEXITSTATUS(childExitStatusIn);
//print the exit value of the process that terminated normally
printf("exit value %d\n", exitStatus); fflush(stdout);
}
//if WIFSIGNALED returns non-zero, means process was terminated by a signal.
else if(WIFSIGNALED(childExitStatusIn) != 0){
//if process was terminated by a signal, use WTERMSIG to get the terminating signal number
//(WTERMSIG will return this number)
int termSignal = WTERMSIG(childExitStatusIn);
//print the signal number of the process that was terminated by the signal
printf("terminated by signal %d\n", termSignal); fflush(stdout);
}
//if neither one returns non-zero, is a major problem (one and only one of them should be returing non-zero)
else{
//if somehow neither WIFEXITED nor WIFSIGNALED returned non-zero, print the error and exit w/ a non-zero value
perror("neither WIFEXITED nor WIFSIGNALED returned a non-zero value, major error in your status checking!\n");
exit(1);
}
}
/*
NAME
execute
SYNOPSIS
calls execvp() on commands that aren't built-ins
DESCRIPTION
takes in an array of strings (a non-buil-in command and either no arguments or up to 512 arguments (as listed
in assignment specs). calls execvp(), which is an exec() function variant that uses the path variable to look
for non-built-in commands and requires the path start (i.e. pointer to the array of string inputs) as the first argument,
and the array itself as the second argument. (Note: the array must be null-terminated in order for execvp() to know
when the end of the array has been reached!). if execvp() encounters an error, set the exit status to a non-zero
value and call exit with this value. returns void.
*/
void Execute(char **parsedInput, int *childExitStatusIn){
//execvp() use also adapted from:
////https://stackoverflow.com/questions/27541910/how-to-use-execvp and
//http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/exec.html and
//http://www.cs.ecu.edu/karl/4630/sum01/example1.html and
//http://man7.org/linux/man-pages/man3/exec.3.html
//call execvp() with the parsed input array pointer as the path (first arg) and the parsed input array
//itself as the second arg. parsed input is a NULL-terminated array. check if execvp returns < 0 (which
//would indicate an execvp() error).
if(execvp(parsedInput[0], parsedInput) < 0){
//if execvp had an error, print an error message to the user
printf("%s: no such file or directory\n", parsedInput[0]); fflush(stdout);
//set the exit value to non-zero
*childExitStatusIn = 1;
//exit with the non-zero exit value
exit(*childExitStatusIn);
}
}
/*
NAME
changedirbuiltinnoargs
SYNOPSIS
changes the directory to home
DESCRIPTION
when the user doesn't enter any args and just enters "cd" (i.e. change directory), use getenv() to get
the home path, then chdir() to this home path. (didn't do error check on directory because isn't a user-et
directory, all users will have a home path). returns void.
*/
void ChangeDirBuiltInNoArgs(){
//using getenv and home to find the home directory path adapted from:
//https://stackoverflow.com/questions/31906192/how-to-use-environment-variable-in-a-c-program and
//http://man7.org/linux/man-pages/man3/getenv.3.html and
//https//www.tutorialspoint.com/c_standard_library/c_function_getenv.htm and
//https://stackoverflow.com/questions/9493234/chdir-to-home-directory
//chdir information adapted from:
//https://linux.die.net/man/2/chdir
//get home path using getenv()
char *homeDir = getenv("HOME");
//change directory using chdir() and the home path found by getenv()
chdir(homeDir);
}
/*
NAME
changedirbuiltinonearg
SYNOPSIS
changes directory to a user-specified directory
DESCRIPTION
is called when the user enters "cd" and one arg (or more, but any args beyond the first will be ignored). first
arg is the relative or absolute file path the user is wanting to change directory to. will attempt to change to
this specified directory using chdir(). if chdir() returns non-zero (i.e. fails), print error message to the
user. returns void.
*/
void ChangeDirBuiltInOneArg(char *directoryArg){
//attempt to change to the user-specified directory by calling chdir() with their first input arg. If
//chdir() returns non-zero, print message to user that chdir() to their specified directory has failed.
if(chdir(directoryArg) != 0){
printf("chdir() to your specified directory has failed, no such directory there.\n"); fflush(stdout);
}
}
/*
NAME
isblank
SYNOPSIS
checks if user entered a blank line
DESCRIPTION
takes in the user's input as a parameter. checks if all of the user's input are spaces (i.e. a "blank"
line). i it is a blank line, returns true. else returns false.
*/
int IsBlank(char *userInputIn){
//create counter for space chars and initialize to 0
int spaceCount = 0;
//loop through each char of the user input string
for(int j = 0; j < strlen(userInputIn); j++){
//if the current char is a space, increment the space count
if(userInputIn[j] == ' '){
spaceCount++;
}
}
//after looping through all of the characters in the user input string,
//check if the number of spaces is equal to the string length of the user input (minus one
//for the null-terminator). else return false.
if(spaceCount == (strlen(userInputIn) - 1)){
//if these numbers are equal, the entire line was composed of just
//spaces (i.e. blank). return true.
return TRUE;
}
//else return false
return FALSE;
}
/*
NAME
iscomment
SYNOPSIS
checks if the user entered a comment
DESCRIPTION
receives user input string as a parameter. checks the first char of the user's input
to see if it's a '#' char. if it is, return true (is a comment). else, return false.
*/
int IsComment(char *userInputIn){
//check if first char of the user's input string is a '#' char
if(userInputIn[0] == '#'){
//if it is, return true
return TRUE;
}
//else, return false
return FALSE;
}
/*
NAME
isnewline
SYNOPSIS
checks if the user only hit enter (i.e. line is "blank" because the user only entered a newline char)
DESCRIPTION
receives user input string as a parameter. checks if the entire string is just one newline char. if
it is, return true (is a "blank" line composed of only a newline char). else return false.
*/
int IsNewline(char *userInputIn){
//call StringMatch to check if the user input string is just a newline character.
if(StringMatch(userInputIn, "\n") == TRUE){
//if it is, return true
return TRUE;
}
//else, return false
return FALSE;
}
/*
NAME
getinputstring
SYNOPSIS
obtains user input as one long string from the user using getline
DESCRIPTION
receives userinputstring (blank) as a parameter. uses getline to get the user's input
as one long string. checks if getline returns -1 to account for getline problems that
can arise due to signals. checks if the user input is blank, a comment, or just a newline
character. if it's none of those things, newline char added by getline will be removed
and the getilne buffer will be copied into the user input string. if any of the input checks
(isblank, iscomment, isnewline) return true, the user input string will instead be set to
NO_ACTION (since these inputs should cause no action to be taken and the user just returned
back to the command line for new input). the buffer then gets freed. returns void.
*/
void GetInputString(char *userInputString){
//getline use adapted from my own work in OSU CS 344 Winter 2019 Assignment 2
//create buffer of size MAX_CHARS (2048 as given in the assignment instructions plus one for null terminator)
char *buffer;
size_t bufsize = MAX_CHARS;
size_t characters;
//malloc the buffer and check that malloc is successful
buffer = (char *)malloc(bufsize * sizeof(char));
//if buffer == NULL, malloc had an error. print error message and exit with value of 1.
if(buffer == NULL){
perror("GETLINE BUFFER ERROR, UNABLE TO ALLOCATE\n");
exit(1);
}
//keep looping (by using while(1)) to get the line of user input. check for if getline returns -1
//is used to make sure getline doesn't encounter any problems due to signals
while(1){
//call getline to get user input from stdin and put it in the buffer
characters = getline(&buffer, &bufsize, stdin);
//check if getline returned -1
if(characters == -1){
//if getline returned -1, use clearerr on stdin and let it loop back around
clearerr(stdin);
}
else{
//else if getline was successful (didn't return -1), go ahead and break out of loop
break;
}
}
//check if the user entered a blank line, a comment, or only a newline char
if((IsBlank(buffer) == FALSE) && (IsComment(buffer) == FALSE) && (IsNewline(buffer) == FALSE)){
//if user didn't enter any of these types of input, remove the newline char from the buffer,
//replacing it with a null terminating character
buffer[strcspn(buffer, "\n")] = '\0';
//copy the buffer with the newline char removed into the userinput string variable
strcpy(userInputString, buffer);
}
else{
//else if the user did enter either a blank line, comment, or only a newline char, don't
//do anything with the buffer. set the user input string to NO_ACTION (since those types
//of inputs should result in no action taking place and the user being returned to
//the command line and re-prompted).
strcpy(userInputString, NO_ACTION);
}
//free the buffer that was malloc'd for getline and set to NULL
free(buffer);
buffer = NULL;
}
/*
NAME
getpid
SYNOPSIS
gets the int pid of the current process and transforms it into the same pid but in string form
DESCRIPTION
takes no parameters. gets the int pid of the current process using getpid(). converts this into
a string version of the pid using snprintf. returns the static string version of the process pid.
*/
char *GetPID(){
//create static string to hold the string version of the int pid (make it static so the string
//will remain after function exits)
static char returnStringPID[] = "";
//get current process pid int
int pid = getpid();
//using snprintf to convert an int into a string is adapted from my own work previously
//created for OSU CS 344 Winter term, assignment 2/13/19
//convert int pid into string pid.
//get length of pid
int length = snprintf(NULL, 0, "%d", pid); fflush(stdout);
//create a temporary string variable for the string version of the PID and malloc it to length + 1 (for null term)
char *stringPID = malloc(length + 1);
//check that malloc was succcessful. if not successful (i.e. returns NULL), print error and exit w/ non-zero exit status
if(stringPID == NULL){
perror("ERROR, NOT ALLOCATED\n");
exit(1);
}
//use snprintf to set the string variable to the string version of the int pid
snprintf(stringPID, length + 1, "%d", pid); fflush(stdout);
//create another temporary string variable. copy the string version of the pid into another temporary variable
//(so the original one that was malloc'd can be freed. (when didn't use a malloc'd string had some errors when trying to use snprintf,
//and when tried to copy the malloc'd version directly into the static string to be returned also got errors, but worked when using a
//temporary variable).
char *copyStringPID = stringPID;
//copy the temporary string pid variable into the static string that will be returned
strcpy(returnStringPID, copyStringPID);
//free memory created for the original temporary string variable that was malloc'd
free(stringPID);
stringPID = NULL;
//return static string version of the int pid
return returnStringPID;
}
/*
NAME
replacestring
SYNOPSIS
replaces a specified substring in a string with another specified substring
DESCRIPTION
takes in three strings, one (str) which is the complete string, another (orig)
which is the substring that's to be replaces, and another (rep) that is the replacement
string meant to be inserted in the place of the original substring. returns the
new string that contains the replacement substring in place of the original substring.
*/
char *ReplaceString(char *str, char *orig, char *rep){
//replace string method adapted from:
//Adapted from: https://stackoverflow.com/questions/32413667/replace-all-occurrences-of-a-substring-in-a-string-in-c and
//https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/
//create result string (will be a temporary string variable to hold the newly expanded string with the replacement substring)
char *result;
//create two counter variables and initialize to zero
int i = 0;
int cnt = 0;
//save original length of the replacement substring (string version of the pid)
int newWlen = strlen(rep);
//save the original length of the original substring to be replaced ("$$")
int oldWlen = strlen(orig);
//loop through each char in the original long string for the entire length of the string (i.e. until null term is reached),
//checking for occurrences of the original substring ("$$")
for(i = 0; str[i] != '\0'; i++){
//strstr information adapted from:
//https://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm
//strstr returns a pointer to the first occurence in the main string of any of
//the sequence of characters specified in the substring to be searched for. returns
//null if no sequence found. so in this case, strstr shoud return null if no instances
//of $$ are found. Should return pointer to the first occurrence in the main string of the substring.
if (strstr(&str[i], orig) == &str[i]){
//if an occurrence of '$' was found, increment the substring count (cnt)
cnt++;
//set the i loop variable to add the old length of the substring ('$$') - 1.
i += oldWlen - 1;
}
}
//malloc the result string to the appropriate size using the i loop variable, cnt '$' counter
//variable, and length of the replacement string (pid) minus the length of the original substring ("$$")
result = (char *)malloc(i + cnt *(newWlen - oldWlen) + 1);
//reset the i looping variable to zero.
i = 0;
//replace each occurrence of the orig substring("$$") with the new subtsring (pid)
//loop through the entire string
while (*str){
//if strstr returns a pointer to the first occurrence of the "$$" substring (instead of null)
if(strstr(str, orig) == str){
//copy the replacement substring into the result string, starting at the pointer where
//"$$" was first found.
strcpy(&result[i], rep);
//set i to add the new length to itself (i.e. the length of the new replacement
//substring, the pid)
i += newWlen;
//set the string to add the old length to itself (i.e. the length of the original
//substring, "$$"
str += oldWlen;
}
//if strstr returns null, set the next char after the current one in result to the next
//char in the complete original string (since will need the rest of the original string added to
//the end of the newly inserted substring)
else{
result[i++] = *str++;
}
}
//set result to a new string to be returned so result memory can be freed
//set last char value in result to a null terminator
result[i] = '\0';
//create another static string of maxchars + 1 (for null term). make statis so it persists
//after the function exits, and memset it to null terminators.
static char returnStr[MAX_CHARS + 1];
memset(returnStr, '\0', sizeof(returnStr));
//use strcpy to copy the malloc'd result string (the new string with the substring replacement/
//expansion) into the static string
strcpy(returnStr, result);
//free memory from the malloc'd temp string and set to NULL
free(result);
result = NULL;
//return newly expanded static string, which should now contain the string version of the pid
//in place of every instance of "$$"
return returnStr;
}
/*
NAME
variableexpand
SYNOPSIS
performs variable expansion, replacing every "$$" with the pid
DESCRIPTION
takes in a variable string. creates an original long string, an original substring,
the new substring to be inserted into the main string to replace the original substring,
and the newly expanded string. will call the ReplaceString function to obtain the newly
expanded string. will set the string passed in to this new string using strcpy. returns void.
*/
void VariableExpand(char *varIn){
//create original long string
char *str;
//create original substring ("$$")
char *orig;
//create new substring for expansion (pid)
char *rep;
//malloc these strings
str = (char*)malloc((MAX_CHARS) * sizeof(char));
orig = (char*)malloc((MAX_CHARS) * sizeof(char));
rep = (char*)malloc((MAX_CHARS) * sizeof(char));
//check that malloc was successful (i.e. none of them == NULL)
if((str == NULL) || (orig == NULL) || (rep == NULL)){
//if any were not malloc'd successfully, print error message and exit w non-zero value
perror("VARIABLE EXPANSION STRING MALLOC ERROR\n");
exit(1);
}
//call GetPID function to get string version of the pid
char *pid = GetPID();
//copy the variable passed in as a parameter to the original long string
strcpy(str, varIn);
//set the original substring to "$$" (per the assignment instructions)
strcpy(orig, "$$");
//set the replacement string to the string version of the pid returned by GetPID()
strcpy(rep, pid);
//create the newly expanded string and set it to the string returned by the call to
//the ReplaceString function. pass in the main long string, original substring, and new
//replacement substring to the ReplaceString function
char *newStr = ReplaceString(str, orig, rep);
//copy the newly expanded string to the variable passed into this function
strcpy(varIn, newStr);
//free and set to NULL all of the temporary string and substring variables that were malloc'd
free(str);
str = NULL;
free(orig);
orig = NULL;
free(rep);
rep = NULL;
}
/*
NAME
stringmatch
SYNOPSIS
checks if two strings match each other
DESCRIPTION
receives two strings as parameters. uses strcmp to compare the two strings. if they match, returns
true. else returns false. created this function because was getting tripped up at times with how
strcmp returns 0 if two strings match, while i have 0 set to false everywhere else in the program so
it seemed like should be returning non-zero. after making mistakes with it a few times decided to create
my own function to check strings.
*/
int StringMatch(char *string1, char *string2){
//use strcmp to see if string one and two are equal. (if equal, strcmp will return 0. will return
//non-zero if the two strings are not equal.
if(strcmp(string1, string2) == 0){
//if the two strings are a match, return true
return TRUE;
}
//if the two strings are not a match, return false
return FALSE;
}
/*
NAME
getargs
SYNOPSIS
parses the user input string into separate pieces
DESCRIPTION
takes the long user input string and parses it into arguments, input file (if there), output file (if there),
and sets the background bool to either true or false (depending on if the user entered '&' as their last arg
or not). puts the parsed arguments into an array of strings (parsedInput). returns the number of inputs.
*/
int GetArgs(char **parsedInput, char *userInputString, char *inputFileIn, char *outputFileIn, int *isBackgroundBool){
//use of strtok to parse input with a given token (i.e. the "spaces" between input arguments for this assignment)
//is adapted from my own work for OSU cs 344 assignment 2 winter 2019 2/13/19 and
//http://www.cplusplus.com/reference/cstring/strtok/
//initialize input count to 0
int inputCount = 0;
//initialize is output file bool to false
int isOutFile = FALSE;
//initialize is input file bool to false
int isInFile = FALSE;
//create "space" string and a string for the "token" to be used by strtok
char *space = " ";
char *token;
//use strtok to split the string into tokens (each token is a piece of input) using
//the space " " chars as delimiters between each argument to be parsed.
token = strtok(userInputString, space);
//malloc the first parsed input in the parsed input array of strings
parsedInput[inputCount] = malloc((MAX_CHARS) * sizeof(char));
//check that it malloc'd correctly. if not, print error and exit w non-zero value
if(parsedInput[inputCount] == NULL){
perror("USER INPUT MALLOC ERROR\n");
exit(1);
}
//copy the input arg (token) into the current empty string in the array of strings
strcpy(parsedInput[inputCount], token);
//use strstr to check if the current input from the user needs variable expansion.
//if strstr == NULL, doesn't need expansion. if returns not equal to NULL, call
//the variable expand function to expand any instance of $$ in the input arg to
//the pid
if(strstr(parsedInput[inputCount], "$$") != NULL){
VariableExpand(parsedInput[inputCount]);
}
//increment the input count
inputCount++;
//loop through entire user input string (i.e. while the token != NULL)
while(token != NULL){
//get the next arg using space " " as a delimiter
token = strtok(NULL, space);
//check that the next arg isn't NULL
if(token != NULL){
//if next arg isn't NULL, check if it's "<" (which would mean the next arg is going to be an input file for redirection)
if(StringMatch(token, "<") == TRUE){
//if the current arg is a "<", set input file bool to true so next arg will go to input file string instead of arg array
isInFile = TRUE;
}