xref: /freebsd/lib/libc/string/wcslcat.c (revision 5864b79cd2852339f3025ee81769233071d60934)
19829d36aSTakuya SHIOZAKI /*
29829d36aSTakuya SHIOZAKI  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
39829d36aSTakuya SHIOZAKI  * All rights reserved.
49829d36aSTakuya SHIOZAKI  *
59829d36aSTakuya SHIOZAKI  * Redistribution and use in source and binary forms, with or without
69829d36aSTakuya SHIOZAKI  * modification, are permitted provided that the following conditions
79829d36aSTakuya SHIOZAKI  * are met:
89829d36aSTakuya SHIOZAKI  * 1. Redistributions of source code must retain the above copyright
99829d36aSTakuya SHIOZAKI  *    notice, this list of conditions and the following disclaimer.
109829d36aSTakuya SHIOZAKI  * 2. Redistributions in binary form must reproduce the above copyright
119829d36aSTakuya SHIOZAKI  *    notice, this list of conditions and the following disclaimer in the
129829d36aSTakuya SHIOZAKI  *    documentation and/or other materials provided with the distribution.
139829d36aSTakuya SHIOZAKI  * 3. The name of the author may not be used to endorse or promote products
149829d36aSTakuya SHIOZAKI  *    derived from this software without specific prior written permission.
159829d36aSTakuya SHIOZAKI  *
169829d36aSTakuya SHIOZAKI  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
179829d36aSTakuya SHIOZAKI  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
189829d36aSTakuya SHIOZAKI  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
199829d36aSTakuya SHIOZAKI  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
209829d36aSTakuya SHIOZAKI  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
219829d36aSTakuya SHIOZAKI  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
229829d36aSTakuya SHIOZAKI  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
239829d36aSTakuya SHIOZAKI  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
249829d36aSTakuya SHIOZAKI  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
259829d36aSTakuya SHIOZAKI  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269829d36aSTakuya SHIOZAKI  *
275864b79cSDavid E. O'Brien  *	from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp
289829d36aSTakuya SHIOZAKI  */
299829d36aSTakuya SHIOZAKI 
309829d36aSTakuya SHIOZAKI #include <sys/cdefs.h>
319829d36aSTakuya SHIOZAKI #if defined(LIBC_SCCS) && !defined(lint)
329829d36aSTakuya SHIOZAKI __RCSID("$NetBSD: wcslcat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $");
339829d36aSTakuya SHIOZAKI #endif /* LIBC_SCCS and not lint */
345864b79cSDavid E. O'Brien #ifndef lint
355864b79cSDavid E. O'Brien static const char rcsid[] =
365864b79cSDavid E. O'Brien   "$FreeBSD$";
375864b79cSDavid E. O'Brien #endif
389829d36aSTakuya SHIOZAKI 
399829d36aSTakuya SHIOZAKI #include <sys/types.h>
409829d36aSTakuya SHIOZAKI #include <assert.h>
419829d36aSTakuya SHIOZAKI #include <wchar.h>
429829d36aSTakuya SHIOZAKI 
439829d36aSTakuya SHIOZAKI /*
449829d36aSTakuya SHIOZAKI  * Appends src to string dst of size siz (unlike wcsncat, siz is the
459829d36aSTakuya SHIOZAKI  * full size of dst, not space left).  At most siz-1 characters
469829d36aSTakuya SHIOZAKI  * will be copied.  Always NUL terminates (unless siz == 0).
479829d36aSTakuya SHIOZAKI  * Returns wcslen(initial dst) + wcslen(src); if retval >= siz,
489829d36aSTakuya SHIOZAKI  * truncation occurred.
499829d36aSTakuya SHIOZAKI  */
509829d36aSTakuya SHIOZAKI size_t
519829d36aSTakuya SHIOZAKI wcslcat(dst, src, siz)
529829d36aSTakuya SHIOZAKI 	wchar_t *dst;
539829d36aSTakuya SHIOZAKI 	const wchar_t *src;
549829d36aSTakuya SHIOZAKI 	size_t siz;
559829d36aSTakuya SHIOZAKI {
569829d36aSTakuya SHIOZAKI 	wchar_t *d = dst;
579829d36aSTakuya SHIOZAKI 	const wchar_t *s = src;
589829d36aSTakuya SHIOZAKI 	size_t n = siz;
599829d36aSTakuya SHIOZAKI 	size_t dlen;
609829d36aSTakuya SHIOZAKI 
619829d36aSTakuya SHIOZAKI 	/* Find the end of dst and adjust bytes left but don't go past end */
629829d36aSTakuya SHIOZAKI 	while (*d != '\0' && n-- != 0)
639829d36aSTakuya SHIOZAKI 		d++;
649829d36aSTakuya SHIOZAKI 	dlen = d - dst;
659829d36aSTakuya SHIOZAKI 	n = siz - dlen;
669829d36aSTakuya SHIOZAKI 
679829d36aSTakuya SHIOZAKI 	if (n == 0)
689829d36aSTakuya SHIOZAKI 		return(dlen + wcslen(s));
699829d36aSTakuya SHIOZAKI 	while (*s != '\0') {
709829d36aSTakuya SHIOZAKI 		if (n != 1) {
719829d36aSTakuya SHIOZAKI 			*d++ = *s;
729829d36aSTakuya SHIOZAKI 			n--;
739829d36aSTakuya SHIOZAKI 		}
749829d36aSTakuya SHIOZAKI 		s++;
759829d36aSTakuya SHIOZAKI 	}
769829d36aSTakuya SHIOZAKI 	*d = '\0';
779829d36aSTakuya SHIOZAKI 
789829d36aSTakuya SHIOZAKI 	return(dlen + (s - src));	/* count does not include NUL */
799829d36aSTakuya SHIOZAKI }
80