Browse Source

:bug: Debuf redirection all

Loquicom 6 years ago
parent
commit
be18178fd5
6 changed files with 31 additions and 34 deletions
  1. 17 0
      error.c
  2. 9 1
      error.h
  3. 3 3
      mysh.c
  4. 2 13
      parser.c
  5. 0 1
      parser.h
  6. 0 16
      test

+ 17 - 0
error.c

@@ -130,6 +130,23 @@ int redirect_fd(int fd1, int fd2){
     return tmpfd;
 }
 
+int redirect_fd2(int fd1, int fd2){
+    int tmpfd;
+    //On duplique le 1er fd
+    tmpfd = dup(fd1);
+    if(tmpfd == ERR){
+        addperror("Erreur pendant la duplication");
+        return ERR;
+    }
+    //On fait en sorte que fd1 soit lié au fichier de fd2
+    if(dup2(fd2, fd1) == ERR){
+        addperror("Erreur pendant le changement de fd");
+        return ERR;
+    }
+    //On retourne le nouveau fd de fd1
+    return tmpfd;
+}
+
 void error_init(){
     //Creation nom fichier puis initialisation
     char filename[19];

+ 9 - 1
error.h

@@ -60,11 +60,19 @@ extern Error error;
 /**
  * Change un fd par la valeur d'un autre fd
  * @param int fd accueil
- * @param int fd source
+ * @param int fd source fermé par la fonction
  * @return int Le nouveau fd du fichier qui avait le fd d'accueil
  */
 int redirect_fd(int, int);
 
+/**
+ * Change un fd par la valeur d'un autre fd
+ * @param int fd accueil
+ * @param int fd source
+ * @return int Le nouveau fd du fichier qui avait le fd d'accueil
+ */
+int redirect_fd2(int, int);
+
 /**
  * Initialise la gestion d'erreur
  * Le log sera nommé err-timestamp.log

+ 3 - 3
mysh.c

@@ -247,19 +247,19 @@ int run(CommandTab ct){
         }
         //Effectue les redirections IO
         if(c->input != STDIN){
-            infd = redirect_fd(STDIN, c->input);
+            infd = redirect_fd2(STDIN, c->input);
             if(infd == ERR){
                 return SHELL_FAIL;
             }
         }
         if(c->output != STDOUT){
-            outfd = redirect_fd(STDOUT, c->output);
+            outfd = redirect_fd2(STDOUT, c->output);
             if(outfd == ERR){
                 return SHELL_FAIL;
             }
         }
         if(c->error != STDERR){
-            errfd = redirect_fd(STDERR, c->error);
+            errfd = redirect_fd2(STDERR, c->error);
             if(errfd == ERR){
                 return SHELL_FAIL;
             }

+ 2 - 13
parser.c

@@ -135,8 +135,6 @@ int get_command(Command* c, char* line){
     c->input = STDIN;
     c->output = STDOUT;
     c->error = STDERR;
-    c->erase[0] = false;
-    c->erase[1] = false;
     //Retour
     return length + separator;
 }
@@ -149,6 +147,7 @@ int get_command(Command* c, char* line){
  * @return int SHELL_OK si réussite, SHELL_ERR sinon
  */
 int set_io(Command* c, char* filename, int redir){
+    printf("Redir : %d\n", redir);
     //Declaration variable
     int file;
     //Si fichier existe et on supprime
@@ -195,9 +194,6 @@ int set_io(Command* c, char* filename, int redir){
                 }
             }
             c->output = file;
-            if(redir == SHELLRE_OUT){
-                c->erase[STDOUT - 1] = true;
-            }
             break;
         case SHELLR_ERR:
         case SHELLRE_ERR:
@@ -209,9 +205,6 @@ int set_io(Command* c, char* filename, int redir){
                 }
             }
             c->error = file;
-            if(redir == SHELLRE_ERR){
-                c->erase[STDERR - 1] = true;
-            }
             break;
         case SHELLR_ALL:
         case SHELLRE_ALL:
@@ -230,10 +223,6 @@ int set_io(Command* c, char* filename, int redir){
             }
             c->output = file;
             c->error = file;
-            if(redir == SHELLRE_ALL){
-                c->erase[STDOUT - 1] = true;
-                c->erase[STDERR - 1] = true;
-            }
             break;
         default :
             serrno = SEREDIRTYPE;
@@ -291,7 +280,7 @@ int set_redirection(Command* c){
                                     return SHELL_ERR;
                                 }
                                 buffer++;
-                                redir = SHELLRE_ALL;
+                                redir = SHELLR_ALL;
                             }
                             //Sinon toujours >>
                             else {

+ 0 - 1
parser.h

@@ -20,7 +20,6 @@ typedef struct {
     int input; //Descripteur de fichier d'entré
     int output; //Descripteur de fichier de sortie
     int error; //Descripteur de fihier d'erreur
-    boolean erase[2]; //Si on efface le fichier
     int next; //Lien avec la prochaine commande
 } Command;
 

+ 0 - 16
test

@@ -1,16 +0,0 @@
-command.c
-error.c
-execute.c
-mysh.c
-parser.c
-str.c
-wildcard.c
-0
-command.c
-error.c
-execute.c
-mysh.c
-parser.c
-str.c
-wildcard.c
-0