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