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