diff options
Diffstat (limited to 'src/builtins_part_one.c')
| -rw-r--r-- | src/builtins_part_one.c | 153 |
1 files changed, 56 insertions, 97 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index d64bb54..11989cc 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,79 +6,12 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/10 14:36:55 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:07:18 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.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 (1); - } - 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); -} - -int pwd(t_env *env) -{ - while (env) - { - if (ft_strncmp(env->name, "PWD", 4) == 0) - { - ft_printf("%s\n", env->value); - break ; - } - env = env->next; - } - return (0); -} - -int ft_env(t_env *env) -{ - while (env != NULL) - { - printf("%s", env->name); - printf("=%s\n", env->value); - env = env->next; - } - return (0); -} - -// int exit(char *av) -// { -// freenode free toke free sequence stop repl free env; -// clear history; -// } -//// - -void free_env_node(t_env *node) -{ - free(node->name); - free(node->value); - free(node); -} +#include <stdio.h> int unset(char **av, t_env **env) { @@ -109,21 +42,12 @@ int unset(char **av, t_env **env) return (0); } -t_env *env_new(char *name) -{ - t_env *result; - - result = malloc(sizeof(t_env)); - if (!result) - return (NULL); - result->name = name; - return (result); -} - 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; @@ -131,31 +55,66 @@ 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; + if (!ft_isalpha(str[0]) && str[0] != '_') + return (0); + while (str[i] && str[i] != '=') + { + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); + i++; + } + return (1); +} + +int export(char **av, t_env **env, int f) +{ + char *equal_sign; + int i; + i = 0; while (av[++i]) { - if ((ft_strchr(av[i], '='))) + equal_sign = ft_strchr(av[i], '='); + if (equal_sign) + *equal_sign = '\0'; + if (!is_valid_identifier(av[i])) { - tmp = ft_strchr(av[i], '='); - *tmp = '\0'; - 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); + write(1, "Minishell $ export: not a valid identifier\n", 43); + if (equal_sign) + *equal_sign = '='; + f++; + continue ; + } + if (equal_sign) + { + *equal_sign = '='; + export_export(av[i], env); } } - return (0); -}
\ No newline at end of file + return (check_flag(f)); +} |
