1*9e66e6d7Sabs /* $NetBSD: wcslcat.c,v 1.3 2012/06/25 22:32:46 abs Exp $ */
2383f218aSitojun /* from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp */
3383f218aSitojun
4383f218aSitojun /*
5383f218aSitojun * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6383f218aSitojun * All rights reserved.
7383f218aSitojun *
8383f218aSitojun * Redistribution and use in source and binary forms, with or without
9383f218aSitojun * modification, are permitted provided that the following conditions
10383f218aSitojun * are met:
11383f218aSitojun * 1. Redistributions of source code must retain the above copyright
12383f218aSitojun * notice, this list of conditions and the following disclaimer.
13383f218aSitojun * 2. Redistributions in binary form must reproduce the above copyright
14383f218aSitojun * notice, this list of conditions and the following disclaimer in the
15383f218aSitojun * documentation and/or other materials provided with the distribution.
16383f218aSitojun * 3. The name of the author may not be used to endorse or promote products
17383f218aSitojun * derived from this software without specific prior written permission.
18383f218aSitojun *
19383f218aSitojun * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20383f218aSitojun * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21383f218aSitojun * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22383f218aSitojun * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23383f218aSitojun * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24383f218aSitojun * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25383f218aSitojun * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26383f218aSitojun * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27383f218aSitojun * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28383f218aSitojun * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29383f218aSitojun */
30383f218aSitojun
31383f218aSitojun #include <sys/cdefs.h>
32383f218aSitojun #if defined(LIBC_SCCS) && !defined(lint)
33*9e66e6d7Sabs __RCSID("$NetBSD: wcslcat.c,v 1.3 2012/06/25 22:32:46 abs Exp $");
34383f218aSitojun #endif /* LIBC_SCCS and not lint */
35383f218aSitojun
36383f218aSitojun #include <sys/types.h>
37383f218aSitojun #include <assert.h>
38383f218aSitojun #include <wchar.h>
39383f218aSitojun
40383f218aSitojun /*
41383f218aSitojun * Appends src to string dst of size siz (unlike wcsncat, siz is the
42383f218aSitojun * full size of dst, not space left). At most siz-1 characters
43383f218aSitojun * will be copied. Always NUL terminates (unless siz == 0).
44383f218aSitojun * Returns wcslen(initial dst) + wcslen(src); if retval >= siz,
45383f218aSitojun * truncation occurred.
46383f218aSitojun */
47383f218aSitojun size_t
wcslcat(wchar_t * dst,const wchar_t * src,size_t siz)48*9e66e6d7Sabs wcslcat(wchar_t *dst, const wchar_t *src, size_t siz)
49383f218aSitojun {
5061017941Slukem wchar_t *d = dst;
5161017941Slukem const wchar_t *s = src;
5261017941Slukem size_t n = siz;
53383f218aSitojun size_t dlen;
54383f218aSitojun
55383f218aSitojun _DIAGASSERT(dst != NULL);
56383f218aSitojun _DIAGASSERT(src != NULL);
57383f218aSitojun
58383f218aSitojun /* Find the end of dst and adjust bytes left but don't go past end */
59383f218aSitojun while (*d != '\0' && n-- != 0)
60383f218aSitojun d++;
61383f218aSitojun dlen = d - dst;
62383f218aSitojun n = siz - dlen;
63383f218aSitojun
64383f218aSitojun if (n == 0)
65383f218aSitojun return(dlen + wcslen(s));
66383f218aSitojun while (*s != '\0') {
67383f218aSitojun if (n != 1) {
68383f218aSitojun *d++ = *s;
69383f218aSitojun n--;
70383f218aSitojun }
71383f218aSitojun s++;
72383f218aSitojun }
73383f218aSitojun *d = '\0';
74383f218aSitojun
75383f218aSitojun return(dlen + (s - src)); /* count does not include NUL */
76383f218aSitojun }
77