diff options
Diffstat (limited to 'src/u_vars_next.c')
-rw-r--r-- | src/u_vars_next.c | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 22288d2..5ffdbb1 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -65,34 +65,37 @@ static t_bool u_get_custom_var_existance(const char varname[], t_msh *msh) } /* -** void +** SYNOPSIS +** char ** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); ** ** DESCRIPTION -** The subst_var_value() changes the value of msh->envp +** The u_subst_var_value() function changes the value of msh->envp ** variable varname[] with newval[]. If varname[] wasn't found ** in msh->envp, varname[] is searched in msh->vars. Otherwise ** varname[] is added to msh->vars. +** +** RETURN VALUES +** The u_subst_var_value() function returns 0 upon success +** and 1 in case of malloc(3) failure. */ -/* -** TODO: Handle malloc fail -*/ - -void u_subst_var_value(const char varname[], +char u_subst_var_value(const char varname[], const char newval[], t_msh *msh) { - char new_line_fmt[ARG_MAX]; long env_line; + char new_line_fmt[ARG_MAX]; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); - if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * - sizeof(char))) == NULL) + /* if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * */ + /* sizeof(char))) == NULL) */ + if ((msh->envp[env_line] = NULL) == NULL) { + return (1); } (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); @@ -105,4 +108,5 @@ void u_subst_var_value(const char varname[], { lvars_add_front(&msh->vars, lvars_new(varname + 1, newval)); } + return (0); } |