-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_funcs.c
More file actions
67 lines (61 loc) · 1.12 KB
/
env_funcs.c
File metadata and controls
67 lines (61 loc) · 1.12 KB
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
#include "minishell.h"
int dup_envp(t_data *d, const char **envp)
{
int i;
i = 0;
while (envp[i])
i++;
d->env = (char **)malloc((i + 1) * sizeof(char *));
if (!d->env)
return (global_error(d));
i = -1;
while (envp[++i])
{
d->amount_of_alloc_lines = i;
d->env[i] = (char *)malloc((ft_strlen(envp[i]) + 1) * sizeof(char));
if (!d->env[i])
return (global_error(d));
ft_strlcpy(d->env[i], envp[i], ft_strlen(envp[i]) + 1);
}
d->env[i] = NULL;
d->amount_of_alloc_lines++;
return (0);
}
int get_env_val(t_data *d, char *key, char **val)
{
int i;
*val = NULL;
i = -1;
while (d->env[++i])
{
if (!ft_strncmp(key, d->env[i], ft_strlen(key)))
{
*val = ft_strdup(&(d->env[i][ft_strlen(key)]));
if (!*val)
return (1);
return (0);
}
}
return (0);
}
int change_env_val(t_data *d, char *key, char *val)
{
int i;
char *new_line;
char *old_line;
new_line = ft_strjoin(key, val);
if (!new_line)
return (1);
i = -1;
while (d->env[++i])
{
if (!ft_strncmp(key, d->env[i], ft_strlen(key)))
{
old_line = d->env[i];
d->env[i] = new_line;
free(old_line);
return (0);
}
}
return (0);
}