xref: /netbsd-src/external/bsd/unbound/dist/compat/strlcat.c (revision 3b6c3722d8f990f9a667d638078aee8ccdc3c0f3)
1*3b6c3722Schristos /* compat/strlcat.c */
2*3b6c3722Schristos 
3*3b6c3722Schristos /*-
4*3b6c3722Schristos  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5*3b6c3722Schristos  * All rights reserved.
6*3b6c3722Schristos  *
7*3b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
8*3b6c3722Schristos  * modification, are permitted provided that the following conditions
9*3b6c3722Schristos  * are met:
10*3b6c3722Schristos  * 1. Redistributions of source code must retain the above copyright
11*3b6c3722Schristos  *    notice, this list of conditions and the following disclaimer.
12*3b6c3722Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*3b6c3722Schristos  *    notice, this list of conditions and the following disclaimer in the
14*3b6c3722Schristos  *    documentation and/or other materials provided with the distribution.
15*3b6c3722Schristos  * 3. The name of the author may not be used to endorse or promote products
16*3b6c3722Schristos  *    derived from this software without specific prior written permission.
17*3b6c3722Schristos  *
18*3b6c3722Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19*3b6c3722Schristos  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20*3b6c3722Schristos  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21*3b6c3722Schristos  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22*3b6c3722Schristos  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23*3b6c3722Schristos  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24*3b6c3722Schristos  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25*3b6c3722Schristos  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*3b6c3722Schristos  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27*3b6c3722Schristos  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*3b6c3722Schristos  */
29*3b6c3722Schristos 
30*3b6c3722Schristos /* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
31*3b6c3722Schristos 
32*3b6c3722Schristos #include "config.h"
33*3b6c3722Schristos #ifndef HAVE_STRLCAT
34*3b6c3722Schristos 
35*3b6c3722Schristos #include <sys/types.h>
36*3b6c3722Schristos #include <string.h>
37*3b6c3722Schristos 
38*3b6c3722Schristos /*
39*3b6c3722Schristos  * Appends src to string dst of size siz (unlike strncat, siz is the
40*3b6c3722Schristos  * full size of dst, not space left).  At most siz-1 characters
41*3b6c3722Schristos  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
42*3b6c3722Schristos  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
43*3b6c3722Schristos  * If retval >= siz, truncation occurred.
44*3b6c3722Schristos  */
45*3b6c3722Schristos size_t
strlcat(char * dst,const char * src,size_t siz)46*3b6c3722Schristos strlcat(char *dst, const char *src, size_t siz)
47*3b6c3722Schristos {
48*3b6c3722Schristos 	char *d = dst;
49*3b6c3722Schristos 	const char *s = src;
50*3b6c3722Schristos 	size_t n = siz;
51*3b6c3722Schristos 	size_t dlen;
52*3b6c3722Schristos 
53*3b6c3722Schristos 	/* Find the end of dst and adjust bytes left but don't go past end */
54*3b6c3722Schristos 	while (n-- != 0 && *d != '\0')
55*3b6c3722Schristos 		d++;
56*3b6c3722Schristos 	dlen = d - dst;
57*3b6c3722Schristos 	n = siz - dlen;
58*3b6c3722Schristos 
59*3b6c3722Schristos 	if (n == 0)
60*3b6c3722Schristos 		return(dlen + strlen(s));
61*3b6c3722Schristos 	while (*s != '\0') {
62*3b6c3722Schristos 		if (n != 1) {
63*3b6c3722Schristos 			*d++ = *s;
64*3b6c3722Schristos 			n--;
65*3b6c3722Schristos 		}
66*3b6c3722Schristos 		s++;
67*3b6c3722Schristos 	}
68*3b6c3722Schristos 	*d = '\0';
69*3b6c3722Schristos 
70*3b6c3722Schristos 	return(dlen + (s - src));	/* count does not include NUL */
71*3b6c3722Schristos }
72*3b6c3722Schristos 
73*3b6c3722Schristos #endif /* !HAVE_STRLCAT */
74