From: Lukáš Jiřiště Date: Mon, 26 Aug 2024 09:53:30 +0000 (+0200) Subject: Implement built-in function unset X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=218cdd52ec7567997066abfa665ce5de4d6c7964;p=42%2Fminishell.git Implement built-in function unset --- diff --git a/Makefile b/Makefile index dc168cd..27fa6d7 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ SOURCES := main.c \ builtins/pwd.c \ builtins/env.c \ builtins/export.c \ + builtins/unset.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/builtins.h b/inc/builtins.h index 752d10a..0afbd1d 100644 --- a/inc/builtins.h +++ b/inc/builtins.h @@ -20,5 +20,8 @@ int echo(int argc, char **argv); int pwd(void); int ft_env(int argc, t_execution_env *env); int export(int argc, char **argv, t_execution_env *env); +int unset(int argc, char **argv, t_execution_env *env); + +void unset_single(const char *var_name, t_execution_env *env); #endif // BUILTINS_H diff --git a/src/builtins/export.c b/src/builtins/export.c index c0ea1b0..1189227 100644 --- a/src/builtins/export.c +++ b/src/builtins/export.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/23 09:40:38 by ljiriste #+# #+# */ -/* Updated: 2024/08/26 11:44:35 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 11:47:56 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,19 +14,6 @@ #include "minishell_structs.h" #include -static void unset_single(const char *name, t_execution_env *env) -{ - ssize_t index; - - index = get_var_index(&env->vars->other, name); - if (index >= 0) - ft_vec_erase(&env->vars->other, index, free_str); - index = get_var_index(&env->vars->exported, name); - if (index >= 0) - ft_vec_erase(&env->vars->exported, index, free_str); - return ; -} - static int export_single(const char *var, t_execution_env *env) { char *name; diff --git a/src/builtins/unset.c b/src/builtins/unset.c new file mode 100644 index 0000000..d8b6395 --- /dev/null +++ b/src/builtins/unset.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/26 08:18:30 by ljiriste #+# #+# */ +/* Updated: 2024/08/26 11:47:56 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" +#include "minishell_structs.h" + +void unset_single(const char *name, t_execution_env *env) +{ + ssize_t index; + + index = get_var_index(&env->vars->other, name); + if (index >= 0) + ft_vec_erase(&env->vars->other, index, free_str); + index = get_var_index(&env->vars->exported, name); + if (index >= 0) + ft_vec_erase(&env->vars->exported, index, free_str); + return ; +} + +int unset(__attribute__((unused)) int argc, char **argv, t_execution_env *env) +{ + size_t i; + + i = 1; + while (argv[i]) + { + unset_single(argv[i], env); + ++i; + } + return (0); +} diff --git a/src/execution.c b/src/execution.c index 2c42b31..e5d6a61 100644 --- a/src/execution.c +++ b/src/execution.c @@ -685,6 +685,8 @@ int is_builtin(const char *str) return (1); if (!ft_strcmp(str, "export")) return (1); + if (!ft_strcmp(str, "unset")) + return (1); return (0); } @@ -710,6 +712,8 @@ int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attr env->ret_val = ft_env(count_fields(fields), env); else if (!ft_strcmp(fields[0], "export")) env->ret_val = export(count_fields(fields), fields, env); + else if (!ft_strcmp(fields[0], "unset")) + env->ret_val = unset(count_fields(fields), fields, env); else return (-1); return (0);