-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_export.c
68 lines (61 loc) · 2.17 KB
/
exec_export.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fulloa-s <fulloa-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/26 15:42:12 by fulloa-s #+# #+# */
/* Updated: 2021/10/26 15:49:35 by fulloa-s ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_export3(char **rebuild, t_list *tmp)
{
int len_norme;
int len2_norme;
ft_memcpy(*rebuild, tmp->old_word, ft_strlen(tmp->old_word));
ft_memcpy(*rebuild + ft_strlen(tmp->old_word), "=", 1);
len_norme = ft_strlen(tmp->old_word);
len2_norme = ft_strlen(tmp->new_word);
ft_memcpy(*rebuild + len_norme + 1, tmp->new_word, len2_norme);
(*rebuild)[len_norme + 1 + ft_strlen(tmp->new_word)] = '\0';
}
void ft_export2(t_term *term, char **cmd)
{
t_list *tmp;
char *rebuild;
tmp = term->env;
while (tmp)
{
if (!ft_strncmp(tmp->old_word, cmd[1], ft_strlen(tmp->old_word)) && \
tmp->old_word[ft_strlen(tmp->old_word)] == '\0' && \
cmd[1][ft_strlen(tmp->old_word)] == '\0')
{
rebuild = malloc(sizeof(char) * (ft_strlen(tmp->old_word) \
+ ft_strlen(tmp->new_word) + 2));
ft_export3(&rebuild, tmp);
ft_matrix_remove_w(term, cmd[1], '=', 1);
ft_matrix_add(&term->info.env_copy, rebuild);
free(rebuild);
}
tmp = tmp->next;
}
}
void ft_export(t_term *term, char **cmd)
{
int i;
i = -1;
if (!cmd[1])
{
while (term->info.env_copy[++i])
printf("declare -x %s\n", term->info.env_copy[i]);
}
else if (handler_env(term, &cmd[1], -1))
{
ft_matrix_remove_w(term, cmd[1], '=', 0);
ft_matrix_add(&term->info.env_copy, cmd[1]);
}
else
ft_export2(term, cmd);
}