-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_print_env.c
executable file
·113 lines (103 loc) · 1.77 KB
/
utils_print_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
#include "minishell.h"
int exec_exports(char *cmd, t_env *envp)
{
char *var;
char *value;
char *new_var;
char *tmp;
if (!cmd)
return (0);
var = get_var(cmd);
value = get_value(cmd);
if (!var)
return (0);
if (!value)
value = ft_strdup("=");
exec_unset(var, envp);
new_var = ft_strjoin(var, "");
tmp = new_var;
new_var = ft_strjoin(new_var, value);
free(tmp);
new_var = ft_new_trim(new_var);
envp->env = change_envp(envp->env, new_var);
freetwo_ptrs(&var, &value);
return (0);
}
char *ft_get_env(char **envp, char *var)
{
int i;
i = 0;
while (envp[i])
{
if (!ft_strncmp(envp[i], var, ft_strlen(var))
&& envp[i][ft_strlen(var)] == '=')
break ;
i++;
}
if (envp[i])
return (ft_substr(envp[i], ft_strlen(var) + 1, ft_strlen(envp[i])
- ft_strlen(var) - 1));
return (NULL);
}
char *get_var(char *cmd)
{
int i;
char *var;
int j;
i = 0;
j = -1;
while (cmd[i] && cmd[i] != '=')
i++;
if (i == 0 || !cmd[i])
return (NULL);
var = malloc(sizeof(char *) * i + 1);
if (!var)
return (NULL);
if (ft_ismetachar(cmd[j]) || ft_isdigit(cmd[j]))
{
ft_free(&var);
return (NULL);
}
while (++j < i && cmd[j])
var[j] = cmd[j];
var[j] = '\0';
var = ft_new_trim(var);
return (var);
}
char *get_value(char *cmd)
{
int i;
char *value;
char *tmp;
i = 0;
value = NULL;
while (cmd[i])
{
if (cmd[i] == '=' && cmd[i + 1])
{
value = ft_strdup(&cmd[i + 1]);
value = ft_new_trim(value);
tmp = value;
value = ft_strjoin("=", value);
ft_free(&tmp);
return (value);
}
i++;
}
return (value);
}
void exec_unset(char *cmd, t_env *envp)
{
int i;
int pos;
i = 0;
pos = -1;
while (envp->env[i])
{
if (!ft_strncmp(cmd, envp->env[i], ft_strlen(cmd)))
pos = i;
i++;
}
if (pos >= 0)
envp->env = rmv_envp(envp->env, pos);
}