From b40ea37ea00831ded2d2d644d44fe1f0a7a7cf87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Sun, 23 May 2021 15:49:32 +0200 Subject: [PATCH] f5 --- minishell.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/minishell.c b/minishell.c index 01e9e0b..6d66e13 100644 --- a/minishell.c +++ b/minishell.c @@ -103,6 +103,8 @@ void handler_sigtstp(int sig_num) siglongjmp(goto_prompt, sig_num); } +// TODO handler dans un autre fichier + int main(int argc, char *argv[]) { initialiser(&jobs); @@ -131,6 +133,7 @@ int main(int argc, char *argv[]) prompting = 0; // TODO: créer fonction "builtin" ? + // continue ou break en fonction de l'entier de retour if (cmd == NULL) { // EOF break; @@ -226,11 +229,10 @@ int main(int argc, char *argv[]) continue; } - // compter le nombre de commandes séparées par des pipes int nb_pipe = -1; char ***cursor = cmd->seq; while (*cursor) - { + { // compter le nombre de commandes séparées par des pipes cursor++; nb_pipe++; } @@ -254,18 +256,18 @@ int main(int argc, char *argv[]) if (cmd->in) { // "<" file_in = open(cmd->in, O_RDONLY); - dup2(file_in, 0); + dup2(file_in, STDIN_FILENO); } if (cmd->out) { // ">" file_out = open(cmd->out, O_CREAT | O_TRUNC | O_WRONLY, 0640); - dup2(file_out, 1); + dup2(file_out, STDOUT_FILENO); } if (nb_pipe > 0) - { // "|" présent(s) dans la commande + { // "|" for (int i = 0; i <= nb_pipe; i++) - { // on crée itérativement nb_pipe fils et pipes + { // on créé itérativement nb_pipe fils et pipes if (pipe(pipes[i]) < 0) { // pipe failed ? fprintf(stderr, "ERROR: pipe error, (%d) %s\n", errno, strerror(errno)); @@ -329,13 +331,11 @@ int main(int argc, char *argv[]) } for (int i = 0; i <= nb_pipe; i++) { // on attend chaque sous-fils - pid_t id = waitpid(sous_fils[i], &wait_code, 0); - - if (id == -1) + if (waitpid(sous_fils[i], &wait_code, 0) == -1) { // wait fail ? fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno)); exit(errno); - } + } // TODO: factoriser dans une fonction, cf dessous ? if (wait_code) { // execvp fail ? fprintf(stderr, "ERROR: child failed, (%d) %s\n", wait_code, strerror(wait_code)); @@ -344,7 +344,7 @@ int main(int argc, char *argv[]) exit(0); // on termine le fils } else - { // pas de piping + { // pas de pipes dans la commande execvp(cmd->seq[0][0], cmd->seq[0]); fprintf(stderr, "ERROR: execvp failed, (%d) %s\n", errno, strerror(errno)); exit(errno); // si execvp échoue on exit avec une erreur @@ -359,9 +359,7 @@ int main(int argc, char *argv[]) } else { // foreground - pid_t id = waitpid(pid_fils, &wait_code, 0); - - if (id == -1) + if (waitpid(pid_fils, &wait_code, 0) == -1) { // wait fail ? fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno)); exit(errno); @@ -374,5 +372,6 @@ int main(int argc, char *argv[]) } } + liberer(&jobs); return EXIT_SUCCESS; } \ No newline at end of file