-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strlcat.c
39 lines (36 loc) · 1.79 KB
/
ft_strlcat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgasc <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 16:09:25 by lgasc #+# #+# */
/* Updated: 2024/01/10 17:20:34 by lgasc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
///The **`strlcat`**() function appends the NUL-terminated
/// string `src`_ to the end of _`dst`_. It will append at most
/// _`size`_ - `strlen(dst)` - 1 bytes, NUL-terminating the result.
///@see https://stackoverflow.com/a/33155352
///@remark This function aims to replicate the `libc` function `strlcat`.
size_t ft_strlcat(char *destination, const char *source, size_t size)
{
size_t old_destination_size;
if ((! source) || (! source))
return (0);
if (size <= ft_strlen(destination))
return (size + ft_strlen(source));
old_destination_size = ft_strlen(destination);
if (ft_strlen(source) <= size - 1 - old_destination_size)
ft_memmove(
&destination[old_destination_size], source, ft_strlen(source) + 1);
else
{
ft_memmove(&destination[old_destination_size],
source, size - old_destination_size - 1);
destination[size -1] = '\0';
}
return (old_destination_size + ft_strlen(source));
}