-
Notifications
You must be signed in to change notification settings - Fork 0
/
unset.c
63 lines (58 loc) · 1.55 KB
/
unset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 18:00:34 by almelo #+# #+# */
/* Updated: 2023/03/20 22:28:28 by dioda-si ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *remove_env(t_envl *env_lst, char *key)
{
t_env *tmp;
t_env *prev;
tmp = env_lst->head;
prev = NULL;
while (tmp)
{
if (ft_strcmp(tmp->key, key) == 0)
{
if (prev == NULL)
env_lst->head = tmp->next;
else
{
if (tmp->next == NULL)
env_lst->tail = prev;
prev->next = tmp->next;
}
env_lst->length--;
return (tmp);
}
prev = tmp;
tmp = tmp->next;
}
return (NULL);
}
int unset(char **argv, t_envl *env_lst)
{
char *key;
t_env *tmp;
size_t i;
i = 1;
while (argv[i])
{
key = argv[i];
tmp = remove_env(env_lst, key);
if (tmp)
{
free(tmp->key);
free(tmp->value);
free(tmp);
}
i++;
}
return (0);
}