summaryrefslogtreecommitdiffstats
path: root/src/b_export_next.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-03 19:00:47 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-03 19:00:47 +0200
commitbfaf4c53a8406ea0bfbd8699807b567a5322a03f (patch)
treeb8ce7d07b3094de0c4ba98160f35cf7e64eeae1d /src/b_export_next.c
parentAdded $ for varname (diff)
download42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.gz
42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.bz2
42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.xz
42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.zst
42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.zip
Export is working
Diffstat (limited to 'src/b_export_next.c')
-rw-r--r--src/b_export_next.c64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/b_export_next.c b/src/b_export_next.c
index 683fdd0..0c143b2 100644
--- a/src/b_export_next.c
+++ b/src/b_export_next.c
@@ -15,7 +15,9 @@
#include "b_export_next.h"
#include "f_fail.h"
+#include "s_lvars.h"
#include "s_struct.h"
+#include "u_vars.h"
static char
**b_get_var(const char arg[],
@@ -40,13 +42,73 @@ static char
return (var);
}
+static int64_t
+ b_is_it_in_env(const char varname[],
+ t_msh *msh)
+{
+ char **p_env;
+
+ p_env = msh->envp;
+ while (*p_env != NULL)
+ {
+ if (ft_strncmp(varname, *p_env, ft_strlen(varname)) == 0)
+ {
+ return (p_env - msh->envp);
+ }
+ p_env++;
+ }
+ return (-1);
+}
+
+static void
+ b_add_to_env(const char arg[],
+ t_msh *msh)
+{
+ size_t i;
+ char **nenvp;
+
+ i = 0;
+ while (msh->envp[i] != NULL)
+ i++;
+ if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*))))
+ f_fail_alloc_and_destroy(msh);
+ i = 0;
+ while (msh->envp[i] != NULL)
+ {
+ if (!(nenvp[i] = ft_strdup(msh->envp[i])))
+ f_fail_alloc_and_destroy(msh);
+ i++;
+ }
+ if (!(nenvp[i] = ft_strdup(arg)))
+ f_fail_alloc_and_destroy(msh);
+ nenvp[i + 1] = 0;
+ ft_delwords(msh->envp);
+ msh->envp = nenvp;
+}
+
void
b_export_with_equals(const char arg[],
t_msh *msh)
{
+ char *varval;
char **var;
+ int64_t env_i;
var = b_get_var(arg, msh);
- ft_printf("[%s] - [%s]\n", var[FT_VAR_NAME], var[FT_VAR_VAL]);
+ if ((env_i = b_is_it_in_env(var[FT_VAR_NAME] + 1, msh)) != -1)
+ {
+ ft_memdel((void*)&msh->envp[env_i]);
+ if ((msh->envp[env_i] = ft_strdup(arg)) == NULL)
+ f_fail_alloc_and_destroy(msh);
+ }
+ else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL)
+ {
+ b_add_to_env(arg, msh);
+ lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1);
+ }
+ else
+ {
+ b_add_to_env(arg, msh);
+ }
ft_delwords(var);
}