Browse Source

moduled, with mytop left

master
10195501441 3 years ago
parent
commit
dbacf0bdfa
4 changed files with 128 additions and 89 deletions
  1. +10
    -5
      result.txt
  2. BIN
      yeeshell
  3. +109
    -84
      yeeshell.c
  4. +9
    -0
      yeeshell.h

+ 10
- 5
result.txt View File

@ -1,5 +1,10 @@
README.md
result.txt
yeeshell
yeeshell.c
yeeshell.h
total 44
drwxr-xr-x 4 root root 120 Mar 17 22:11 .
dr-xr-x---. 14 root root 4096 Mar 16 23:39 ..
drwxr-xr-x 8 root root 201 Mar 17 20:08 .git
-rw-r--r-- 1 root root 46 Mar 10 17:40 README.md
-rw-r--r-- 1 root root 0 Mar 17 22:11 result.txt
drwxr-xr-x 2 root root 64 Mar 16 11:27 .vscode
-rwxr-xr-x 1 root root 22872 Mar 17 22:11 yeeshell
-rw-r--r-- 1 root root 7801 Mar 17 22:11 yeeshell.c
-rw-r--r-- 1 root root 1711 Mar 17 22:07 yeeshell.h

BIN
yeeshell View File


+ 109
- 84
yeeshell.c View File

@ -147,6 +147,38 @@ int check_redirect(char **args, char *redirect_filename, char **redirect_args)
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 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;
}
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 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;
char *redirect_filename = NULL;
char *redirect_args[ARGS_MAX_QUANTITY];
@ -196,98 +295,24 @@ int execute(char *cmdline, char **args)
{
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
{
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)

+ 9
- 0
yeeshell.h View File

@ -24,9 +24,18 @@ int parseline(char *cmdline, char **args);
/* check_redirect - check if the command contains redirection */
int check_redirect(char **args, char *redirect_filename, char **redirect_args);
/* do_redirect - execute redirection command */
int do_redirect(int redirect_flag, char *redirect_filename, char **redirect_args);
/* check_redirect - check if the command contains pipe */
int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2);
/* do_pipe - execute pipe command */
int do_pipe(char **pipe_arg_1, char **pipe_arg_2);
/* do_bgfg - fork and execute background/foreground tasks */
int do_bg_fg(char **args, int bg);
/* execute - Execute the command line */
int execute(char *cmdline, char **args);

Loading…
Cancel
Save