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