Implement built-in function unset
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 26 Aug 2024 09:53:30 +0000 (11:53 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 26 Aug 2024 09:59:20 +0000 (11:59 +0200)
Makefile
inc/builtins.h
src/builtins/export.c
src/builtins/unset.c [new file with mode: 0644]
src/execution.c

index dc168cddcefc7c2a9aa794e49d59d32c9f814fc8..27fa6d7bfe557a37f102ec4500714d52a4ab3e6c 100644 (file)
--- 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))
 
index 752d10aa94ee95f09b53735574f1f72e5d550c0e..0afbd1d2d4ad6c45325b3459372a7deafcd3d374 100644 (file)
@@ -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
index c0ea1b046ab75a7604d218af6ce9d2c0b155445a..1189227357ccce6c8f4eaf748192487db949e936 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "minishell_structs.h"
 #include <stdlib.h>
 
-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 (file)
index 0000000..d8b6395
--- /dev/null
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   unset.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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);
+}
index 2c42b31366a03a39dca387b657792ebc8e02bdf3..e5d6a61e1e4f09503eb8cfcc74db18497d5fd5bf 100644 (file)
@@ -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);