1185573Srwatson/*-
2162503Srwatson * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
3162503Srwatson * All rights reserved.
4162503Srwatson *
5162503Srwatson * Redistribution and use in source and binary forms, with or without
6162503Srwatson * modification, are permitted provided that the following conditions
7162503Srwatson * are met:
8162503Srwatson * 1. Redistributions of source code must retain the above copyright
9162503Srwatson *    notice, this list of conditions and the following disclaimer.
10162503Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11162503Srwatson *    notice, this list of conditions and the following disclaimer in the
12162503Srwatson *    documentation and/or other materials provided with the distribution.
13162503Srwatson * 3. The name of the author may not be used to endorse or promote products
14162503Srwatson *    derived from this software without specific prior written permission.
15162503Srwatson *
16162503Srwatson * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17162503Srwatson * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18162503Srwatson * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19162503Srwatson * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20162503Srwatson * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21162503Srwatson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22162503Srwatson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23162503Srwatson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24162503Srwatson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25162503Srwatson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26162503Srwatson *
27162503Srwatson * dollar OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp dollar
28162503Srwatson */
29162503Srwatson
30162503Srwatson/*
31162503Srwatson * Appends src to string dst of size siz (unlike strncat, siz is the
32162503Srwatson * full size of dst, not space left).  At most siz-1 characters
33162503Srwatson * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
34162503Srwatson * Returns strlen(src) + MIN(siz, strlen(initial dst)).
35162503Srwatson * If retval >= siz, truncation occurred.
36162503Srwatson */
37162503Srwatsonstatic size_t
38162503Srwatsonstrlcat(dst, src, siz)
39162503Srwatson	char *dst;
40162503Srwatson	const char *src;
41162503Srwatson	size_t siz;
42162503Srwatson{
43162503Srwatson	char *d = dst;
44162503Srwatson	const char *s = src;
45162503Srwatson	size_t n = siz;
46162503Srwatson	size_t dlen;
47162503Srwatson
48162503Srwatson	/* Find the end of dst and adjust bytes left but don't go past end */
49162503Srwatson	while (n-- != 0 && *d != '\0')
50162503Srwatson		d++;
51162503Srwatson	dlen = d - dst;
52162503Srwatson	n = siz - dlen;
53162503Srwatson
54162503Srwatson	if (n == 0)
55162503Srwatson		return(dlen + strlen(s));
56162503Srwatson	while (*s != '\0') {
57162503Srwatson		if (n != 1) {
58162503Srwatson			*d++ = *s;
59162503Srwatson			n--;
60162503Srwatson		}
61162503Srwatson		s++;
62162503Srwatson	}
63162503Srwatson	*d = '\0';
64162503Srwatson
65162503Srwatson	return(dlen + (s - src));	/* count does not include NUL */
66162503Srwatson}
67