forked from Kinnune/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyenv.c
123 lines (112 loc) · 2.54 KB
/
copyenv.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* copyenv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: djames <djames@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/12 16:01:51 by djames #+# #+# */
/* Updated: 2023/07/19 13:42:28 by djames ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_length_word(char **envp)
{
int i;
i = 0;
while (envp[i] != NULL)
{
i++;
}
return (i);
}
void copy_env(char **envp)
{
int lenght;
int i;
i = 0;
lenght = ft_length_word(envp);
g_data.envir = malloc((lenght + 1) * (sizeof(char *)));
if (!g_data.envir)
exit(-1);
g_data.envir[lenght] = NULL;
while (i < lenght)
{
g_data.envir[i] = ft_strdup(envp[i]);
if (!g_data.envir[i])
exit(-1);
i++;
}
if (g_data.flag1 == 0)
{
g_data.flag1 = 1;
remove_string("OLDPWD");
add_string("OLDPWD");
}
}
void aux_remove(void)
{
int lenght;
int i;
char **temp;
i = 0;
lenght = ft_length_word(g_data.envir);
temp = malloc((lenght + 1) * (sizeof(char *)));
if (!temp)
return ;
temp[lenght] = NULL;
while (i < (lenght))
{
temp[i] = ft_strdup(g_data.envir[i]);
i++;
}
free_array(g_data.envir);
copy_env(temp);
free_array(temp);
}
void ft_copy(int j)
{
char **temp;
int i;
int lenght;
i = 0;
lenght = ft_length_word(g_data.envir);
temp = malloc((lenght) * (sizeof(char *)));
if (!temp)
exit(-1);
temp[lenght - 1] = NULL;
while (i < (lenght - 1))
{
if (i >= j)
temp[i] = ft_strdup(g_data.envir[i + 1]);
else
temp[i] = ft_strdup(g_data.envir[i]);
if (!temp[i])
exit(-1);
i++;
}
free_array(g_data.envir);
copy_env(temp);
free_array(temp);
}
int remove_string(char *target_string)
{
int i;
int length;
char *remo;
i = 0;
length = ft_strlen(target_string);
remo = (check_temp(target_string, 1, target_string));
if (remo == NULL)
return (-1);
while (g_data.envir[i] != NULL)
{
if (ft_strncmp(g_data.envir[i], target_string, length) == 0)
{
ft_copy(i);
break ;
}
i++;
}
return (0);
}