-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_to_lst.c
83 lines (73 loc) · 1.89 KB
/
env_to_lst.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_to_lst.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/27 17:25:45 by nvideira #+# #+# */
/* Updated: 2023/03/22 18:32:01 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env_lst *ft_lstlast(t_env_lst *lst)
{
if (!lst)
return (NULL);
while (lst->next != NULL)
{
lst = lst->next;
}
return (lst);
}
void lst_add_back(t_env_lst **lst, t_env_lst *new)
{
t_env_lst *last;
if (!lst)
return ;
if (*lst == NULL)
{
*lst = new;
return ;
}
last = ft_lstlast(*lst);
last->next = new;
new->prev = last;
}
int find_es_or_eol(char *str)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == '=' || str[i] == '\0')
return (i);
i++;
}
return (i);
}
t_env_lst *new_node(char *env)
{
t_env_lst *env_node;
env_node = malloc(sizeof(t_env_lst));
if (!env_node)
return (NULL);
env_node->name = ft_substr(env, 0, find_es_or_eol(env) + 1);
env_node->value = ft_substr(env, find_es_or_eol(env) + 1, ft_strlen(env));
env_node->next = NULL;
env_node->prev = NULL;
return (env_node);
}
t_env_lst *env_to_lst(char **env)
{
int i;
t_env_lst *env_lst;
i = 0;
env_lst = NULL;
while (env[i])
{
lst_add_back(&env_lst, new_node(env[i]));
i++;
}
return (env_lst);
}