|
|
@ -8,6 +8,8 @@ |
|
|
|
#include <dirent.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "yeeshell.h"
|
|
|
|
|
|
|
|
/* record cmdline history */ |
|
|
@ -64,6 +66,7 @@ int main() |
|
|
|
{ |
|
|
|
free(history[i]); |
|
|
|
} |
|
|
|
free(history); |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |
|
|
|
|
|
|
@ -137,14 +140,36 @@ int parseline(char *cmdline, char **args) |
|
|
|
return bg; |
|
|
|
} |
|
|
|
|
|
|
|
int redirect(char **args, char *redirect_file, char **redirect_args) |
|
|
|
{ |
|
|
|
int i = 0; |
|
|
|
while (args[i] != NULL) |
|
|
|
{ |
|
|
|
redirect_args[i] = args[i]; |
|
|
|
if (args[i + 1] == ">") |
|
|
|
{ |
|
|
|
redirect_file = args[i + 2]; |
|
|
|
return OUT_REDRT; |
|
|
|
} |
|
|
|
else if (args[i + 1] == "<") |
|
|
|
{ |
|
|
|
redirect_file = args[i + 2]; |
|
|
|
return IN_REDRT; |
|
|
|
} |
|
|
|
} |
|
|
|
return NO_REDRT; |
|
|
|
} |
|
|
|
|
|
|
|
int execute(char *cmdline, char **args) |
|
|
|
{ |
|
|
|
int bg = 0, i = 0; |
|
|
|
int bg = 0, redirect_flag = 0, fd = 0; |
|
|
|
char *redirect_file, *redirect_args[ARGS_MAX_QUANTITY]; |
|
|
|
pid_t pid; |
|
|
|
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); |
|
|
|
|
|
|
|
if (args[0] == NULL) |
|
|
|
{ |
|
|
@ -164,6 +189,38 @@ 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]); |
|
|
@ -172,10 +229,6 @@ int execute(char *cmdline, char **args) |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
do |
|
|
|
{ |
|
|
|
} while (!args[i++]); |
|
|
|
|
|
|
|
if (bg) /* bg task */ |
|
|
|
{ |
|
|
|
addjob(jobs, pid, BG, cmdline); |
|
|
|