forked from lsubaldo/Linux-Bash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
436 lines (357 loc) · 8.82 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#include <signal.h>
//Conor Oliver, Connor Van Cleave, Matt Condit
//
struct node {
pid_t pid;
int childrv;
char** cmd;
struct node *next;
};
struct wordnode {
char word[256];
struct wordnode *next;
};
//##################
struct node * insert_head(pid_t pid, int childrv, char**cmd, struct node *head) {
//printf("in insert_head \n");
head = (struct node *)malloc(sizeof(struct node));
head->pid = pid;
head->next = NULL;
head->cmd = cmd;
head->childrv = childrv;
return head;
}
//#####################
struct node * append(pid_t pid, int childrv, char**cmd, struct node *head) {
//printf("in append \n");
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
new_node = insert_head(pid, childrv, cmd, head);
}
else {
struct node * current = head;
while (current->next != NULL) { //current = last node in list
current = current->next;
}
current->next = new_node;
new_node->pid = pid;
new_node->next = NULL;
new_node->cmd = cmd;
new_node->childrv = childrv;
}
return new_node;
}
void free_list(struct node *wordlist)
{
struct node * victim = wordlist;
if(wordlist != NULL) {
while (wordlist -> next != NULL)
{
victim = wordlist->next;
free(wordlist);
wordlist = victim;
}
}
}
void free_wordlist(struct wordnode *wordlist)
{
struct wordnode * victim = wordlist;
while (wordlist -> next != NULL)
{
victim = wordlist->next;
free(wordlist);
wordlist = victim;
}
}
//######################
void make_array(char* buf, char** argv, int size) {
char* token;
char* cop = strdup(buf);
int i=0;
for (token=strtok(cop,";"); token!=NULL; token=strtok(NULL,";")){
argv[i] = strdup(token);
i++;
}
argv[size] = NULL;
free(cop);
}
void make_array2(char* buf, char** argv, int size) {
char* token;
char* cop = strdup(buf);
int i=0;
for (token=strtok(cop," \n\t"); token!=NULL; token=strtok(NULL," \n\t")){
argv[i] = strdup(token);
i++;
}
argv[size] = NULL;
free(cop);
}
// gets size of array from buffer
int size_of_array(char* buf) {
char* token;
char* cop = strdup(buf);
int count=0;
for (token=strtok(cop,";"); token!=NULL;token=strtok(NULL,";")) {
count++;
}
free(cop);
return count;
}
int size_of_array2(char* buf) {
char* token;
char* cop = strdup(buf);
int count=0;
for (token=strtok(cop," \n\t"); token!=NULL;token=strtok(NULL," \n\t")) {
count++;
}
free(cop);
return count;
}
//test function: print array
void print_array(char** a, int size) {
int i=0;
while (i<size) {
printf("%s\n", a[i]);
i++;
}
}
int run_sequential(char** argv, int size, struct wordnode* shell_list) {
//printf("in sequential\n");
int e=0;
int output = 0;
for (int i=0;i<size; i++) {
int subsize = size_of_array2(argv[i]);
char** cmd = (char**) malloc(sizeof(char*)*(subsize+1));
//printf("%d\n",subsize);
make_array2(argv[i],cmd,subsize);
//print_array(cmd,subsize);
int childrv;
//char *cmd[] = { "/bin/ls", "-ltr", ".", NULL };
if (strcmp(cmd[0],"mode")==0) {
if (subsize==1) {
printf("sequential\n");
}
else if (strcmp(cmd[1],"parallel") == 0||strcmp(cmd[1],"p")== 0)
{output=1;}
}
else if (strcmp(cmd[0],"exit")==0) {
e=1;
}
else {
/* printf("%s\n", shell_list->word);
if(shell_list != NULL) {
while (shell_list !=NULL) {
printf("in loop \n");
char* concat = NULL;
strcpy(concat, "/");
concat = strcat(concat, cmd[0]);
printf("%s\n", concat);
concat = strcat(shell_list->word,concat);
printf("%s\n", concat);
struct stat statresult;
int rv = stat(concat, &statresult);
if (rv < 0) {
printf("doesnt exist \n");
if(shell_list -> next != NULL) {
shell_list = shell_list->next; //not found
}
else {
break; //command not going to be found
}
}
else {
printf("%s%s\n", cmd[0], "= concat");
strcpy(cmd[0], concat);
break;
}
}
}
else {printf("shell_list = null\n");}; */
pid_t pid = fork();
if (pid==0) {
//printf("%s\n", argv[0]);
childrv = execv(cmd[0], cmd);
if (childrv<0) {
fprintf(stderr, "execv failed: %s%s%s%s\n"," ", cmd[0], " ", strerror(1));
}
} else {
wait(&childrv);
//printf("Child process finished\n");
} }
free(cmd);
} //end sequential for loop
if (e==1) {
exit(0);
}
if(output == 1) {printf("changed to p\n");}
return output;
}
int run_par(char** argv, int size) {
//printf("in parallel\n");
struct node *head = NULL;
int e=0;
int output = 1;
int process_count = 0;
for (int i=0;i<size; i++) {
int subsize = size_of_array2(argv[i]);
char** cmd = (char**) malloc(sizeof(char*)*(subsize+1));
make_array2(argv[i],cmd,subsize);
int childrv = 0;
if (strcmp(cmd[0],"mode")==0) {
if (subsize==1) {
printf("parallel\n");
}
else if (strcmp(cmd[1],"sequential\n") == 0||strcmp(cmd[1],"s") == 0)
{output=0;}
}
else if (strcmp(cmd[0],"exit")==0) {
e=1;
}
else {
//printf("about to fork\n");
pid_t pid = fork();
process_count++;
if (pid==0) { //children
//printf("%s\n", argv[0]);
childrv = execv(cmd[0], cmd);
if (childrv<0) {
process_count--;
fprintf(stderr, "execv failed: %s\n", strerror(1));
}
}
else { //parent process
if(head == NULL) {
head = append(pid, childrv, cmd, head);
}
else {append(pid, childrv, cmd, head);}
//printf("calling append from p\n");
}
}
}
if(head != NULL) {
struct node * current_node = head;
while(1) { //calls wait for all children
waitpid(current_node -> pid, ¤t_node->childrv, 0); //for stage 2: call waitpid with WNOHANG as option, pid = waitpid("","",WNHOHANG)
printf("%s%s%s", "*process ", *(current_node->cmd), " finished*\n");
if(current_node -> next != NULL) {
//printf("advanced node\n");
current_node = current_node -> next;
}
else {
free(current_node);
break;
}
}
}
else { //head = NULL
//printf("currnode = NULL\n");
}
if (e==1) {
exit(0);
}
//*********NEED TO CLEAR AND FREE LIST HERE**************
free_list(head);
return output;
} // end run par
struct wordnode *load_commands(const char* filename, int* num_words) {
/*FILE *f = fopen(filename,"r");
int ch = fgetc(f);
struct wordnode *first_word;
first_word = (struct wordnode*) malloc( sizeof(struct wordnode));
int i = 0;
int num = *num_words;
//to get first word
while (ch!= '\n') {
first_word->word[i] = ch;
i++;
ch = fgetc(f);
}
num++;
first_word->word[i] = '\0';
ch = fgetc(f);
struct wordnode *previous_word;
previous_word = (struct wordnode*) malloc( sizeof(struct wordnode));
previous_word = first_word;
while (! feof(f)) {
struct wordnode* current_word = (struct wordnode*) malloc( sizeof(struct wordnode));
i = 0;
while (ch!= '\n') {
current_word->word[i] = ch;
i++;
ch=fgetc(f);
}
current_word->word[i] = '\0';
previous_word->next = current_word;
previous_word = current_word;
ch = fgetc(f);
num++;
}
*num_words = num;*/
struct wordnode * first_word = (struct wordnode*) malloc( sizeof(struct wordnode));
strcpy(first_word->word, "/bin");
first_word->next = NULL;
return first_word;
}
int main(int argc, char **argv) {
// test shell config
struct stat shelltest;
struct wordnode* commands; //head for list of possible paths to commands
int rv = stat("shell-config", &shelltest);
if (rv < 0) {
printf("shell-config DNE. Please enter full path");
}
else {
int num_words = 0;
commands = load_commands("shell-config",&num_words);
//commands = "/bin/";
}
char prompt[] = "prompt> ";
printf("%s",prompt);
fflush(stdout);
int m=0; //keep track of mode
char buffer[1024];
while (fgets(buffer, 1024, stdin) != NULL) {
//handles comments, but only at the end of command
int i=0;
while (buffer[i]!='\0') {
if (buffer[i]=='#') {
buffer[i] = '\0';
i--;
}
i++;
}
//
int size = size_of_array(buffer);
char** argv = (char**) malloc(sizeof(char*)*(size+1));
make_array(buffer,argv,size);
//print_array(argv,size);
//printf("%d\n", size);
if (m==0) {
m = run_sequential(argv,size,commands);
}
else if (m==1) {
m = run_par(argv,size);
//pid_t pid;
//int num;
/*while ((pid = waitpid(-1, &stat, WNOHANG))>0) {
printf("child process finished /n");
}*/
}
printf("%s",prompt);
} // end shell while loop
free(argv);
free_wordlist(commands);
return 0;
}