From f144aa5b6a85ea1a3a084825f165aa1739f06996 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Thu, 27 Jun 2024 13:32:29 +0200 Subject: [PATCH] fix: free empty variables --- dev/TODO | 1 - src/main.c | 9 +++++---- src/parse_command.c | 28 +++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/dev/TODO b/dev/TODO index 6b5cb89..81d685f 100644 --- a/dev/TODO +++ b/dev/TODO @@ -9,7 +9,6 @@ - env (with no options or arguments) - exit (with no options) - fix - - empty variables aren't freed - the builtins should get all arguments and check if they're good (unnecessary part) diff --git a/src/main.c b/src/main.c index 7109804..59e6700 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/06/24 17:30:19 by mcolonna ### ########.fr */ +/* Updated: 2024/06/27 13:15:27 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,15 +20,16 @@ static t_list variables_from_envp(t_env env, char *const envp[]) int i; r = list_createempty(env.mc_global); - i = 0; - while (envp[i]) + i = -1; + while (envp[++i]) { splitted = str_split(fatal_error, env.mc_global, envp[i], "="); + if (!splitted[0] || ! splitted[1]) + continue ; var = mem_alloc(fatal_error, env.mc_global, sizeof(t_variable)); var->name = splitted[0]; var->value = splitted[1]; list_add(fatal_error, &r, var); - i++; } return (r); } diff --git a/src/parse_command.c b/src/parse_command.c index 8e544bf..64ff68f 100644 --- a/src/parse_command.c +++ b/src/parse_command.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */ -/* Updated: 2024/06/25 15:39:55 by mcolonna ### ########.fr */ +/* Updated: 2024/06/27 13:30:12 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,21 +14,39 @@ #define SYMBOL_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" +static void variables_free(t_list *variables, t_list_element *el) +{ + if (el->previous) + el->previous->next = el->next; + else + variables->first = el->next; + if (el->next) + el->next->previous = el->previous; + else + variables->last = el->previous; + variables->size--; + mem_free(el->value); + mem_free(el); +} + // Return a pointer to the variable 'name'. -// If the variable doesn't exist, it's created with an empty value. // (An undefined variable is considered of an empty value) static t_variable *variables_find(t_list *variables, const char *name) { t_list_element *el; + t_list_element *next; t_variable *r; el = variables->first; while (el) { + next = el->next; r = (t_variable *)el->value; if (str_eq(r->name, name)) return (r); - el = el->next; + if (str_eq(r->value, "")) + variables_free(variables, el); + el = next; } r = mem_alloc(fatal_error, variables->mc, sizeof(t_variable)); r->name = str_dup(fatal_error, variables->mc, name); @@ -40,8 +58,8 @@ static t_variable *variables_find(t_list *variables, const char *name) // Set a variable to a new value. void variables_set(t_list *variables, const t_variable var) { - variables_find(variables, var.name)->value = - str_dup(fatal_error, variables->mc, var.value); + variables_find(variables, var.name)->value = ( + str_dup(fatal_error, variables->mc, var.value)); } // Get the value of a variable from its name.