ft_export.c 5.4 KB
Newer Older
pêle-mêle's avatar
pêle-mêle committed
1
#include "builtins.h"
2

pêle-mêle's avatar
pêle-mêle committed
3
char	**sort_variables(char **env, char **exported)
4
{
pêle-mêle's avatar
pêle-mêle committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	char		**sorted_array;
	size_t		env_len;
	size_t		exported_len;

	env_len = ft_arraylen(env);
	exported_len = ft_arraylen(exported);
	sorted_array = (char **)malloc(sizeof(char *) * (env_len + exported_len + 1));
	if (sorted_array == NULL)
		return (NULL);
	ft_memcpy(sorted_array, env, sizeof(char *) * env_len);
	ft_memcpy(sorted_array + env_len, exported, sizeof(char *) * exported_len);
	sorted_array[env_len + exported_len] = NULL;
	sort_str_array(sorted_array);
	return (sorted_array);
}

pêle-mêle's avatar
pêle-mêle committed
21
int		print_export_var_value(char *value)
22
23
24
25
{
	while (*value != '\0')
	{
		if (ft_isset(*value, "$\"\\`") == TRUE)
pêle-mêle's avatar
pêle-mêle committed
26
27
28
29
			if (ft_putchar_fd('\\', STDOUT_FILENO) != SUCCESS)
				return (WRITE_ERR);
		if (ft_putchar_fd(*value, STDOUT_FILENO) != SUCCESS)
				return (WRITE_ERR);
30
31
		value++;
	}
pêle-mêle's avatar
pêle-mêle committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	return (SUCCESS);
}

int		print_export_one_var(char *variable, ssize_t equalsign_pos)
{
	if (write(STDOUT_FILENO, variable, equalsign_pos + 1) != equalsign_pos + 1)
		return (WRITE_ERR);
	if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
		return (WRITE_ERR);
	if (print_export_var_value(variable + equalsign_pos + 1) != SUCCESS)
		return (WRITE_ERR);
	if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
		return (WRITE_ERR);
	return (SUCCESS);
46
47
}

pêle-mêle's avatar
pêle-mêle committed
48
49
50
51
52
int		print_export(char **env, char **exported)
{
	char	**variables;
	size_t	i;
	ssize_t	equalsign_pos;
pêle-mêle's avatar
pêle-mêle committed
53
	int		ret;
54

pêle-mêle's avatar
pêle-mêle committed
55
56
	variables = sort_variables(env, exported);
	if (variables == NULL)
Abenoit's avatar
Abenoit committed
57
		return (MALLOC_ERR);
pêle-mêle's avatar
pêle-mêle committed
58
59
	i = 0;
	while (variables[i] != NULL)
60
	{
pêle-mêle's avatar
pêle-mêle committed
61
		ret = ft_putstr_fd("declare -x ", STDOUT_FILENO);
pêle-mêle's avatar
pêle-mêle committed
62
		equalsign_pos = ft_index(variables[i], '=');
pêle-mêle's avatar
pêle-mêle committed
63
64
65
66
67
68
		if (equalsign_pos != NOT_FOUND && ret == WRITE_SUCCESS)
			ret = print_export_one_var(variables[i], equalsign_pos);
		if (ret == SUCCESS || ret == WRITE_SUCCESS)
			ret = ft_putchar_fd('\n', STDOUT_FILENO);
		if (ret != SUCCESS)
			break ;
pêle-mêle's avatar
pêle-mêle committed
69
70
71
		i++;
	}
	free(variables);
pêle-mêle's avatar
pêle-mêle committed
72
	return (ret);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

static int	ft_env_append_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
	ssize_t	var_pos;
	char	**new_array;
	char	*new_var;

	var_pos = get_var_pos(xe->exported, var, equalsign_pos - 1);
	if (var_pos != NOT_FOUND)
	{
		new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
		if (new_array == NULL)
			return (MALLOC_ERR);
		xe->exported = new_array;
	}
	new_var = (char *)malloc(sizeof(char) * ft_strlen(var));
	if (new_var == NULL)
		return (MALLOC_ERR);
	ft_memcpy(new_var, var, equalsign_pos - 1);
	ft_strlcpy(new_var + equalsign_pos - 1,
				var + equalsign_pos, ft_strlen(var));
pêle-mêle's avatar
pêle-mêle committed
95
	new_array = push_str_to_array(xe->env, ft_strdup(new_var));
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
125
126
127
128
129
130
131
132
133
134
135
136
137
	if (new_array == NULL)
		return (MALLOC_ERR);
	free(new_var);
	xe->env = new_array;
	return (SUCCESS);
}

static int	ft_env_append(char *var, t_xe *xe, ssize_t equalsign_pos)
{
	ssize_t	var_pos;
	char	*new_var;

	var_pos = get_var_pos(xe->env, var, equalsign_pos - 1);
	if (var_pos != NOT_FOUND)
	{
		new_var = ft_strjoin(xe->env[var_pos], var + equalsign_pos + 1); // ft_strjoin_free?
		if (new_var == NULL)
			return (MALLOC_ERR);
		free(xe->env[var_pos]);
		xe->env[var_pos] = new_var;
	}
	else
	{
		if (ft_env_append_from_exp(var, xe, equalsign_pos) != SUCCESS)
			return (MALLOC_ERR);
	}
	return (SUCCESS);
}

static int	ft_env_set_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
	char	**new_array;
	ssize_t	var_pos;

	var_pos = get_var_pos(xe->exported, var, equalsign_pos);
	if (var_pos != NOT_FOUND)
	{
		new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
		if (new_array == NULL)
			return (MALLOC_ERR);
		xe->exported = new_array;
	}
pêle-mêle's avatar
pêle-mêle committed
138
	new_array = push_str_to_array(xe->env, ft_strdup(var));
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	if (new_array == NULL)
		return (MALLOC_ERR);
	xe->env = new_array;
	return (SUCCESS);
}

static int	ft_env_set(char *var, t_xe *xe, ssize_t equalsign_pos)
{
	ssize_t	var_pos;

	var_pos = get_var_pos(xe->env, var, equalsign_pos);
	if (var_pos != NOT_FOUND)
	{
		free(xe->env[var_pos]);
		xe->env[var_pos] = ft_strdup(var);
		if (xe->env[var_pos] == NULL)
			return (MALLOC_ERR);
	}
	else
	{
		if (ft_env_set_from_exp(var, xe, equalsign_pos) != SUCCESS)
			return (MALLOC_ERR);
	}
	return (SUCCESS);
}

static int	ft_env_export(char *var, t_xe *xe, ssize_t equalsign_pos)
{
	if (var[equalsign_pos - 1] == '+')
	{
		if (ft_env_append(var, xe, equalsign_pos) != SUCCESS)
			return (MALLOC_ERR);
	}
	else
	{
		if (ft_env_set(var, xe, equalsign_pos) != SUCCESS)
			return (MALLOC_ERR);
	}
	return (SUCCESS);
}

static int	ft_exported_export(char *var, t_xe *xe)
{
	char	**new_array;

	if (get_var_pos(xe->env, var, ft_strlen(var)) == NOT_FOUND
			&& get_var_pos(xe->exported, var, ft_strlen(var)) == NOT_FOUND)
	{
pêle-mêle's avatar
pêle-mêle committed
187
		new_array = push_str_to_array(xe->exported, ft_strdup(var));
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
		if (new_array == NULL)
			return (MALLOC_ERR);
		xe->exported = new_array;
	}
	return (SUCCESS);
}

static int	ft_export_checks(char **args, t_xe *xe)
{
	ssize_t	equalsign_pos;

	while (*args != NULL)
	{
		equalsign_pos = ft_index(*args, '=');
		if (check_var_name(*args, equalsign_pos) == SUCCESS)
		{
			if (equalsign_pos != NOT_FOUND)
			{
				if (ft_env_export(*args, xe, equalsign_pos) != SUCCESS)
					return (MALLOC_ERR);
			}
			else
			{
				if (ft_exported_export(*args, xe) != SUCCESS)
					return (MALLOC_ERR);
			}
		}
		else
216
217
218
219
		{
			xe->stat_loc = 1;
			return (INVALID_EXP_ID);
		}
220
221
222
223
224
225
226
		args++;
	}
	return (SUCCESS);
}

int		ft_export(char **args, t_xe *xe)
{
pêle-mêle's avatar
pêle-mêle committed
227
228
	int		ret;

229
	if (ft_arraylen(args) == 1)
pêle-mêle's avatar
pêle-mêle committed
230
		ret = print_export(xe->env, xe->exported);
231
	else
pêle-mêle's avatar
pêle-mêle committed
232
		ret = ft_export_checks(args + 1, xe);
abenoit's avatar
abenoit committed
233
	xe->stat_loc = 0;
pêle-mêle's avatar
pêle-mêle committed
234
	return (ret);
235
}