|
@ -147,6 +147,38 @@ int check_redirect(char **args, char *redirect_filename, char **redirect_args) |
|
|
return redirect_flag; |
|
|
return redirect_flag; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int do_redirect(int redirect_flag, char *redirect_filename, char **redirect_args) |
|
|
|
|
|
{ |
|
|
|
|
|
pid_t pid; |
|
|
|
|
|
int fd = 1; |
|
|
|
|
|
if ((pid = fork()) == 0) /* Child process */ |
|
|
|
|
|
{ |
|
|
|
|
|
if (redirect_flag == 1) /* in or out? */ |
|
|
|
|
|
{ |
|
|
|
|
|
fd = open(redirect_filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR); |
|
|
|
|
|
close(1); |
|
|
|
|
|
dup(fd); |
|
|
|
|
|
} |
|
|
|
|
|
else if (redirect_flag == 2) |
|
|
|
|
|
{ |
|
|
|
|
|
fd = open(redirect_filename, O_RDONLY, S_IRUSR); |
|
|
|
|
|
close(0); |
|
|
|
|
|
dup(fd); |
|
|
|
|
|
} |
|
|
|
|
|
if (execvp(redirect_args[0], redirect_args) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", redirect_args[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
close(fd); |
|
|
|
|
|
} |
|
|
|
|
|
else /* parent process */ |
|
|
|
|
|
{ |
|
|
|
|
|
waitpid(pid, NULL, 0); |
|
|
|
|
|
} |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2) |
|
|
int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2) |
|
|
{ |
|
|
{ |
|
|
int pipe_flag = 0, i = 0, j = 0; |
|
|
int pipe_flag = 0, i = 0, j = 0; |
|
@ -172,9 +204,76 @@ int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2) |
|
|
return pipe_flag; |
|
|
return pipe_flag; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int do_pipe(char **pipe_arg_1, char **pipe_arg_2) |
|
|
|
|
|
{ |
|
|
|
|
|
int fds[2]; |
|
|
|
|
|
pipe(fds); |
|
|
|
|
|
pid_t prog_1, prog_2; |
|
|
|
|
|
|
|
|
|
|
|
if ((prog_1 = fork()) == 0) /* Child process 1 */ |
|
|
|
|
|
{ |
|
|
|
|
|
close(1); |
|
|
|
|
|
dup(fds[1]); |
|
|
|
|
|
close(fds[0]); |
|
|
|
|
|
close(fds[1]); |
|
|
|
|
|
|
|
|
|
|
|
if (execvp(pipe_arg_1[0], pipe_arg_1) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", pipe_arg_1[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ((prog_2 = fork()) == 0) /* Child process 1 */ |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
dup2(fds[0], 0); |
|
|
|
|
|
close(fds[0]); |
|
|
|
|
|
close(fds[1]); |
|
|
|
|
|
|
|
|
|
|
|
if (execvp(pipe_arg_2[0], pipe_arg_2) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", pipe_arg_2[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
close(fds[0]); |
|
|
|
|
|
close(fds[1]); |
|
|
|
|
|
|
|
|
|
|
|
waitpid(prog_1, NULL, 0); |
|
|
|
|
|
waitpid(prog_2, NULL, 0); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int do_bg_fg(char **args, int bg) |
|
|
|
|
|
{ |
|
|
|
|
|
pid_t pid; |
|
|
|
|
|
if ((pid = fork()) == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
if (execvp(args[0], args) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
if (bg) |
|
|
|
|
|
{ |
|
|
|
|
|
signal(SIGCHLD, SIG_IGN); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
waitpid(pid, NULL, 0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int execute(char *cmdline, char **args) |
|
|
int execute(char *cmdline, char **args) |
|
|
{ |
|
|
{ |
|
|
int bg = 0, i = 0, redirect_flag = 0, fd = 1, pipe_num = 0; |
|
|
|
|
|
|
|
|
int bg = 0, i = 0, redirect_flag = 0, pipe_num = 0; |
|
|
pid_t pid; |
|
|
pid_t pid; |
|
|
char *redirect_filename = NULL; |
|
|
char *redirect_filename = NULL; |
|
|
char *redirect_args[ARGS_MAX_QUANTITY]; |
|
|
char *redirect_args[ARGS_MAX_QUANTITY]; |
|
@ -196,98 +295,24 @@ int execute(char *cmdline, char **args) |
|
|
{ |
|
|
{ |
|
|
if (!built_in(args)) /* built-in cmd? */ |
|
|
if (!built_in(args)) /* built-in cmd? */ |
|
|
{ |
|
|
{ |
|
|
if ((pid = fork()) == 0) /* Child process */ |
|
|
|
|
|
|
|
|
if (redirect_flag != 0) /* redirection? */ |
|
|
{ |
|
|
{ |
|
|
if (redirect_flag == 1) /* redirect? */ |
|
|
|
|
|
{ |
|
|
|
|
|
fd = open(redirect_filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR); |
|
|
|
|
|
close(1); |
|
|
|
|
|
dup(fd); |
|
|
|
|
|
} |
|
|
|
|
|
else if (redirect_flag == 2) |
|
|
|
|
|
{ |
|
|
|
|
|
fd = open(redirect_filename, O_RDONLY, S_IRUSR); |
|
|
|
|
|
close(0); |
|
|
|
|
|
dup(fd); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ((redirect_flag == 1) || (redirect_flag == 2)) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
if (execvp(redirect_args[0], redirect_args) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
close(fd); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
if (execvp(args[0], args) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return do_redirect(redirect_flag, redirect_filename, redirect_args); |
|
|
} |
|
|
} |
|
|
else /* parent process */ |
|
|
|
|
|
|
|
|
else |
|
|
{ |
|
|
{ |
|
|
if (bg) |
|
|
|
|
|
{ |
|
|
|
|
|
signal(SIGCHLD, SIG_IGN); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
waitpid(pid, NULL, 0); |
|
|
|
|
|
} |
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
return do_bg_fg(args, bg); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
int fds[2]; |
|
|
|
|
|
pipe(fds); |
|
|
|
|
|
pid_t prog_1, prog_2; |
|
|
|
|
|
|
|
|
|
|
|
printf("%s %s %s %s", pipe_arg_1[0], pipe_arg_1[1], pipe_arg_1[2], pipe_arg_1[3]); |
|
|
|
|
|
|
|
|
|
|
|
if ((prog_1 = fork()) == 0) /* Child process 1 */ |
|
|
|
|
|
{ |
|
|
|
|
|
close(1); |
|
|
|
|
|
dup(fds[1]); |
|
|
|
|
|
close(fds[0]); |
|
|
|
|
|
close(fds[1]); |
|
|
|
|
|
|
|
|
|
|
|
if (execvp(pipe_arg_1[0], pipe_arg_1) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", pipe_arg_1[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ((prog_2 = fork()) == 0) /* Child process 1 */ |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
dup2(fds[0], 0); |
|
|
|
|
|
close(fds[0]); |
|
|
|
|
|
close(fds[1]); |
|
|
|
|
|
|
|
|
|
|
|
if (execvp(pipe_arg_2[0], pipe_arg_2) <= 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s: Command not found\n", pipe_arg_2[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
close(fds[0]); |
|
|
|
|
|
close(fds[1]); |
|
|
|
|
|
|
|
|
|
|
|
waitpid(prog_1, NULL, 0); |
|
|
|
|
|
waitpid(prog_2, NULL, 0); |
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
return do_pipe(pipe_arg_1, pipe_arg_2); |
|
|
} |
|
|
} |
|
|
return 1; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int built_in(char **args) |
|
|
int built_in(char **args) |
|
|