diff options
Diffstat (limited to 'src/builtins_part_one.c')
| -rw-r--r-- | src/builtins_part_one.c | 172 |
1 files changed, 70 insertions, 102 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 6b92d9c..11989cc 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,147 +6,115 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/10/25 20:52:36 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:07:18 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" +#include <stdio.h> -int echo(char **av) +int unset(char **av, t_env **env) { - int i; - int f; + t_env *current; + t_env *prev; + int i; - i = 1; - f = 1; - if (av[1][0] == '\0') - { - write(1, "\n", 1); - return (1); - } - if (ft_strcmp(av[1], "-n")) - { - i++; - f = 0; - } - while (av[i]) + i = 0; + while (av[++i]) { - write(1, &av[1], ft_strlen(av[i])); - i++; + current = *env; + prev = NULL; + while (current) + { + if (ft_strcmp(current->name, av[i]) == 0) + { + if (prev) + prev->next = current->next; + else + *env = current->next; + free_env_node(current); + break ; + } + prev = current; + current = current->next; + } } - if (f) - write(1, "\n", 1); return (0); } -int pwd(t_env **env, char *av) +t_env *check_existing(t_env *env, char *av) { - t_env *current; - t_env *prev; - char *tmp; - - current = env; - prev = NULL; - while (current) + while (env) { - if (ft_strcmp(current->name, av == 0)) - break ; - prev = current; - current = current->next; + if (ft_strcmp("$", av) == 0) + return (NULL); + if (ft_strcmp(env->name, av) == 0) + return (env); + env = env->next; } - ft_printf("%s\n", current->value); - return (0); + return (NULL); } -int env(t_env **env) +void export_export(char *av, t_env **env) { + char *tmp; t_env *current; - t_env *prev; - current = env; - prev = NULL; - while (current) + current = NULL; + tmp = ft_strchr(av, '='); + *tmp = '\0'; + current = check_existing(*env, av); + if (current) + free(current->value); + else { - ft_printf("%s", current->name); - ft_printf("=%s\n", current->value); - prev = current; - current = current->next; + current = env_new(ft_strdup(av)); + current->next = *env; + *env = current; } - return (0); + current->value = ft_strdup(tmp + 1); } -int exit(char *av) +int is_valid_identifier(char *str) { - freenode free toke free sequence stop repl free env; - clear history; -} - -int export(char **av, t_env **env) -{ - char *tmp; - t_env *current; - int i; + int i; - i = i; - while (av[i]) + i = 0; + if (!ft_isalpha(str[0]) && str[0] != '_') + return (0); + while (str[i] && str[i] != '=') { - if (t_strchr(av[i], '=')) - { - tmp = ft_strchr(av[i], '='); - tmp = '\0'; - current = *env; - while (current) - { - if (ft_strcmp(current->name, tmp[i]) == 0) - { - free(current->value); - current->value = ft_strdup(tmp + 1); - break ; - } - current = current->next; - } - if (!current) - { - current = malloc(sizeof(t_env)); - current->name = ft_strdup(av[i]); - current->value = ft_strdup(tmp + 1); - current->next = *env; - *env = current; - } - } + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); i++; } return (1); } -int unset(char **av, t_env **env) +int export(char **av, t_env **env, int f) { - t_env *current; - t_env *prev; + char *equal_sign; int i; i = 0; - current = env; - prev = NULL; - while (av[i]) + while (av[++i]) { - while (current) + equal_sign = ft_strchr(av[i], '='); + if (equal_sign) + *equal_sign = '\0'; + if (!is_valid_identifier(av[i])) { - if (ft_strcmp(current->name, av[i] == 0)) - break ; - prev = current; - current = current->next; + write(1, "Minishell $ export: not a valid identifier\n", 43); + if (equal_sign) + *equal_sign = '='; + f++; + continue ; } - if (current) + if (equal_sign) { - if (prev) - prev->next = current->next; - else - *env = current->next; - free(current->value); - free(current); + *equal_sign = '='; + export_export(av[i], env); } - i++; } - return (0); -}
\ No newline at end of file + return (check_flag(f)); +} |
