-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_shell.c
More file actions
558 lines (528 loc) · 14.1 KB
/
Copy pathsimple_shell.c
File metadata and controls
558 lines (528 loc) · 14.1 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
/* ICS 53
* Project: Simple Shell
* Yi-Ju, Tsai/ Hongji Wu
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdbool.h>
typedef struct job
{
int jid; // Job ID
pid_t pid; // Process ID
char ground; // bg = 1, fg = 0
char status[11]; // Running, Foreground, Stopped
char title[80]; // cmdline, ex: "hello &"
}Job;
int arg_count;
int remove_job(pid_t pid);
int add_job(pid_t pid, char ground, char **argv);
void unix_error(const char *msg);
int numOfJob;
struct job job_list[5];
bool stopped;
int child_Pid;
// parse the cmdline and build the argv list
int parseline(char *cpycmd, char **argv)
{
cpycmd[strlen(cpycmd)-1] = ' ';
char *token = strtok(cpycmd, " ");
while(token != NULL)
{
argv[arg_count++] = token;
token = strtok(NULL," ");
}
argv[arg_count] = NULL;
int bg = 0;
if (arg_count==0)
{
return -1;
}
if (!strcmp(argv[arg_count-1],"&")) // bg process
{
bg = 1;
}
return bg; // return 1 if is bg process
}
void IOredirection(char **argv)
{
int fd_out = dup(STDOUT_FILENO);
int fd_in = dup(STDIN_FILENO);
int i;
for (i=0; i<arg_count; i++)
{
if (argv[i]==NULL)
{
break;
}
else if (strcmp(argv[i], "<") == 0)
{
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
int inFileID = open(argv[i + 1], O_RDONLY, mode);
if(inFileID < 0)
{
printf("Input Error!!\n");
exit(1); // end of the program
}
dup2(inFileID, STDIN_FILENO);
}
else if (strcmp(argv[i], ">") == 0) // output
{
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
close(1);
int outFileID = open(argv[i + 1], O_CREAT|O_WRONLY|O_TRUNC, mode);
if(outFileID < 0)
{
printf("Output Error!!\n");
exit(1); // end of the program
}
}
}
}
void display_jobs(char **argv)
{
int i;
for(i=0; i<numOfJob; i++)
{
if (job_list[i].pid != 0)
{
printf("[%d] (%d) %s %s\n", job_list[i].jid, job_list[i].pid, job_list[i].status, job_list[i].title);
}
}
}
int builtin_command(char **argv)
{
int pid = 0;
if (strcmp(argv[0], "jobs")==0)
{
display_jobs(argv);
return 1;
}
if (strcmp(argv[0], "fg")==0)
{
// Change a stopped or running background job to a running in the foreground.
if (strncmp(argv[1], "%", 1)==0) // Job ID
{
int i;
for(i=0; i<numOfJob; i++)
{
int jd = atoi(&argv[1][1]);
if (job_list[i].pid != 0 && job_list[i].jid==jd)
{
// foreground the background job which is already RUNNING
if (strcmp(job_list[i].status, "Running")==0)
{
pid = waitpid(-1, NULL, WUNTRACED);
}
else
{
// foregroung the background job which is STOPPED
strcpy(job_list[i].status, "Running");
kill(job_list[i].pid, SIGCONT);
pid = waitpid(-1, NULL, WUNTRACED);
}
}
}
}
else // Process ID
{
int i;
for(i=0; i<numOfJob; i++)
{
int pd = atoi(argv[1]);
if (job_list[i].pid != 0 && job_list[i].pid==pd)
{
// foreground the background job which is already RUNNING
if (strcmp(job_list[i].status, "Running")==0)
{
pid = waitpid(-1, NULL, WUNTRACED);
}
else
{
// foregroung the background job which is STOPPED
strcpy(job_list[i].status, "Running");
kill(job_list[i].pid, SIGCONT);
pid = waitpid(-1, NULL, WUNTRACED);
}
}
}
}
int i;
for (i=0; i<numOfJob; i++)
{
if (job_list[i].pid !=0 && job_list[i].pid==pid)
{
strcpy(job_list[i].status, "Stopped");
job_list[i].ground = 'b';
}
}
return 1;
}
if (strcmp(argv[0], "bg")==0)
{
if (strncmp(argv[1], "%", 1)==0) // Job ID
{
int i;
for(i=0; i<numOfJob; i++)
{
int input_jid = atoi(&argv[1][1]);
if (job_list[i].jid == input_jid) // argv[1]= "%1",
{
kill(job_list[i].pid, SIGCONT);
strcpy(job_list[i].status, "Running");
}
}
}
else
{
int i;
for(i=0; i<numOfJob; i++)
{
int input_pid = atoi(argv[1]);
if (job_list[i].pid == input_pid)
{
kill(job_list[i].pid, SIGCONT);
strcpy(job_list[i].status, "Running");
return 1;
}
}
}
return 1;
}
if (strcmp(argv[0], "kill")==0)
{
if (strncmp(argv[1], "%", 1)==0) // Job ID
{
int i;
for(i=0; i<numOfJob; i++)
{
int input_jid = atoi(&argv[1][1]);
if (job_list[i].jid == input_jid)
{
if (strcmp(job_list[i].status, "Stopped")==0)
{
kill(job_list[i].pid, SIGCONT);
kill(job_list[i].pid, SIGINT);
}
else
{
kill(job_list[i].pid, SIGINT);
}
job_list[i].pid = 0;
job_list[i].ground = 'n';
strcpy(job_list[i].status, " ");
strcpy(job_list[i].title, " ");
return 1;
}
}
}
else // Process ID
{
int i;
for(i=0; i<numOfJob; i++)
{
int input_pid = atoi(argv[1]);
if (job_list[i].pid == input_pid)
{
if (strcmp(job_list[i].status, "Stopped")==0)
{
kill(job_list[i].pid, SIGCONT);
kill(job_list[i].pid, SIGINT);
}
else
{
kill(job_list[i].pid, SIGINT);
}
job_list[i].pid = 0;
job_list[i].ground = 'n';
strcpy(job_list[i].status, " ");
strcpy(job_list[i].title, " ");;
return 1;
}
}
}
return 1;
}
if (strcmp(argv[0], "quit")==0)
{
int i;
for(i =0; i<numOfJob; i++)
{
if(job_list[i].pid != 0 && job_list[i].ground== 'b')
{
pid_t pid = job_list[i].pid;
kill(pid, SIGKILL);
job_list[i].pid = 0;
job_list[i].ground = 'n';
strcpy(job_list[i].status, " ");
strcpy(job_list[i].title, " ");
}
}
exit(0);
return 1;
}
return 0;
}
void eval(char *cmdline)
{
char cpycmd[80]; // line of command
char *argv[80]; // split cmdline to argv list
int bg;
arg_count = 0;
pid_t pid;
strcpy(cpycmd, cmdline);
bg = parseline(cpycmd, argv);
if (!builtin_command(argv))
{
pid = fork();
if(pid > 0)
{
if (!bg)
{
pid = waitpid(-1, NULL, WUNTRACED);
}
}
else if(pid == 0)// child runs user job
{
IOredirection(argv);
unsigned i = 0;
for (i = 0; i< arg_count; i++)
{
if (!strcmp(argv[i], ">") || !strcmp(argv[i], "<"))
{
argv[i] = NULL;
}
}
if (execvp(argv[0],argv)<0 && execv(argv[0],argv)<0)
{
printf("%s: Command not found.\n", argv[0]);
exit(0);
}
wait(NULL);
}
if (!bg)
{
int status;
add_job(pid, 'f', argv); // add fg job to job_list
if (!stopped)
{
remove_job(pid); // remove the completed fg job
}
else
{
int i;
for (i=0; i<numOfJob; i++)
{
if (job_list[i].pid !=0 && job_list[i].pid==pid)
{
strcpy(job_list[i].status, "Stopped");
job_list[i].ground = 'b';
}
}
}
}
else // if job runs in background
{
add_job(pid, 'b', argv); // add bg job to job_list
if (stopped)
{
int i;
for (i=0; i<numOfJob; i++)
{
if (job_list[i].pid !=0 && job_list[i].pid==pid)
{
strcpy(job_list[i].status, "Stopped");
job_list[i].ground = 'b';
}
}
}
}
}
}
void unix_error(const char *msg)
{
int errnum = errno;
fprintf(stderr, "%s (%d: %s)\n", msg, errnum, strerror(errnum));
exit(EXIT_FAILURE);
}
void int_handler(int sig)
{
// printf("Process %d received INT signal %d\n", getpid(), sig);
}
void tstp_handler(int sig)
{
stopped = true;
}
void child_handler(int sig)
{
int real_pid = getpid();
int pid = waitpid(-1,NULL,WNOHANG);
if (pid>1)
{
int i;
for(i=0; i<numOfJob; i++)
{
if(job_list[i].pid == pid)
{
job_list[i].pid = 0;
job_list[i].ground = 'n';
strcpy(job_list[i].status, " ");
strcpy(job_list[i].title, " ");
}
}
}
}
void term_handler(int sig)
{
// printf("Process %d received TERMINATE signal %d\n", getpid(), sig);
}
void cont_handler(int sig)
{
// printf("Process %d received CONTINUE signal %d\n", getpid(), sig);
}
int add_job(pid_t pid, char ground, char **argv)
{
if(pid < 1)
{
return 0;
}
int i = 0;
if(ground == 'f')
{
if(numOfJob == 0)
{
job_list[0].jid = i+1;
job_list[0].pid = pid;
job_list[0].ground = ground;
strcpy(job_list[0].status,"Running");
int k;
for (k=0; k<arg_count; k++)
{
strcat(job_list[0].title, argv[k]);
strcat(job_list[0].title, " ");
}
numOfJob += 1;
return 1;
}
for(i=0; i<numOfJob+1; i++)
{
if(job_list[i].pid ==0)
{
job_list[i].jid = i+1;
job_list[i].pid = pid;
job_list[i].ground = ground;
strcpy(job_list[i].status,"Running");
int k;
for (k=0; k<arg_count; k++)
{
strcat(job_list[i].title, argv[k]);
strcat(job_list[i].title, " ");
}
numOfJob += 1;
return 1; // make the new job to be background
}
}
}
else
{
if(numOfJob == 0)
{
job_list[0].jid = i+1;
job_list[0].pid = pid;
job_list[0].ground = ground;
strcpy(job_list[0].status,"Running");
int k;
for (k=0; k<arg_count; k++)
{
strcat(job_list[0].title, argv[k]);
strcat(job_list[0].title, " ");
}
numOfJob += 1;
return 1;
}
for(i=0; i<numOfJob+1; i++)
{
if(job_list[i].pid ==0)
{
job_list[i].jid = i+1;
job_list[i].pid = pid;
job_list[i].ground = ground;
strcpy(job_list[i].status,"Running");
int k;
for (k=0; k<arg_count; k++)
{
strcat(job_list[i].title, argv[k]);
strcat(job_list[i].title, " ");
}
numOfJob += 1;
return 1; // make the new job to be background
}
}
}
return 0;
}
int remove_job(pid_t pid)
{
if (pid<0) // non-exist job
{
return 0;
}
int i;
for(i=0; i<numOfJob; i++)
{
if (job_list[i].pid==pid)
{
job_list[i].pid = 0;
job_list[i].ground = 'n';
strcpy(job_list[i].status, " ");
strcpy(job_list[i].title, " ");
return 1;
}
}
}
void create_job_list()
{
int i;
for (i=0; i<5; i++)
{
job_list[i].jid = 0;
job_list[i].pid = 0;
job_list[i].ground = 'n';
strcpy(job_list[i].status, " ");
strcpy(job_list[i].title, " ");
}
}
int main(int argc, char *argv[])
{
char cmdline[80];
create_job_list();
signal(SIGINT, int_handler);
signal(SIGTSTP, tstp_handler);
signal(SIGCHLD, child_handler);
signal(SIGTERM, term_handler);
signal(SIGCONT, cont_handler);
numOfJob = 0;
while(1)
{
stopped = false;
child_Pid = 0;
printf("prompt> ");
fgets(cmdline, 80, stdin);
if (feof(stdin))
{
exit(0);
}
eval(cmdline);
}
exit(0);
}