|
6 | 6 | /* By: pchung <[email protected]> +#+ +:+ +#+ */
|
7 | 7 | /* +#+#+#+#+#+ +#+ */
|
8 | 8 | /* Created: 2025/02/09 15:41:50 by jidler #+# #+# */
|
9 |
| -/* Updated: 2025/03/04 02:17:38 by pchung ### ########.fr */ |
| 9 | +/* Updated: 2025/03/06 13:01:48 by pchung ### ########.fr */ |
10 | 10 | /* */
|
11 | 11 | /* ************************************************************************** */
|
12 | 12 |
|
13 | 13 | #include "../../shell.h"
|
14 | 14 |
|
15 |
| -static void print_env_list(t_env *env_list) |
| 15 | +static void print_env_list(t_env *env_list) |
16 | 16 | {
|
17 |
| - while (env_list) |
18 |
| - { |
19 |
| - printf("declare -x %s", env_list->key); |
20 |
| - if (env_list->value) |
21 |
| - printf("=\"%s\"", env_list->value); |
22 |
| - printf("\n"); |
23 |
| - env_list = env_list->next; |
24 |
| - } |
| 17 | + while (env_list) |
| 18 | + { |
| 19 | + printf("declare -x %s", env_list->key); |
| 20 | + if (env_list->value) |
| 21 | + printf("=\"%s\"", env_list->value); |
| 22 | + printf("\n"); |
| 23 | + env_list = env_list->next; |
| 24 | + } |
25 | 25 | }
|
26 | 26 |
|
27 |
| -int shell_export(char **args, t_env *env_list) |
| 27 | +static int export_arg(char *arg, t_env **env_list) |
28 | 28 | {
|
29 |
| - int i = 1; |
| 29 | + char *eq; |
30 | 30 |
|
31 |
| - if (!args || !args[1]) |
32 |
| - { |
33 |
| - print_env_list(env_list); |
34 |
| - return 0; |
35 |
| - } |
| 31 | + if (ft_strlen(arg) == 0 || arg[0] == '=') |
| 32 | + { |
| 33 | + printf("export: `%s': not a valid identifier\n", arg); |
| 34 | + return (1); |
| 35 | + } |
| 36 | + eq = ft_strchr(arg, '='); |
| 37 | + if (eq) |
| 38 | + { |
| 39 | + *eq = '\0'; |
| 40 | + set_env_value(env_list, arg, eq + 1); |
| 41 | + *eq = '='; |
| 42 | + } |
| 43 | + else |
| 44 | + set_env_value(env_list, arg, NULL); |
| 45 | + return (0); |
| 46 | +} |
| 47 | + |
| 48 | +int shell_export(char **args, t_env *env_list) |
| 49 | +{ |
| 50 | + int i; |
36 | 51 |
|
37 |
| - while (args[i]) |
38 |
| - { |
39 |
| - char *eq = ft_strchr(args[i], '='); |
40 |
| - if (!eq) |
41 |
| - { |
42 |
| - set_env_value(&env_list, args[i], NULL); |
43 |
| - } |
44 |
| - else |
45 |
| - { |
46 |
| - *eq = '\0'; |
47 |
| - set_env_value(&env_list, args[i], eq + 1); |
48 |
| - *eq = '='; |
49 |
| - } |
50 |
| - i++; |
51 |
| - } |
52 |
| - return 0; |
| 52 | + i = 1; |
| 53 | + if (!args || !args[1]) |
| 54 | + { |
| 55 | + print_env_list(env_list); |
| 56 | + return (0); |
| 57 | + } |
| 58 | + while (args[i]) |
| 59 | + { |
| 60 | + if (export_arg(args[i], &env_list)) |
| 61 | + return (1); |
| 62 | + i++; |
| 63 | + } |
| 64 | + return (0); |
53 | 65 | }
|
0 commit comments