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