diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-26 17:18:16 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-26 17:18:16 +0200 |
commit | 75492244a377c8d5d61459a162d73d60dd32759e (patch) | |
tree | c1d86b502143a97a7fde36db303bdab4a62ae2dd /src/ft_u_utils.c | |
parent | Todo update (diff) | |
download | 42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.gz 42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.bz2 42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.xz 42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.zst 42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.zip |
Tryna subst vars
Diffstat (limited to '')
-rw-r--r-- | src/ft_u_utils.c | 61 |
1 files changed, 54 insertions, 7 deletions
diff --git a/src/ft_u_utils.c b/src/ft_u_utils.c index cd9e53f..594f2c8 100644 --- a/src/ft_u_utils.c +++ b/src/ft_u_utils.c @@ -14,8 +14,24 @@ #include <stdlib.h> #include <stdint.h> +#include "ft_f_fail.h" +#include "ft_s_destroy.h" +#include "ft_s_lcom.h" #include "ft_s_struct.h" +uint64_t + ft_get_argc(const char *args[]) +{ + uint64_t argc; + + argc = 0; + while (args[argc]) + { + argc++; + } + return (argc); +} + char *ft_get_home_dir(char **envp) { @@ -35,15 +51,46 @@ char return (NULL); } -uint64_t - ft_get_argc(const char *args[]) +/* +** char * +** ft_subst_var_value(const char varname[], const t_msh *msh); +** +** DESCRIPTION +** The ft_subst_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. +*/ + +char + *ft_subst_var_value(const char varname[], + t_msh *msh) { - uint64_t argc; + char **p_env; + char *varval; - argc = 0; - while (args[argc]) + p_env = msh->envp; + varval = NULL; + while (*p_env) { - argc++; + if (!ft_strncmp(*p_env, varname + 1, ft_strlen(varname + 1) + 1)) + { + ft_printf("(%s)\n", *p_env); + while (**p_env != '\0' && **p_env != '=') + **p_env += 1; + if (**p_env == '=') + **p_env += 1; + if (!(varval = ft_strdup(*p_env))) + { + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + return (varval); + } + p_env += 1; } - return (argc); + return (NULL); } |