diff options
Diffstat (limited to 'src/builtins_part_one.c')
| -rw-r--r-- | src/builtins_part_one.c | 133 |
1 files changed, 50 insertions, 83 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index f3bcfc0..7cbcdeb 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,60 +6,13 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:51:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:33:33 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include <stdio.h> -int echo(char **av) -{ - int i; - int f; - - i = 1; - f = 1; - if (av[1] == NULL || av[1][0] == '\0') - { - write(1, "\n", 1); - return (0); - } - if (ft_strncmp(av[1], "-n", 3) == 0) - { - i++; - f = 0; - } - while (av[i]) - { - write(1, av[i], ft_strlen(av[i])); - i++; - if (av[i]) - write(1, " ", 1); - } - if (f) - write(1, "\n", 1); - return (0); -} - -void exit_shell(t_env **env, int exit_status) -{ - free_envlst(env); - exit(exit_status); -} - -int builtin_exit(char **args, t_env **env) -{ - int exit_status; - - if (args[1]) - exit_status = ft_atoi(args[1]); - else - exit_status = 0; - exit_shell(env, exit_status); - return (exit_status); -} - int unset(char **av, t_env **env) { t_env *current; @@ -69,8 +22,6 @@ int unset(char **av, t_env **env) i = 0; while (av[++i]) { - if (ft_strchr(av[i], '?')) - return (1); current = *env; prev = NULL; while (current) @@ -95,6 +46,8 @@ t_env *check_existing(t_env *env, char *av) { while (env) { + if (ft_strcmp("$", av) == 0) + return (NULL); if (ft_strcmp(env->name, av) == 0) return (env); env = env->next; @@ -102,51 +55,65 @@ t_env *check_existing(t_env *env, char *av) return (NULL); } -int export(char **av, t_env **env) +void export_export(char *av, t_env **env) { char *tmp; t_env *current; - int i; current = NULL; + tmp = ft_strchr(av, '='); + *tmp = '\0'; + current = check_existing(*env, av); + if (current) + free(current->value); + else + { + current = env_new(ft_strdup(av)); + current->next = *env; + *env = current; + } + current->value = ft_strdup(tmp + 1); +} + +int is_valid_identifier(char *str) +{ + int i; + i = 0; - while (av[++i]) + if (!ft_isalpha(str[0]) && str[0] != '_') + return (0); + while (str[i] && str[i] != '=') { - if ((ft_strchr(av[i], '='))) - { - tmp = ft_strchr(av[i], '='); - *tmp = '\0'; - if (ft_strchr(av[i], '?')) - return (1); - current = check_existing(*env, av[i]); - if (current) - free(current->value); - else - { - current = env_new(ft_strdup(av[i])); - current->next = *env; - *env = current; - } - current->value = ft_strdup(tmp + 1); - } - else - return (1); + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); + i++; } - return (0); + return (1); } -void set_return_code(int return_code, t_env **env) +int export(char **av, t_env **env) { - t_env *cur; + char *equal_sign; + int i; - cur = check_existing(*env, "?"); - if (cur) - free(cur->value); - else + i = 0; + while (av[++i]) { - cur = env_new(ft_strdup("?")); - cur->next = *env; - *env = cur; + equal_sign = ft_strchr(av[i], '='); + if (equal_sign) + *equal_sign = '\0'; + if (!is_valid_identifier(av[i])) + { + write(1, "Minishell $ export: not a valid identifier\n", 43); + if (equal_sign) + *equal_sign = '='; + continue ; + } + if (equal_sign) + { + *equal_sign = '='; + export_export(av[i], env); + } } - cur->value = ft_itoa(return_code); + return (0); } |
