-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
581 lines (512 loc) · 15.6 KB
/
main.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
/******************************************************************************
*
* File Name........: main.c
*
* Description......: Simple driver program for ush's parser
*
* Author...........: Vincent W. Freeh
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "parse.h"
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#define NUM_BUILTINS 13
#define MAXLEN 1024
#define DEFAULT_PRIO 4
#define err(x) fprintf(stderr, "%s\n", x);
extern char **environ;
char shell_built_ins[NUM_BUILTINS][20] = {"cd", "pwd", "echo", "setenv", "logout", "end", "jobs",
"where", "nice", "kill", "unsetenv", "fg", "bg"};
int check_if_builtin(char *cmd) {
int i;
for (i =0; i < NUM_BUILTINS; i++) {
if (strcmp(cmd, shell_built_ins[i]) == 0) {
return 1; // True
}
}
return 0; //False
}
char * cmd_path(char * cname) {
struct stat sti;
char env_path[MAXLEN];
char cmd_str[MAXLEN];
char * c = getenv("PATH");
char * token;
if (cname == NULL) return NULL;
// This is an out parameter needs to be on heap
char * buffer = calloc(MAXLEN, sizeof(char));
int cmd_found = 0;
strncpy(env_path, c, MAXLEN);
token = strtok(env_path, ":");
while (token) {
strncpy(cmd_str, token, MAXLEN);
strncat(cmd_str, "/", MAXLEN - strlen(cmd_str));
strncat(cmd_str, cname, MAXLEN - strlen(cmd_str));
if (stat(cmd_str, &sti) == 0) {
if (strlen(buffer) == 0) {
snprintf(buffer, MAXLEN, "%s\n", cmd_str);
} else {
strncat(buffer, cmd_str, MAXLEN - strlen(buffer));
strncat(buffer, "\n", MAXLEN - strlen(buffer));
}
cmd_found = 1;
}
token = strtok(NULL, ":");
}
if (cmd_found) {
return buffer;
} else {
free(buffer);
return NULL;
}
}
void setup_pipes_io(Cmd c, int *lp, int *rp) {
int fd = -1;
if (c) {
//printf("%s%s ", c->exec == Tamp ? "BG " : "", c->args[0]);
if (c->in != Tnil) {
switch (c->in) {
case Tin: {
//printf("<(%s) ", c->infile);
fd = open(c->infile, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->infile);
exit(1);
} else {
dup2(fd, 0);
close(fd);
}
}
break;
case Tpipe:
case TpipeErr: {
if (lp == NULL) {
err("Something went wrong in inp pipes");
exit(1);
}
//printf("cmd %s\n", c->args[0]);
//fflush(stdout);
//printf("Desc1 is %d Desc2 is %d\n",lp[0], lp[1]);
dup2(lp[0], 0);
close(lp[1]);
}
break;
default: err("should not come here");
}
}
if (c->out != Tnil) {
switch (c->out) {
case Tout: {
fd = open(c->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0660);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->outfile);
exit(1);
}
dup2(fd, 1);
close(fd);
//printf(">(%s) ", c->outfile);
}
break;
case Tapp: {
fd = open(c->outfile, O_WRONLY | O_APPEND | O_CREAT, 0660);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->outfile);
exit(1);
}
dup2(fd, 1);
close(fd);
//printf(">>(%s) ", c->outfile);
}
break;
case ToutErr: {
fd = open(c->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0660);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->outfile);
exit(1);
}
dup2(fd, 1);
dup2(fd, 2);
close(fd);
//printf(">&(%s) ", c->outfile);
}
break;
case TappErr: {
fd = open(c->outfile, O_WRONLY | O_APPEND | O_CREAT, 0660);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->outfile);
exit(1);
}
dup2(fd, 1);
dup2(fd, 2);
close(fd);
//printf(">>&(%s) ", c->outfile);
}
break;
case Tpipe: {
if (rp == NULL) {
err("Something went wrong in output pipes");
exit(1);
}
//printf("| ");
//printf("Desc1 is %d Desc2 is %d\n",rp[0], rp[1]);
dup2(rp[1], 1);
close(rp[0]);
}
break;
case TpipeErr: {
if (rp == NULL) {
err("Something went wrong in output pipes");
exit(1);
}
dup2(rp[1], 2);
dup2(rp[1], 1);
close(rp[0]);
//printf("|& ");
}
break;
default:
err("Shouldn't get here");
exit(-1);
}
}
}
}
void run_builtin(Cmd c) {
if ((strcmp(c->args[0], "end") == 0) ||
(strcmp(c->args[0], "logout") == 0)) {
exit(0);
}
if (strcmp(c->args[0], "pwd") == 0 ) {
char pwd[2000];
char *c = getcwd(pwd, 2000);
if (c) {printf("%s\n", pwd);}
else {err("unable to get pwd");}
return;
}
if (strcmp(c->args[0], "cd") == 0) {
if (c->args[1] == NULL) {
char * home_dir = getenv("HOME");
if (home_dir == NULL) {err("cd: failed");}
else if (chdir(home_dir) == -1) {err("cd: failed");}
} else {
if (chdir(c->args[1]) == -1) {err("cd: failed");}
}
return;
}
if (strcmp(c->args[0], "echo") == 0) {
if (c->nargs > 1 ) {
int i;
printf("%s", c->args[1]);
for (i = 2; c->args[i]!= NULL; i++)
printf(" %s",c->args[i]);
printf("\n");
}
return;
}
if(strcmp(c->args[0], "where") == 0) {
if (c->args[1] == NULL) {
err("where: too few arguments");
return;
}
if (check_if_builtin(c->args[1])) {
printf("%s: shell built-in command.\n", c->args[1]);
return;
}
char * path = cmd_path(c->args[1]);
if (path) {
printf("%s", path);
free(path);
} else {
fprintf(stderr, "%s: command not found\n", c->args[1]);
}
return;
}
if(strcmp(c->args[0],"setenv") == 0) {
if (c->args[1] == NULL) {
int i;
char *c = *environ;
for (i = 1; c; i++) {
printf("%s\n", c);
c = *(environ+i);
}
} else {
if (setenv(c->args[1], c->args[2], 1) != 0) {
err("setenv: error");
}
}
return;
}
if(strcmp(c->args[0], "unsetenv") == 0) {
if (c->args[1] != NULL) {
if (unsetenv(c->args[1]) != 0) { err("unsetenv: error ");}
} else {
err("unsetenv: too few arguments");
}
return;
}
if(strcmp(c->args[0], "nice") == 0) {
if (c->nargs == 1) {
int rc = setpriority(PRIO_PROCESS, getpid(), DEFAULT_PRIO);
if (rc < 0) { err("nice: setpriority failed");}
return;
}
int prio = DEFAULT_PRIO;
int cmd_without_num = 0;
if (c->args[1][0] == '-' || c->args[1][0] == '+') { // +(-)prio given
char * num = c->args[1] + 1;
prio = atoi(num);
if (c->args[1][0] == '-') prio = prio * -1;
} else if (c->args[1][0] >= '0' && c->args[1][0] <= '9') { // prio given
prio = atoi(c->args[1]);
} else { //command specified without number
cmd_without_num = 1;
}
if (c->args[2] == NULL && cmd_without_num == 0) {
int rc = setpriority(PRIO_PROCESS, getpid(), prio);
if (rc < 0) { err("nice: setpriority failed");}
return;
}
char * cmd_to_execute = c->args[2];
if (cmd_without_num == 1) {
cmd_to_execute = c->args[1];
}
int pid = fork();
if (pid < 0) {
err("nice: fork failed");
return;
} else if (pid == 0) {
setpriority(PRIO_PROCESS, 0, prio);
if (cmd_without_num == 1) {
execvp(cmd_to_execute, c->args + 1);
} else {
execvp(cmd_to_execute, c->args + 2);
}
if (errno == ENOENT) {
fprintf(stderr, "%s: command not found\n", cmd_to_execute);
} else {
fprintf(stderr, "%s: permission denied\n", cmd_to_execute);
}
exit(1);
} else {
waitpid(pid, NULL, 0);
return;
}
}
}
static void prCmd(Cmd c, int * left, int * right)
{
pid_t pid;
if (check_if_builtin(c->args[0])) {
if (c->next == NULL) {
// reward:effort is very low for this complex trickery.
// Just calling run_builtin may do minus few cases.
int fd;
int saved_stdout;
int saved_stderr;
int saved_stdin;
if (c->in == Tin) {
int fdin = open(c->infile, O_RDONLY);
if (fdin == -1) {
if (errno == ENOENT) {
fprintf(stderr, "%s: File not found\n", c->infile);
} else {
fprintf(stderr, "%s: Permission denied\n", c->infile);
}
return;
} else {
saved_stdin = dup(0);
dup2(fdin, 0);
close(fdin);
}
} else if (c->in == Tpipe || c->in == TpipeErr) {
if (left == NULL) {
err("Something went wrong in pipes");
return;
}
saved_stdin = dup(0);
dup2(left[0], 0);
close(left[1]);
close(left[0]);
}
// Last command in pipe is a builtin. Execute directly (csh).
// Take care only of redirection. Last command has no right pipe.
if (c->out == Tout || c->out == ToutErr) {
fd = open(c->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0660);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->outfile);
return;
}
} else if (c->out == Tapp || c->out == TappErr) {
fd = open(c->outfile, O_WRONLY | O_APPEND | O_CREAT, 0660);
if (fd == -1) {
fprintf(stderr, "%s: Permission denied\n", c->outfile);
return;
}
}
if (c->out == Tout || c->out == Tapp) {
saved_stdout = dup(1);
dup2(fd, 1);
close(fd);
} else if (c->out == ToutErr || c->out == TappErr) {
saved_stdout = dup(1);
saved_stderr = dup(2);
dup2(fd,1);
dup2(fd,2);
close(fd);
}
run_builtin(c);
if (c->out == Tout || c->out == Tapp) {
dup2(saved_stdout, 1);
close(saved_stdout);
} else if (c->out == ToutErr || c->out == TappErr) {
dup2(saved_stdout,1);
dup2(saved_stderr,2);
close(saved_stdout);
close(saved_stderr);
}
if(c->in == Tin || c->in == Tpipe || c->in == TpipeErr) {
dup2(saved_stdin, 0);
close(saved_stdin);
}
} else {
// builtin has to be run in pipe
pid = fork();
if (pid < 0) {
err("Fork Failed");
} else if (pid == 0) {
setup_pipes_io(c, left, right);
run_builtin(c);
exit(0);
}
}
} else {
pid = fork();
if (pid < 0) {
err("fork: failed");
} else if (pid == 0) {
/*child */
setup_pipes_io(c, left, right);
execvp(c->args[0], c->args);
if (errno == ENOENT) {
fprintf(stderr, "%s: command not found\n", c->args[0]);
} else {
fprintf(stderr, "%s: permission denied\n", c->args[0]);
}
exit(1);
}
}
}
static void prPipe(Pipe p)
{
int i;
Cmd c;
Cmd temp;
int * fd;
if ( p == NULL )
return;
int pipe_count = -1;
for (temp = p->head; temp != NULL; temp = temp->next) {
pipe_count = pipe_count + 1;
}
fd = calloc(pipe_count*2, sizeof(int));
i = 0;
for (i = 0; i <= (pipe_count - 1)*2; i= i + 2) {
pipe(fd + i);
}
//printf("Begin pipe%s\n", p->type == Pout ? "" : " Error");
i = 0;
for ( c = p->head; c != NULL; c = c->next, i++) {
int * left = NULL;
int * right = NULL;
if (i != 0) {
left = fd + 2*(i -1);
//printf("LEFT %d,%d\n", 2*(i-1), *left);
}
if (i != pipe_count) {
right = fd + 2*i;
//printf(" RIGHT %d,%d\n", 2*i, *right);
}
prCmd(c, left, right);
if (i > 0) {
close(fd[2*i - 2]);
close(fd[2*i - 1]);
}
//printf("Cmd #%d:%s \n", i+1, c->args[0]);
}
int pid,status;
while ((pid = wait(&status)) != -1); /* wait for all children */
//fprintf(stderr, "process %d exits with %d\n", pid, WEXITSTATUS(status));
free(fd);
prPipe(p->next);
}
/*
*Below two functions are inspired from
*http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code
*/
void sig_handler(int signalno)
{
if (signalno == SIGTERM)
printf("received SIGTERM\n");
}
void setup_signals()
{
if (signal(SIGTERM, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGTERM\n");
if (signal(SIGQUIT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGQUIT\n");
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
}
void process_ushrc() {
char * home = getenv("HOME");
char rcpath[512];
int fd;
int saved_stdin;
strncpy(rcpath, home, 512);
strncat(rcpath, "/.ushrc", 512);
fd = open(rcpath, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
err(".ushrc: not found");
} else {
err(".ushrc: Permission denied.")
}
} else {
saved_stdin = dup(0);
dup2(fd, 0);
close(fd);
Pipe p1 = parse();
prPipe(p1);
freePipe(p1);
dup2(saved_stdin, 0);
close(saved_stdin);
}
}
int main(int argc, char *argv[])
{
Pipe p;
char host[50];
setup_signals();
process_ushrc();
while ( 1 ) {
if (gethostname(host,50) != 0) {
strncpy(host,"armadillo",50);
}
printf("%s%% ", host);
//weird behavior without below line
fflush(stdout);
p = parse();
prPipe(p);
freePipe(p);
}
}
/*........................ end of main.c ....................................*/