From 870ce44765ae948471d3509d2a80ca589b53457c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 15 Oct 2024 16:17:49 +0200 Subject: [PATCH] ft_strlcat: initial implementation --- Makefile | 5 +++-- ft_strlcat.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 3 ++- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 ft_strlcat.c diff --git a/Makefile b/Makefile index 60ece69..b1b2aaf 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/15 14:53:02 by kcolin ### ########.fr # +# Updated: 2024/10/15 16:00:12 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -22,7 +22,8 @@ SOURCES = ft_isalpha.c \ ft_bzero.c \ ft_memcpy.c \ ft_memmove.c \ - ft_strlcpy.c + ft_strlcpy.c \ + ft_strlcat.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_strlcat.c b/ft_strlcat.c new file mode 100644 index 0000000..9afbd85 --- /dev/null +++ b/ft_strlcat.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/15 16:00:39 by kcolin #+# #+# */ +/* Updated: 2024/10/15 16:17:34 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_size ft_strlcat(char *dst, const char *src, t_size size) +{ + t_size dst_len; + t_size src_len; + t_size i; + + dst_len = 0; + while (dst[dst_len] != '\0') + dst_len++; + src_len = 0; + while (src[src_len] != '\0') + src_len++; + if (size <= dst_len) + return (size + src_len); + i = 0; + while (src[i] != '\0' && i < size - dst_len - 1) + { + dst[dst_len + i] = src[i]; + i++; + } + dst[dst_len + i] = '\0'; + return (dst_len + src_len); +} + +/* +#include +#include + +int main(void) +{ + char dest1[40] = "Hi there, hello!!!!!"; + char dest2[40] = "Hi there, hello!!!!!"; + char *src = " Hello!"; + int res1; + size_t res2; + + res1 = ft_strlcat(dest1, src, 20); + printf("mine:\t%u\t'%s'\n", res1, dest1); + res2 = strlcat(dest2, src, 20); + printf("lib:\t%zu\t'%s'\n", res2, dest2); + res1 = ft_strlcat(dest1, src, 40); + printf("mine:\t%u\t'%s'\n", res1, dest1); + res2 = strlcat(dest2, src, 40); + printf("lib:\t%zu\t'%s'\n", res2, dest2); + return (0); +} +*/ diff --git a/libft.h b/libft.h index 8a54f1d..5d9298a 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ -/* Updated: 2024/10/15 14:42:16 by kcolin ### ########.fr */ +/* Updated: 2024/10/15 15:59:51 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,5 +28,6 @@ void ft_bzero(void *s, t_size n); void *ft_memcpy(void *dest, const void *src, t_size n); void *ft_memmove(void *dest, const void *src, t_size n); t_size ft_strlcpy(char *dst, const char *src, t_size size); +t_size ft_strlcat(char *dst, const char *src, t_size size); #endif