-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strlcat.c
executable file
·43 lines (40 loc) · 1.56 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
40
41
42
43
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mframbou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/03 19:48:03 by mframbou #+# #+# */
/* Updated: 2021/08/08 15:23:12 by mframbou ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
// If the dest is more bytes than n, it will be considered as n bytes long
// So if it wasn't null terminated, it will not be in the result
// strlcat("Hey", "This is a test", 2) => Traverse 2 bytes, don't find
// terminating char, assume that dest is 2 bytes long, then return 2 + len(src)
size_t ft_strlcat(char *dest, const char *src, size_t n)
{
size_t src_len;
size_t dst_len;
size_t i;
size_t j;
src_len = ft_strlen(src);
i = 0;
while (dest[i] != '\0' && i < n)
i++;
if (i == n)
return (n + src_len);
dst_len = i;
j = 0;
while (src[j] != '\0' && i < n - 1)
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return (dst_len + src_len);
}