|
|
@ -8,8 +8,6 @@ |
|
|
|
#include <dirent.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "yeeshell.h"
|
|
|
|
|
|
|
|
/* record cmdline history */ |
|
|
@ -66,7 +64,6 @@ int main() |
|
|
|
{ |
|
|
|
free(history[i]); |
|
|
|
} |
|
|
|
free(history); |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |
|
|
|
|
|
|
@ -140,36 +137,56 @@ int parseline(char *cmdline, char **args) |
|
|
|
return bg; |
|
|
|
} |
|
|
|
|
|
|
|
int redirect(char **args, char *redirect_file, char **redirect_args) |
|
|
|
int check_redirect(char **args, char *redirect_filename, char **redirect_args) |
|
|
|
{ |
|
|
|
int i = 0; |
|
|
|
int i = 0, j = 0, redirect_flag = REDIRECT_NO; |
|
|
|
while (args[i] != NULL) |
|
|
|
{ |
|
|
|
redirect_args[i] = args[i]; |
|
|
|
if (args[i + 1] == ">") |
|
|
|
if (!strcmp(args[i], ">")) |
|
|
|
{ |
|
|
|
redirect_file = args[i + 2]; |
|
|
|
return OUT_REDRT; |
|
|
|
redirect_flag = REDIRECT_OUT; |
|
|
|
break; |
|
|
|
} |
|
|
|
else if (args[i + 1] == "<") |
|
|
|
else if (!strcmp(args[i], "<")) |
|
|
|
{ |
|
|
|
redirect_file = args[i + 2]; |
|
|
|
return IN_REDRT; |
|
|
|
redirect_flag = REDIRECT_IN; |
|
|
|
break; |
|
|
|
} |
|
|
|
i++; |
|
|
|
} |
|
|
|
return NO_REDRT; |
|
|
|
|
|
|
|
if (redirect_flag == 1) /* redirect output */ |
|
|
|
{ |
|
|
|
redirect_filename = args[i + 1]; |
|
|
|
for (j = 0; j < i; j++) |
|
|
|
{ |
|
|
|
redirect_args[i] = args[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
else if (redirect_flag == 2) /* redirect input */ |
|
|
|
{ |
|
|
|
redirect_filename = args[0]; |
|
|
|
i++; |
|
|
|
while (args[i] != NULL) |
|
|
|
{ |
|
|
|
redirect_args[j++] = args[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
return redirect_flag; |
|
|
|
} |
|
|
|
|
|
|
|
int execute(char *cmdline, char **args) |
|
|
|
{ |
|
|
|
int bg = 0, redirect_flag = 0, fd = 0; |
|
|
|
char *redirect_file, *redirect_args[ARGS_MAX_QUANTITY]; |
|
|
|
int bg = 0, i = 0, redirect_flag = 0; |
|
|
|
pid_t pid; |
|
|
|
char *redirect_filename; |
|
|
|
char *redirect_args[ARGS_MAX_QUANTITY]; |
|
|
|
sigset_t mask_all, mask_prev; |
|
|
|
sigprocmask(SIG_BLOCK, NULL, &mask_all); |
|
|
|
sigaddset(&mask_all, SIGCHLD); |
|
|
|
bg = parseline(cmdline, args); |
|
|
|
redirect_flag = redirect(args, redirect_file, redirect_args); |
|
|
|
redirect_flag = check_redirect(args, redirect_filename, redirect_args); |
|
|
|
printf("%d %s %s\n", redirect_flag, redirect_filename, redirect_args); |
|
|
|
|
|
|
|
if (args[0] == NULL) |
|
|
|
{ |
|
|
@ -189,38 +206,6 @@ int execute(char *cmdline, char **args) |
|
|
|
/* Set the pid of the current process to the group number of the process group it belongs to. */ |
|
|
|
/* avoid being grouped with tsh */ |
|
|
|
setpgid(0, 0); |
|
|
|
|
|
|
|
switch (redirect_flag) |
|
|
|
{ |
|
|
|
case 0: /* no redirection */ |
|
|
|
if (execvp(args[0], args) <= 0) |
|
|
|
{ |
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|
exit(0); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 1: /* redirect output */ |
|
|
|
fd = open(redirect_file, O_CREAT | O_WRONLY | O_TRUNC, 0664); |
|
|
|
dup(fd, 1); |
|
|
|
if (execvp(args[0], redirect_args) <= 0) |
|
|
|
{ |
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|
exit(0); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 2: /* redirect input */ |
|
|
|
fd = open(info.in_file, O_CREAT | O_RDONLY, 0666); |
|
|
|
close(fileno(stdin)); |
|
|
|
dup2(fd, fileno(stdin)); |
|
|
|
close(fd); |
|
|
|
if (execvp(args[0], redirect_args) <= 0) |
|
|
|
{ |
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|
exit(0); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if (execvp(args[0], args) <= 0) |
|
|
|
{ |
|
|
|
printf("%s: Command not found\n", args[0]); |
|
|
|