-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_to_array.c
65 lines (58 loc) · 1.71 KB
/
env_to_array.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_to_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebalgruu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:39:31 by ebalgruu #+# #+# */
/* Updated: 2021/11/27 01:39:34 by ebalgruu ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int ft_env_lstsize(t_env *envp)
{
t_env *tmp;
int i;
i = 0;
tmp = envp;
while (tmp)
{
i++;
tmp = tmp->next;
}
return (i);
}
static void ft_make_array(t_env *envp, char ***env, int len, t_arg *arg)
{
int i;
char *arr;
i = -1;
while (++i < len)
{
(*env)[i] = ft_strdup_pars(envp->key, arg);
if (*envp->sep)
{
arr = ft_strjoin_pars((*env)[i], envp->sep, arg);
free((*env)[i]);
(*env)[i] = arr;
}
if (*envp->value)
{
arr = ft_strjoin_pars((*env)[i], envp->value, arg);
free((*env)[i]);
(*env)[i] = arr;
}
envp = envp->next;
}
(*env)[i] = NULL;
}
void ft_env_list_to_array(t_env *envp, char ***env, t_arg *arg)
{
int len;
len = ft_env_lstsize(envp);
*env = malloc(sizeof(char *) * (len + 1));
if (!*env)
ft_exit(12, "malloc", arg);
ft_make_array(envp, env, len, arg);
}