From 8d4256f9d749850bf3844f5e8c1bbb17632d990f Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 19 Feb 2021 02:00:54 +0100 Subject: [PATCH] =?UTF-8?q?R=C3=A9solution=20temporaire=20pour=20#8=20&=20?= =?UTF-8?q?ajout=20commentaires?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shellOpt.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/shellOpt.c b/src/shellOpt.c index df0fd39..5bb6f28 100644 --- a/src/shellOpt.c +++ b/src/shellOpt.c @@ -44,26 +44,36 @@ int main() while (commands[index] != NULL) { parse_string(commands[index], args, ' '); - if (!change_dir(args)) + if (!change_dir(args)) /* si la commande demandée n’est pas un cd */ { pid = fork(); - if (!pid) + if (!pid) /* le fils */ { - if (commands[index+1] == NULL) + if (commands[index+1] == NULL) + { dup2(my_pipe[0], STDIN_FILENO); + close(my_pipe[1]); + } else if (index == 0) + { dup2(my_pipe[1], STDOUT_FILENO); - close(my_pipe[0]); - close(my_pipe[1]); + close(my_pipe[0]); + } + else + fclose(fdopen(my_pipe[0], "w")); + /* si la commande est intermédiaire dans le pipe, on vide le buffer + * de sortie */ execvp(args[0], args); - return 0; + exit(0); } + /* lorsqu’on arrive à la dernière commande, on peut fermer le pipe et + * attendre le dernier processus */ if (commands[index + 1] == NULL) { - close(my_pipe[1]); close(my_pipe[0]); + close(my_pipe[1]); waitpid(pid, NULL, 0); } }