summaryrefslogtreecommitdiffstats
path: root/src/u_vars.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-05 23:05:03 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-05 23:05:03 +0200
commitf7b13a353092017e37045344528c985172eafc3a (patch)
treec0bf8c6e8611cb72055f6a65634d1d0d35d6c4d8 /src/u_vars.c
parentNew utils functions (diff)
download42-minishell-f7b13a353092017e37045344528c985172eafc3a.tar.gz
42-minishell-f7b13a353092017e37045344528c985172eafc3a.tar.bz2
42-minishell-f7b13a353092017e37045344528c985172eafc3a.tar.xz
42-minishell-f7b13a353092017e37045344528c985172eafc3a.tar.zst
42-minishell-f7b13a353092017e37045344528c985172eafc3a.zip
Vars are fixed and finished
Diffstat (limited to 'src/u_vars.c')
-rw-r--r--src/u_vars.c51
1 files changed, 15 insertions, 36 deletions
diff --git a/src/u_vars.c b/src/u_vars.c
index 2a78f3d..e949cc9 100644
--- a/src/u_vars.c
+++ b/src/u_vars.c
@@ -19,68 +19,50 @@
#include "s_destroy.h"
#include "s_lcom.h"
#include "s_struct.h"
-
-static char
- *u_set_rva(const char varname[],
- t_msh *msh)
-{
- char *rvarname;
-
- if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char))))
- {
- lcom_clear(&msh->curr);
- s_destroy(msh);
- f_fail_alloc(msh);
- }
- (void)ft_memcpy((char*)rvarname, (const char*)varname + 1,
- ft_strlen(varname + 1));
- *(rvarname + ft_strlen(varname + 1)) = '=';
- *(rvarname + ft_strlen(varname + 1) + 1) = '\0';
- return (rvarname);
-}
+#include "u_utils.h"
static char
*u_dup_env(char *p_env,
- char *rvarname,
t_msh *msh)
{
char *varval;
if (!(varval = ft_strdup(p_env)))
{
- ft_memdel((void*)&rvarname);
lcom_clear(&msh->curr);
s_destroy(msh);
f_fail_alloc(msh);
}
- ft_memdel((void*)&rvarname);
return (varval);
}
static char
- *u_get_frm_env(char rvarname[],
- t_msh *msh)
+ *u_get_frm_env(const char varname[],
+ t_msh *msh)
{
- char **p_env;
+ char **env_dup;
char *pp_env;
char *varval;
+ size_t i;
- p_env = msh->envp;
- while (*p_env)
+ env_dup = u_get_env_var_names(msh);
+ i = 0;
+ while (env_dup[i] != NULL)
{
- if (!ft_strncmp(rvarname, *p_env, ft_strclen(*p_env, '=')))
+ if (ft_strncmp(varname + 1, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
{
- pp_env = *p_env;
+ pp_env = msh->envp[i];
while (*pp_env != '\0' && *pp_env != '=')
pp_env += 1;
if (*pp_env == '=')
pp_env += 1;
- varval = u_dup_env(pp_env, rvarname, msh);
+ varval = u_dup_env(pp_env, msh);
+ ft_delwords(env_dup);
return (varval);
}
- p_env += 1;
+ i++;
}
- ft_memdel((void*)&rvarname);
+ ft_delwords(env_dup);
return (NULL);
}
@@ -160,9 +142,7 @@ char
t_msh *msh)
{
/* TODO: check behaviour on empty vars -> "QWE=" */
- /* TODO: add support for global variables -> "$hey $nigga..." */
char *varval;
- char *rvarname;
varval = NULL;
if ((varval = u_get_special_var(varname, msh)) != NULL)
@@ -175,8 +155,7 @@ char
}
else
{
- rvarname = u_set_rva(varname, msh);
- varval = u_get_frm_env(rvarname, msh);
+ varval = u_get_frm_env(varname, msh);
return (varval);
}
return (NULL);