environment_del en simple unset

This commit is contained in:
whaffman 2025-02-11 14:35:00 +01:00
parent 5c1d76fe83
commit bb0e90c846
3 changed files with 46 additions and 1 deletions

View File

@ -21,5 +21,6 @@ t_environment *environment_get(t_list *environment, char *name);
void environment_free(t_list *environment);
int environment_parse(char **envp, t_list **environment);
char **environment_get_arr(t_list *environment);
void environment_del(t_list **environment, char *name);
#endif // environment_H

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* environment_del.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/11 14:17:53 by whaffman #+# #+# */
/* Updated: 2025/02/11 14:33:29 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void environment_del(t_list **environment, char *name)
{
t_list *prev;
t_list *current;
t_list *next;
prev = NULL;
next = NULL;
current = *environment;
while (current != NULL)
{
if (ft_strncmp(((t_environment *)current->content)->name, name, ft_strlen(name) + 1) == 0)
{
next = current->next;
free(((t_environment *)current->content)->name);
free(((t_environment *)current->content)->value);
free(current->content);
free(current);
if (prev == NULL)
*environment = next;
else
prev->next = next;
return ;
}
prev = current;
current = current->next;
}
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
/* Updated: 2025/02/11 14:09:49 by whaffman ######## odam.nl */
/* Updated: 2025/02/11 14:30:38 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -94,6 +94,8 @@ void simple_builtins(t_minishell *minishell)
}
else if (cmp_value(minishell->tokens, "export"))
builtin_export(minishell);
else if (cmp_value(minishell->tokens, "unset"))
environment_del(&(minishell->environment), ((t_token *)minishell->tokens->next->content)->value);
else
{
path = executor_absolute_path(minishell->environment, ((t_token *)minishell->tokens->content)->value);