1/*	$NetBSD: strlcat.c,v 1.4 2005/05/16 06:55:48 lukem Exp $	*/
2/*	from	NetBSD: strlcat.c,v 1.16 2003/10/27 00:12:42 lukem Exp	*/
3/*	from OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp	*/
4
5/*
6 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
13 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
15 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
17 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21#include "tnftp.h"
22
23/*
24 * Appends src to string dst of size siz (unlike strncat, siz is the
25 * full size of dst, not space left).  At most siz-1 characters
26 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
27 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
28 * If retval >= siz, truncation occurred.
29 */
30size_t
31strlcat(char *dst, const char *src, size_t siz)
32{
33	char *d = dst;
34	const char *s = src;
35	size_t n = siz;
36	size_t dlen;
37
38	/* Find the end of dst and adjust bytes left but don't go past end */
39	while (n-- != 0 && *d != '\0')
40		d++;
41	dlen = d - dst;
42	n = siz - dlen;
43
44	if (n == 0)
45		return(dlen + strlen(s));
46	while (*s != '\0') {
47		if (n != 1) {
48			*d++ = *s;
49			n--;
50		}
51		s++;
52	}
53	*d = '\0';
54
55	return(dlen + (s - src));	/* count does not include NUL */
56}
57