diff options
Diffstat (limited to '')
-rw-r--r-- | src/u_vars.c (renamed from src/ft_u_vars.c) | 118 |
1 files changed, 69 insertions, 49 deletions
diff --git a/src/ft_u_vars.c b/src/u_vars.c index cb020e3..2a78f3d 100644 --- a/src/ft_u_vars.c +++ b/src/u_vars.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_u_vars.c :+: :+: :+: */ +/* u_vars.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -14,26 +14,25 @@ #include <stdlib.h> #include <stdint.h> -#include "ft_d_define.h" -#include "ft_f_fail.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - +#include "d_define.h" +#include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" static char - *ft_set_rva(const char varname[], - t_msh *msh) + *u_set_rva(const char varname[], + t_msh *msh) { char *rvarname; if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char)))) { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); } - ft_memcpy((char*)rvarname, (const char*)varname + 1, + (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'; @@ -41,25 +40,25 @@ static char } static char - *ft_dup_env(char *p_env, - char *rvarname, - t_msh *msh) + *u_dup_env(char *p_env, + char *rvarname, + t_msh *msh) { char *varval; if (!(varval = ft_strdup(p_env))) { ft_memdel((void*)&rvarname); - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); } ft_memdel((void*)&rvarname); return (varval); } static char - *ft_get_frm_env(char rvarname[], + *u_get_frm_env(char rvarname[], t_msh *msh) { char **p_env; @@ -69,65 +68,86 @@ static char p_env = msh->envp; while (*p_env) { - if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname))) + if (!ft_strncmp(rvarname, *p_env, ft_strclen(*p_env, '='))) { pp_env = *p_env; while (*pp_env != '\0' && *pp_env != '=') pp_env += 1; if (*pp_env == '=') pp_env += 1; - varval = ft_dup_env(pp_env, rvarname, msh); + varval = u_dup_env(pp_env, rvarname, msh); return (varval); } p_env += 1; } + ft_memdel((void*)&rvarname); return (NULL); } static char - *ft_get_special_var(const char varname[], - t_msh *msh) + *u_get_special_var(const char varname[], + t_msh *msh) { char *varval; - if (!ft_strncmp(varname, FT_RET_VAR, 3)) + if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { - if (!(varval = ft_uitoa(msh->ret))) + if ((varval = ft_uitoa(msh->ret)) == NULL) { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); } return (varval); } - else if (!ft_strncmp(varname, FT_ZER_VAR, 3)) + else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) { - if (!(varval = ft_strdup(msh->shname))) + if ((varval = ft_strdup(msh->shname)) == NULL) { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); } return (varval); } return (NULL); } -static char - *ft_get_cstm_vr(const char varname[], - t_msh *msh) +char + *u_get_cstm_vr(const char varname[], + t_msh *msh) { - (void)varname; - (void)msh; - return (NULL); + t_lvars *ptr; + char *varval; + + ptr = msh->vars; + while (ptr != NULL && + ft_strncmp(varname + 1, ptr->name, ft_strlen(varname + 1) + 1) != 0) + { + ptr = ptr->next; + } + if (ptr != NULL) + { + if (!(varval = ft_strdup(ptr->val))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + return (varval); + } + else + { + return (NULL); + } } /* -** char * -** ft_subst_var_value(const char varname[], const t_msh *msh); +** char* +** u_get_var_value(const char varname[], t_msh *msh); ** ** DESCRIPTION -** The ft_subst_var_value() function returns +** The u_get_var_value() function returns ** a heap-allocated, null-terminated string ** that may later be free'd containing the ** value of the variable varname[] including @@ -136,8 +156,8 @@ static char */ char - *ft_subst_var_value(const char varname[], - t_msh *msh) + *u_get_var_value(const char varname[], + t_msh *msh) { /* TODO: check behaviour on empty vars -> "QWE=" */ /* TODO: add support for global variables -> "$hey $nigga..." */ @@ -145,18 +165,18 @@ char char *rvarname; varval = NULL; - if ((varval = ft_get_special_var(varname, msh)) != NULL) + if ((varval = u_get_special_var(varname, msh)) != NULL) { return (varval); } - else if ((varval = ft_get_cstm_vr(varname, msh)) != NULL) + else if ((varval = u_get_cstm_vr(varname, msh)) != NULL) { return (varval); } else { - rvarname = ft_set_rva(varname, msh); - varval = ft_get_frm_env(rvarname, msh); + rvarname = u_set_rva(varname, msh); + varval = u_get_frm_env(rvarname, msh); return (varval); } return (NULL); |