ft_strlcat - gabrielcoelhodacunha-old/42sp-libft GitHub Wiki

Description

Concatenates dstsize - strlen(src) - 1 characters from src to the end of dst null terminating it.

Implementation

size_t	ft_strlcat(char *dst, const char *src, size_t dstsize)
{
	size_t	dst_len;
	size_t	src_len;

	dst_len = ft_strlen(dst);
	src_len = ft_strlen(src);
	// Checks if `dstsize` and `dst` are valid
	if (!dstsize || dst_len >= dstsize)
		return (dstsize + src_len);
	// Checks if it should copy everything (including the null terminating character) from `src` to `dst`
	if (src_len < dstsize - dst_len)
		ft_memcpy(dst + dst_len, src, src_len + 1);
	else
	{
		ft_memcpy(dst + dst_len, src, dstsize - dst_len - 1);
		dst[dstsize - 1] = '\0';
	}
	return (dst_len + src_len);
}

References