1*9e66e6d7Sabs /* $NetBSD: wcslcpy.c,v 1.3 2012/06/25 22:32:46 abs Exp $ */
2383f218aSitojun /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert 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: wcslcpy.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 * Copy src to string dst of size siz. At most siz-1 characters
42383f218aSitojun * will be copied. Always NUL terminates (unless siz == 0).
43383f218aSitojun * Returns wcslen(src); if retval >= siz, truncation occurred.
44383f218aSitojun */
45383f218aSitojun size_t
wcslcpy(wchar_t * dst,const wchar_t * src,size_t siz)46*9e66e6d7Sabs wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz)
47383f218aSitojun {
4861017941Slukem wchar_t *d = dst;
4961017941Slukem const wchar_t *s = src;
5061017941Slukem size_t n = siz;
51383f218aSitojun
52383f218aSitojun _DIAGASSERT(dst != NULL);
53383f218aSitojun _DIAGASSERT(src != NULL);
54383f218aSitojun
55383f218aSitojun /* Copy as many bytes as will fit */
56383f218aSitojun if (n != 0 && --n != 0) {
57383f218aSitojun do {
58383f218aSitojun if ((*d++ = *s++) == 0)
59383f218aSitojun break;
60383f218aSitojun } while (--n != 0);
61383f218aSitojun }
62383f218aSitojun
63383f218aSitojun /* Not enough room in dst, add NUL and traverse rest of src */
64383f218aSitojun if (n == 0) {
65383f218aSitojun if (siz != 0)
66383f218aSitojun *d = '\0'; /* NUL-terminate dst */
67383f218aSitojun while (*s++)
68383f218aSitojun ;
69383f218aSitojun }
70383f218aSitojun
71383f218aSitojun return(s - src - 1); /* count does not include NUL */
72383f218aSitojun }
73