ft_strlcat: initial implementation

This commit is contained in:
Khaïs COLIN 2024-10-15 16:17:49 +02:00
parent a7ffedf420
commit 870ce44765
3 changed files with 66 additions and 3 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

61
ft_strlcat.c Normal file
View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <bsd/string.h>
#include <stdio.h>
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);
}
*/

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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