-
Notifications
You must be signed in to change notification settings - Fork 0
/
splintersh.c
554 lines (501 loc) · 11.5 KB
/
splintersh.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/acct.h>
#include <sys/time.h>
#include <error.h>
#include <errno.h>
#include <glob.h>
#define LINEMAX 4096
#define PROMPT_SIZE 100
#define FALSE 0
#define TRUE 1
typedef int funcPtr(char *);
int USE_ACCOUNTING = FALSE;
int doGlob = 1;
int
exitbuiltin() {
exit(EXIT_SUCCESS);
}
int
globon() {
doGlob = 1;
return 0;
}
int
globoff() {
doGlob = 0;
return 0;
}
typedef struct {
char *cmd;
funcPtr *name;
} dTable;
dTable fdt[] = {
{"_exit", exitbuiltin},
{"_globon", globon},
{"_globoff", globoff}
};
void
dobuiltin(char *cmd) {
for(int i = 0; i < 3; ++i) {
if(strncmp(cmd, fdt[i].cmd, strlen(fdt[i].cmd)) == 0)
(*fdt[i].name)(cmd);
}
}
static void
Error(int status, int errnum, char *msg)
{
char errmsg[LINEMAX];
char *errString;
char *sep = ": ";
errString = strerror(errnum);
// program_invocation_name not portable, requires the _GNU_SOURCE definiton
strcpy(errmsg, program_invocation_name);
strcat(errmsg, sep);
strcat(errmsg, msg);
strcat(errmsg, sep);
strcat(errmsg, errString);
strcat(errmsg, "\n");
write(STDERR_FILENO, errmsg, strlen(errmsg));
exit(status);
}
void
globify(char **args, glob_t *globber)
{
int nargs = 0;
char **ptr;
ptr = args;
while(*ptr)
{
++nargs;
++ptr;
}
if(nargs == 0)
return;
globber->gl_offs = 0;
if(glob(args[0],
GLOB_NOCHECK | GLOB_TILDE | GLOB_NOMAGIC,
NULL, globber) < 0)
{
Error(EXIT_FAILURE, errno, "glob failed");
}
for(int i = 1; i < nargs; ++i)
{
glob(args[i],
GLOB_NOCHECK | GLOB_TILDE | GLOB_NOMAGIC | GLOB_APPEND,
NULL, globber);
}
return;
}
// Malloc for strings with error handling
static void
strMalloc(char **buf, uint size)
{
*buf = malloc(size);
if (buf == NULL)
Error(EXIT_FAILURE, 0, "malloc failed");
}
// malloc for arrays of strings with error handling
static void
strStarMalloc(char ***buf, uint size)
{
*buf = malloc(size);
if (buf == NULL)
Error(EXIT_FAILURE, 0, "malloc failed");
}
void
setResource(char *buf) {
char alt[strlen(buf) + 1];
char *p;
USE_ACCOUNTING = TRUE;
strcpy(alt, buf);
p = alt + strlen("rs ");
while(*p != '\0' && *p == ' ') // chomp spaces
++p;
strcpy(buf, p);
}
void
rsPrint(struct rusage *r) {
int maxLen, len;
char ** p;
char *prompts[] = {"User time", "System time", "Max size",
"Shared text memory", "Unshared memory", "Unshared stack space",
"Page fault (No I/O)", "Page fault", "Times swapped",
"Reads from disk", "Writes to disk", "IPC messages sent",
"IPC messages recieved", "Signals recieved",
"Voluntary context switches", "Involuntary context switches", NULL};
p = prompts;
maxLen = strlen(*p++);
while(*p) {
len = strlen(*p++);
if(len > maxLen)
maxLen = len;
}
maxLen += 2;
p = prompts;
printf("%-*s %ld usec\n", maxLen, *p++, r->ru_utime.tv_usec);
printf("%-*s %ld usec\n", maxLen, *p++, r->ru_utime.tv_usec);
printf("%-*s %ld\n", maxLen, *p++, r->ru_maxrss);
printf("%-*s %ld\n", maxLen, *p++, r->ru_ixrss);
printf("%-*s %ld\n", maxLen, *p++, r->ru_idrss);
printf("%-*s %ld\n", maxLen, *p++, r->ru_isrss);
printf("%-*s %ld\n", maxLen, *p++, r->ru_minflt);
printf("%-*s %ld\n", maxLen, *p++, r->ru_majflt);
printf("%-*s %ld\n", maxLen, *p++, r->ru_nswap);
printf("%-*s %ld\n", maxLen, *p++, r->ru_inblock);
printf("%-*s %ld\n", maxLen, *p++, r->ru_oublock);
printf("%-*s %ld\n", maxLen, *p++, r->ru_msgsnd);
printf("%-*s %ld\n", maxLen, *p++, r->ru_msgrcv);
printf("%-*s %ld\n", maxLen, *p++, r->ru_nsignals);
printf("%-*s %ld\n", maxLen, *p++, r->ru_nvcsw);
printf("%-*s %ld\n", maxLen, *p++, r->ru_nivcsw);
}
// Trims whitespace from both ends of a string
// " ls -l -F " -> "ls -l -F"
void
trim(char **str) {
char *p;
char cpy[strlen(*str) + 1];
int length;
strcpy(cpy, *str);
p = cpy;
while(*p == ' ' && *p != '\0')
++p;
length = strlen(p) - 1;
if(p[length] == ' ') {
while(p[length] == ' ') {
--length;
}
p[++length] = '\0';
}
strcpy(*str, p);
}
// Returns the number of pipes in an entered command
int
countPipes(char *buf) {
int count = 0;
int length = strlen(buf);
for(int i = 0; i < length; ++i) {
if(buf[i] == '|')
++count;
}
return count;
}
// Extract individual commands from a pipeline
// cat| foo | bar will return an array of strings
// "cat", "foo", "bar", NULL
// return array is null terminated
char**
pipeify(char *buf)
{
char cpy[strlen(buf) + 1];
char *token;
char **pipedCmds;
char **p;
int count;
strcpy(cpy, buf);
count = countPipes(buf);
strStarMalloc(&pipedCmds, sizeof(char*) * (count + 2));
pipedCmds[count + 1] = NULL;
p = pipedCmds;
token = strtok(cpy, "|");
while(token) {
strMalloc(p, sizeof(char) * (strlen(token) + 1));
strcpy(*p, token);
trim(p);
++p;
token = strtok(NULL, "|");
}
return pipedCmds;
}
// Returns true if the command begins with a / ./ or ../
// Returns false otherwise
static int
directPath(char *buf)
{
int length = strlen(buf);
if(length == 0)
return 1;
if(buf[0] == '/') {
return 1;
}
else if(buf[0] == '.') {
if(length > 1) {
if(buf[1] == '/')
return 1;
else if(buf[1] == '.')
if(length > 2 && buf[2] == '/')
return 1;
}
}
return 0;
}
// Splits up a buffered command into substrings to isolate optional
// argument flags from the base command
// e.g. "ls -l" becomes ["ls", "-l", NULL]
// Return: Pointer to array of dynamically allocated strings
static char**
parseArgs(char *buf)
{
char *tok; // strtok token
char *delimiter = " ";
int numArgs = 1; // total number of arguments from buffer
char *buf2;
char **list;
// Find number of arguments to size list
strMalloc(&buf2, sizeof(char) * (strlen(buf) + 1));
strcpy(buf2, buf);
strtok(buf2, delimiter);
while(strtok(NULL, delimiter) != NULL)
++numArgs;
free(buf2);
// Size of argument list is the number of args + the NULL at the end
strStarMalloc(&list, sizeof(char*) * (numArgs + 1));
// Extract arguments
tok = strtok(buf, delimiter);
for(int i = 0; i < numArgs; ++i) {
strMalloc(&list[i], strlen(tok) + 1);
strcpy(list[i], tok);
tok = strtok(NULL, delimiter);
}
list[numArgs] = NULL;
return list;
}
void
freeList(char **optList)
{
char **ptr;
ptr = optList;
while(*ptr)
{
free(*ptr);
++ptr;
}
free(optList);
}
// Concatenates p1 with p2 into path, adding a seperator (/) between
static void
createPath(char *path, char *p1, char *p2)
{
strcpy(path, p1);
strcat(path, "/");
strcat(path, p2);
}
void
Exec(char *buf)
{
char * path; // PATH environment variable
char *tok; // path tokens returned from strok
char *cmd; // command to be executed by exec
char *delim = ":"; // delimiter used by strtok for path
char **optList; // arguments for execv
unsigned int length;
glob_t gl;
optList = parseArgs(buf);
if(doGlob)
globify(optList, &gl);
if(directPath(optList[0])) {
execv(optList[0], doGlob ? gl.gl_pathv : optList);
}
else {
length = strlen(optList[0]);
strMalloc(&path, sizeof(char) * (strlen(getenv("PATH")) + 1));
strcpy(path, getenv("PATH"));
tok = strtok(path, delim);
while(tok != NULL) {
// total size = size of buffer command + '/' + size of path token + '\0'
strMalloc(&cmd, sizeof(char) * (length + strlen(tok) + 2));
createPath(cmd, tok, optList[0]);
if(access(cmd, F_OK) == 0)
execv(cmd, doGlob ? gl.gl_pathv : optList);
free(cmd);
tok = strtok(NULL, delim);
}
free(path);
}
freeList(optList);
if(doGlob)
globfree(&gl);
}
static int
Fork()
{
pid_t pid;
if ((pid = fork()) < 0)
Error(EXIT_FAILURE, errno, "fork error");
return(pid);
}
void
Dup2(int ofd, int nfd)
{
if(dup2(ofd, nfd) == -1)
Error(EXIT_FAILURE, errno, "dup2 failed");
}
void
Pipe(int *fds)
{
if(pipe(fds) < 0)
Error(EXIT_FAILURE, errno, "pipe failed");
}
void
Chdir(char *path)
{
trim(&path);
if(chdir(path) < 0)
{
perror(strerror(errno));
}
}
void
pipeExec(char ** cmds, int npipes)
{
// Code derived from pseudocode from Christopher Neylan
// https://stackoverflow.com/questions/8389033/implementation-of-multiple-pipes-in-c
// Exec on pipelined commands
int pid, cmdIdx;
char **cmd = cmds;
int pipefds[npipes * 2];
struct rusage resource;
int status;
for(int i = 0; i < npipes; ++i) {
Pipe(pipefds + i * 2);
}
cmdIdx = 0;
while(*cmd) {
pid = Fork();
if(pid == 0) {
if(cmdIdx != 0) {
// not the first command
Dup2(pipefds[(cmdIdx - 1) * 2], 0);
}
if(cmdIdx != npipes) {
// not the last command
Dup2(pipefds[cmdIdx * 2 + 1], 1);
}
for(int i = 0; i < npipes * 2; ++i) {
close(pipefds[i]);
}
Exec(*cmd);
}
++cmdIdx;
++cmd;
}
for(int i = 0; i < npipes * 2; ++i) {
close(pipefds[i]);
}
for(int i = 0; i < cmdIdx; ++i) {
if(USE_ACCOUNTING) {
if(wait3(&status, 0, &resource) == -1)
Error(EXIT_FAILURE, errno, "wait failed");
}
else {
if(wait(0) == -1)
Error(EXIT_FAILURE, errno, "wait failed");
}
}
if(USE_ACCOUNTING) {
rsPrint(&resource);
USE_ACCOUNTING = FALSE;
}
}
void
makePrompt(char *prompt, char *name)
{
char cwd[LINEMAX];
getcwd(cwd, LINEMAX);
sprintf(prompt, "%s: %s $ ", name, cwd);
}
int
main(int argc, char **argv)
{
char buf[LINEMAX];
char *name;
char **pipes, **ptr, **args;
pid_t pid;
int status;
int n;
int count;
char prompt[PROMPT_SIZE];
struct rusage resource;
glob_t gl;
if(argc > 0)
name = argv[0];
do {
makePrompt(prompt, name);
write(STDOUT_FILENO, prompt, strlen(prompt));
memset(buf, 0, LINEMAX);
if((n = read(STDIN_FILENO, buf, LINEMAX)) == 0) {
printf("use exit to exit shell\n");
continue;
}
buf[strlen(buf) - 1] = '\0'; // chomp '\n'
if(strncmp(buf, "rs ", 3) == 0) {
setResource(buf);
}
if((count = countPipes(buf)) > 0) {
pipes = pipeify(buf);
pipeExec(pipes, count);
ptr = pipes;
while(*ptr)
free(*ptr++);
free(pipes);
continue;
}
if(buf[0] == '_') {
dobuiltin(buf);
continue;
}
if(strcmp(buf, "exit") == 0)
{
break;
}
else if(strncmp(buf, "cd", 2) == 0)
{
args = parseArgs(buf);
if(args[1] == NULL)
{
// just have ["cd", NULL], go to home dir
Chdir(getenv("HOME"));
}
else if(args[2] == NULL)
{
// only have two arguments, change dir to second arg
if(doGlob)
globify(args, &gl);
Chdir(gl.gl_pathv[1]);
if(doGlob)
globfree(&gl);
}
else
{
Error(0, errno, "too many arguments to cd");
}
freeList(args);
continue;
}
// Run other commands
pid = Fork();
if (pid == 0) { // child
Exec(buf);
Error(EXIT_FAILURE, errno, "exec failure");
}
// parent
if(USE_ACCOUNTING) {
if ((pid = wait4(pid, &status, 0, &resource)) < 0)
Error(EXIT_FAILURE, errno, "waitpid error");
USE_ACCOUNTING = FALSE;
rsPrint(&resource);
}
else {
if ((pid = waitpid(pid, &status, 0)) < 0)
Error(EXIT_FAILURE, errno, "waitpid error");
}
} while(1);
return 0;
}