forked from MaroIsLife/Minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_one.c
116 lines (107 loc) · 2.12 KB
/
export_one.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
#include "minishell.h"
void ft_expn_add(char *add, t_source *src, char **args)
{
int id;
char **tmp1;
char **tmp2;
id = found_eq(add);
if (id)
{
tmp1 = src->our_envp;
tmp2 = src->export;
src->our_envp = our_realloc(src->our_envp,
sizeof(char *) * (ft_alloc_count(src->our_envp, args) + 2));
src->export = our_realloc(src->export,
sizeof(char *) * (ft_alloc_count(src->export, args) + 2));
src->our_envp[src->lastenv++] = ft_strdup(add);
src->export[src->lastexp++] = ft_strdup(add);
src->export[src->lastexp] = NULL;
src->our_envp[src->lastenv] = NULL;
ft_expn_add_two(tmp1, tmp2, 1, 0);
}
else
{
tmp1 = src->export;
ft_expn_add_else(add, src, args);
ft_expn_add_two(tmp1, NULL, 2, 0);
}
}
void ft_expn_chng_two(char *add, t_source *src)
{
int i;
char **tmp;
if (ft_search(src->our_envp, add))
replace_env(src, add);
else
{
tmp = src->our_envp;
src->our_envp = our_realloc(src->our_envp,
sizeof(char *) * (arg_counter(src->our_envp) + 2));
src->our_envp[src->lastenv++] = ft_strdup(add);
src->our_envp[src->lastenv] = NULL;
i = 0;
while (tmp[i] != NULL && tmp[i][0] != 0)
{
free(tmp[i]);
tmp[i] = NULL;
i++;
}
free(tmp);
}
}
void ft_expn_chng(char *add, t_source *src)
{
int i;
char *s_tmp;
char *see;
i = 0;
while (src->export[i] != NULL)
{
see = src->export[i];
if (is_equal(add, src->export[i]))
{
if (found_eq(add))
{
s_tmp = src->export[i];
src->export[i] = ft_strdup(add);
free (s_tmp);
}
if (i == src->lastexp)
src->export[++i] = NULL;
}
i++;
}
if (found_eq(add))
ft_expn_chng_two(add, src);
}
void ft_set_enxp(char **args, t_source *src)
{
int argn;
int i;
int n;
i = 0;
argn = arg_counter(args);
while (i < argn)
{
n = check_exsyn(args[i]);
if (n)
{
write (2, "Minishell : not a valid identifier\n", 35);
g_global.return_value = 1;
i++;
continue ;
}
if (ft_search(src->export, args[i]))
ft_expn_chng(args[i], src);
else
ft_expn_add(args[i], src, args);
i++;
}
}
void ft_export(char **args, t_source *src)
{
if (args[0] == NULL)
em_export(src);
else
ft_set_enxp(args, src);
}