diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-26 18:10:47 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-26 18:10:47 +0200 |
commit | c334947c7ae9722a3f3eca14410e244d62d95a54 (patch) | |
tree | 1d6723a1fe3614de25fa585ad01e40cf0bc1b365 | |
parent | Tryna subst vars (diff) | |
download | 42-minishell-c334947c7ae9722a3f3eca14410e244d62d95a54.tar.gz 42-minishell-c334947c7ae9722a3f3eca14410e244d62d95a54.tar.bz2 42-minishell-c334947c7ae9722a3f3eca14410e244d62d95a54.tar.xz 42-minishell-c334947c7ae9722a3f3eca14410e244d62d95a54.tar.zst 42-minishell-c334947c7ae9722a3f3eca14410e244d62d95a54.zip |
Nice vars gathering
Diffstat (limited to '')
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | libft/src/ft_nrealloc.c | 4 | ||||
-rw-r--r-- | src/ft_p_lcom_next.c | 2 | ||||
-rw-r--r-- | src/ft_u_utils.c | 50 | ||||
-rw-r--r-- | src/ft_u_utils.h | 2 | ||||
-rw-r--r-- | src/ft_u_vars.c | 118 | ||||
-rw-r--r-- | src/ft_u_vars.h | 21 |
7 files changed, 144 insertions, 54 deletions
@@ -42,6 +42,7 @@ SRCS_NAME += ft_p_line.c SRCS_NAME += ft_p_lcom.c SRCS_NAME += ft_p_lcom_next.c SRCS_NAME += ft_u_utils.c +SRCS_NAME += ft_u_vars.c #------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR}, ${SRCS_NAME}) #------------------------------------------------------------------------------# diff --git a/libft/src/ft_nrealloc.c b/libft/src/ft_nrealloc.c index ea13f19..82975d1 100644 --- a/libft/src/ft_nrealloc.c +++ b/libft/src/ft_nrealloc.c @@ -27,12 +27,12 @@ void } else if (!newsize) { - free(ptr); + ft_memdel((void*)&ptr); return (NULL); } if (!(nptr = malloc(newsize))) return (ptr); ft_memcpy(nptr, ptr, oldsize); - free(ptr); + ft_memdel((void*)&ptr); return (nptr); } diff --git a/src/ft_p_lcom_next.c b/src/ft_p_lcom_next.c index d1ab206..4715e47 100644 --- a/src/ft_p_lcom_next.c +++ b/src/ft_p_lcom_next.c @@ -15,7 +15,7 @@ #include <stdint.h> #include "ft_s_struct.h" -#include "ft_u_utils.h" +#include "ft_u_vars.h" char **ft_subst_vars(char *words[], diff --git a/src/ft_u_utils.c b/src/ft_u_utils.c index 594f2c8..7e2eb53 100644 --- a/src/ft_u_utils.c +++ b/src/ft_u_utils.c @@ -14,11 +14,6 @@ #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[]) { @@ -41,6 +36,7 @@ char ptr = envp; while (*ptr) { + /* TODO: rework this correctly */ if (ft_strncmp("HOME", *ptr, 4) == 0) { path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); @@ -50,47 +46,3 @@ char } return (NULL); } - -/* -** 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) -{ - char **p_env; - char *varval; - - p_env = msh->envp; - varval = NULL; - while (*p_env) - { - 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 (NULL); -} diff --git a/src/ft_u_utils.h b/src/ft_u_utils.h index 05b3ab3..d025cb2 100644 --- a/src/ft_u_utils.h +++ b/src/ft_u_utils.h @@ -17,7 +17,5 @@ uint64_t ft_get_argc(const char *args[]); char *ft_get_home_dir(char *envp[]); -char *ft_subst_var_value(const char varname[], - t_msh *msh); #endif diff --git a/src/ft_u_vars.c b/src/ft_u_vars.c new file mode 100644 index 0000000..4d10aeb --- /dev/null +++ b/src/ft_u_vars.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_u_vars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#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" + + +static char + *ft_set_rva(const char varname[], + t_msh *msh) +{ + char *rvarname; + + if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char)))) + { + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + ft_memcpy((char*)rvarname, (const char*)varname + 1, + ft_strlen(varname + 1)); + ft_strlcpy(rvarname + ft_strlen(varname + 1), "=", 2); + return (rvarname); +} + +static char + *ft_dup_val(char *p_env, + char *rvarname, + t_msh *msh) +{ + char *varval; + + if (!(varval = ft_strdup(p_env))) + { + ft_memdel((void*)&rvarname); + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + ft_memdel((void*)&rvarname); + return (varval); +} + +static char + *ft_get_frm_env(char rvarname[], + t_msh *msh) +{ + char **p_env; + char *varval; + + p_env = msh->envp; + while (*p_env) + { + if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname))) + { + while (**p_env != '\0' && **p_env != '=') + *p_env += 1; + if (**p_env == '=') + *p_env += 1; + varval = ft_dup_val(*p_env, rvarname, msh); + return (varval); + } + p_env += 1; + } + return (NULL); +} + +/* +** 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) +{ + /* TODO: check behaviour on empty vars -> "QWE=" */ + /* TODO: add support for special variables -> "$? $0..." */ + /* TODO: add support for global variables -> "$hey $nigga..." */ + char *varval; + char *rvarname; + + varval = NULL; + rvarname = ft_set_rva(varname, msh); + if ((varval = ft_get_special()) != NULL) + { + return (varval); + } + else + { + varval = ft_get_frm_env(rvarname, msh); + return (varval); + } + return (NULL); +} diff --git a/src/ft_u_vars.h b/src/ft_u_vars.h new file mode 100644 index 0000000..82aa668 --- /dev/null +++ b/src/ft_u_vars.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_u_vars.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_U_VARS_H +#define FT_U_VARS_H + +#include "ft_s_struct.h" + +char *ft_subst_var_value(const char varname[], + t_msh *msh); + +#endif |