From dbacf0bdfa8202f1b116ae8cddb436230f645b1e Mon Sep 17 00:00:00 2001 From: 10195501441 <10195501441@stu.ecnu.edu.com> Date: Wed, 17 Mar 2021 22:16:42 +0800 Subject: [PATCH] moduled, with mytop left --- result.txt | 15 +++-- yeeshell | Bin 22776 -> 22872 bytes yeeshell.c | 193 ++++++++++++++++++++++++++++++++++--------------------------- yeeshell.h | 9 +++ 4 files changed, 128 insertions(+), 89 deletions(-) mode change 100755 => 100644 result.txt diff --git a/result.txt b/result.txt old mode 100755 new mode 100644 index 5c5a139..148f6c6 --- a/result.txt +++ b/result.txt @@ -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 diff --git a/yeeshell b/yeeshell index 24395984e595f3ce066da9dc46c66ade009121a9..d5cbff83df72054f4535e51198efb3f12beaccf5 100755 GIT binary patch delta 3248 zcmZWseNa@_6~FHxupr>BKp(ry^3gU0?F9UwAR6=`t~^@&YN4Z@q`?nN_#;5fQnBr3 zYZBUHe>=s+klgVgpQL!M;$t2CfhDez!17x!uEGR9*TW4 zd(XM&{LaTc=iK*Zqut8rMdetbGJ1U3ACnt$34a=LkN>?d-;ynMmTa9fCcPlU!sa(?}XuR8#ny*ql4~Q zh1qu8iN*ZxXoojSmUO-eNr!hrX)A%d@!kO-`YKgI4F6z=NBa0SL(lee({^kE?pgm} zcs;nt8d%cW42SQ$1aS+d5}-xk;C9pq5F=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NlwB8srq&P&P-&!!J-BMOl=ZWZS1qCmMprLnO{>dj72CO5rU#fC zbFY|xFXprC{A$_TRlpCmhlG~28Q!sLe2g->My6w2lPz9T=(hS|poCl%KAh}fXw4V% ztzuTiyosHLyj`sK@VP#oPe>^#CRa*B!i6|ZSTR%nJC7k3n+*A3VSAFbG|4o>V|fQn z%@VU2NL-AR(EOCo-YHY(aaTxSi}{hjC}e+alD!o$aS@qJE^ZT2YpUfay+h0`Y?UHM z+IUwaCpq?w#y=*yg?%*Td>{DO2RW~9tUM%maTletHN4}kJL4A!^72jTzK#P~CN80bYa*3#mT zkT#h;`G=^Nu!C!5s`ItG6noFB_^p}ko$T=10Ie4qikR=`vv-@R_qyBgC|(Zu-osJ0 zp*fE|+gxVvhE|ZBr}OA<*zsnW9_6Su+B}wrcPO=fKkLh{(870BOWM)9w@SDxcFl87 z&_)&3&|2rP&RY-L*}BYa_4|lVlt4DH{?Ot996im3@HCs+xQsr)>L0k_fA@7(-2eap diff --git a/yeeshell.c b/yeeshell.c index 4d01ec5..5b8ef5f 100644 --- a/yeeshell.c +++ b/yeeshell.c @@ -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) diff --git a/yeeshell.h b/yeeshell.h index a26c1f8..b1fe10f 100644 --- a/yeeshell.h +++ b/yeeshell.h @@ -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);