diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-07-27 22:38:54 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-07-27 22:38:54 +0200 |
commit | 280ecd341f1ec88abe325fa69b197bca3e43d2f1 (patch) | |
tree | 3f10bdb634f5a727bbf2e410072bc8f6537310e4 /src | |
parent | In progress (diff) | |
download | 42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.gz 42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.bz2 42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.xz 42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.zst 42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.zip |
Now can subst env vars
Diffstat (limited to 'src')
-rw-r--r-- | src/b_cd.c | 4 | ||||
-rw-r--r-- | src/m_minishell.c | 2 | ||||
-rw-r--r-- | src/p_lcom_next.c | 2 | ||||
-rw-r--r-- | src/u_vars.c | 6 | ||||
-rw-r--r-- | src/u_vars.h | 2 | ||||
-rw-r--r-- | src/u_vars_next.c | 55 | ||||
-rw-r--r-- | src/u_vars_next.h | 5 |
7 files changed, 61 insertions, 15 deletions
@@ -33,7 +33,7 @@ static void if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) { if (!(*path = ft_strsubst(*path, - "~", get_var_value("$HOME", msh)))) + "~", u_get_var_value("$HOME", msh)))) { s_destroy(msh); fail_alloc(msh); @@ -55,7 +55,7 @@ uint8_t } else if (argc == 0) { - if (!(path = get_var_value("$HOME", msh))) + if (!(path = u_get_var_value("$HOME", msh))) return (1); } else diff --git a/src/m_minishell.c b/src/m_minishell.c index 1e065b4..66426b7 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -21,6 +21,7 @@ #include "s_struct.h" #include "s_init.h" #include "s_destroy.h" +#include "u_vars_next.h" int main(int argc, @@ -39,6 +40,7 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } + /* TODO: DELET THIS */ ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index a4fbb61..a0ca8d1 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -34,7 +34,7 @@ static int8_t varlen += 1; if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) return (-1); - varval = get_var_value(s_varname, msh); + varval = u_get_var_value(s_varname, msh); *p_words = ft_strsubst(*p_words, s_varname, varval); ft_memdel((void*)&s_varname); ft_memdel((void*)&varval); diff --git a/src/u_vars.c b/src/u_vars.c index 2b7fc4a..cd9d3f2 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -124,10 +124,10 @@ static char /* ** char* -** get_var_value(const char varname[], t_msh *msh); +** u_get_var_value(const char varname[], t_msh *msh); ** ** DESCRIPTION -** The get_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,7 +136,7 @@ static char */ char - *get_var_value(const char varname[], + *u_get_var_value(const char varname[], t_msh *msh) { /* TODO: check behaviour on empty vars -> "QWE=" */ diff --git a/src/u_vars.h b/src/u_vars.h index cad825c..fb8b924 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,7 @@ #include "s_struct.h" -char *get_var_value(const char varname[], +char *u_get_var_value(const char varname[], t_msh *msh); #endif diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 5626376..8134de3 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -10,21 +10,64 @@ /* */ /* ************************************************************************** */ +#include <libft.h> +#include <inttypes.h> +#include <stdlib.h> + #include "s_struct.h" + +static int64_t + u_get_var_line(const char varname[], + t_msh *msh) +{ + char **env_ptr; + int64_t i; + + env_ptr = msh->envp; + i = 0; + while (*env_ptr && + ft_strncmp(*env_ptr, varname, ft_strlen(varname)) != 0) + { + env_ptr++; + i++; + } + if (*env_ptr) + return (i); + else + return (-1); +} + /* ** void -** subst_var_value(const char varname[], t_msh *msh); +** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); ** ** DESCRIPTION -** The subst_var_value() changes the msh->envp value -** of the variable varname[]. If varname[] wasn't found +** The subst_var_value() changes the value of msh->envp +** variable varname[] with newval[]. If varname[] wasn't found ** in msh->envp, varname[] is searched in msh->vars. */ void - subst_var_value(const char varname[], + u_subst_var_value(const char varname[], + const char newval[], t_msh *msh) { - (void)varname; - (void)msh; + char new_line_fmt[2048]; + int64_t env_line; + + if ((env_line = u_get_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)))) + { + /* TODO: Handle this fail */ + } + (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); + } + else + { + /* TODO: Search in custom vars */ + } } diff --git a/src/u_vars_next.h b/src/u_vars_next.h index 1360190..118451f 100644 --- a/src/u_vars_next.h +++ b/src/u_vars_next.h @@ -15,7 +15,8 @@ #include "s_struct.h" -void subst_var_value(const char varname[], - t_msh *msh) +void u_subst_var_value(const char varname[], + const char newval[], + t_msh *msh); #endif |