-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvp_utils.c
62 lines (56 loc) · 1.67 KB
/
envp_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* envp_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: schakkou <schakkou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/22 21:09:21 by schakkou #+# #+# */
/* Updated: 2024/09/29 21:48:04 by schakkou ### ########.fr */
/* */
/* ************************************************************************** */
#include "init.h"
static void free_vars(char *trimmed_value, char *full_key)
{
free(trimmed_value);
free(full_key);
}
static int get_the_size(t_env *envs)
{
t_env *curr;
int i;
curr = envs;
i = 0;
while (curr)
{
i++;
curr = curr->next;
}
return (i);
}
char **lst_to_envp(t_env *envs)
{
t_env *new;
t_util_vars vars;
char **res;
int size;
size = get_the_size(envs);
res = malloc(sizeof(char *) * (size + 1));
if (!res)
exit(1);
size = 0;
new = envs->next;
while (new)
{
if (new->in_export == FALSE)
{
vars.trimmed_value = ft_strtrim(new->value, "\x03");
vars.full_key = ft_strjoin(new->key, "=");
res[size++] = ft_strjoin(vars.full_key, vars.trimmed_value);
free_vars(vars.trimmed_value, vars.full_key);
}
new = new->next;
}
res[size] = 0;
return (res);
}