xref: /freebsd/sys/libkern/strlcat.c (revision 105d189b78f746db41e478b77c0389f253df696f)
1d64292d2SWarner Losh /*	$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $	*/
2a41df9e3SWarner Losh 
3a41df9e3SWarner Losh /*
4a41df9e3SWarner Losh  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5a41df9e3SWarner Losh  * All rights reserved.
6a41df9e3SWarner Losh  *
7a41df9e3SWarner Losh  * Redistribution and use in source and binary forms, with or without
8a41df9e3SWarner Losh  * modification, are permitted provided that the following conditions
9a41df9e3SWarner Losh  * are met:
10a41df9e3SWarner Losh  * 1. Redistributions of source code must retain the above copyright
11a41df9e3SWarner Losh  *    notice, this list of conditions and the following disclaimer.
12a41df9e3SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13a41df9e3SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14a41df9e3SWarner Losh  *    documentation and/or other materials provided with the distribution.
15a41df9e3SWarner Losh  * 3. The name of the author may not be used to endorse or promote products
16a41df9e3SWarner Losh  *    derived from this software without specific prior written permission.
17a41df9e3SWarner Losh  *
18a41df9e3SWarner Losh  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19a41df9e3SWarner Losh  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20a41df9e3SWarner Losh  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21a41df9e3SWarner Losh  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22a41df9e3SWarner Losh  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23a41df9e3SWarner Losh  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24a41df9e3SWarner Losh  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25a41df9e3SWarner Losh  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26a41df9e3SWarner Losh  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27a41df9e3SWarner Losh  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28a41df9e3SWarner Losh  */
29a41df9e3SWarner Losh 
30a41df9e3SWarner Losh #if defined(LIBC_SCCS) && !defined(lint)
31de5fe5d5SDavid E. O'Brien static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $");
32a41df9e3SWarner Losh #endif /* LIBC_SCCS and not lint */
33de5fe5d5SDavid E. O'Brien #include <sys/cdefs.h>
34de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
35a41df9e3SWarner Losh 
36a41df9e3SWarner Losh #include <sys/types.h>
37105d189bSPeter Wemm #include <sys/libkern.h>
38a41df9e3SWarner Losh 
39a41df9e3SWarner Losh /*
40a41df9e3SWarner Losh  * Appends src to string dst of size siz (unlike strncat, siz is the
41a41df9e3SWarner Losh  * full size of dst, not space left).  At most siz-1 characters
42420923e9SChris D. Faulhaber  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
436979f76fSKris Kennaway  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
446979f76fSKris Kennaway  * If retval >= siz, truncation occurred.
45a41df9e3SWarner Losh  */
466979f76fSKris Kennaway size_t
476979f76fSKris Kennaway strlcat(dst, src, siz)
48a41df9e3SWarner Losh 	char *dst;
49a41df9e3SWarner Losh 	const char *src;
50a41df9e3SWarner Losh 	size_t siz;
51a41df9e3SWarner Losh {
528fb3f3f6SDavid E. O'Brien 	char *d = dst;
538fb3f3f6SDavid E. O'Brien 	const char *s = src;
548fb3f3f6SDavid E. O'Brien 	size_t n = siz;
55a41df9e3SWarner Losh 	size_t dlen;
56a41df9e3SWarner Losh 
57d64292d2SWarner Losh 	/* Find the end of dst and adjust bytes left but don't go past end */
58420923e9SChris D. Faulhaber 	while (n-- != 0 && *d != '\0')
59a41df9e3SWarner Losh 		d++;
60a41df9e3SWarner Losh 	dlen = d - dst;
61d64292d2SWarner Losh 	n = siz - dlen;
62a41df9e3SWarner Losh 
63a41df9e3SWarner Losh 	if (n == 0)
64a41df9e3SWarner Losh 		return(dlen + strlen(s));
65a41df9e3SWarner Losh 	while (*s != '\0') {
66a41df9e3SWarner Losh 		if (n != 1) {
67a41df9e3SWarner Losh 			*d++ = *s;
68a41df9e3SWarner Losh 			n--;
69a41df9e3SWarner Losh 		}
70a41df9e3SWarner Losh 		s++;
71a41df9e3SWarner Losh 	}
72a41df9e3SWarner Losh 	*d = '\0';
73a41df9e3SWarner Losh 
74a41df9e3SWarner Losh 	return(dlen + (s - src));	/* count does not include NUL */
75a41df9e3SWarner Losh }
76