-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_to_arr.c
98 lines (87 loc) · 1.95 KB
/
env_to_arr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_to_arr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhalid <akhalid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/20 03:18:14 by akhalid #+# #+# */
/* Updated: 2022/03/03 01:33:34 by akhalid ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *get_node(char *key)
{
t_env *env;
env = g_all.env;
if (env)
{
while (env)
{
if (!ft_strcmp(env->key, key))
return (env);
env = env->next;
}
}
return (0);
}
void free_env_arr(void)
{
int i;
i = 0;
while (g_all.env_arr[i])
free(g_all.env_arr[i++]);
free(g_all.env_arr);
}
int env_length(t_env *env)
{
int i;
i = 0;
while (env)
{
i++;
env = env->next;
}
return (i);
}
char *get_env(t_env *env, int index)
{
char *str;
int i;
i = 0;
while (env->next && i < index)
{
env = env->next;
i++;
}
str = NULL;
if (env->value)
{
if (env->value || env->value[0] == '\0')
{
str = ft_strjoin(ft_strdup(env->key), ft_strdup("="));
str = ft_strjoin(str, ft_strdup(env->value));
}
}
return (str);
}
char **env_to_arr(void)
{
t_env *env;
int size;
char **env_arr;
int i;
env = g_all.env;
size = env_length(env);
env_arr = (char **)malloc(sizeof(char *) * (size + 2));
if (!env_arr)
return (0);
i = 0;
while (i < size)
{
env_arr[i] = get_env(env, i);
i++;
}
env_arr[i] = 0;
return (env_arr);
}