wcslcpy.c revision 1.1 1 1.1 itojun /* $NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ */
2 1.1 itojun /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 1998 Todd C. Miller <Todd.Miller (at) courtesan.com>
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. The name of the author may not be used to endorse or promote products
17 1.1 itojun * derived from this software without specific prior written permission.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 1.1 itojun * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 1.1 itojun * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 1.1 itojun * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 1.1 itojun * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 1.1 itojun * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 1.1 itojun * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 1.1 itojun * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 1.1 itojun * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 1.1 itojun * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 itojun */
30 1.1 itojun
31 1.1 itojun #include <sys/cdefs.h>
32 1.1 itojun #if defined(LIBC_SCCS) && !defined(lint)
33 1.1 itojun __RCSID("$NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $");
34 1.1 itojun #endif /* LIBC_SCCS and not lint */
35 1.1 itojun
36 1.1 itojun #include <sys/types.h>
37 1.1 itojun #include <assert.h>
38 1.1 itojun #include <wchar.h>
39 1.1 itojun
40 1.1 itojun /*
41 1.1 itojun * Copy src to string dst of size siz. At most siz-1 characters
42 1.1 itojun * will be copied. Always NUL terminates (unless siz == 0).
43 1.1 itojun * Returns wcslen(src); if retval >= siz, truncation occurred.
44 1.1 itojun */
45 1.1 itojun size_t
46 1.1 itojun wcslcpy(dst, src, siz)
47 1.1 itojun wchar_t *dst;
48 1.1 itojun const wchar_t *src;
49 1.1 itojun size_t siz;
50 1.1 itojun {
51 1.1 itojun register wchar_t *d = dst;
52 1.1 itojun register const wchar_t *s = src;
53 1.1 itojun register size_t n = siz;
54 1.1 itojun
55 1.1 itojun _DIAGASSERT(dst != NULL);
56 1.1 itojun _DIAGASSERT(src != NULL);
57 1.1 itojun
58 1.1 itojun /* Copy as many bytes as will fit */
59 1.1 itojun if (n != 0 && --n != 0) {
60 1.1 itojun do {
61 1.1 itojun if ((*d++ = *s++) == 0)
62 1.1 itojun break;
63 1.1 itojun } while (--n != 0);
64 1.1 itojun }
65 1.1 itojun
66 1.1 itojun /* Not enough room in dst, add NUL and traverse rest of src */
67 1.1 itojun if (n == 0) {
68 1.1 itojun if (siz != 0)
69 1.1 itojun *d = '\0'; /* NUL-terminate dst */
70 1.1 itojun while (*s++)
71 1.1 itojun ;
72 1.1 itojun }
73 1.1 itojun
74 1.1 itojun return(s - src - 1); /* count does not include NUL */
75 1.1 itojun }
76