176612Stshiozak/*
276612Stshiozak * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
376612Stshiozak * All rights reserved.
476612Stshiozak *
576612Stshiozak * Redistribution and use in source and binary forms, with or without
676612Stshiozak * modification, are permitted provided that the following conditions
776612Stshiozak * are met:
876612Stshiozak * 1. Redistributions of source code must retain the above copyright
976612Stshiozak *    notice, this list of conditions and the following disclaimer.
1076612Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
1176612Stshiozak *    notice, this list of conditions and the following disclaimer in the
1276612Stshiozak *    documentation and/or other materials provided with the distribution.
1376612Stshiozak * 3. The name of the author may not be used to endorse or promote products
1476612Stshiozak *    derived from this software without specific prior written permission.
1576612Stshiozak *
1676612Stshiozak * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1776612Stshiozak * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
1876612Stshiozak * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
1976612Stshiozak * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2076612Stshiozak * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2176612Stshiozak * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2276612Stshiozak * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2376612Stshiozak * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2476612Stshiozak * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2576612Stshiozak * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2676612Stshiozak *
2777117Sobrien *	from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp
2876612Stshiozak */
2976612Stshiozak
3076612Stshiozak#include <sys/cdefs.h>
3192986Sobrien#if 0
3276612Stshiozak#if defined(LIBC_SCCS) && !defined(lint)
3376612Stshiozak__RCSID("$NetBSD: wcslcat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $");
3476612Stshiozak#endif /* LIBC_SCCS and not lint */
3592986Sobrien#endif
3686170Sobrien__FBSDID("$FreeBSD$");
3776612Stshiozak
3876612Stshiozak#include <sys/types.h>
3976612Stshiozak#include <wchar.h>
4076612Stshiozak
4176612Stshiozak/*
4276612Stshiozak * Appends src to string dst of size siz (unlike wcsncat, siz is the
4376612Stshiozak * full size of dst, not space left).  At most siz-1 characters
4476612Stshiozak * will be copied.  Always NUL terminates (unless siz == 0).
4576612Stshiozak * Returns wcslen(initial dst) + wcslen(src); if retval >= siz,
4676612Stshiozak * truncation occurred.
4776612Stshiozak */
4876612Stshiozaksize_t
49188080Sdangerwcslcat(wchar_t *dst, const wchar_t *src, size_t siz)
5076612Stshiozak{
5176612Stshiozak	wchar_t *d = dst;
5276612Stshiozak	const wchar_t *s = src;
5376612Stshiozak	size_t n = siz;
5476612Stshiozak	size_t dlen;
5576612Stshiozak
5676612Stshiozak	/* Find the end of dst and adjust bytes left but don't go past end */
5776612Stshiozak	while (*d != '\0' && n-- != 0)
5876612Stshiozak		d++;
5976612Stshiozak	dlen = d - dst;
6076612Stshiozak	n = siz - dlen;
6176612Stshiozak
6276612Stshiozak	if (n == 0)
6376612Stshiozak		return(dlen + wcslen(s));
6476612Stshiozak	while (*s != '\0') {
6576612Stshiozak		if (n != 1) {
6676612Stshiozak			*d++ = *s;
6776612Stshiozak			n--;
6876612Stshiozak		}
6976612Stshiozak		s++;
7076612Stshiozak	}
7176612Stshiozak	*d = '\0';
7276612Stshiozak
7376612Stshiozak	return(dlen + (s - src));	/* count does not include NUL */
7476612Stshiozak}
75