-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathush.c
647 lines (557 loc) · 17.2 KB
/
ush.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
Author: Siddharth Sharma, NCSU
2016
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "parse.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
int pipefd[2];
int oldpipefd[2];
int status, interactive, shell_terminal, forks;
int fdin, fdout, prev_in, prev_out, prev_err;
struct sigaction sa;
char home_config_path[1000];
int jobs[1000];
extern char** environ;
pid_t shell_pgid,pipe_group;
typedef struct {
char *cmd;
int (*handler)(int, char **);
} inbuilt_cmd;
int handle_unsetenv(int, char **);
int handle_setenv(int, char **);
int handle_getenv(int, char **);
int handle_exit(int, char **);
int handle_cd(int, char **);
int handle_where(int, char **);
int handle_nice(int, char **);
int handle_pwd(int, char **);
int handle_echo(int, char **);
int handle_bg(int, char **);
int handle_fg(int, char **);
int find_config();
int count_lines(FILE*);
inbuilt_cmd inbuilt_cmds[] = {
{"getenv", handle_getenv},
{"setenv", handle_setenv},
{"unsetenv", handle_unsetenv},
{"nice", handle_nice},
{"fg", handle_fg},
{"bg", handle_bg},
{"kill", NULL},
{"jobs", NULL},
{"echo", handle_echo},
{"pwd", handle_pwd},
{"cd", handle_cd},
{"where", handle_where},
{"end", handle_exit},
{"exit", handle_exit},
{"logout", handle_exit}
};
int handle_bg(int argc, char ** args)
{
printf("bg: No current job.\n");
return 0;
}
int handle_fg(int argc, char ** args)
{
printf("fg: No current job.\n");
return 0;
}
int handle_echo(int argc, char ** args)
{
int i=1;
while ( i < argc ) {
printf("%s", args[i]);
if ( i==argc-1 ) {
printf("\n");
}
else {
printf(" ");
}
i+=1;
}
return 0;
}
int handle_pwd(int argc, char ** args)
{
char *result = realpath(".", NULL);
if ( result != NULL ) {
printf("%s\n", result);
free(result);
return 0;
}
return -1;
}
int handle_exit(int argc, char ** args)
{
exit(0);
}
int handle_nice(int argc, char ** args)
{
int status,num;
pid_t child;
if ( argc < 3 ) {
if ( argc < 2 ) {
status = setpriority(PRIO_PROCESS, 0, 4);
}
else {
num = (int) strtol(args[1], (char **)NULL, 10);
//fprintf(stderr,"priority num: %d\n", num);
status = setpriority(PRIO_PROCESS, 0, num);
}
}
else {
num = (int) strtol(args[1], (char **)NULL, 10);
child = fork();
switch( child ) {
case 0:
//input is "nice <number> <cmd>"
//child execute "nice -n <number> <cmd>"
execvp(args[2], args+2);
case -1:
//error
perror("nice-fork");
return -1;
default:
//parent
status = setpriority(PRIO_PROCESS, child, num);
wait(NULL);
if ( status>0 ){
return 0;
}
return -1;
}
}
if ( status < 0 ) {
perror("nice");
return -1;
}
return 0;
}
int is_file(const char* path) {
struct stat buf;
stat(path, &buf);
return S_ISREG(buf.st_mode);
}
int handle_where(int argc, char **args)
{
if( argc < 2 ){
return -1;
}
int i;
for(i = 0; i < sizeof(inbuilt_cmds)/sizeof(*inbuilt_cmds); i++) {
if( strcmp(args[1],inbuilt_cmds[i].cmd)==0 ) {
printf("%s: shell built-in command.\n", args[1]);
}
}
char *path = getenv("PATH");
char *item = NULL;
int found = 0;
if (!path) {
return -1;
}
path = strdup(path);
char real_path[4096]; // or PATH_MAX or something smarter
for (item = strtok(path, ":"); item; item = strtok(NULL, ":")){
sprintf(real_path, "%s/%s", item, args[1]);
if ( is_file(real_path) && !(
access(real_path, F_OK)
|| access(real_path, X_OK))) // check if the file exists and is executable
{
printf("%s\n",real_path);
}
}
free(path);
return 0;
}
int handle_cd(int argc, char **args)
{
int status;
if(argc<2){
args[1] = getenv("HOME");
}
status = chdir(args[1]);
if ( status < 0 ) {
perror("cd");
return -1;
}
return 0;
}
int handle_setenv(int argc, char **args)
{
if(argc<2){
int i = 0;
while(environ[i]) {
printf("%s\n", environ[i++]);
}
return 0;
}
else if(argc<3){
args[2] = "NULL";
}
int status;
status = setenv(args[1],args[2],1);
if ( status < 0 ) {
perror("setenv");
return -1;
}
return 0;
}
int handle_unsetenv(int argc, char **args)
{
if(argc<2){
return -1;
}
int status;
//todo handle error when args doesn't have 1
status = unsetenv(args[1]);
if ( status < 0 ) {
perror("unsetenv");
return -1;
}
return 0;
}
int handle_getenv(int argc, char **args)
{
if(argc<2){
return -1;
}
char *var;
var = getenv(args[1]);
if ( var == NULL ) {
//fprintf(stderr,"cannot find %s\n", args[1]);
return -1;
}
printf("%s\n",var);
return 0;
}
static void execCmd(Cmd c, int first)
{
int i, foreground=1;
pid_t childpid;
if ( c->exec == Tamp ) {
foreground = 0;
}
if ( c ) {
if ( c->next == NULL ) {
for(i = 0; i < sizeof(inbuilt_cmds)/sizeof(*inbuilt_cmds); i++) {
if( strcmp(c->args[0],inbuilt_cmds[i].cmd)==0 ) {
if( inbuilt_cmds[i].handler != NULL ) {
//deal with output and error redirect
if ( c->out == Tout || c->out == Tapp || c->out == ToutErr || c->out == TappErr ) {
if ( c->out == Tapp || c->out == TappErr ) {
fdout = open(c->outfile, O_WRONLY|O_CREAT|O_APPEND, 0666);
}
else {
fdout = open(c->outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
}
if ( fdout < 0 ) {
perror("open error");
exit(0);
}
//save stdout
prev_out = dup(STDOUT_FILENO);
fcntl(prev_out, FD_CLOEXEC);
dup2(fdout, STDOUT_FILENO);
//check for stderr
if ( c->out == ToutErr || c->out == TappErr ) {
prev_err = dup(STDERR_FILENO);
fcntl(prev_err, FD_CLOEXEC);
dup2(fdout, STDERR_FILENO);
}
close(fdout);
}
//deal with input redirect
if ( c->in == Tin ) {
fdin = open(c->infile,O_RDONLY);
if ( fdin < 0 ) {
perror("open error");
exit(0);
}
//save stdout
prev_in = dup(STDIN_FILENO);
fcntl(prev_in, FD_CLOEXEC);
dup2(fdin, STDIN_FILENO);
close(fdin);
}
//execute command
inbuilt_cmds[i].handler(c->nargs, c->args);
//restore stdout and stderr
if ( c->out == Tout || c->out == Tapp || c->out == ToutErr || c->out == TappErr) {
dup2(prev_out, STDOUT_FILENO);
close(prev_out);
//check for stderr
if( c->out == ToutErr || c->out == TappErr ) {
dup2(prev_err, STDERR_FILENO);
close(prev_err);
}
}
//restore stdin
if ( c->in == Tin ) {
dup2(prev_in, STDIN_FILENO);
close(prev_in);
}
}
for ( i = 0; i < forks;i++ ) {
wait(NULL);
}
if (interactive == 1) {
tcsetpgrp (shell_terminal, shell_pgid);
}
return;
}
}
}
//handle pipe
if ( c->out == Tpipe || c->out == TpipeErr ){
pipe(pipefd);
}
forks+=1;
childpid = fork();
switch ( childpid ) {
case 0:
if ( interactive == 1 ) {
//setting process groups
pid_t pgid;
childpid = getpid ();
if (first == 1){
pgid = childpid;
}
else {
pgid = pipe_group;
}
setpgid (childpid, pgid);
//if ending with a & block output and input
if ( foreground == 1 ){
tcsetpgrp (shell_terminal, pgid);
}
else {
if (c->in == 12) {
c->in = Tin;
c->infile = "/dev/null";
}
if (c->out == 12) {
c->out = Tout;
c->outfile = "/dev/null";
}
}
// set handler to default behaviour
sa.sa_handler = SIG_DFL;
status = sigaction(SIGINT, &sa, NULL);
status = sigaction(SIGQUIT, &sa, NULL);
status = sigaction(SIGTERM, &sa, NULL);
status = sigaction(SIGTTIN, &sa, NULL);
status = sigaction(SIGTTOU, &sa, NULL);
status = sigaction(SIGCHLD, &sa, NULL);
status = sigaction(SIGTSTP, &sa, NULL);
}
//deal with input redirect
if ( c->in == Tin ) {
fdin = open(c->infile,O_RDONLY);
if ( fdin < 0 ) {
perror("open");
exit(0);
}
dup2(fdin, STDIN_FILENO);
close(fdin);
}
//deal with output and error redirect
if ( c->out == Tout || c->out == Tapp || c->out == ToutErr || c->out == TappErr ) {
if ( c->out == Tapp || c->out == TappErr ) {
fdout = open(c->outfile, O_WRONLY|O_CREAT|O_APPEND, 0666);
}
else {
fdout = open(c->outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
}
if ( fdout < 0 ) {
perror("open");
exit(0);
}
dup2(fdout, STDOUT_FILENO);
if ( c->out == ToutErr || c->out == TappErr ) {
dup2(fdout, STDERR_FILENO);
}
close(fdout);
}
// deal with pipes and pipeErr redirects
if ( c->in == Tpipe || c->in == TpipeErr ) {
dup2(oldpipefd[0], STDIN_FILENO);
close(oldpipefd[0]);
}
if ( c->out == Tpipe || c->out == TpipeErr ) {
dup2(pipefd[1], STDOUT_FILENO);
if ( c->out == TpipeErr ) {
dup2(pipefd[1], STDERR_FILENO);
}
close(pipefd[1]);
}
if ( c->in == Tpipe || c->in == TpipeErr ) {
close(oldpipefd[1]);
}
if ( c->out == Tpipe || c->out == TpipeErr ) {
close(pipefd[0]);
}
// execute builtin command
for(i = 0; i < sizeof(inbuilt_cmds)/sizeof(*inbuilt_cmds); i++) {
if( strcmp(c->args[0],inbuilt_cmds[i].cmd)==0 ) {
if( inbuilt_cmds[i].handler != NULL ) {
inbuilt_cmds[i].handler(c->nargs, c->args);
exit(0);
}
}
}
//exec
status = execvp(c->args[0], c->args);
// error is executable/command not found
if ( errno == 2 )
fprintf(stderr, "command not found\n");
// error is permission denied
else if ( errno == 13 )
fprintf(stderr, "permission denied\n");
else
fprintf(stderr, "%d: %s\n", errno,strerror(errno));
exit(EXIT_FAILURE);
case -1:
fprintf(stderr, "Fork error\n");
exit(EXIT_FAILURE);
default:
if ( interactive == 1 ) {
if (first == 1){
pipe_group = childpid;
}
else {
if ( getpgid(childpid) != pipe_group ) {
setpgid(childpid, pipe_group);
}
}
}
if (c->next==NULL){
for ( i=0; i<forks;i++ ) {
wait(NULL);
}
tcsetpgrp (shell_terminal, shell_pgid);
}
// deal with pipes and pipeerr
if ( c->in == Tpipe || c->in == TpipeErr) {
close(oldpipefd[0]);
}
if ( c->out == Tpipe || c->out == TpipeErr) {
oldpipefd[0] = pipefd[0];
oldpipefd[1] = pipefd[1];
close(pipefd[1]);
}
}
}
}
static void execPipe(Pipe p)
{
int i = 0;
Cmd c;
if ( p == NULL )
return;
forks = 0;
for ( c = p->head; c != NULL; c = c->next ) {
if ( c == p->head )
execCmd(c, 1);
else
execCmd(c, 0);
}
execPipe(p->next);
}
int main(int argc, char *argv[])
{
Pipe p;
char host[1024];
//see if interaactive or input from file
shell_terminal = STDIN_FILENO;
interactive = isatty(STDIN_FILENO);
//set buffers off
setbuf(stdin, NULL);
setbuf(stdout, NULL);
setbuf(stderr, NULL);
//handle .ushrc
if ( find_config() == 0 ) {
int lines, config;
//open file and read lines
FILE* conf = fopen(home_config_path,"r");
lines = count_lines(conf);
fclose(conf);
//redirect file to stdin
config = open(home_config_path,O_RDONLY);
prev_in = dup(STDIN_FILENO);
fcntl(prev_in, FD_CLOEXEC);
dup2(config, STDIN_FILENO);
close(config);
//run execute loop
while( lines > 0 ) {
p = parse();
execPipe(p);
freePipe(p);
lines-=1;
}
//restore stdin
dup2(prev_in, STDIN_FILENO);
close(prev_in);
}
//normal shell
if ( interactive ==1 ) {
//check if ush is not in background
while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
kill (- shell_pgid, SIGTTIN);
//handle signals
sa.sa_handler = SIG_IGN;
status = sigaction(SIGINT, &sa, NULL);
status = sigaction(SIGQUIT, &sa, NULL);
status = sigaction(SIGTERM, &sa, NULL);
status = sigaction(SIGTSTP, &sa, NULL);
status = sigaction(SIGTTIN, &sa, NULL);
status = sigaction(SIGTTOU, &sa, NULL);
status = sigaction(SIGCHLD, &sa, NULL);
//put shell into its own process group
shell_pgid = getpid ();
setpgid (shell_pgid, shell_pgid);
/* Grab control of the terminal. */
tcsetpgrp (shell_terminal, shell_pgid);
//get hostname
gethostname(host, 1023);
}
while ( 1 ) {
if ( interactive == 1 ) {
fprintf(stdout,"%s%% ", host);
}
p = parse();
execPipe(p);
freePipe(p);
}
}
int find_config(){
int status;
strcpy(home_config_path, getenv("HOME"));
strcat(home_config_path, "/");
strcat(home_config_path, ".ushrc");
return access(home_config_path, F_OK );
//returns 0 if success -1 if failure
}
int count_lines(FILE* f) {
int lines = 0;
int ch = 0;
while(!feof(f)){
ch = fgetc(f);
if(ch == '\n') {
lines++;
}
}
return lines;
}
/*........................ end of main.c ....................................*/