-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_builtins3.c
116 lines (105 loc) · 2.66 KB
/
s_builtins3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* s_builtins3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebalgruu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:29:22 by ebalgruu #+# #+# */
/* Updated: 2021/11/27 01:29:25 by ebalgruu ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_env *ft_one_lst(t_env *env, t_arg *arg)
{
t_env *one;
one = malloc(sizeof(t_env));
if (!one)
ft_exit(12, "malloc", arg);
one->key = ft_strdup_pars(env->key, arg);
one->sep = ft_strdup_pars(env->sep, arg);
one->value = ft_strdup_pars(env->value, arg);
one->next = NULL;
return (one);
}
static int if_not_cmd1(t_env *env)
{
t_env *tmp;
tmp = env;
while (tmp)
{
if (tmp->sep[0])
printf("declare -x %s%s\"%s\"\n", (tmp)->key, \
(tmp)->sep, (tmp)->value);
else
printf("declare -x %s%s%s\n", (tmp)->key, \
(tmp)->sep, (tmp)->value);
tmp = (tmp)->next;
}
return (0);
}
void ft_search_dups(t_env *env, t_env *new, t_arg *arg)
{
t_env *tmp2;
while (new)
{
tmp2 = env;
while (tmp2)
{
if (!ft_strcmp(new->key, tmp2->key))
{
if (new->sep[0] && !tmp2->sep[0])
{
free(tmp2->sep);
tmp2->sep = ft_strdup_pars(new->sep, arg);
}
free(tmp2->value);
tmp2->value = ft_strdup_pars(new->value, arg);
break ;
}
else
tmp2 = tmp2->next;
}
if (!tmp2)
ft_env_lst_add_back(&env, ft_one_lst(new, arg));
new = new->next;
}
}
void free_new(t_env *new)
{
t_env *tmp;
while (new)
{
tmp = new;
new = new->next;
free(tmp->key);
free(tmp->sep);
free(tmp->value);
free(tmp);
}
}
int ft_export(t_cmd *cmd, t_arg *arg)
{
int i;
t_env *new;
arg->errnum = 0;
i = 0;
new = NULL;
if (!cmd->cmd[1])
return (if_not_cmd1(arg->envp));
while (cmd->cmd[++i])
{
if (ft_isalpha(cmd->cmd[i][0]))
ft_env_lst_add_back(&new, ft_env_lst_new(cmd->cmd[i], arg));
else
{
arg->errnum = 1;
write(2, "export: `", 9);
write(2, cmd->cmd[i], ft_strlen(cmd->cmd[i]));
write(2, "': not a valid identifier\n", 26);
}
}
ft_search_dups(arg->envp, new, arg);
free_new(new);
return (arg->errnum);
}