diff options
Diffstat (limited to 'src/u_vars.c')
-rw-r--r-- | src/u_vars.c | 51 |
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); |