From 002294fd362cac50ae599ea26b699cbae728f95f Mon Sep 17 00:00:00 2001 From: 10195501441 <10195501441@stu.ecnu.edu.com> Date: Tue, 16 Mar 2021 16:26:43 +0800 Subject: [PATCH] added check redirection func --- yeeshell | Bin 32640 -> 23520 bytes yeeshell.c | 81 +++++++++++++++++++++++++------------------------------------ yeeshell.h | 20 +++++++-------- 3 files changed, 43 insertions(+), 58 deletions(-) diff --git a/yeeshell b/yeeshell index 3b06487a9836e2dd37eff8e1458cacdf3be0e70b..7525816d6303dd5888787c94d777659590275ea3 100755 GIT binary patch delta 4321 zcmZ`-4OCQR8vgEuL6`<+fPsM-!hwNtKtLdWkV?mN>gM0UlC$kriV9}=lS`83hEt)% zG>xmi+c}=y?MKhHYwc!kqi0=CG3b_br_nO;_>Of41lCEmUWF z3*CwkMT4O72KuXT)cnE2*IqfKH+MC6{3_wCFL%H9@wC&sg4@Ma`lND)n@ODqI`YYw zd_@qprA%8;l<#IC5$@r+X^gcm;8Qc7PUX|;JWNyhw46_gYvcGd57UtQ?m8B3H!vpo zJ>hnSBqU#Fc=GqMEN$v&GKU^=&>Jb);!6%{OPMbojSHHL_lw?^OXZTUIV|~_x+Gui z*-p<80Dsyx4ZC$qK+;OXD{@NjF z$!Ai)a9XXzTYFX#j-V9DC$@M7k-^}do}p(yjx57kV$))!R<8AQ@dP6iei$@h5aAW5 z5q!0SH*FFkAC&w8BLC1y**?_eZFHp+!wUY0=^!%lA9Y3`X&ti8Z-@cQ8TONoM z*i`plZgQ}lml5Ja*f0o2RkZUa;pejbTX6nYc_7KZNez33EAAVTWgiaJ8+f&gNAk}J z!$7g~o!|!lLErd1{Zb&H>TAQu(|0KJCJu1FvR)})PxHYJ;rR9Y`Qcxv!5CE_(sWGu z0gmT1mgBiZ^FQQObARBvEQh~GK%b|(rMa6mHNu2jw(+GTtgE~fV_`O|;#Y3YK&#WI z6zGNytNxP5k0!&<@lfa;N3??l5<{`Gmj_u<6{S@kdFa`CxO# z2(_MkMN=iFp#CN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u1#hja*0OHm&r!Z9=l?L#0Y%jriEd(jm%z4dIfKNQ2h zas9GLeJ#Nk2O&2D>o^kc4YW_AzM+WGDPwv~^e^VHOJC|8*f`?RXdn@ZEvKE*KbnN! zhZTe6LrX2Wenhx>D~s)y`Y6;M@U{D6ep*Sb*n6qAy;xsJXSC16cG}TN>l$LTso;CS zm`D4)lryw;PNUS2{1+l)nE39K&i0pj$C3nW!cTYCw86RACfrnLFV4-@KAZbHe38S& z0xQJ7{m90z+(pJX!5E8a#387JG}gL@e%-iOE^v)A{wltBF*%LxDAmdqyt8~f`wYH} z5j#FkO~N;-zelHg7Tw&?B5$!IU#rqu7xu~5%C+|w*2>#UHQS;k@;Q%o>!MnDeZF@8 zqWMyR_Tr+=(x=+&n{F%^Uy>$#k;`OhEaA}}yeT3r*BY8~HP>R#w6z&tIKo5W zIR18g$e=%(qqQzxBJVHOezbUsHnS;H%G0E#-292CAvqZTv(XIlBrOx4che`|3`q|v z>dxN@C__$XhsM=JBe{mBh&yH$sUY+mGdzvIaQ_u)CEhl;7)1P{RaK|A6gjOEh0H& zaK~TuJ|(y(z|BA;$)87)Ghx-F=3MC{$w(iF&rijttofUB^KvBk zr_Z77%ZAO;jG@r#B6;ZHHLsZ#)V2p&*r zWfOHOo2bjOp%+_xGSA25h(wD?oi#^i)MJY(5&uk3(5D6s`W@Ks#y)hbGWNM4X=&>5 z6*~O~#+Hzj8n3Y^sc}nSwJ!54+I{RZUSlT #include #include -#include -#include #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]); diff --git a/yeeshell.h b/yeeshell.h index c2cd9c6..08c1bef 100644 --- a/yeeshell.h +++ b/yeeshell.h @@ -2,7 +2,7 @@ #define CMDLINE_MAX_SIZE 1024 /* max length of a single command line */ #define ARGS_MAX_QUANTITY 128 /* max args on a command line */ -#define BUFFER_MAX_SIZE 64 /* max size of a buffer which contains parsed arguments */ +#define BUFFER_MAX_SIZE 64 /* max size of a buffer which contains parsed arguments */ #define CMDLINE_DIV ' \t\r\n\a' #define CMDLINE_HISTORY_MAX_QUANTITY 256 #define JOBS_MAX_QUANTITY 16 @@ -14,17 +14,17 @@ #define BG 2 /* running in background */ #define ST 3 /* stopped */ -#define NO_REDRT 0 /* no redirection */ -#define OUT_REDRT 1 /* the latter is output */ -#define IN_REDRT 2 /* the former is input */ +#define REDIRECT_NO 0 /* no redirection */ +#define REDIRECT_OUT 1 /* redirect output */ +#define REDIRECT_IN 2 /* redirect input */ /* job_t - The job struct */ struct job_t { - pid_t pid; /* job PID */ - int jid; /* job ID [1, 2, ...] */ - int state; /* UNDEF, BG, FG, or ST */ - char cmdline[CMDLINE_MAX_SIZE]; /* command line */ + pid_t pid; /* job PID */ + int jid; /* job ID [1, 2, ...] */ + int state; /* UNDEF, BG, FG, or ST */ + char cmdline[CMDLINE_MAX_SIZE]; /* command line */ }; /* readline - Get the command line */ @@ -33,8 +33,8 @@ char *readline(); /* parseline - Evaluate the command line that the user has just typed in */ int parseline(char *cmdline, char **args); -/* redirect - Judge if the command contains redirection */ -int redirect(char **args, char *redirect_file, char **redirect_args); +/* check_redirect - check if the command contains redirection */ +int check_redirect(char **args, char *redirect_filename, char **redirect_args); /* execute - Execute the command line */ int execute(char *cmdline, char **args);