diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-26 20:07:03 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-26 20:07:03 +0200 |
commit | 2cc6f50e5b04b903ca97d8a5479c95ca72b52547 (patch) | |
tree | b6b6616a1dc0425aa167eeda90dbe0f040183a50 | |
parent | Got special variables running (diff) | |
download | 42-minishell-2cc6f50e5b04b903ca97d8a5479c95ca72b52547.tar.gz 42-minishell-2cc6f50e5b04b903ca97d8a5479c95ca72b52547.tar.bz2 42-minishell-2cc6f50e5b04b903ca97d8a5479c95ca72b52547.tar.xz 42-minishell-2cc6f50e5b04b903ca97d8a5479c95ca72b52547.tar.zst 42-minishell-2cc6f50e5b04b903ca97d8a5479c95ca72b52547.zip |
substsubtsubst
-rw-r--r-- | libft/Makefile | 1 | ||||
-rw-r--r-- | libft/inc/libft.h | 3 | ||||
-rw-r--r-- | libft/src/ft_strsubst.c | 36 | ||||
-rw-r--r-- | src/ft_p_lcom_next.c | 2 | ||||
-rw-r--r-- | src/ft_u_vars.c | 3 |
5 files changed, 42 insertions, 3 deletions
diff --git a/libft/Makefile b/libft/Makefile index a5fabcb..858e90b 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -106,6 +106,7 @@ SRCS_NAME += ft_printf_get_s_putlen.c SRCS_NAME += ft_printf_process.c SRCS_NAME += ft_printf_cat_output.c SRCS_NAME += ft_printf_flag_to_atoi.c +SRCS_NAME += ft_strsubst.c #------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #------------------------------------------------------------------------------# diff --git a/libft/inc/libft.h b/libft/inc/libft.h index f85f4b3..5ddb471 100644 --- a/libft/inc/libft.h +++ b/libft/inc/libft.h @@ -106,6 +106,9 @@ char *ft_uitoa_base(unsigned long n, char *base); char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); char *ft_nstr(size_t size); +char *ft_strsubst(char *str, + const char *pattern, + const char *subst); char **ft_split(const char *s, char c); /* diff --git a/libft/src/ft_strsubst.c b/libft/src/ft_strsubst.c new file mode 100644 index 0000000..dd89917 --- /dev/null +++ b/libft/src/ft_strsubst.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsubst.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> + +char + *ft_strsubst(char *str, const char *pattern, const char *subst) +{ + size_t nlen; + char *nstr; + char *ptr; + + if (!(ptr = ft_strnstr(str, pattern, ft_strlen(str)))) + return (NULL); + nlen = ft_strlen(str) - ft_strlen(pattern) + ft_strlen(subst); + if (!(nstr = (char*)malloc((nlen + 1) * sizeof(char)))) + return (NULL); + ft_memcpy(nstr, str, ptr - str); + ft_memcpy(nstr + (ptr - str), subst, ft_strlen(subst)); + ft_memcpy(nstr + (ptr - str + ft_strlen(subst)), + str + (ptr - str + ft_strlen(pattern)), + ft_strlen(str + (ptr - str + ft_strlen(pattern)))); + nstr[nlen + 1] = '\0'; + ft_memdel((void*)&str); + return (nstr); +} diff --git a/src/ft_p_lcom_next.c b/src/ft_p_lcom_next.c index 4715e47..5e374b6 100644 --- a/src/ft_p_lcom_next.c +++ b/src/ft_p_lcom_next.c @@ -23,7 +23,6 @@ char { /* TODO: norme */ char **p_words; - char **p_env; char *s_varname; int64_t i; size_t varlen; @@ -43,7 +42,6 @@ char if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen))) return (NULL); ft_printf("{%s} | {%s}\n", s_varname, ft_subst_var_value(s_varname, msh)); - p_env = msh->envp; ft_memdel((void*)&s_varname); varlen += 1; } diff --git a/src/ft_u_vars.c b/src/ft_u_vars.c index 80288e6..619e83d 100644 --- a/src/ft_u_vars.c +++ b/src/ft_u_vars.c @@ -35,7 +35,8 @@ static char } ft_memcpy((char*)rvarname, (const char*)varname + 1, ft_strlen(varname + 1)); - ft_strlcpy(rvarname + ft_strlen(varname + 1), "=", 2); + *(rvarname + ft_strlen(varname + 1)) = '='; + *(rvarname + ft_strlen(varname + 1) + 1) = '\0'; return (rvarname); } |