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