-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOLDsmallsh.c
More file actions
895 lines (824 loc) · 25.2 KB
/
Copy pathOLDsmallsh.c
File metadata and controls
895 lines (824 loc) · 25.2 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
/*
Name: Molly Johnson
ONID: johnsmol
CS 344 Fall 2018
Due: 11/14/18
All information used to create this code is adapted from the OSU CS 344 Fall 2018
lectures, assignment instructions/hints, and my own work from Assignment 2 from this
class unless otherwise specifically indicated.
*/
//added 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
#define MAX_CHARS 2048
#define MAX_ARGS 512
#define EXIT "exit"
#define CD "cd"
#define STATUS "status"
#define FALSE 0
#define TRUE 1
#define INVALID "INVALID"
//global variables
int exitStatus = 0; //exit status for the program. set to 0 to start with by default
int backgroundPossible = TRUE; //flag for if background mode is possible (if SIGSTP command given, should ignore "&" and just run foreground commands)
//function declarations
void catchSIGINT(int signo);
int isBuiltIn(char **inputArgs);
char *getPID();
char *replaceString(char *str, char *orig, char *rep);
void commandOrFileExpand(char *commandOrFile);
void argExpand(char **argsArrayIn, int argCounter);
void exitBuiltIn(char *buffer, char **inputArgs, int argCounter, char *inputFile, char *outputFile, char *background, char *userInput, int *backgroundArrayCopy,
int *backgroundArray, int backgroundProcessCount);
void changeDirBuiltInWithArg(char **argArray);
void changeDirBuiltInNoArg();
void statusBuiltIn();
/*
NAME
statusbuiltin
SYNOPSIS
built in status command
DESCRIPTION
checks if process exited normally or due to signal
*/
void statusBuiltIn()
{
//information on how to obtain exit status and terminating signal information using WIFEXITED
//and WEXITSTATUS 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
//and lecture (only either WIFEXITED or WTERMSIG macros will be non zero, not both)
if(WIFEXITED(exitStatus)!=0) //returns not 0 if process exited normally
{
//if no foreground command has been called yet, the value of statusIn passed in will just be 0, so is
//no need for an additional if/else statement to check if a foreground process has been called or not.
int exitStat = WEXITSTATUS(exitStatus); //returns exit code of the child when child process exited normally
printf("Exit code was: %d\n", exitStat);
fflush(stdout);
}
else //if process did not exit normally (i.e. ended due to signal), checks termination status to see which signal caused child process to exit
{
int termSignal = WTERMSIG(exitStatus);
printf("Signal code was: %d\n", termSignal);
fflush(stdout);
}
}
/*
NAME
changedirbuiltinnoarg
SYNOPSIS
changes current directory w no args
DESCRIPTION
changes current directory to the home directory
*/
void changeDirBuiltInNoArg()
{
//getting home environment variable 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 directory, then change current directory to home directory
char *homeDir = getenv("HOME");
chdir(homeDir);
}
/*
NAME
changedirbuiltinwitharg
SYNOPSIS
changes to specified directory
DESCRIPTION
changes current directory to one specified by user. if doesn't exist, gives error message
*/
void changeDirBuiltInWithArg(char **argArray)
{
//chdir information adapted from:
//https://linux.die.net/man/2/chdir
//info on how to check if chdir() was successful or not adapted from:
//https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/rtchd.htm
if(chdir(argArray[1]) != 0)
{
perror("chdir() to your specified directory has failed, no such directory here\n");
fflush(stdout);
}
else
{
chdir(argArray[1]);
}
}
/*
NAME
exitbuiltin
SYNOPSIS
built in exit function
DESCRIPTION
cleans up memory and exits
*/
void exitBuiltIn(char *buffer, char **inputArgs, int argCounter, char *inputFile, char *outputFile, char *background, char *userInput, int *backgroundArrayCopy,
int *backgroundArray, int backgroundProcessCount)
{
//free memory (since if exit called will use sigterm and thus terminate the job/process, not allowing
//the main function to reach completion and free this allocated memory normally
//kill remaining processes
//using kill adapted from:
//http://www.linuxquestions.org/questions/programming-9/how-to-kill-a-child-and-all-its-subsequent-child-process-in-c-247798/ and
//https://www.booleanworld.com/kill-process-linux/ and
//https://www.quora.com/What-is-the-difference-between-the-SIGINT-and-SIGTERM-signals-in-Linux-What%E2%80%99s-the-difference-between-the-SIGKILL-and-SIGSTOP-signals
//call kill command and exit
for(int j = 0; j < backgroundProcessCount; j++)
{
kill(backgroundArray[j], SIGTERM);
}
free(buffer);
buffer = NULL;
for(int k = 0; k < argCounter; k++)
{
free(inputArgs[k]);
}
free(inputArgs);
inputArgs = NULL;
//free(command);
//command = NULL;
free (inputFile);
inputFile = NULL;
free (outputFile);
outputFile = NULL;
free (background);
background = NULL;
free(backgroundArrayCopy);
backgroundArrayCopy = NULL;
free(userInput);
userInput = NULL;
//kill(0, SIGTERM);
//kill(0, SIGKILL);
exit(0);
}
/*
NAME
replacestring
SYNOPSIS
replaces a string for var expansion
DESCRIPTION
replaces specified substring of a string with a new substring for variable expansion
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/
*/
char *replaceString(char *str, char *orig, char *rep)
{
char *result;
int i = 0;
int cnt = 0;
//save lengths of the replacement substring (pid) and the original substring ("$$")
int newWlen = strlen(rep);
int oldWlen = strlen(orig);
//go through each char in the original long string, to check for occurrences of the original substring ("$$")
for(i = 0; str[i] != '\0'; i++)
{
if (strstr(&str[i], orig) == &str[i])
{
cnt++;
i += oldWlen - 1;
}
}
result = (char *)malloc(i + cnt *(newWlen - oldWlen) + 1);
i = 0;
//replace each occurrence of the orig substring("$$") with the new subtsring (pid)
while (*str)
{
if(strstr(str, orig) == str)
{
strcpy(&result[i], rep);
i += newWlen;
str += oldWlen;
}
else
{
result[i++] = *str++;
}
}
//set result to a new string to be returned so result memory can be freed
result[i] = '\0';
static char returnStr[MAX_CHARS + 1];
memset(returnStr, '\0', sizeof(returnStr));
strcpy(returnStr, result);
//free memory
free(result);
result = NULL;
//return newly expanded string
return returnStr;
}
/*
NAME
commandorfileexpand
SYNOPSIS
expands var name of a command or file
DESCRIPTION
expands every $$ in a variable to the parent pid
*/
void commandOrFileExpand(char *commandOrFile)
{
char *str;
char *orig;
char *rep;
str = (char*)malloc((MAX_CHARS +1) * sizeof(char));
orig = (char*)malloc((MAX_CHARS +1) * sizeof(char));
rep = (char*)malloc((MAX_CHARS +1) * sizeof(char));
//get parent process pid
char *pid = getPID();
strcpy(str, commandOrFile); //set str to orig command/filename
strcpy(orig, "$$"); //set orig substring to be replaced to be "$$"
strcpy(rep, pid); //set replacement string for substring to be the parent pid
//call replaceString to replace $$ with pid
char *newStr = replaceString(str, orig, rep);
//copy the newly expanded string into the command or file
strcpy(commandOrFile, newStr);
fflush(stdout);
//free memory
free(str);
str = NULL;
free(orig);
orig = NULL;
free(rep);
rep = NULL;
}
/*
NAME
argexpand
SYNOPSIS
expands var name of each arg
DESCRIPTION
expands every $$ in every arg element to the parent pid
*/
void argExpand(char **argsArrayIn, int argCounter)
{
for(int k = 0; k < argCounter; k++)
{
//check for each occurrence of 2 $$ signs in all args
if(strstr(argsArrayIn[k], "$$") != NULL)
{
char *str;
char *orig;
char *rep;
str = (char*)malloc((MAX_CHARS +1) * sizeof(char));
orig = (char*)malloc((MAX_CHARS +1) * sizeof(char));
rep = (char*)malloc((MAX_CHARS +1) * sizeof(char));
//get the parent process pid
char *pid = getPID();
strcpy(str, argsArrayIn[k]); //set str to orig command/filename
strcpy(orig, "$$"); //set orig substring to be replaced to be "$$"
strcpy(rep, pid); //set replacement string for substring to be the parent pid
//call replaceString to replace $$ with pid
char *newStr = replaceString(str, orig, rep);
//copy the newly expanded string into the argument
strcpy(argsArrayIn[k], newStr);
//free memory
free(str);
str = NULL;
free(orig);
orig = NULL;
free(rep);
rep = NULL;
}
}
}
/*
NAME
getpid
SYNOPSIS
gets parent pid
DESCRIPTION
gets parent pid,converts the int to a string
Adapted directly from my own previous work (assignment 2)
*/
char *getPID()
{
//static so will remain after function exits
static char returnStringPID[] = "";
//get parent process pid int
int pid = getpid();
//convert int pid into string pid
int length = snprintf(NULL, 0, "%d", pid);
fflush(stdout);
char *stringPID = malloc(length + 1);
if(stringPID == NULL)
{
printf("ERROR, NOT ALLOCATED\n");
fflush(stdout);
exit(1);
}
snprintf(stringPID, length + 1, "%d", pid);
fflush(stdout);
char *copyStringPID = stringPID;
strcpy(returnStringPID, copyStringPID);
//free memory
free(stringPID);
stringPID = NULL;
//return string version of the int pid
return returnStringPID;
}
/*
NAME
isbuiltin
SYNOPSIS
checks if command is built in
DESCRIPTION
checks if command is built in (exit, status, or cd). if yes, returns true. else returns false.
*/
int isBuiltIn(char **inputArgs)
{
//int used instead of bool. 0 = false, 1 = true. initialize to false
int boolVal = FALSE;
//check if first element of argument array is a built in command
if((strcmp(inputArgs[0], EXIT) == 0) || (strcmp(inputArgs[0], CD) == 0) || (strcmp(inputArgs[0], STATUS) == 0))
{
//command is a built in, set bool to true
boolVal = TRUE;
}
return boolVal;
}
/*
NAME
catchsigint
SYNOPSIS
sigint handler
DESCRIPTION
catches the sigint and handles it, prints message
*/
void catchSIGINT(int signo)
{
//information about using write() adapted from:
//http://codewiki.wikidot.com/c:system-calls:write (in addition to lecture example)
//sigaction information adapted from:
//http://www.tutorialspoint.com/unix_system_calls/sigaction.htm and
//http://man7.org/linux/man-pages/man2/sigaction.2.html and
//https://web.mst.edu/~ercal/284/SignalExamples/sigaction-EX1.c
char *message = "SIGINT. CTRL-Z to stop.\n";
write(STDOUT_FILENO, message, 28);
fflush(stdout);
//0 = stdin, 1 = stdout, 2 = stderr
//write(1, message, 28);
}
/*
NAME
main
SYNOPSIS
is the main function
DESCRIPTION
creates signal structs, args/command/input and output file arrays, calls other functions,
takes and parses user's input, calls builtin commands or forks child processes if needed, determines
if need to run in background mode or foreground mode, uses execvp to call non-built in
processes. realize it is too long and a bit messy but ran out of time to break up into more functions.
*/
int main()
{
//SIGINT handler
//sigaction information adapted from:
//http://www.tutorialspoint.com/unix_system_calls/sigaction.htm and
//http://man7.org/linux/man-pages/man2/sigaction.2.html and
//https://web.mst.edu/~ercal/284/SignalExamples/sigaction-EX1.c
struct sigaction SIGINT_action = {0};
//struct sigaction SIGINT_action = {{0}};
SIGINT_action.sa_handler = catchSIGINT;
sigfillset(&SIGINT_action.sa_mask);
sigaction(SIGINT, &SIGINT_action, NULL);
//dynamic array of ints adapted from:
//https://stackoverflow.com/questions/40464927/how-to-dynamically-allocate-an-array-of-integers-in-c
int max = MAX_ARGS + 1;
int *backgroundArray = (int*)malloc(max * sizeof(int));
int *backgroundArrayCopy = backgroundArray;
int backgroundProcessCount = 0;
int sourceFD, targetFD, result;
char *userInput;
int numInputs;
userInput = (char *)malloc((MAX_CHARS + 1) * sizeof(char));
if(userInput == NULL)
{
printf("ERROR, UNABLE TO ALLOCATE\n");
fflush(stdout);
exit(1);
}
do{
//char *command;
char *inputFile;
char *outputFile;
char *background;
//command = (char *)malloc((MAX_CHARS + 1) * sizeof(char));
inputFile = (char *)malloc((MAX_CHARS + 1) * sizeof(char));
outputFile = (char *)malloc((MAX_CHARS + 1) * sizeof(char));
background = (char *)malloc((MAX_CHARS + 1) * sizeof(char));
if(inputFile == NULL ||
outputFile == NULL || background == NULL)
{
printf("ERROR, UNABLE TO ALLOCATE\n");
fflush(stdout);
exit(1);
}
//strcpy(command, INVALID);
strcpy(inputFile, INVALID);
strcpy(outputFile, INVALID);
strcpy(background, INVALID);
char **inputArgs;
inputArgs = malloc((MAX_CHARS + 1) * sizeof(char*));
if(inputArgs == NULL)
{
printf("ERROR, UNABLE TO ALLOCATE\n");
fflush(stdout);
exit(1);
}
int argCounter = 0;
numInputs = 0;
printf(": ");
//use of fflush adapted from:
//https://www.tutorialspoint.com/c_standard_library/c_function_fflush.htm
fflush(stdout);
char *buffer;
size_t bufsize = MAX_CHARS + 1;
size_t characters;
buffer = (char *)malloc(bufsize * sizeof(char));
if (buffer == NULL)
{
printf("ERROR, UNABLE TO ALLOCATE\n");
fflush(stdout);
exit(1);
}
//make sure to account for possible signal interrupts with getline (from lecture)
while(1)
{
characters = getline(&buffer, &bufsize, stdin);
//if getline gets interrupted and returns -1, remove the error
//status from stdin before resuming
if(characters == -1)
{
clearerr(stdin);
}
else
{
//break out of loop, received valid input
break;
}
}
int isBuilt = FALSE;
//if user entered some input other than just hitting enter (i.e. a blank line)
//and also didn't enter a comment (check pos 0 of buffer string)
if((strcmp(buffer, "\n") != 0) && (buffer[0] != '#'))
{
//remove the newline char that getline adds
char *bufferNoNewLine = strtok(buffer, "\n");
strcpy(userInput, bufferNoNewLine);
//strtok use adapted from:
//https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
char *space = " ";
char *token;
token = strtok(userInput, space);
//inputArgs[argCounter] = malloc((MAX_CHARS + 1) * sizeof(char));
inputArgs[argCounter] = malloc((MAX_CHARS + 1) * sizeof(char));
strcpy(inputArgs[argCounter], token);
numInputs++;
argCounter++;
//commandOrFileExpand(command);
//strcpy(inputArgs[argCounter], token);
int isOutFile = FALSE;
int isInFile = FALSE;
//int isBackground = FALSE;
while(token != NULL)
{
token = strtok(NULL, space);
if(token != NULL)
{
//check if token is an input file redirect, output file redirect, or background indicator
if(strcmp(token, "<") == 0)
{
//set to true so next arg can be assigned to input file
isInFile = TRUE;
}
else if(strcmp(token, ">") == 0)
{
//set to true so next arg can be assigned to output file
isOutFile = TRUE;
}
/*else if(strcmp(token, "&") == 0)
{
strcpy(background, "&");
isBackground = TRUE;
}*/
else
{
//check if this token is an input file, output file, or argument
if(isInFile == TRUE)
{
//if this is input file, copy it to inputFile variable,
strcpy(inputFile, token);
commandOrFileExpand(inputFile);
isInFile = FALSE;
}
else if(isOutFile == TRUE)
{
//if this is output file, copy it to outputFile variable,
strcpy(outputFile, token);
commandOrFileExpand(outputFile);
isOutFile = FALSE;
}
else //if isn't an input file or an output file or redirect char (> or <), add to args array
{
inputArgs[argCounter] = malloc((MAX_CHARS + 1) * sizeof(char));
strcpy(inputArgs[argCounter], token);
argCounter++;
numInputs++;
}
}
}
}
//check if last arg is the "&" background character or not.
//if is, remove from args array.
//skip if no args present
if(argCounter >1)
{
if(strcmp(inputArgs[argCounter - 1], "&") == 0)
{
strcpy(background, "&");
free(inputArgs[argCounter - 1]);
inputArgs[argCounter - 1] = NULL;
argCounter--;
}
}
//check all args for $$ and expand if needed
argExpand(inputArgs, argCounter);
//check if command is a built in
isBuilt = isBuiltIn(inputArgs);
if(isBuilt == TRUE)
{
if(strcmp(inputArgs[0], STATUS) == 0)
{
//since status is initialized to 0 to start with, so if no foreground process
//has been run before this command it will have an exit status of 0 as described
//in the assignment instructions
statusBuiltIn();
}
else if(strcmp(inputArgs[0], CD) == 0)
{
if(argCounter == 1) //if cd called w no args
{
changeDirBuiltInNoArg();
}
else if(argCounter == 2) //if cd called w one arg
{
changeDirBuiltInWithArg(inputArgs);
}
else //if cd called w too many args
{
printf("you entered too many arguments for the cd command. This command can only take 0 or 1 arguments\n");
fflush(stdout);
}
}
else if(strcmp(inputArgs[0], EXIT) == 0)
{
//exitBuiltIn();
//won't check other args, just exits if exit entered as command, other args if present will be ignored and program will exit
exitBuiltIn(buffer, inputArgs, argCounter, inputFile, outputFile, background, userInput, backgroundArrayCopy,backgroundArray,backgroundProcessCount );
}
//else
//{
//}
}
else //else isn't a built in command
{
//forking info adapted from https://www.geeksforgeeks.org/fork-system-call/ and
//https://stackoverflow.com/questions/32810981/fork-function-in-c (and the lecture)
pid_t spawnpid = -5;
//fork w the child pid
spawnpid = fork();
//if something went wrongy, errno var set, no child process created
if(spawnpid == -1)
{
perror("forking error!\n");
fflush(stdout);
exit(1);
}
//in the child process, fork() returns 0
else if (spawnpid == 0)
{
//file in/out redirect w forking adapted from https://stackoverflow.com/questions/5938139/redirecting-input-and-output-of-a-child-process-in-c
//(and lecture)
//redirect if needed. if a file name = INVALID, no redirect required
if(strcmp(inputFile, INVALID) != 0) //if input file redirect needed
{
//open input file
sourceFD = open(inputFile, O_RDONLY);
if(sourceFD == -1)
{
perror("error opening input file\n");
exit(1);
}
else
{
//dup2 (duplicate file descriptor) info adapted from:
//https://linux.die.net/man/2/dup2 (and lecture)
result = dup2(sourceFD, 0);
if (result == -1)
{
perror("error with dup2 and input file\n");
exit(2);
}
//close on exec
fcntl(sourceFD, F_SETFD, FD_CLOEXEC);
}
}
if(((strcmp(inputFile, INVALID) == 0) && backgroundPossible == TRUE) && (strcmp(background,INVALID) != 0))
{
static char *backgroundRedirect = "/dev/null";
//open input file
sourceFD = open(backgroundRedirect, O_RDONLY);
if(sourceFD == -1)
{
perror("error opening input file\n");
exit(1);
}
else
{
//dup2 (duplicate file descriptor) info adapted from:
//https://linux.die.net/man/2/dup2 (and lecture)
result = dup2(sourceFD, 0);
if (result == -1)
{
perror("error with dup2 and input file\n");
exit(2);
}
//close on exec
fcntl(sourceFD, F_SETFD, FD_CLOEXEC);
}
}
//}
if(strcmp(outputFile, INVALID) != 0) //if output file redirect needed
{
//open output file
//unix permissions info adapted from:
//http://permissions-calculator.org/decode/0644/ (and lecture)
targetFD = open(outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(targetFD == -1)
{
perror("error opening output file\n");
exit(1);
}
else
{
result = dup2(targetFD, 1);
if(result == -1)
{
perror("error with dup2 and output file\n");
exit(2);
}
//close on exec
fcntl(targetFD, F_SETFD, FD_CLOEXEC);
}
}
if(((strcmp(outputFile, INVALID) == 0) && backgroundPossible == TRUE) && (strcmp(background,INVALID) != 0))
{
static char *backgroundRedirectOut = "/dev/null";
//open output file
//unix permissions info adapted from:
//http://permissions-calculator.org/decode/0644/ (and lecture)
targetFD = open(backgroundRedirectOut, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(targetFD == -1)
{
perror("error opening output file\n");
exit(1);
}
else
{
result = dup2(targetFD, 1);
if(result == -1)
{
perror("error with dup2 and output file\n");
exit(2);
}
//close on exec
fcntl(targetFD, F_SETFD, FD_CLOEXEC);
}
}
//using execvp() to make sure use PATH variable (suggested by TA
//in OSU CS 344 slack discussion) instead of other variants of exec functions
//execvp info 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 lecture)
if(execvp(inputArgs[0], inputArgs) < 0)
{
//printf("execvp error with command %s\n", inputArgs[0]);
//fflush(stdout);
perror("execvp error with your command\n");
exit(2);
}
}
//in the parent process, fork() returns process id of the child process that was just created
else
{
//check if background is possible and user requested it
if((backgroundPossible == TRUE) && (strcmp(background, "&") == 0))
{
//waitpid, background, and WNOHANG info adapted from:
//https://linux.die.net/man/2/waitpid and lecture and
//https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwaip.htm
//and discussion on the CS 344 OSU slack discussion board
pid_t waitPID = waitpid(spawnpid, &exitStatus, WNOHANG);
if(waitPID == -1)
{
perror("wait() error\n");
exit(1);
}
printf("this process is in the background: %d\n", spawnpid);
fflush(stdout);
}
else //don't use background keep in foreground
{
pid_t waitPID = waitpid(spawnpid, &exitStatus, 0);
if(waitPID == -1)
{
perror("wait() error\n");
exit(1);
}
backgroundProcessCount++;
backgroundArray[backgroundProcessCount] = spawnpid;
//printf("this pid is in the foreground: %d\n", spawnpid);
fflush(stdout);
}
}
}
}
//if user just hits enter (i.e. a blank line) or writes a comment, set userInput to invalid (set to something to prevent segfault) and re prompt
else
{
strcpy(userInput, "INVALID");
printf("\n");
fflush(stdout);
}
//free memory
free(buffer);
buffer = NULL;
//printf("the command is: %s\n", command);
//fflush(stdout);
//printf("the input file is: %s\n", inputFile);
//fflush(stdout);
//printf("the output file is: %s\n", outputFile);
//fflush(stdout);
//printf("the background is: %s\n", background);
//fflush(stdout);
//printf("the parent pid is: %s\n", getPID());
//fflush(stdout);
for(int k = 0; k < argCounter; k++)
{
//printf("arg %d is: %s\n", k, inputArgs[k]);
//fflush(stdout);
free(inputArgs[k]);
}
free(inputArgs);
inputArgs = NULL;
//free(command);
//command = NULL;
free (inputFile);
inputFile = NULL;
free (outputFile);
outputFile = NULL;
free (background);
background = NULL;
//print non-terminated remaining background processes
//adapted from: https://stackoverflow.com/questions/26381944/how-to-check-if-a-forked-process-is-still-running-from-the-c-program
/*if((strcmp(background, "&") == 0) && (backgroundProcessCount > 0)){
for(int i = 0; i < backgroundProcessCount; i++)
{
if((waitpid(backgroundArray[i], &exitStatus, WNOHANG)) < 0)
{
printf("background process pid %d not completed\n", backgroundArray[i]);
fflush(stdout);
}
}
}*/
//background process print causing core dump, needs fixed
}while((strcmp(userInput, "exit") != 0) || (numInputs != 1));
//free memory
free(backgroundArrayCopy);
backgroundArrayCopy = NULL;
free(userInput);
userInput = NULL;
return 0;
}