diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-10 19:53:00 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-10 19:53:00 +0200 |
commit | 89c27ba72e11665823cba7023f694a8639891ccb (patch) | |
tree | dfdd9771eb2e4bb2ae5d354424350a8edcfbd43b | |
parent | I was born an idiot I swear (diff) | |
download | 42-minishell-89c27ba72e11665823cba7023f694a8639891ccb.tar.gz 42-minishell-89c27ba72e11665823cba7023f694a8639891ccb.tar.bz2 42-minishell-89c27ba72e11665823cba7023f694a8639891ccb.tar.xz 42-minishell-89c27ba72e11665823cba7023f694a8639891ccb.tar.zst 42-minishell-89c27ba72e11665823cba7023f694a8639891ccb.zip |
New libft func, cool stuff
-rw-r--r-- | libft/Makefile | 1 | ||||
-rw-r--r-- | libft/include/libft.h | 5 | ||||
-rw-r--r-- | libft/src/ft_strsubst_s.c | 31 | ||||
-rw-r--r-- | src/m_prompt.c | 9 | ||||
-rw-r--r-- | src/u_vars_next.c | 2 |
5 files changed, 40 insertions, 8 deletions
diff --git a/libft/Makefile b/libft/Makefile index 431ec0b..3c11199 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -112,6 +112,7 @@ 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_NAME += ft_strsubst_s.c #------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #------------------------------------------------------------------------------# diff --git a/libft/include/libft.h b/libft/include/libft.h index 68b8d9b..c31482c 100644 --- a/libft/include/libft.h +++ b/libft/include/libft.h @@ -15,7 +15,7 @@ #include <stddef.h> #include <stdarg.h> -#include <inttypes.h> +#include <stdint.h> # define FT_MIN_HEX_BASE "0123456789abcdef" # define FT_MAJ_HEX_BASE "0123456789ABCDEF" @@ -123,6 +123,9 @@ char *ft_nstr(size_t size); char *ft_strsubst(char *str, const char *pattern, const char *subst); +int8_t ft_strsubst_s(char *str, + const char *pattern, + const char *subst); char *ft_strtok_r(char *s, const char *delim, char **last); char *ft_strtok(char *s, const char *delim); char **ft_split(const char *s, char c); diff --git a/libft/src/ft_strsubst_s.c b/libft/src/ft_strsubst_s.c new file mode 100644 index 0000000..ea61610 --- /dev/null +++ b/libft/src/ft_strsubst_s.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsubst_s.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 <stdint.h> +#include <stdlib.h> + +int8_t + ft_strsubst_s(char *str, const char *pattern, const char *subst) +{ + char *ptr; + + if ((ptr = ft_strnstr(str, pattern, ft_strlen(str))) == NULL) + return (1); + (void)ft_memmove(str + ((ptr - str) + ft_strlen(subst)), + str + ((ptr - str) + ft_strlen(pattern)), + ft_strlen(str + ((ptr - str) + ft_strlen(pattern))) + 1); + (void)ft_memmove(str + (ptr - str), + subst, + ft_strlen(subst)); + return (0); +} diff --git a/src/m_prompt.c b/src/m_prompt.c index dc17939..2d88088 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -24,7 +24,6 @@ static char *m_subst_prompt_rice(char var[], t_msh *msh) { char tmp[PATH_MAX]; - char *ptr; size_t i; size_t j; @@ -42,9 +41,9 @@ static char { u_get_var_value(tmp, "$PWD", PATH_MAX, msh); if (tmp[0] != C_NUL) - var = ft_strsubst(var, "\\w", tmp); + ft_strsubst_s(var, "\\w", tmp); else - var = ft_strsubst(var, "\\w", "?"); + ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'W') { @@ -60,10 +59,10 @@ static char (void)ft_memmove(tmp, tmp + j, ((ft_strlen(tmp) - j) + 1) * sizeof(char)); } - var = ft_strsubst(var, "\\W", tmp); + ft_strsubst_s(var, "\\W", tmp); } else - var = ft_strsubst(var, "\\w", "?"); + ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'v') var = ft_strsubst(var, "\\v", FT_MSH_VERSION); diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 2135c6b..2f23667 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -83,13 +83,11 @@ void u_subst_var_value(const char varname[], const char newval[], t_msh *msh) { char new_line_fmt[ARG_MAX]; - /* char newval_subst[ARG_MAX]; */ int64_t env_line; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); - /* ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval_subst); */ 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))) == NULL) |