diff --git a/fun_1.c b/fun_1.c index 5a0a413..02ae6ad 100644 --- a/fun_1.c +++ b/fun_1.c @@ -2,6 +2,8 @@ #include #include #include +#include + //#include "fun.h" @@ -22,6 +24,10 @@ void eval(char *cmdline) { if((pid=fork())==0) { + if(isBg) + { + signal(SIGCHLD, SIG_IGN); + } if(execvp(argv[0],argv)<0) { printf("%s:Command not found.\n",argv[0]); @@ -30,10 +36,12 @@ void eval(char *cmdline) } if(!isBg) { - wait(NULL); + wait(0); + return; } else { + signal(SIGCHLD, SIG_IGN); return; } } @@ -86,7 +94,7 @@ int parseline(const char *cmdline, char **argv) argv[argc] = NULL; if (argc == 0) - return 1; + return 1; if ((bg = (*argv[argc-1] == '&')) != 0) { @@ -101,6 +109,10 @@ int builtin_cmd(char **argv) { return 1; } + if(Is_open(argv)==1) + { + return 1; + } if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit")) { exit(0); diff --git a/fun_2.c b/fun_2.c index 9afceea..52d0b77 100644 --- a/fun_2.c +++ b/fun_2.c @@ -1,4 +1,5 @@ #include +#include //#include "fun.h" static int num = 0; @@ -93,4 +94,63 @@ void shell_pip(char **prog1, char **prog2) wait(0); wait(0); return 0; -} \ No newline at end of file +} + +int Is_open(char **argv) +{ + int i = 0; + int Isopen = 0; + char *cmd_0[MAXLINE]; + char *cmd_1[MAXLINE]; + while (argv[i]!=NULL) + { + if (argv[i][0] == '>') + { + Isopen = 1; + } + i++; + } + if(Isopen == 0) + { + return 0; + } + i = 0; + while (argv[i][0] != '>') + { + cmd_0[i] = argv[i]; + i++; + } + cmd_0[i] = NULL; + i++; + int j = 0; + while (argv[i]!=NULL) + { + cmd_1[j] = argv[i]; + i++; + j++; + } + cmd_1[j] = NULL; + shell_open(cmd_0, cmd_1); + return 1; +} + +void shell_open(char **prog1, char **prog2) +{ + //根据解析命令参数,创建/打开一文件 + int fd; + if(fork() == 0) + { + if( (fd=open(prog2[0],O_RDWR | O_CREAT | O_NOCTTY | O_NDELAY))<0 ) + { + perror("open"); + exit(0); + } + close(1); + dup(fd); + close(fd); + execvp(prog1[0], prog1); + exit(1); + } + wait(0); +} + diff --git a/main b/main index c70b2b2..e9cf204 100755 Binary files a/main and b/main differ