-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_utils.c
78 lines (70 loc) · 1.92 KB
/
env_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: btaha <btaha@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 17:13:32 by btaha #+# #+# */
/* Updated: 2023/01/03 17:38:47 by btaha ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/minishell.h"
void free_env(t_env *env)
{
if (!env)
return ;
free(env->name);
free(env->value);
free(env);
}
char *ft_getenv(char *name)
{
t_list *lst;
t_env *env;
if (!name || !*name || ft_strchr(name, '='))
return (NULL);
lst = g_shell.env;
while (lst)
{
env = lst->content;
if (!ft_strcmp(env->name, name))
return (env->value);
lst = lst->next;
}
return (NULL);
}
t_env *set_var(char *name, char *value)
{
t_env *env;
char *n;
char *v;
env = malloc(sizeof(t_env));
n = ft_strdup(name);
v = NULL;
if (value)
v = ft_strdup(value);
if (!env || !name || (value && !v))
return (free(env), free(n), free(v), NULL);
env->print = 1;
return (env->name = n, env->value = v, env);
}
int init_env(char *envp[])
{
int i;
t_env *line;
t_list *lst;
char **tab;
i = 0;
while (envp[i])
{
tab = ft_split(envp[i], '=');
line = set_var(tab[0], tab[1]);
lst = ft_lstnew(line);
if (!line || !lst)
return (free_env(line), free(lst), -1);
ft_lstadd_back(&g_shell.env, lst);
i++;
}
return (free(tab), 0);
}