-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_envp.c
executable file
·74 lines (68 loc) · 1.07 KB
/
copy_envp.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
#include "minishell.h"
char **copy_env(char **envp)
{
int len;
int i;
char **copy;
int j;
len = 0;
i = -1;
j = -1;
while (envp[len])
len++;
copy = malloc(sizeof(char **) * len);
if (!copy)
return (NULL);
copy[len] = NULL;
while (envp[++j])
{
if (!ft_strncmp("OLDPWD", envp[j], 6))
j++;
copy[++i] = ft_strdup(envp[j]);
}
return (copy);
}
char **change_envp(char **env, char *new_env)
{
int size;
char **var_env;
int i;
i = -1;
size = size_matrix(env) + 1;
var_env = malloc(sizeof(char **) * size + 1);
if (!var_env)
return (NULL);
var_env[size] = NULL;
while (++i < size - 1)
{
var_env[i] = ft_strdup(env[i]);
free(env[i]);
}
var_env[i] = ft_strdup(new_env);
free(env);
free(new_env);
return (var_env);
}
char **rmv_envp(char **env, int i)
{
int j;
int pos;
char **var_env;
j = 0;
pos = 0;
var_env = ft_calloc(sizeof(char **), size_matrix(env));
if (!var_env)
return (NULL);
while (env[pos])
{
if (pos != i)
{
var_env[j] = ft_strdup(env[pos]);
j++;
}
free(env[pos]);
pos++;
}
free(env);
return (var_env);
}