-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.c
124 lines (114 loc) · 2.69 KB
/
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
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
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhalid <akhalid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/18 00:56:44 by aelkhalo #+# #+# */
/* Updated: 2022/03/02 02:30:04 by akhalid ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_env2(void)
{
t_env *tmp;
tmp = g_all.env;
while (tmp != NULL)
{
if (tmp->value == NULL)
printf("declare -x %s\n", tmp->key);
else
printf("declare -x %s=\"%s\"\n", tmp->key, tmp->value);
tmp = tmp->next;
}
g_all.exit_status = 0;
}
void add_replace(char **tmp)
{
if (!key_exist(tmp[0]))
{
if (tmp[1])
add_envv(tmp[0], tmp[1]);
else
add_envv(tmp[0], "");
}
else if (tmp[0][0] == '_')
;
else
{
if (tmp[1])
replace_value(tmp[0], tmp[1]);
else
replace_value(tmp[0], "");
}
}
int check_arg(char *tmp, char *identifier)
{
int i;
if (!ft_isalpha(tmp[0]) && tmp[0] != '_')
{
printf("minishell: export: `%s': not a valid identifier\n",
identifier);
return (0);
}
i = 1;
while (tmp[i])
{
if (!isalpha_num(tmp[i]) && tmp[i] != '_')
{
printf("minishell: export: `%s': not a valid identifier\n",
identifier);
return (0);
}
i++;
}
return (1);
}
static char **split_env(char *str, char c)
{
int i;
char **env;
int size;
size = 0;
env = (char **)malloc(sizeof(char *) * 3);
while (str[size] && str[size] != c)
size++;
env[0] = (char *)malloc(size + 1);
ft_strlcpy(env[0], str, size + 1);
size++;
i = size;
while (str[size])
size++;
env[1] = (char *)malloc(size - i);
ft_strlcpy(env[1], &str[i], size - i + 1);
env[2] = "\0";
return (env);
}
void ft_export(char **args)
{
int i;
char **tmp;
g_all.exit_status = 1;
if (n_args(args) == 1)
ft_env2();
else if (n_args(args) == 2)
{
i = 1;
while (args[i])
{
tmp = split_env(args[i], '=');
if (!check_arg(tmp[0], args[i]))
return ;
else if (ft_strchr(args[i], '='))
add_replace(tmp);
else if (!key_exist(tmp[0]))
add_envv(tmp[0], NULL);
i++;
}
g_all.exit_status = 0;
free_tmp(tmp);
}
else
printf("minishell: export: not a valid identifier\n");
}