-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.c
More file actions
544 lines (512 loc) · 9.36 KB
/
sim.c
File metadata and controls
544 lines (512 loc) · 9.36 KB
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
/*An UNIX Shell Simulator */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
#define BUFFER_SIZE 80 //max 80 chars input from user
void print_prompt()
{
printf("%s ","$");
}
char *get_input()
{
char *command = malloc(BUFFER_SIZE);
if(fgets(command,BUFFER_SIZE,stdin))
return command;
else
return NULL;
}
/*To add spaces after special characters if there are none. This will help
to make sure our command is always in specific format.*/
char *add_spaces(char *command)
{
char *cmd = malloc(BUFFER_SIZE);
char *ptr = command;
int s = 0;
while(*ptr!=0)
{
//if there is a special character and there is no space
if(*ptr == '|' || *ptr == '<' || *ptr == '>' || *ptr == '&')
{
//no space before
if(s>0 && cmd[s-1]!=' ')
{
cmd[s++] = ' ';
}
cmd[s++] = *ptr;
//no space after
if(*(ptr + 1)!= ' ')
cmd[s++] = ' ';
else if((ptr+1)!=0)
{
cmd[s++] = ' ';
ptr++;
}
ptr++;
}
else
{
cmd[s++] = *ptr;
ptr++;
}
}
//mark the end of the string
cmd[s] = '\0';
strcpy(command,cmd);
free(cmd);
return command;
}
//to get the total number of arguments in the command
int get_arg_count(char *command)
{
int i=0,arg_count = 0;
while(command[i]!='\0')
{
if(command[i] == ' ')
arg_count++;
i++;
}
return arg_count;
}
int check_pipe(char **cmd_args)
{
int i=0,pipe_count=0;
for(i=0;cmd_args[i]!=NULL;i++)
{
if(strcmp(cmd_args[i],"|")==0)
{
pipe_count+=1;
}
}
return pipe_count;
}
char **remove_redirection(char **cmd_args)
{
int i, j;
for (i = 0, j = 0; cmd_args[i] != NULL; ++i)
{
char *arg = cmd_args[i];
if ((strcmp(arg, ">") == 0 || strcmp(arg, "<") == 0))
{
free(arg); // free the <, >
free(cmd_args[i + 1]); // free the filename
++i;
}
else
{
cmd_args[j] = cmd_args[i];
++j;
}
}
cmd_args[j] = NULL;
return cmd_args;
}
void input_redirection(char *filename)
{
int fd;
if((fd = open(filename, O_RDONLY)) == -1)
{
exit(1);
}
dup2(fd, STDIN_FILENO);
close(fd);
}
void output_redirection(char *filename) {
int fd;
if((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
{
exit(1);
}
dup2(fd, STDOUT_FILENO);
close(fd);
}
void handle_redirection(char **cmd_args)
{
int i=0;
for(i=0;cmd_args[i]!=NULL;i++)
{
if(strcmp(cmd_args[i],"<")==0)
{
if(cmd_args[i+1]==NULL)
return;
else
input_redirection(cmd_args[i+1]);
}
if(strcmp(cmd_args[i],">")==0)
{
if(cmd_args[i+1]==NULL)
return;
else
output_redirection(cmd_args[i+1]);
}
}
}
//Checks the redirection here
int check_redirection(char **cmd_args)
{
int i = 0;
for(i=0;cmd_args[i]!=NULL;i++)
{
if(strcmp(cmd_args[i],"<")==0||strcmp(cmd_args[i],">")==0)
return 1;
}
return 0;
}
void print_parsed(char **command_args)
{
int i=0;
char *c;
while((c=command_args[i++])!=NULL)
printf("Item %d : %s\n",i,c);
}
//This is where actual raw input goes and comes out formatted
char **parse_command(char *command)
{
char *cmd,*token;
char **cmd_args;
char seperator[] = " \n";
int i=0;
command = add_spaces(command);
cmd_args = (char **)malloc((get_arg_count(command)+1)*sizeof(char *));
cmd = strdup(command);
token = strtok(cmd,seperator);
while(token!=NULL)
{
//printf("Token: %s i value: %d\n",token,i);
cmd_args[i] = (char *)malloc((strlen(token)+1)*sizeof(char *));
strcpy(cmd_args[i],token);
token = strtok(NULL,seperator);
i+=1;
}
cmd_args[i] = NULL;
//printf("Command is %s\n",cmd);
//free(cmd);
return cmd_args;
}
void cleanup(char **command_args)
{
char *c;
int i=0;
while((c=command_args[i])!=NULL)
{
free(c);
c = NULL;
i++;
}
//free(command_args);
}
//Used to remove any trailing space for commands like pwd,ls
//which is unnecessary
char *remove_trailing_space(char *command)
{
int end = strlen(command);
if(isspace(command[end-1]))
{
printf("Space found\n");
command[end-1] = '\0';
}
return command;
}
int execute_subcommand(char **command_args)
{
char *cmd = command_args[0];
char *path = command_args[1];
char *temp;
char *env;
int pid,status;
temp = malloc(BUFFER_SIZE);
cmd = remove_trailing_space(cmd);
//print_parsed(command_args);
//Sort out if there is a pipe or redirection before, else do normally
if(strcmp(cmd,"cd") == 0)
{
if(path!=NULL)
{
//Absolute path
if(path[0] == '/')
{
if(chdir(path)!=0)
{
perror(path);
}
}
//Relative path
else
{
//Max 80 char address here
getcwd(temp,BUFFER_SIZE);
strcat(temp,"/");
strcat(temp,path);
if(chdir(temp)!=0)
{
perror(temp);
}
}
}
}
else if(strcmp(cmd,"set")==0)
{
if(path!=NULL)
{
//since putenv doesn't copy the string
env = strdup(path);
if(putenv(env)!=0)
printf("Error in setting path!\n");
}
}
else if(strcmp(cmd,"pwd")==0)
{
if (getcwd(temp,BUFFER_SIZE)!=NULL)
{
printf("%s\n",temp);
}
else
printf("Failed fetching current working directory\n");
}
else if(strcmp(cmd,"exit")==0)
{
free(temp);
cleanup(command_args);
exit(0);
}
else if(strcmp(cmd,"myls")==0)
{
//get MYPATH
env = getenv("MYPATH");
if(env==NULL)
{
printf("Did not find MYPATH\n");
}
else
{
strcat(temp,env);
strcat(temp,"./myls");
printf("%s\n",temp);
if((pid=fork())==0)
{
execv(temp,&command_args[0]);
exit(0);
}
else if(pid>0)
{
waitpid(pid,&status,0);
}
}
}
else
{//Pass these to the shell
if((pid=fork())==0)
{
execvp(cmd,&command_args[0]);
exit(0);
}
else if(pid>0)
{//parent process
waitpid(pid,&status,0);
if(status==1)
{
printf("%s\n","Invalid Command!");
}
}
}
free(temp);
return 1;
}
char ***get_pipe_commands(char **cmd_args,char ***new_cmdargs)
{
int subcmds=1;
int i, j = 0;
for (i = 0; cmd_args[i] != NULL; ++i)
{
if (cmd_args[i][0] == '|')
{
cmd_args[i] = NULL;
subcmds = 1;
}
else if (subcmds)
{
new_cmdargs[j++] = &cmd_args[i];
subcmds = 0;
}
}
return new_cmdargs;
}
void pipe_config(int pid,int fd[2])
{
if(pid==0)
{
close(fileno(stdout));
dup(fd[1]);
close(fd[0]);
close(fd[1]);
}
else
{
close(fileno(stdin));
dup(fd[0]);
close(fd[0]);
close(fd[1]);
}
}
void start_pipe_execution(char ***new_cmdargs,int count)
{
int fd[2];
int pid;
if(count==0)
{
execute_subcommand(new_cmdargs[count]);
return;
}
pipe(fd);
pid = fork();
pipe_config(pid,fd);
if(pid==0)
{
start_pipe_execution(new_cmdargs,count-1);
//exit(0);
}
else
execute_subcommand(new_cmdargs[count]);
}
int check_background(char **cmd_args)
{
int i=0;
for(i=0;cmd_args[i]!=NULL;i++)
{
if(strcmp(cmd_args[i],"&")==0)
{
return 1;
}
}
return 0;
}
char **remove_background(char **cmd_args)
{
int i, j;
for (i = 0, j = 0; cmd_args[i] != NULL; ++i)
{
if (strcmp(cmd_args[i],"&") == 0)
{
cmd_args[i] = '\0';
//return cmd_args;
}
else
{
cmd_args[j] = cmd_args[i];
++j;
}
}
cmd_args[j] = NULL;
return cmd_args;
}
int execute_command(char **command_args)
{
//print_parsed(command_args);
int status;
//Split them individually
char ***new_cmdargs;
int p = check_pipe(command_args);
int r = check_redirection(command_args);
int pid;
int pipe_pid;
int bg_pid;
int b = check_background(command_args);
//Check and see if redirection is needed
if(r==1)
{//setup the redirection
if((pid=fork())==0)
{//child process
handle_redirection(command_args);
command_args = remove_redirection(command_args);
execute_command(command_args);
exit(1);
}
else if(pid>0)
{//parent process
waitpid(pid,&status,0);
fflush(0);
return 1;
}
}
if(p==0&&b==0)
{//No pipes and background. Resume normal execution
return execute_subcommand(command_args);
}
else if(b==1)
{//It is a background process
//printf("Background process\n");
//remove the & symbol. assuming it will be in the end
command_args = remove_background(command_args);
if((bg_pid=fork())==0)
{//child process
setpgid(0, 0);
execute_subcommand(command_args);
exit(1);
}
else if(bg_pid>0)
{//parent process
//to signal the kernel to automatically reap the child
signal(SIGCHLD, SIG_IGN);
printf("%d\n",bg_pid);
}
}
else if(p>0)
{//Pipes logic goes here
//Store all pipe commands and process one by one by a recursive
//process
//printf("Pipes in command %d \n",p);
new_cmdargs = malloc((p+1)*sizeof(char **));
new_cmdargs = get_pipe_commands(command_args,new_cmdargs);
if((pipe_pid=fork())==0)
{
//child process. Should we kill?
start_pipe_execution(new_cmdargs,p);
exit(0); // this is stable now
}
else
//parent process
waitpid(pipe_pid,&status,0);
free(new_cmdargs);
}
return 1;
}
int main(void)
{
int command_status = 1; //if 0 exit
char *command;
char **command_args;
while(command_status)
{
print_prompt();
if((command = get_input())==NULL)
{
//Ctrl+D comes here, terminate?
//printf("Read Error\n");
exit(0);
//continue;
}
else if (strcmp(command,"\n")==0)
{
free(command);
command = NULL;
continue;
}
else
{
command_args = parse_command(command);
command_status = execute_command(command_args);
free(command);
cleanup(command_args);
command = NULL;
}
}
return 0;
}