xref: /freebsd/lib/libc/amd64/string/strncat.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Robert Clausecker
5  */
6 
7 #include <sys/cdefs.h>
8 
9 #include <string.h>
10 
11 #undef strncat	/* _FORTIFY_SOURCE */
12 
13 void *__memccpy(void *restrict, const void *restrict, int, size_t);
14 
15 char *
16 strncat(char *dest, const char *src, size_t n)
17 {
18 	size_t len;
19 	char *endptr;
20 
21 	len = strlen(dest);
22 	endptr = __memccpy(dest + len, src, '\0', n);
23 
24 	/* avoid an extra branch */
25 	if (endptr == NULL)
26 		endptr = dest + len + n + 1;
27 
28 	endptr[-1] = '\0';
29 
30 	return (dest);
31 }
32