libft/str/ft_strlcat.c

58 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 22:28:26 by rparodi #+# #+# */
/* Updated: 2024/10/31 18:10:19 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_strnlen(char *dest, size_t size)
{
size_t i;
i = 0;
while (i < size && dest[i] != '\0')
i++;
return (i);
}
/**
* @brief Appends a string with size limit.
*
* @param dst The destination buffer.
* @param src The source string.
* @param size The size of the destination buffer.
*
* @return The total length of the string it tried to create.
*/
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t i;
size_t j;
size_t destlen;
i = 0;
j = 0;
destlen = ft_strnlen(dest, size);
while (i < size && dest[i])
i++;
if (i == size)
return (i + ft_strlen(src));
while (src[j])
{
if (j < size - destlen - 1)
{
dest[i] = src[j];
i++;
}
j++;
}
dest[i] = '\0';
return (destlen + j);
}