-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw_run.c
85 lines (73 loc) · 1.79 KB
/
w_run.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
#include "w_run.h"
int
run(FILE *ofp, char ** const tokens, int nTokens, int verbosity)
{
char*** listTokens = malloc(sizeof(char**));
listTokens[0] = tokens;
int numLists = 1;
int numRedir = 0;
int statLoc = 0;
int ret;
// print the tokens
printCmd(ofp, tokens);
if (!strcmp(tokens[0], "cd")) {
free(listTokens);
ret = cd(tokens[1]);
} else if (!strcmp(tokens[0], "exit")) {
free(listTokens);
exitProgram(tokens, nTokens);
} else {
// populate the array of strings seprated by "|" characters
for (int i = 1; i < nTokens; i++) {
if (tokens[i] && !strcmp(tokens[i], "|")) {
listTokens = realloc(listTokens, sizeof(char**) * ++numLists);
listTokens[numLists-1] = &tokens[i+1];
tokens[i] = NULL;
}
if (tokens[i] && (!strcmp(tokens[i], "<") || !strcmp(tokens[i], ">"))) {
numRedir++;
}
}
if (numRedir) {
free(listTokens);
listTokens = NULL;
ret = simpleRedirect(ofp, tokens);
} else if (numLists > 1) {
ret = ipc(ofp, listTokens, numLists);
} else {
ret = runCmd(ofp, tokens, &statLoc);
/**
* FOLLOWING CODE SEGMENT IS COPIED FORM THE ORIGINAL vssh.c
*
* TODO: cite this code properly
*/
if(WIFEXITED(statLoc)) {
fprintf(ofp, "Child (%d) exited -- ", ret);
if (statLoc == 0) {
fprintf(ofp, "success");
} else {
fprintf(ofp, "failure");
}
fprintf(ofp, "(%d)\n", statLoc);
} else {
fprintf(ofp, "Child (%d) did not exit (crashed?)\n", ret);
}
}
if (listTokens) {
free(listTokens);
}
}
return ret;
}
int
printCmd(FILE *ofp, char** tokens) {
char** globTokens = tokenGlob(tokens);
fprintf(ofp, "+");
for (int i = 0; globTokens[i] != NULL; i++) {
fprintf(ofp, " \"%s\"", globTokens[i]);
free(globTokens[i]);
}
free(globTokens);
fprintf(ofp, "\n");
return 0;
}