diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-06 21:56:20 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-06 21:56:20 +0200 |
commit | 4543c3ba3222d47780ad3e091cfe6f3098cc2bca (patch) | |
tree | fb8a70bf05464a6fa2c820cc43ea34421340c903 /src/u_vars.c | |
parent | Commit from minishell (diff) | |
download | 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.gz 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.bz2 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.xz 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.zst 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.zip |
Stacked
Diffstat (limited to 'src/u_vars.c')
-rw-r--r-- | src/u_vars.c | 109 |
1 files changed, 37 insertions, 72 deletions
diff --git a/src/u_vars.c b/src/u_vars.c index 9594036..95c6ffa 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -21,26 +21,11 @@ #include "s_struct.h" #include "u_utils.h" -static char - *u_dup_env(char *p_env, - t_msh *msh) -{ - char *varval; - - if (!(varval = ft_strdup(p_env))) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); -} - -static char - *u_get_frm_env(const char varname[], - t_msh *msh) +static void + u_get_frm_env(char str[], const char varname[], size_t dstsize, t_msh *msh) { char **env_dup; char *pp_env; - char *varval; size_t i; env_dup = u_get_env_var_names(msh); @@ -54,47 +39,38 @@ static char pp_env += 1; if (*pp_env == '=') pp_env += 1; - varval = u_dup_env(pp_env, msh); + ft_strlcpy(str, pp_env, dstsize); ft_delwords(env_dup); - return (varval); + return ; } i++; } ft_delwords(env_dup); - return (NULL); } -static char - *u_get_special_var(const char varname[], +static void + u_get_special_var(char str[], + const char varname[], + size_t dstsize, t_msh *msh) { - char *varval; - if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { - if ((varval = ft_uitoa(msh->ret)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); + ft_strlcpy(str, ft_uitoa(msh->ret), 4); } else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) { - if ((varval = ft_strdup(msh->shname)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); + ft_strlcpy(str, msh->shname, dstsize); } - return (NULL); } -char - *u_get_cstm_vr(const char varname[], - t_msh *msh) +void + u_get_custom_var(char str[], + const char varname[], + size_t dstsize, + t_msh *msh) { t_lvars *ptr; - char *varval; ptr = msh->vars; while (ptr != NULL && @@ -104,51 +80,40 @@ char } if (ptr != NULL) { - if (!(varval = ft_strdup(ptr->val))) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); - } - else - { - return (NULL); + ft_strlcpy(str, ptr->val, dstsize); } } /* -** char* -** u_get_var_value(const char varname[], t_msh *msh); +** void +** u_get_var_value(char str[], const char varname[], size_t dstsize, t_msh *msh) ** ** DESCRIPTION -** 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 -** the '$' prefix. NULL is returned if varname[] -** wasn't found. +** The u_get_var_value() function fills +** str[] with a null-terminated string +** containing the value of the variable varname[] +** including the '$' prefix, in a similar way as +** sprintf does. No more than dstsize bytes will be +** copied into str[]. str[0] is set to \000 if +** varname[] wasn't found. */ -char - *u_get_var_value(const char varname[], +void + u_get_var_value(char str[], + const char varname[], + size_t dstsize, t_msh *msh) { - /* TODO: check behaviour on empty vars -> "QWE=" */ - char *varval; - - varval = NULL; - if ((varval = u_get_special_var(varname, msh)) != NULL) - { - return (varval); - } - else if ((varval = u_get_cstm_vr(varname, msh)) != NULL) + str[0] = C_NUL; + u_get_special_var(str, varname, dstsize, msh); + if (str[0] != C_NUL) { - return (varval); + return ; } - else + u_get_custom_var(str, varname, dstsize, msh); + if (str[0] != C_NUL) { - varval = u_get_frm_env(varname, msh); - return (varval); + return ; } - return (NULL); + u_get_frm_env(str, varname, dstsize, msh); } |