-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunset.c
99 lines (90 loc) · 2.27 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adel-cor <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/05 13:34:50 by adel-cor #+# #+# */
/* Updated: 2022/04/12 12:14:10 by adel-cor ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char **ms_matrix_remove_line(char **matrix, char *line)
{
int i;
int j;
char **new_matrix;
i = 0;
j = 0;
while (matrix[i])
i++;
new_matrix = malloc(sizeof(char *) * (i + 1));
i = 0;
while (matrix[i])
{
if (ft_strcmp(matrix[i], line) == 0)
i++;
if (matrix[i])
{
new_matrix[j++] = ft_strdup(matrix[i]);
i++;
}
}
new_matrix[j] = NULL;
ft_free_tab(matrix);
return (new_matrix);
}
int check_unset_arg(char *arg)
{
int i;
i = 0;
if (ft_isalpha(arg[i]) == 0 && arg[i] != '_')
{
ft_put_error("unset: '", arg, "': not a valid identifier\n", 2);
return (1);
}
i++;
while (arg[i])
{
if ((ft_isalnum(arg[i]) == 0 && arg[i] != '_') || arg[i] == '=')
{
ft_put_error("unset: '", arg, "': not a valid identifier\n", 2);
return (1);
}
i++;
}
return (0);
}
char **unset_remove(char **env, char *arg)
{
char *line;
line = NULL;
line = ms_get_env(env, arg);
if (line != NULL)
env = ms_matrix_remove_line(env, line);
return (env);
}
int built_unset(char **arg, t_cdata *t_cdata)
{
int i;
char **strings;
int ret;
ret = 0;
i = 0;
while (arg[i])
{
if (check_unset_arg(arg[i]) != 0)
{
i++;
ret = 1;
continue ;
}
strings = ft_splitc(arg[i], '=');
t_cdata->envp = unset_remove(t_cdata->envp, strings[0]);
t_cdata->envp_export = unset_remove(t_cdata->envp_export, strings[0]);
ft_free_tab(strings);
i++;
}
return (ret);
}