diff --git a/builtins/env.c b/builtins/env.c index 0ba0787..3557710 100644 --- a/builtins/env.c +++ b/builtins/env.c @@ -6,17 +6,17 @@ /* By: cchauvet next != NULL) { ft_putstr_fd(((t_env *)(current->content))->key, fd); diff --git a/builtins/export.c b/builtins/export.c index bcf7955..fa00097 100644 --- a/builtins/export.c +++ b/builtins/export.c @@ -6,17 +6,25 @@ /* By: cchauvet next != NULL) { write(fd, "declare -x ", 11); @@ -27,5 +35,50 @@ int print_export(t_list **head, int fd) write(fd, "\"\n", 2); current = current->next; } - return (0); } + +void add_export(t_list **env, char *args, int fd, int *err) +{ + char *key; + char *value; + + key = args; + value = ""; + if (ft_strchr(args, '=') != NULL) + { + key = ft_strndup(args, ft_strnchr(args, '=')); + value = ft_strchr(args, '=') + 1; + } + if (!possible_key(key)) + { + *err = error(key, fd); + return ; + } + create_value_by_key_dup(key, value, env); +} + +int export(t_list **env, char **args, int fd) +{ + int err; + int i; + + err = 0; + if (args[0] == NULL) + print_export(env, fd); + else + { + i = -1; + while (args[++i]) + add_export(env, args[i], fd, &err); + } + return (err); +} + +/*int main(int argc, char *argv[], char **env) +{ + t_list **n_env; + + n_env = init_env(env); + export(n_env, argv + 1, 1); + return (0); +}*/ diff --git a/builtins/unset.c b/builtins/unset.c index 1de43be..8522d16 100644 --- a/builtins/unset.c +++ b/builtins/unset.c @@ -6,13 +6,28 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/22 13:28:27 by erey-bet #+# #+# */ -/* Updated: 2023/02/22 13:37:27 by erey-bet ### ########.fr */ +/* Updated: 2023/02/23 15:49:21 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ +int error(char *str, int fd) +{ + write(fd, "bash: unset: `", 14); + write(fd, str, ft_strlen(str)); + write(fd, "': not a valid identifier", 25); + return (1); +} + int unset(t_list **env, char **args, int fd) { - while (args++) - delete_by_key(*args, env); - return (0); + int i; + int err; + + i = -1; + err = 0; + while (args[++i]) + if (delete_by_key(args[i], env)) + if (!possible_key(args[i])) + err = error(args[i], fd); + return (err); } diff --git a/env3.c b/env3.c index bca6060..d7f2ec0 100644 --- a/env3.c +++ b/env3.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */ -/* Updated: 2023/02/22 13:28:51 by erey-bet ### ########.fr */ +/* Updated: 2023/02/23 15:46:46 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,10 +76,23 @@ int delete_by_key(char *key, t_list **head) last->next = current->next; else *head = current->next; - return (1); + return (0); } last = current; current = current->next; } - return (0); + return (1); +} + +int possible_key(char *key) +{ + int i; + + i = -1; + if (ft_isdigit(key[i + 1]) || key[i + 1] == '@') + return (0); + while (key[++i]) + if (!ft_isalnum(key[i]) && key[i] != '_' && key[i] != '#' && key[i] != '@') + return (0); + return (1); } diff --git a/execution.c b/execution.c index ffa0a1a..3a2576c 100644 --- a/execution.c +++ b/execution.c @@ -6,7 +6,7 @@ /* By: cchauvet executable, "env") == 0) return_code = print_env(env, cmd->fd_out); else if (ft_strcmp(cmd->executable, "export") == 0) - return_code = (print_export(env, cmd->fd_out)); + return_code = (export(env,cmd->args + 1, cmd->fd_out)); else if (ft_strcmp(cmd->executable, "cd") == 0) return_code = (move_folder(cmd->args[1], cmd->fd_out)); /* if (ft_strcmp(cmd->executable, "unset") == 0) */ diff --git a/minishell.h b/minishell.h index 77366d0..dce9248 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet