-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_to_envp.c
47 lines (42 loc) · 1.56 KB
/
list_to_envp.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_to_envp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/25 15:58:10 by almelo #+# #+# */
/* Updated: 2023/03/10 12:43:49 by almelo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *join_key_value(char *key, char *value)
{
char *content;
size_t key_len;
size_t value_len;
key_len = ft_strlen(key);
value_len = ft_strlen(value);
content = malloc((key_len + value_len + 2) * sizeof(*content));
ft_memcpy(content, key, key_len + 1);
content[key_len] = '=';
ft_memcpy(content + key_len + 1, value, value_len + 1);
return (content);
}
char **list_to_envp(t_envl *env_lst)
{
char **envp;
t_env *tmp;
size_t i;
envp = malloc((env_lst->length + 1) * sizeof(char *));
tmp = env_lst->head;
i = 0;
while (i < env_lst->length)
{
envp[i] = join_key_value(tmp->key, tmp->value);
tmp = tmp->next;
i++;
}
envp[i] = NULL;
return (envp);
}