-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
231 lines (181 loc) · 4.79 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define BUFSIZE 1024
#define DELIM " \t\r\n\a"
/*
* function tokenize(char *line)
* tokenizes the string with delimeter
*/
char **tokenize(char *line) {
int n = 0; //number of strings
char *line_cpy = (char*)malloc(strlen(line) * sizeof(char)+1);
strcpy(line_cpy, line);
line_cpy = strtok(line_cpy, DELIM);
/*this loop gets the number of words*/
while(line_cpy != NULL) {
line_cpy = strtok(NULL, DELIM);
n++;
}
//free(line_cpy);
int ptr = 0;
char **p = (char**)malloc(n+1 * sizeof(char*)+1);
/*this loop tokenize the string*/
line = strtok(line, DELIM);
while(line != NULL) {
p[ptr] = line;
p[ptr][strlen(line)] = '\0'; //mark the end of each string
line = strtok(NULL, DELIM);
ptr++;
}
p[ptr] = '\0'; //mark the end of the array
free(line_cpy);
return p;
}
/*
* function *pwd(char *cmd)
* is executed if user inputs 'pwd',
* prints the current working directory
*/
char *pwd(char *cmd) {
long size;
char *buf;
char *ptr;
size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL)
ptr = getcwd(buf, (size_t)size);
free(buf);
return ptr;
}
/*
* function *cd(char *cmd, char *path)
* is executed if user inputs 'cd',
* then will generate a path based on 'path'
*/
char *cd(char *cmd, char *path) {
long size;
char *buf;
char *ptr;
size = pathconf(".", _PC_PATH_MAX);
buf = (char *)malloc((size_t)size);
if(path == NULL) {
chdir(getenv("HOME"));
ptr = getcwd(buf, (size_t)size);
printf("cwd changed to %s\n", ptr);
return ptr;
} else if(chdir(path) == -1) {
printf("no such directory: %s\n", path);
return path;
} else if(buf != NULL) {
ptr = getcwd(buf, (size_t)size);
printf("cwd changed to %s\n", ptr);
return ptr;
}
free(buf);
return 0;
}
/*
* function *isBuiltIn(char *cmd, char *path)
* checks if command equals any of the builtins
*/
char *isBuiltIn(char *cmd, char *path) {
if(strncmp(cmd, "cd", strlen(cmd)) == 0)
return cd(cmd, path);
else if(strncmp(cmd, "pwd", strlen(cmd)) == 0) {
printf("%s ", pwd(cmd));
return pwd(cmd);
}
else if(strncmp(cmd, "exit", strlen(cmd)) == 0)
{printf("Aborting...\n"); exit(1);}
return 0;
}
/*
* function make_baby(char * argv[])
* fires the fork() system call, creating
* another process from the parent process
*/
int make_baby(char **args, char *path){
pid_t pid;
int status;
char *route;
pid = fork();
if(pid < 0)
perror("fork error");
//child process
//call exec function, execvp - path, vector
else if(pid == 0) {
//if(strncmp(args[0], "calc", strlen(args[0])) == 0)
if(strcmp(args[0], "calculator") == 0) {
route = (char*)malloc((strlen(path)+strlen("/calculator")) * sizeof(char)+1);
strcpy(route, path);
strcat(route, "/calculator");
execvp(route, args);
}
//else if(strncmp(args[0], "listf", strlen(args[0]) == 0))
else if(strcmp(args[0], "listf") == 0) {
route = (char*)malloc((strlen(path)+strlen("/listf")) * sizeof(char)+1);
strcpy(route, path);
strcat(route, "/listf");
execvp(route, args);
}
else if(execvp(args[0], args) == -1) {
perror("error");
}
free(route);
exit(EXIT_FAILURE);
}
//parent process
//ensures child has not terminated & checks abnormal termination
else {
do {
waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int main(int argc, char *argv[]) {
char **args = { 0 };
char line[BUFSIZE];
long size;
char *buf;
char *ptr;
size = pathconf(".", _PC_PATH_MAX);
buf = (char *)malloc((size_t)size);
ptr = getcwd(buf, (size_t)size);
char *pathToCalc = (char*)malloc((size_t)size);
char *pathToListf = (char*)malloc((size_t)size);
strncpy(pathToCalc, ptr, size);
strncpy(pathToListf, pathToCalc, size);
printf("LISTF%s\n", pathToListf);
while(1) {
printf("$> ");
if(fgets(line, BUFSIZE, stdin) == NULL) exit(1);
line[strlen(line)-1] = '\0'; //replace newline with null terminator
args = tokenize(line);
if(args[0] == '\0') continue;
if(isBuiltIn(args[0], args[1]))
continue;
/*special case for calculator*/
else if(strncmp(args[0], "calculator", strlen(args[0])) == 0) {
//else if(strcmp(args[0], "calc") == 0)
make_baby(args, pathToCalc);
continue;
}
/*special case for listf*/
else if(strncmp(args[0], "listf", strlen(args[0])) == 0) {
//else if(strcmp(args[0], "listf") == 0)
make_baby(args, pathToListf);
continue;
}
else
make_baby(args, '\0');
memset(line, 0, BUFSIZE);
free(args);
free(buf);
}
free(pathToCalc);
free(pathToListf);
return EXIT_SUCCESS;
}