-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_env.c
128 lines (118 loc) · 2.96 KB
/
builtin_env.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: llacaze <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/02 20:37:45 by llacaze #+# #+# */
/* Updated: 2018/02/26 13:41:35 by llacaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void env_line(t_info *info, char *elem)
{
char **str;
int line;
line = 0;
if (info->line_tab[2] == NULL)
{
write(2, "env: option requires an argument -- u\n", 38);
return ;
}
while (info->env[line])
{
str = ft_strsplit(info->env[line], '=');
if (ft_strcmp(str[0], elem) != 0)
{
ft_putstr(info->env[line]);
write(1, "\n", 1);
}
line++;
}
}
t_info *get_env_num(t_info *info, char *elem, char *new_elem)
{
char **str;
int line;
line = 0;
while (info->env[line])
{
str = ft_strsplit(info->env[line], '=');
if (ft_strcmp(str[0], elem) == 0)
{
info->env[line] = new_line(info->env[line], elem, new_elem);
free_tab(str);
info->repl = 1;
return (info);
}
free_tab(str);
line++;
}
info->repl = 0;
return (info);
}
char **new_env(char **env, char *elem, char *new_elem, char *test)
{
char **str;
int line;
line = 0;
while (env[line] != NULL && ft_strcmp(env[line], "NULL") != 0)
{
str = ft_strsplit(env[line], '=');
if (str && str[0] && ft_strcmp(str[0], elem) == 0)
{
env[line] = new_line(env[line], elem, new_elem);
free_tab(str);
return (env);
}
free_tab(str);
line++;
if (env[line] == NULL)
break ;
}
env[line] = ft_strdup(test);
env[line + 1] = NULL;
return (env);
}
void env_equal(t_info *info)
{
char **tmp;
int line;
int i;
i = 1;
tmp = copy_tab_o(info->env);
info->env_n = copy_tab(info->env_n, info->env);
while (info->line_tab[i])
{
i = compare(info, i, 2);
line = i;
info = err_one(info, i, tmp);
if (info->do_ret == 1)
return ;
i = compare(info, i, 2);
while (info->line_tab[i] && ft_check_char(info->line_tab[i], '=') != 0)
{
if (info->line_tab[i] != NULL)
tmp = new_tab(i, info, tmp);
i++;
}
info = check_i(info, tmp, i, line);
if (info->do_ret == 1)
return ;
}
}
void opt_env(t_info *info)
{
if (info->line_tab[1] == NULL)
{
builtin_env_one(info->env);
return ;
}
else if (ft_strcmp(info->line_tab[1], "-u") == 0)
env_line(info, info->line_tab[2]);
else if (ft_strcmp(info->line_tab[1], "-i") == 0)
env_i(info);
else
env_equal(info);
}