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