From 66a830bfea3e0cba1bff406860ccae78dfa2ce40 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 24 Aug 2023 09:47:08 +0200 Subject: [PATCH] Added ft_strndup, because it is usefull for ft_printf. Changed libft.h accordingly. --- ft_strndup.c | 30 ++++++++++++++++++++++++++++++ libft.h | 3 ++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 ft_strndup.c diff --git a/ft_strndup.c b/ft_strndup.c new file mode 100644 index 0000000..84e4bde --- /dev/null +++ b/ft_strndup.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/24 09:18:49 by ljiriste #+# #+# */ +/* Updated: 2023/08/24 09:24:51 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // For the malloc function +#include "libft.h" + +char *ft_strndup(const char *s, size_t n) +{ + char *dest; + size_t s_len; + + s_len = ft_strlen(s); + if (s_len < n) + n = s_len; + dest = malloc((n + 1) * sizeof(char)); + if (dest == NULL) + return (dest); + ft_strlcpy(dest, s, n); + dest[n + 1] = '\0'; + return (dest); +} diff --git a/libft.h b/libft.h index 1d8eb25..9008b3f 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 17:12:57 by ljiriste ### ########.fr */ +/* Updated: 2023/08/24 09:24:32 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strdup(const char *s); +char *ft_strndup(const char *s, size_t n); int ft_toupper(int c); int ft_tolower(int c); -- 2.30.2