-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunset.c
85 lines (76 loc) · 2.08 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/07 15:47:35 by bmachado #+# #+# */
/* Updated: 2021/12/14 17:05:13 by bmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void fill_empty_slots(t_shell *tab, int slot)
{
int save;
t_vars *tmp;
save = 0;
if (tab->env_vars[slot + 1])
save = slot + 1;
else
return ;
tab->env_vars[slot] = tab->env_vars[save];
while (tab->env_vars[slot + 1])
{
tmp = tab->env_vars[slot];
tab->env_vars[slot] = tab->env_vars[slot + 1];
if (!tab->env_vars[slot + 2])
tmp = NULL;
tab->env_vars[slot + 1] = tmp;
slot++;
}
}
static void unset_env(char *key, t_shell *table)
{
t_vars *current;
int i;
i = get_position_env(table, key);
current = table->env_vars[i];
if (!current)
return ;
else if (ft_strcmp(current->key, key) == 0)
{
free_vars(current);
table->env_vars[i] = NULL;
fill_empty_slots(table, i);
table->env_count--;
}
}
void unset(char *key, t_shell *table)
{
t_vars *current;
int i;
i = get_position(table, key);
current = table->local[i];
if (!current)
return ;
else if (ft_strcmp(current->key, key) == 0)
{
free_vars(current);
table->local[i] = NULL;
current = NULL;
}
}
void unset_function(t_shell *table, char **cmds)
{
int i;
i = 1;
while (cmds[i])
{
if (is_in_env(cmds[i], table))
unset_env(cmds[i], table);
else
unset(cmds[i], table);
i++;
}
}