-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_array_convert.c
65 lines (60 loc) · 1.76 KB
/
env_array_convert.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_array_convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aoizel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/09 13:57:30 by aoizel #+# #+# */
/* Updated: 2024/01/11 16:09:58 by aoizel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void env_array_keyvalue(char *to_return, t_envlst *env_list)
{
size_t j;
size_t k;
j = 0;
k = 0;
while (env_list->key[j])
{
to_return[k] = env_list->key[j];
k++;
j++;
}
to_return[k++] = '=';
j = 0;
while (env_list->value[j])
{
to_return[k] = env_list->value[j];
k++;
j++;
}
to_return[k] = '\0';
}
char **env_array_convert(t_envlst *env_list)
{
char **to_return;
ssize_t i;
to_return = malloc(sizeof(char *) * (env_lst_len(env_list) + 1));
if (!to_return)
return (NULL);
i = 0;
while (env_list != NULL)
{
to_return[i] = malloc(sizeof(char)
* (ft_strlen(env_list->key) + ft_strlen(env_list->value) + 2));
if (!to_return[i])
{
while (i >= 0)
free(to_return[i--]);
free(to_return);
return (NULL);
}
env_array_keyvalue(to_return[i], env_list);
i++;
env_list = env_list->next;
}
to_return[i] = NULL;
return (to_return);
}