|
@@ -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]);
|
|
|
}
|