xref: /minix3/lib/libc/string/wcslcat.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: wcslcat.c,v 1.3 2012/06/25 22:32:46 abs Exp $	*/
22fe8fb19SBen Gras /*	from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp 	*/
32fe8fb19SBen Gras 
42fe8fb19SBen Gras /*
52fe8fb19SBen Gras  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
62fe8fb19SBen Gras  * All rights reserved.
72fe8fb19SBen Gras  *
82fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras  * are met:
112fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras  * 3. The name of the author may not be used to endorse or promote products
172fe8fb19SBen Gras  *    derived from this software without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
202fe8fb19SBen Gras  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
212fe8fb19SBen Gras  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
222fe8fb19SBen Gras  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
232fe8fb19SBen Gras  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
242fe8fb19SBen Gras  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
252fe8fb19SBen Gras  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
262fe8fb19SBen Gras  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
272fe8fb19SBen Gras  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
282fe8fb19SBen Gras  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #include <sys/cdefs.h>
322fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
33*f14fb602SLionel Sambuc __RCSID("$NetBSD: wcslcat.c,v 1.3 2012/06/25 22:32:46 abs Exp $");
342fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
352fe8fb19SBen Gras 
362fe8fb19SBen Gras #include <sys/types.h>
372fe8fb19SBen Gras #include <assert.h>
382fe8fb19SBen Gras #include <wchar.h>
392fe8fb19SBen Gras 
402fe8fb19SBen Gras /*
412fe8fb19SBen Gras  * Appends src to string dst of size siz (unlike wcsncat, siz is the
422fe8fb19SBen Gras  * full size of dst, not space left).  At most siz-1 characters
432fe8fb19SBen Gras  * will be copied.  Always NUL terminates (unless siz == 0).
442fe8fb19SBen Gras  * Returns wcslen(initial dst) + wcslen(src); if retval >= siz,
452fe8fb19SBen Gras  * truncation occurred.
462fe8fb19SBen Gras  */
472fe8fb19SBen Gras size_t
wcslcat(wchar_t * dst,const wchar_t * src,size_t siz)48*f14fb602SLionel Sambuc wcslcat(wchar_t *dst, const wchar_t *src, size_t siz)
492fe8fb19SBen Gras {
502fe8fb19SBen Gras 	wchar_t *d = dst;
512fe8fb19SBen Gras 	const wchar_t *s = src;
522fe8fb19SBen Gras 	size_t n = siz;
532fe8fb19SBen Gras 	size_t dlen;
542fe8fb19SBen Gras 
552fe8fb19SBen Gras 	_DIAGASSERT(dst != NULL);
562fe8fb19SBen Gras 	_DIAGASSERT(src != NULL);
572fe8fb19SBen Gras 
582fe8fb19SBen Gras 	/* Find the end of dst and adjust bytes left but don't go past end */
592fe8fb19SBen Gras 	while (*d != '\0' && n-- != 0)
602fe8fb19SBen Gras 		d++;
612fe8fb19SBen Gras 	dlen = d - dst;
622fe8fb19SBen Gras 	n = siz - dlen;
632fe8fb19SBen Gras 
642fe8fb19SBen Gras 	if (n == 0)
652fe8fb19SBen Gras 		return(dlen + wcslen(s));
662fe8fb19SBen Gras 	while (*s != '\0') {
672fe8fb19SBen Gras 		if (n != 1) {
682fe8fb19SBen Gras 			*d++ = *s;
692fe8fb19SBen Gras 			n--;
702fe8fb19SBen Gras 		}
712fe8fb19SBen Gras 		s++;
722fe8fb19SBen Gras 	}
732fe8fb19SBen Gras 	*d = '\0';
742fe8fb19SBen Gras 
752fe8fb19SBen Gras 	return(dlen + (s - src));	/* count does not include NUL */
762fe8fb19SBen Gras }
77