-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallsh.c
More file actions
620 lines (502 loc) · 20.3 KB
/
Copy pathsmallsh.c
File metadata and controls
620 lines (502 loc) · 20.3 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
// Sam Judkis
// 3/3/19
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
// utility function declarations
int myStrLen(char *input);
void insertPID(char* buffer, char* input);
void freeArray(char* array[], int num);
// built in function declarations
void status(int, int);
void cd(char* args[]);
/*********************************************/
// foreground only mode
// global variable, 1 if in foreground mode, 0 if not
int fgMode = 0;
// SIGTSTP signal handler
void catchTSTP(int signo)
{
// not currently in fg mode
if (!fgMode)
{
// enter fg mode
fgMode = 1;
char* msg = "\nEntering foreground-only mode (& is now ignored)\n"; //50
write(STDOUT_FILENO, msg, 50);
}
// currently in fg mode
else
{
// exit fg mode
fgMode = 0;
char* msg = "\nExiting foreground-only mode\n"; //30
write(STDOUT_FILENO, msg, 30);
}
}
/*******************************************/
int main() {
int numCharsEntered = -19; // store num chars entered to getline
size_t bufferSize = 0; // holds size of allocated buffer
char *lineEntered = NULL; // buffer allocated by getline(). string + \n + \0
char lineWithPID[10000]; // buffer to hold input string with $$ replaced by pid
// array of char* to hold args passed to shell, plus one for NULL
char *args[513];
/*******************************/
// signal handling
// shell ignores SIGINT signals
struct sigaction SIGINT_ignore = {{0}};
SIGINT_ignore.sa_handler = SIG_IGN;
sigfillset(&SIGINT_ignore.sa_mask);
SIGINT_ignore.sa_flags = 0;
sigaction(SIGINT, &SIGINT_ignore, NULL);
// SIGTSTP toggles foreground only mode
struct sigaction SIGTSTP_handler = {{0}};
SIGTSTP_handler.sa_handler = catchTSTP;
sigfillset(&SIGTSTP_handler.sa_mask);
SIGTSTP_handler.sa_flags = 0;
sigaction(SIGTSTP, &SIGTSTP_handler, NULL);
/*******************************/
/*****************************/
// variables for status function
// if killedBySig == 1, exitStatus contains signal number that killed the process
int exitStatus = 0; // hold exit status of last command
int killedBySig = 0; // boolean, says if last command was terminated by signal
/************************/
// array of background PIDs
int bgArraySize = 256;
int lastPIDindex = 0;
pid_t bgPIDs[bgArraySize];
int i;
// initialize bg pid array with junk values
for (i = 0; i < bgArraySize; i++)
bgPIDs[i] = -19;
// continous loop to get input from user
while (1)
{
// get input from user
while(1)
{
// check for termination of background children
int j;
int updateLastIndex = 0;
int bgExitStatus = -19;
for (j = 0; j < lastPIDindex + 1; j++)
{
// index stores an unharvested PID
if (bgPIDs[j] != -19)
{
// check if terminated
pid_t termResult = waitpid(bgPIDs[j], &bgExitStatus, WNOHANG);
// returned nonzero, has terminated
if (termResult != 0)
{
if (WIFEXITED(bgExitStatus))
{
killedBySig = 0;
exitStatus = WEXITSTATUS(bgExitStatus);
//printf("%d\n", fgMode);
fflush(stdout);
}
// check if terminated by signal
else if (WIFSIGNALED(bgExitStatus))
{
killedBySig = 1;
exitStatus = WTERMSIG(bgExitStatus);
fflush(stdout);
}
// print termination message
printf("background pid %d is done: ", bgPIDs[j]);
fflush(stdout);
status(exitStatus, killedBySig);
// reset array index to junk
bgPIDs[j] = -19;
}
// process has not terminated
else
{
// save index as current latest unterminated pid
updateLastIndex = j;
}
}
}
// save index
lastPIDindex = updateLastIndex;
// prompt to user
printf(": ");
fflush(stdout);
numCharsEntered = getline(&lineEntered, &bufferSize, stdin);
if (numCharsEntered == -1)
{
clearerr(stdin);
}
else
{
break; // exit loop
}
}
// test for comment
if (lineEntered[0] == '#')
{
free(lineEntered);
lineEntered = NULL;
continue;
}
// test for blank line
else if (numCharsEntered == 1)
{
free(lineEntered);
lineEntered = NULL;
continue;
}
// remove newline
lineEntered[strcspn(lineEntered, "\n")] = '\0';
// memset buffer to hold line containing PIDs
memset(lineWithPID, '\0', sizeof(char) * 10000);
// function to replace any $$ with pid
insertPID(lineWithPID, lineEntered);
// tokenize full line into individual args
char delim[2] = " "; // char than demliminates args
// get first arg
char *token = strtok(lineWithPID, delim);
// check if only spaces entered
if (token == NULL)
{
free(lineEntered);
lineEntered = NULL;
continue;
}
int numArgs = 0;
// loop until all args have been found
while (token != NULL)
{
// allocate memory for arg in args array
args[numArgs] = malloc(sizeof(char) * myStrLen(token) + 1);
// copy token to args array
strcpy(args[numArgs], token);
// get next arg
token = strtok(NULL, delim);
// increment counter
numArgs++;
}
// add NULL ptr after args for call to exec
args[numArgs] = NULL;
/************************************/
// user entered status command
if (strcmp(args[0], "status") == 0)
{
status(exitStatus, killedBySig);
}
/****************************/
// user entered cd command
else if (strcmp(args[0], "cd") == 0)
{
// call built in cd command
cd(args);
}
/************************************/
// user entered exit command
else if (strcmp(args[0], "exit") == 0)
{
int i;
// loop through bg process array
for (i = 0; i < lastPIDindex + 1; i++)
{
// kill any bg PIDs still running
if (bgPIDs[i] != -19)
{
kill(bgPIDs[i], SIGKILL);
//printf("killed at exit: %d\n", bgPIDs[i]);
fflush(stdout);
}
}
freeArray(args, numArgs);
free(lineEntered);
return 0;
}
/*************************************/
/* Command exectution */
/*************************************/
// user entered non-built-in command.
else
{
pid_t spawnPID = -19;
int childExitStatus = -19;
// bool, 1 if new process should be background, otherwise 0
int makebg = 0;
// check if last arg is &
if (strcmp(args[numArgs - 1], "&") == 0)
{
// remove & arg from array
free(args[numArgs - 1]);
args[numArgs - 1] = NULL;
// decrement arg counter
numArgs--;
if (!fgMode)
{
// if not in fg only mode, set to 1
makebg = 1;
}
}
// store arguments for redirection
char *redirectIn = NULL;
char *redirectOut = NULL;
// flags, set if input or output is redirected
int inputProvided = 0;
int outputProvided = 0;
// store indices to delete
int inputIndex, outputIndex;
// parse args for < or >
for (i = 0; i < numArgs; i++)
{
// input redirection found
if (strcmp(args[i], "<") == 0)
{
// set flag
inputProvided = 1;
redirectIn = malloc(sizeof(char) * myStrLen(args[i + 1]) + 1);
strcpy(redirectIn, args[i + 1]);
inputIndex = i;
}
else if (strcmp(args[i], ">") == 0)
{
outputProvided = 1;
redirectOut = malloc(sizeof(char) * myStrLen(args[i + 1]) + 1);
strcpy(redirectOut, args[i + 1]);
outputIndex = i;
}
}
// remove redirection arguments from args array
if (inputProvided)
{
free(args[inputIndex]);
args[inputIndex] = NULL;
free(args[inputIndex + 1]);
args[inputIndex + 1] = NULL;
}
if (outputProvided)
{
free(args[outputIndex]);
args[outputIndex] = NULL;
free(args[outputIndex + 1]);
args[outputIndex + 1] = NULL;
}
/********************/
// fork new process
/********************/
spawnPID = fork();
switch(spawnPID)
{
// fork failed
case -1:
{
perror("Hull Breach!\n");
fflush(stderr);
exit(1);
break;
}
/**********************************/
// in child process
case 0:
{
// set child to ignore SIGTSTP signal
struct sigaction SIGTSTP_ignore = {{0}};
SIGTSTP_ignore.sa_handler = SIG_IGN;
sigfillset(&SIGTSTP_ignore.sa_mask);
SIGTSTP_ignore.sa_flags = 0;
sigaction(SIGTSTP, &SIGTSTP_ignore, NULL);
// stop foreground child from ignoring SIGINT signal
if (!makebg)
{
SIGINT_ignore.sa_handler = SIG_DFL;
sigaction(SIGINT, &SIGINT_ignore, NULL);
}
// redirect input to dev/null for background process if no input provided
else
{
// no redirection given, no file argument to read
if (!inputProvided && (args[1] == NULL))
{
int devNull = open("/dev/null", O_RDONLY);
dup2(devNull, 0);
}
if (!outputProvided)
{
int devNull = open("/dev/null", O_WRONLY);
dup2(devNull, 1);
//dup2(devNull, 2);
}
}
// redirect input from given file
int badRedirect = 0; // flag
char err[32];
// open dev/null fd
//int devNull = open("/dev/null", O_WRONLY);
// currently, invalid input redirection leads to 2 error messages
// being printed. One is the message I wrote, the second seems to
// come from the call to open the invalid file. In order to match the
// sample output, I initially figured out a while to stifle the 2nd
// message by redirecting stderr to /dev/null and printing my error
// message with printf instead. This made the output match but seems
// like an improper way to do things. I spoke with Chase Denecke during
// office hours and he recommended that I leave it in the original form,
// without redirecting stderr, but allowing the extra error message to
// print.
if (inputProvided)
{
// redirect stderr to avoid default error message from failed open
//dup2(devNull, 2);
// attempt to open input file
int inPath = open(redirectIn, O_RDONLY);
// check for error
if (inPath == -1)
{
badRedirect = 1;
memset(err, '\0', sizeof(char) * 32);
sprintf(err, "cannot open %s for input\n", redirectIn);
perror(err);
fflush(stderr);
}
// input file opened properly
else
{
// redirect input
dup2(inPath, 0);
}
}
// redirect output to given file
if (outputProvided)
{
// attempt to open output file
int outPath = open(redirectOut, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (outPath == -1)
{
badRedirect = 1;
memset(err, '\0', sizeof(char) * 32);
sprintf(err, "cannot open %s for output\n", redirectOut);
perror(err);
fflush(stderr);
}
// output file opened properly
else
{
dup2(outPath, 1);
}
}
if (badRedirect == 1)
{
exit(1);
break;
}
else
{
// execute command with execvp
execvp(args[0], args);
const char* errCmd = args[0];
perror(errCmd);
fflush(stderr);
exit(2);
}
break;
}
/*************************************/
// in parent process
default:
{
// if child is a foreground process
if (!makebg)
{
// parent should block SIGTSTP until after fg child has terminated
sigset_t block_signals;
sigemptyset(&block_signals);
sigaddset(&block_signals, SIGTSTP);
sigprocmask(SIG_BLOCK, &block_signals, NULL);
spawnPID = waitpid(spawnPID, &childExitStatus, 0);
// check exit status of child process
if (WIFEXITED(childExitStatus))
{
killedBySig = 0;
exitStatus = WEXITSTATUS(childExitStatus);
//printf("%d\n", fgMode);
fflush(stdout);
}
else if (WIFSIGNALED(childExitStatus))
{
killedBySig = 1;
exitStatus = WTERMSIG(childExitStatus);
status(exitStatus, killedBySig);
fflush(stdout);
}
// unblock SIGTSTP, allow parent to receive it
sigprocmask(SIG_UNBLOCK, &block_signals, NULL);
}
// child is a background process
else
{
printf("background pid is %d\n", spawnPID);
pid_t tempPID = waitpid(spawnPID, &childExitStatus, WNOHANG);
// if not terminated
if (tempPID == 0)
{
// find first open index in bgPIDs array
int i = 0;
while ((i < lastPIDindex + 1) && (bgPIDs[i] != -19))
{
i++;
}
// store PID in array
bgPIDs[i] = spawnPID;
if (i > lastPIDindex)
{
lastPIDindex = i;
}
}
// child has terminated
else
{
// check if exited normally
if (WIFEXITED(childExitStatus))
{
killedBySig = 0;
exitStatus = WEXITSTATUS(childExitStatus);
//printf("%d\n", fgMode);
fflush(stdout);
}
// check if terminated by signal
else if (WIFSIGNALED(childExitStatus))
{
killedBySig = 1;
exitStatus = WTERMSIG(childExitStatus);
status(exitStatus, killedBySig);
fflush(stdout);
}
}
}
// free memory used for redirect files
if (redirectIn != NULL)
{
free(redirectIn);
redirectIn = NULL;
}
if (redirectOut != NULL)
{
free(redirectOut);
redirectOut = NULL;
}
break;
}
}
}
/************************************/
// free memory allocated by getline
freeArray(args, numArgs);
free(lineEntered);
lineEntered = NULL;
}
return 0;
}