diff --git a/fun_1.c b/fun_1.c index 1570845..5a0a413 100644 --- a/fun_1.c +++ b/fun_1.c @@ -134,10 +134,6 @@ int builtin_cmd(char **argv) { return 1; } - if(!strcmp(argv[0], "&")) - { - return 1; - } return 0; } \ No newline at end of file diff --git a/fun_2.c b/fun_2.c index 7892d3c..9afceea 100644 --- a/fun_2.c +++ b/fun_2.c @@ -25,50 +25,72 @@ void print_his(char *argv) int Is_pipe(char **argv) { int i = 0; + int Ispipe = 0; char *cmd_0[MAXLINE]; char *cmd_1[MAXLINE]; while (argv[i]!=NULL) { - cmd_0[i] = argv[i]; - if(!strcmp(argv[i],"|")) + if (argv[i][0] == '|') { - cmd_0[i] = NULL; - i++; - while (argv[i]!=NULL) - { - cmd_1[i] = argv[i]; - i++; - } - cmd_1[i] = NULL; - shell_pip(cmd_0, cmd_1); - return 1; + Ispipe = 1; } i++; } - return 0; + if(Ispipe == 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_pip(cmd_0, cmd_1); + return 1; } -void shell_pip(char **cmd_0, char **cmd_1) -{/* - int pid; - int fd[2]; - pipe(fd); - pid = fork(); - if (pid == 0) - { - dup2(fd[0], 0); - close(fd[0]); - close(fd[1]); - -// mysys_simple(&Cmd[1]); - execvp(cmd_1[0],cmd_1); - exit(0); +void shell_pip(char **prog1, char **prog2) +{ + int des_p[2]; + if(pipe(des_p) == -1) + { + perror("Pipe failed"); + exit(1); } - dup2(fd[1], 1); - close(fd[0]); - close(fd[1]); - -// mysys_simple(&Cmd[0]); - execvp(cmd_0[0],cmd_0); -*/ + if(fork() == 0) + { + close(1); + dup(des_p[1]); + close(des_p[0]); + close(des_p[1]); + execvp(prog1[0], prog1); + perror("execvp of prog1 failed"); + exit(1); + } + if(fork() == 0) + { + close(0); + dup(des_p[0]); + close(des_p[1]); + close(des_p[0]); + execvp(prog2[0], prog2); + perror("execvp of prog2 failed"); + exit(1); + } + close(des_p[0]); + close(des_p[1]); + wait(0); + wait(0); + return 0; } \ No newline at end of file diff --git a/main b/main index 1f82560..c70b2b2 100755 Binary files a/main and b/main differ