fix: free empty variables

This commit is contained in:
mcolonna 2024-06-27 13:32:29 +02:00
parent 446a7655b6
commit f144aa5b6a
3 changed files with 28 additions and 10 deletions

View file

@ -9,7 +9,6 @@
- env (with no options or arguments) - env (with no options or arguments)
- exit (with no options) - exit (with no options)
- fix - fix
- empty variables aren't freed
- the builtins should get all arguments and check if they're good - the builtins should get all arguments and check if they're good
(unnecessary part) (unnecessary part)

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ /* 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; int i;
r = list_createempty(env.mc_global); r = list_createempty(env.mc_global);
i = 0; i = -1;
while (envp[i]) while (envp[++i])
{ {
splitted = str_split(fatal_error, env.mc_global, 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 = mem_alloc(fatal_error, env.mc_global, sizeof(t_variable));
var->name = splitted[0]; var->name = splitted[0];
var->value = splitted[1]; var->value = splitted[1];
list_add(fatal_error, &r, var); list_add(fatal_error, &r, var);
i++;
} }
return (r); return (r);
} }

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:47:40 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_" #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'. // 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) // (An undefined variable is considered of an empty value)
static t_variable *variables_find(t_list *variables, const char *name) static t_variable *variables_find(t_list *variables, const char *name)
{ {
t_list_element *el; t_list_element *el;
t_list_element *next;
t_variable *r; t_variable *r;
el = variables->first; el = variables->first;
while (el) while (el)
{ {
next = el->next;
r = (t_variable *)el->value; r = (t_variable *)el->value;
if (str_eq(r->name, name)) if (str_eq(r->name, name))
return (r); 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 = mem_alloc(fatal_error, variables->mc, sizeof(t_variable));
r->name = str_dup(fatal_error, variables->mc, name); 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. // Set a variable to a new value.
void variables_set(t_list *variables, const t_variable var) void variables_set(t_list *variables, const t_variable var)
{ {
variables_find(variables, var.name)->value = variables_find(variables, var.name)->value = (
str_dup(fatal_error, variables->mc, var.value); str_dup(fatal_error, variables->mc, var.value));
} }
// Get the value of a variable from its name. // Get the value of a variable from its name.