wcslcat.c revision 267654
11539Srgrimes/*
21539Srgrimes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
31539Srgrimes * All rights reserved.
41539Srgrimes *
51539Srgrimes * Redistribution and use in source and binary forms, with or without
61539Srgrimes * modification, are permitted provided that the following conditions
71539Srgrimes * are met:
81539Srgrimes * 1. Redistributions of source code must retain the above copyright
91539Srgrimes *    notice, this list of conditions and the following disclaimer.
101539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111539Srgrimes *    notice, this list of conditions and the following disclaimer in the
121539Srgrimes *    documentation and/or other materials provided with the distribution.
131539Srgrimes * 3. The name of the author may not be used to endorse or promote products
141539Srgrimes *    derived from this software without specific prior written permission.
151539Srgrimes *
161539Srgrimes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
171539Srgrimes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
181539Srgrimes * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
191539Srgrimes * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
201539Srgrimes * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
211539Srgrimes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
221539Srgrimes * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
231539Srgrimes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
241539Srgrimes * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
251539Srgrimes * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261539Srgrimes *
271539Srgrimes *	from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp
281539Srgrimes */
291539Srgrimes
301539Srgrimes#include <sys/cdefs.h>
311539Srgrimes#if 0
321539Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
331539Srgrimes__RCSID("$NetBSD: wcslcat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $");
341539Srgrimes#endif /* LIBC_SCCS and not lint */
358858Srgrimes#endif
361539Srgrimes__FBSDID("$FreeBSD: releng/9.3/lib/libc/string/wcslcat.c 188080 2009-02-03 17:58:20Z danger $");
371539Srgrimes
381539Srgrimes#include <sys/types.h>
391539Srgrimes#include <wchar.h>
401539Srgrimes
411539Srgrimes/*
428858Srgrimes * Appends src to string dst of size siz (unlike wcsncat, siz is the
431539Srgrimes * full size of dst, not space left).  At most siz-1 characters
441539Srgrimes * will be copied.  Always NUL terminates (unless siz == 0).
451539Srgrimes * Returns wcslen(initial dst) + wcslen(src); if retval >= siz,
461539Srgrimes * truncation occurred.
471539Srgrimes */
481539Srgrimessize_t
491539Srgrimeswcslcat(wchar_t *dst, const wchar_t *src, size_t siz)
501539Srgrimes{
511539Srgrimes	wchar_t *d = dst;
521539Srgrimes	const wchar_t *s = src;
531539Srgrimes	size_t n = siz;
541539Srgrimes	size_t dlen;
5536888Speter
5636888Speter	/* Find the end of dst and adjust bytes left but don't go past end */
5736888Speter	while (*d != '\0' && n-- != 0)
5850473Speter		d++;
5936888Speter	dlen = d - dst;
6036888Speter	n = siz - dlen;
611539Srgrimes
621539Srgrimes	if (n == 0)
631539Srgrimes		return(dlen + wcslen(s));
6484463Sbde	while (*s != '\0') {
65102227Smike		if (n != 1) {
6621055Speter			*d++ = *s;
67102227Smike			n--;
68102227Smike		}
69102227Smike		s++;
7072510Sume	}
7172510Sume	*d = '\0';
72102227Smike
73102227Smike	return(dlen + (s - src));	/* count does not include NUL */
74102227Smike}
7584463Sbde