-
Notifications
You must be signed in to change notification settings - Fork 0
/
unset.c
101 lines (93 loc) · 2.77 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 11:31:23 by brenaudo #+# #+# */
/* Updated: 2023/01/12 13:10:50 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static bool is_valid_identifier(char *identifier)
{
int i;
char *err;
i = 1;
if (!ft_isalpha(identifier[0]) && identifier[0] != '_')
{
err = ft_strjoin("minishell: unset: ", identifier);
err = ft_strjoinf(err, ": not a valid identifier\n");
ft_putstr_fd(err, 2);
free(err);
return (false);
}
while (identifier[i])
{
if (!ft_isalnum(identifier[i]) && identifier[i] != '_')
{
err = ft_strjoin("minishell: unset: ", identifier);
err = ft_strjoinf(err, ": not a valid identifier\n");
ft_putstr_fd(err, 2);
free(err);
return (false);
}
i++;
}
return (true);
}
void unset_parent_arg(char **cmd_split, int index)
{
char *name;
t_list *env_cpy;
t_list *tmp;
env_cpy = *g_glob->envp;
name = ft_strjoin(cmd_split[index], "=");
if (env_cpy->next == NULL \
&& !ft_strncmp(env_cpy->content, name, ft_strlen(name)) \
&& !ft_strncmp(env_cpy->content, cmd_split[index], \
ft_strlen(cmd_split[index]) + 1))
return (ft_lstdelone(env_cpy, free));
while (env_cpy && env_cpy->next \
&& ft_strncmp(env_cpy->next->content, name, ft_strlen(name))
&& ft_strncmp(env_cpy->next->content, cmd_split[index], \
ft_strlen(cmd_split[index]) + 1))
env_cpy = env_cpy->next;
tmp = env_cpy->next;
if (tmp != NULL)
{
env_cpy->next = tmp->next;
ft_lstdelone(tmp, free);
}
g_glob->exit_ret = 0;
free(name);
}
bool unset_parent(void)
{
int i;
char **cmd_split;
if (ft_lstsize(*g_glob->cmds) == 1)
{
cmd_split = ft_split_quotes(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (cmd_split && cmd_split[0] && !ft_strncmp(cmd_split[0], "unset", 6))
{
i = 0;
while (cmd_split[++i])
if (is_valid_identifier(cmd_split[i]))
unset_parent_arg(cmd_split, i);
free_tab(cmd_split);
return (true);
}
free_tab(cmd_split);
}
return (false);
}
void unset(char *cmd, int fd_out)
{
(void)cmd;
close(fd_out);
clean_exit(NULL);
exit(0);
}