1187090Sdelphij/*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
249593Simp
349593Simp/*
449593Simp * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
549593Simp *
6187090Sdelphij * Permission to use, copy, modify, and distribute this software for any
7187090Sdelphij * purpose with or without fee is hereby granted, provided that the above
8187090Sdelphij * copyright notice and this permission notice appear in all copies.
949593Simp *
10187090Sdelphij * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11187090Sdelphij * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12187090Sdelphij * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13187090Sdelphij * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14187090Sdelphij * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15187090Sdelphij * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16187090Sdelphij * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1749593Simp */
1849593Simp
1986170Sobrien#include <sys/cdefs.h>
2086170Sobrien__FBSDID("$FreeBSD: releng/10.2/lib/libc/string/strlcat.c 189133 2009-02-28 05:15:02Z das $");
2149593Simp
2249593Simp#include <sys/types.h>
2349593Simp#include <string.h>
2449593Simp
2549593Simp/*
2649593Simp * Appends src to string dst of size siz (unlike strncat, siz is the
2749593Simp * full size of dst, not space left).  At most siz-1 characters
2871191Sjedgar * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
2980274Skris * Returns strlen(src) + MIN(siz, strlen(initial dst)).
3080274Skris * If retval >= siz, truncation occurred.
3149593Simp */
3280274Skrissize_t
33189133Sdasstrlcat(char * __restrict dst, const char * __restrict src, size_t siz)
3449593Simp{
3592889Sobrien	char *d = dst;
3692889Sobrien	const char *s = src;
3792889Sobrien	size_t n = siz;
3849593Simp	size_t dlen;
3949593Simp
4049594Simp	/* Find the end of dst and adjust bytes left but don't go past end */
4171191Sjedgar	while (n-- != 0 && *d != '\0')
4249593Simp		d++;
4349593Simp	dlen = d - dst;
4449594Simp	n = siz - dlen;
4549593Simp
4649593Simp	if (n == 0)
4749593Simp		return(dlen + strlen(s));
4849593Simp	while (*s != '\0') {
4949593Simp		if (n != 1) {
5049593Simp			*d++ = *s;
5149593Simp			n--;
5249593Simp		}
5349593Simp		s++;
5449593Simp	}
5549593Simp	*d = '\0';
5649593Simp
5749593Simp	return(dlen + (s - src));	/* count does not include NUL */
5849593Simp}
59