-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.c
91 lines (82 loc) · 2.02 KB
/
env.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
89
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/16 15:23:50 by msoulaim #+# #+# */
/* Updated: 2022/12/16 13:31:31 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *search_env(char *var, t_shell *shell)
{
int i;
char **g_env;
char *tmp;
i = 0;
g_env = shell->env;
tmp = ft_strjoin(var, "=");
while (g_env[i])
{
if (ft_strncmp(tmp, g_env[i], ft_strlen(tmp)) == 0)
{
free(tmp);
return (ft_strdup(g_env[i]));
}
i++;
}
free(tmp);
return (NULL);
}
void print_env(t_shell *shell)
{
int i;
i = 0;
while (shell->env[i] != NULL)
{
ft_putendl(shell->env[i]);
i++;
}
}
char *get_var_env(char *var, t_shell *shell)
{
char *tmp;
char *ret;
tmp = search_env(var, shell);
ret = NULL;
if (tmp)
{
ret = ft_strdup(ft_strchr(tmp, '=') + 1);
free(tmp);
}
return (ret);
}
void env_dup(char **env, t_shell *shell)
{
int i;
char **g_env;
i = 0;
g_env = (char **)malloc(sizeof(char *) * (str2len(env) + 1));
if (g_env == NULL)
{
ft_putendl_fd("malloc failed at env_dup", 2);
exit(EXIT_FAILURE);
}
while (env[i])
{
g_env[i] = ft_strdup(env[i]);
if (g_env[i] == NULL)
{
free_until(g_env, i);
free(g_env);
ft_putendl_fd("malloc failed at env_dup", 2);
exit(EXIT_FAILURE);
}
i++;
}
g_env[i] = NULL;
shell->env = g_env;
shell->last_ret = 0;
}