-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
78 lines (69 loc) · 1.96 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kblack <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/13 15:27:10 by kblack #+# #+# */
/* Updated: 2019/03/29 15:45:54 by kblack ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
** Get environment variables and stores them in an array
*/
void get_str_arr(int n, char **envp, t_shell *sh)
{
char **tmp;
int i;
i = 0;
tmp = (char **)malloc((n + 1) * sizeof(char*));
while (envp[i])
{
tmp[i] = ft_strdup(envp[i]);
i++;
}
tmp[i] = NULL;
free(sh->arr);
sh->arr = tmp;
}
/*
** Creates a linked list storing environment
** variables in key=value pairs
*/
void get_env_vars(t_shell *sh, char **envp)
{
int i;
size_t sub;
t_env *node;
t_env *tmp;
i = -1;
tmp = NULL;
while (envp[++i])
{
node = new_node();
sub = ft_strchr(envp[i], '=') - envp[i];
node->key = ft_strndup(envp[i], sub);
node->value = ft_strdup(envp[i] + sub + 1);
if (sh->env_info != NULL)
tmp->next = node;
else
sh->env_info = node;
tmp = node;
}
get_str_arr(i, envp, sh);
tmp->next = NULL;
}
int main(int argc, char **argv, char **envp)
{
t_shell sh;
(void)argc;
(void)argv;
ft_printf(BOLDMAGENTA "~*~*~ Starting minishell ~*~*~\n\n" RESET);
ft_bzero(&sh, sizeof(sh));
get_env_vars(&sh, envp);
mini_loop(&sh);
free_struct(&sh);
return (0);
}