Sfoglia il codice sorgente

Ameliortation clean

Loquicom 6 anni fa
parent
commit
93ab6c956a
4 ha cambiato i file con 30 aggiunte e 3 eliminazioni
  1. 0 0
      cat
  2. 5 0
      constante.h
  3. 2 2
      mysh.c
  4. 23 1
      parser.c

+ 0 - 0
cat


+ 5 - 0
constante.h

@@ -13,6 +13,11 @@
 #define SHELL_FAIL 0
 #define SHELL_OK 1
 
+/* --- Fichier --- */
+#define STDIN 0
+#define STDOUT 1
+#define STDERR 2
+
 /* --- Separateur commande --- */
 #define SHELL_END 0 //Aucune autre commande après
 #define SHELL_NONE 1 //Aucun lien entre les 2 commandes

+ 2 - 2
mysh.c

@@ -29,8 +29,8 @@ int main(int argc, char* argv[]) {
     //Parse les commandes
     a = parse_all_command(&ct);
     printf("Result : %d\n\n", a);
-    
-    
+    //Supprime
+    clean_command(&ct);
     return (EXIT_SUCCESS);
 }
 

+ 23 - 1
parser.c

@@ -7,6 +7,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
 #include "parser.h"
 
 /* --- Fonctions privées --- */
@@ -104,6 +106,9 @@ int get_command(Command* c, char* line){
     strncpy(c->cmd, deb, length);
     c->next = next;
     c->argc = 0;
+    c->input = STDIN;
+    c->output = STDOUT;
+    c->error = STDERR;
     //Trim et supprime l'ancienne chaine
     old = c->cmd;
     c->cmd = rtrim(c->cmd, ' ');
@@ -156,6 +161,7 @@ int parse_all_command(CommandTab* ct){
 }
 
 void clean_command(CommandTab* ct){
+    extern int errno;
     //Vide le tableau
     for(int i = 0; i < ct->length; i++){
         //Si la commande a été parsée on vide les arguments
@@ -165,8 +171,24 @@ void clean_command(CommandTab* ct){
                 free(ct->cmd[i]->argv[j]);
             }
         }
+        //Ferme les fichiers ouverts si besoin
+        if(ct->cmd[i]->input != STDIN){
+            if(close(ct->cmd[i]->input)){
+                fprintf(stderr, "Erreur lors de la fermeture du fichier d'input de %s : %s", ct->cmd[i]->cmd, strerror(errno));
+            }
+        }
+        if(ct->cmd[i]->output != STDOUT){
+            if(close(ct->cmd[i]->output)){
+                fprintf(stderr, "Erreur lors de la fermeture du fichier d'output de %s : %s", ct->cmd[i]->cmd, strerror(errno));
+            }
+        }
+        if(ct->cmd[i]->error != STDERR){
+            if(close(ct->cmd[i]->error)){
+                fprintf(stderr, "Erreur lors de la fermeture du fichier d'error de %s : %s", ct->cmd[i]->cmd, strerror(errno));
+            }
+        }
         //Supprime la ligne de commande
-        free(ct->cmd[i]->cmd);
+        free(ct->cmd[i]->cmd);   
         //Supprime la structure
         free(ct->cmd[i]);
     }