-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_utils.c
88 lines (78 loc) · 2.02 KB
/
list_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marlean <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/13 10:53:59 by hcolumbu #+# #+# */
/* Updated: 2022/06/17 11:52:06 by marlean ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *read_the_line(void)
{
char *read_str;
read_str = readline("Myshell 🐚 ");
if (!read_str)
{
ft_putendl_fd("exit", 2);
rl_clear_history();
exit(1);
}
return (read_str);
}
void read_envp(char **env, t_envp **envp_list)
{
int i;
t_envp *new;
i = 0;
*envp_list = envp_new(env[i++]);
while (env[i])
{
new = envp_new(env[i++]);
envp_add_front(envp_list, new);
}
}
t_envp *envp_new(char *content)
{
t_envp *envp;
int len;
int i;
i = 0;
envp = malloc(sizeof(t_envp));
if (!envp)
return (NULL);
len = ft_strlen(content);
while (content[i] != '=')
i++;
envp->key = malloc(sizeof(char) * (i + 1));
envp->value = malloc(sizeof(char) * (len - i + 1));
if (!envp->key || !envp->value)
return (NULL);
ft_strlcpy(envp->key, content, i + 1);
ft_strlcpy(envp->value, &content[i + 1], len - i);
envp->next = NULL;
return (envp);
}
void envp_add_front(t_envp **lst, t_envp *new)
{
if (lst && new)
{
new->next = *lst;
*lst = new;
}
}
void com_add_back(t_com **lst, t_com *new)
{
t_com *elem;
elem = *lst;
if (elem)
{
while (elem->next)
elem = elem->next;
elem->next = new;
}
else
*lst = new;
}