Преглед на файлове

:sparkles: Redirection et pipe fonctionnels

Loquicom преди 6 години
родител
ревизия
51825cd9aa
променени са 1 файла, в които са добавени 16 реда и са изтрити 20 реда
  1. 16 20
      mysh.c

+ 16 - 20
mysh.c

@@ -18,7 +18,6 @@
 #include "command.h"
 #include "execute.h"
 #include "mysh.h"
-#include "pipe_cste.h"
 
 /* --- Extern --- */
 extern Error error;
@@ -214,30 +213,26 @@ int run(CommandTab ct){
     }
     //Sinon execution de chaque commande
     Command* c;
-    int* tube[ct.length];
+    int tube[ct.length][2];
     int tubepos = 0;
     int infd = -1, outfd = -1, errfd = -1;
+    boolean bpipe = false;
     //Parcours les commandes
     for(int i = 0; i < ct.length; i++){
         c = ct.cmd[i];
         printf("Cmd : %s\n", c->name);
         //Si pipe creation d'un fichier commun
-        if(c->next == SHELL_PIPE){
-            int tab[2];
-            tube[tubepos] = tab;
-            //Si il n'y a pas deja une redirection de sortie
-            if(c->output == STDOUT){
-                //Creation tube
-                if(pipe(tube[tubepos]) == ERR){
-                    addperror("Impossible de créer un tube");
-                    return SHELL_FAIL;
-                }
-                //Tube non bloquant
-                c->output = tube[tubepos][TUBE_ECRITURE];
-                //Redirige l'entrée du suivant
-                if(ct.cmd[i + 1]->input == STDIN){
-                    ct.cmd[i + 1]->input = tube[tubepos][TUBE_LECTURE];
-                }
+        if(c->next == SHELL_PIPE && c->output == STDOUT){
+            bpipe = true;
+            //Creation tube
+            if(pipe(tube[tubepos]) == ERR){
+                addperror("Impossible de créer un tube");
+                return SHELL_FAIL;
+            }
+            //Redirection
+            c->output = tube[tubepos][TUBE_ECRITURE];
+            if(ct.cmd[i + 1]->input == STDIN){
+                ct.cmd[i + 1]->input = tube[tubepos][TUBE_LECTURE];
             }
         }
         //Effectue les redirections IO
@@ -288,8 +283,9 @@ int run(CommandTab ct){
             }
         }
         //Fermeture tube
-        if(c->next == SHELL_PIPE){
-            if(close(tube[tubepos++][TUBE_ECRITURE]) == ERR){
+        if(bpipe){
+            bpipe = false;
+            if(close(outfd) == ERR){
                 addperror("Impossible de fermer tube ecriture");
                 return SHELL_FAIL;
             }