xref: /freebsd/contrib/libfido2/openbsd-compat/strlcat.c (revision 0afa8e065e14bb8fd338d75690e0238c00167d40)
1*0afa8e06SEd Maste /*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
2*0afa8e06SEd Maste 
3*0afa8e06SEd Maste /*
4*0afa8e06SEd Maste  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5*0afa8e06SEd Maste  *
6*0afa8e06SEd Maste  * Permission to use, copy, modify, and distribute this software for any
7*0afa8e06SEd Maste  * purpose with or without fee is hereby granted, provided that the above
8*0afa8e06SEd Maste  * copyright notice and this permission notice appear in all copies.
9*0afa8e06SEd Maste  *
10*0afa8e06SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*0afa8e06SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*0afa8e06SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*0afa8e06SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*0afa8e06SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*0afa8e06SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*0afa8e06SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*0afa8e06SEd Maste  */
18*0afa8e06SEd Maste 
19*0afa8e06SEd Maste /* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
20*0afa8e06SEd Maste 
21*0afa8e06SEd Maste #include "openbsd-compat.h"
22*0afa8e06SEd Maste 
23*0afa8e06SEd Maste #if !defined(HAVE_STRLCAT)
24*0afa8e06SEd Maste 
25*0afa8e06SEd Maste #include <sys/types.h>
26*0afa8e06SEd Maste #include <string.h>
27*0afa8e06SEd Maste 
28*0afa8e06SEd Maste /*
29*0afa8e06SEd Maste  * Appends src to string dst of size siz (unlike strncat, siz is the
30*0afa8e06SEd Maste  * full size of dst, not space left).  At most siz-1 characters
31*0afa8e06SEd Maste  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
32*0afa8e06SEd Maste  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
33*0afa8e06SEd Maste  * If retval >= siz, truncation occurred.
34*0afa8e06SEd Maste  */
35*0afa8e06SEd Maste size_t
strlcat(char * dst,const char * src,size_t siz)36*0afa8e06SEd Maste strlcat(char *dst, const char *src, size_t siz)
37*0afa8e06SEd Maste {
38*0afa8e06SEd Maste 	char *d = dst;
39*0afa8e06SEd Maste 	const char *s = src;
40*0afa8e06SEd Maste 	size_t n = siz;
41*0afa8e06SEd Maste 	size_t dlen;
42*0afa8e06SEd Maste 
43*0afa8e06SEd Maste 	/* Find the end of dst and adjust bytes left but don't go past end */
44*0afa8e06SEd Maste 	while (n-- != 0 && *d != '\0')
45*0afa8e06SEd Maste 		d++;
46*0afa8e06SEd Maste 	dlen = d - dst;
47*0afa8e06SEd Maste 	n = siz - dlen;
48*0afa8e06SEd Maste 
49*0afa8e06SEd Maste 	if (n == 0)
50*0afa8e06SEd Maste 		return(dlen + strlen(s));
51*0afa8e06SEd Maste 	while (*s != '\0') {
52*0afa8e06SEd Maste 		if (n != 1) {
53*0afa8e06SEd Maste 			*d++ = *s;
54*0afa8e06SEd Maste 			n--;
55*0afa8e06SEd Maste 		}
56*0afa8e06SEd Maste 		s++;
57*0afa8e06SEd Maste 	}
58*0afa8e06SEd Maste 	*d = '\0';
59*0afa8e06SEd Maste 
60*0afa8e06SEd Maste 	return(dlen + (s - src));	/* count does not include NUL */
61*0afa8e06SEd Maste }
62*0afa8e06SEd Maste 
63*0afa8e06SEd Maste #endif /* !defined(HAVE_STRLCAT) */
64