From 774161d7ff30327c88be9bd39b87df2c7668b25b Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 16 May 2017 22:14:06 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Optimisation=20de=20m=C3=A9moire=20et=20cor?= =?UTF-8?q?rection=20de=20l'orde=20des=20op=C3=A9rateurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_commands.c | 2 +- fish_shell/fish_core.c | 32 ++++++++++++++++++++------------ fish_shell/fish_types.h | 2 ++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/fish_shell/fish_commands.c b/fish_shell/fish_commands.c index 1b64373..039d684 100644 --- a/fish_shell/fish_commands.c +++ b/fish_shell/fish_commands.c @@ -55,7 +55,7 @@ int fishHelp(WordArray *args) { int fishExit(WordArray *args) { args->size = args->size; - exit(EXIT_SUCCESS); + return EXIT_SIGNAL; } diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index 48dac22..b473d30 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -20,14 +20,14 @@ void fishLoop(Settings * settings){ printf("%s", settings->PS1); line = fishReadLine(); - splited = split(line, (char*) FISH_TOKENS); + splited = split(line, (char*) FISH_TOKENS); splited = fishExpand(splited); status = fishExecute(splited); free(line); - } while(status); + } while(status != EXIT_SIGNAL); } int countSeparators(char *string, char *separators) { @@ -152,24 +152,32 @@ int fishExecute(WordList *list) { WordList *splited = NULL; shell_operator op = NONE; WordArray *array = NULL; + int signal = 1; splited = parseWordList(list, &op); array = wordListToWordArray(list); + signal = loadRightCommand(array); + if (signal == EXIT_SIGNAL){ + if (splited != NULL) freeWordList(splited); + if (array != NULL) freeWordArray(array); + return signal; + } switch (op) { case AND: - if (!loadRightCommand(array)) fishExecute(splited); - else freeWordList(splited); + if (!signal) signal = fishExecute(splited); + else { + if (splited != NULL) freeWordList(splited); + } break; case OR: - loadRightCommand(array); - fishExecute(splited); + signal = fishExecute(splited); break; default: - loadRightCommand(array); + break; } + return signal; - return 1; } int loadRightCommand(WordArray *array){ @@ -185,12 +193,12 @@ int loadRightCommand(WordArray *array){ WordList * parseWordList(WordList *list, shell_operator *an_operator) { char *op_str[] = { - (char*) "&&", - (char*) "||" + (char*) "||", + (char*) "&&" }; shell_operator op[] = { - AND, - OR + OR, + AND }; WordList *newList = NULL; int max = sizeof(op_str) / sizeof(char*); diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h index 59afdb7..860efa5 100644 --- a/fish_shell/fish_types.h +++ b/fish_shell/fish_types.h @@ -5,6 +5,8 @@ #ifndef FISH_FISH_TYPES_H #define FISH_FISH_TYPES_H +#define EXIT_SIGNAL -100 + /* Custom types */ typedef enum { From 34b8cbe3cf46c53e70bcb96d78a6586d43d73fd5 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 17 May 2017 18:48:16 +0200 Subject: [PATCH 2/4] En route vers le 0 leak memory --- CMakeLists.txt | 2 ++ fish_shell/fish_utils.c | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 704702c..4c52234 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,8 +44,10 @@ include_directories( "${source_dir}/googlemock/include" ) + set(CMAKE_C_STANDARD 99) set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g") set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h) add_executable(fish ${SOURCE_FILES}) diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index b76511a..078bd20 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -110,12 +110,11 @@ WordArray *wordListToWordArray(WordList *list) { if (array->words == NULL) crash(); while (current != NULL){ - array->words[i] = current->word; - current->word = NULL; + array->words[i] = strdup(current->word); current = current->next; i++; } - array->words[i] = NULL; + array->words[array->size] = NULL; } freeWordList(list); @@ -125,7 +124,7 @@ WordArray *wordListToWordArray(WordList *list) { WordList *wordArrayToWordList(WordArray *array) { WordList *list = createWordList(); - int i = 0; + int i; for (i=0; isize; i++) addWordList(list, array->words[i]); From cb2dc7f7479d81b6ac762c961f3e4865f30d5236 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 18 May 2017 01:30:46 +0200 Subject: [PATCH 3/4] Tryhard pour le 0 leak --- fish_shell/fish_core.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index b473d30..922b4f6 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -145,6 +145,7 @@ int fishLoad(WordArray *array) { } while (!WIFEXITED(status) && !WIFSIGNALED(status)); if (status) fprintf(stderr, "%s\n", getInsult()); } + freeWordArray(array); return status; } From 1ca783f73de655a814bf939ff8265c17fdfcb3cf Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 18 May 2017 01:40:26 +0200 Subject: [PATCH 4/4] 0 leak Memory mother fuckers --- fish_shell/fish_commands.c | 9 +++++---- fish_shell/fish_utils.c | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/fish_shell/fish_commands.c b/fish_shell/fish_commands.c index 039d684..7c3e40d 100644 --- a/fish_shell/fish_commands.c +++ b/fish_shell/fish_commands.c @@ -37,12 +37,12 @@ int fishCd(WordArray *args) { perror("fish"); } } - return 0; + freeWordArray(args); + return EXIT_SUCCESS; } int fishHelp(WordArray *args) { int i; - args->size = args->size; printf("Bartuccio Antoine, Amalvy Arthur, Yann Chevanton\n"); printf("Tape tes putains de noms de programmes et tes arguments de merde et tabasse ENTER !\n"); printf("Les commandes suivantes sont internes :\n"); @@ -50,11 +50,12 @@ int fishHelp(WordArray *args) { printf("\t%s\n", builtinCommandsStr[i]); } printf("Et sinon pour le reste, RTFM !"); - return 0; + freeWordArray(args); + return EXIT_SUCCESS; } int fishExit(WordArray *args) { - args->size = args->size; + freeWordArray(args); return EXIT_SIGNAL; } diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index 078bd20..9876aff 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -30,7 +30,7 @@ char *getInsult(){ int picked = 0; char *insults[] = { (char *) "Apprend à écrire crétin !", - (char *) "Bolos !", + (char *) "Boloss !", (char *) "Mois aussi je sais écrire de la merde, pourtant je le fait pas !", (char *) "Oh ! Une erreur ! Comme ta vie en fait...", (char *) "Nul !",