diff options
Diffstat (limited to 'src/u_vars_next.c')
-rw-r--r-- | src/u_vars_next.c | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 22288d2..11027cb 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -65,26 +65,29 @@ 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. When returning 1, the +** entire environment is destroyed. */ -/* -** 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]; + size_t i; long env_line; + char new_line_fmt[ARG_MAX]; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { @@ -93,6 +96,12 @@ void u_subst_var_value(const char varname[], if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char))) == NULL) { + i = env_line; + while (msh->envp[++i] != NULL) + ft_memdel((void*)&msh->envp[i]); + ft_delwords(msh->envp); + msh->envp = NULL; + return (1); } (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); @@ -105,4 +114,5 @@ void u_subst_var_value(const char varname[], { lvars_add_front(&msh->vars, lvars_new(varname + 1, newval)); } + return (0); } |