1 | /* |
---|
2 | FUNCTION |
---|
3 | <<wcslcpy>>---copy a wide-character string to specified length |
---|
4 | |
---|
5 | SYNOPSIS |
---|
6 | #include <wchar.h> |
---|
7 | size_t wcslcpy(wchar_t *<[dst]>, const wchar_t *<[src]>, size_t <[siz]>); |
---|
8 | |
---|
9 | DESCRIPTION |
---|
10 | <<wcslcpy>> copies wide characters from <[src]> to <[dst]> |
---|
11 | such that up to <[siz]> - 1 characters are copied. A |
---|
12 | terminating null is appended to the result, unless <[siz]> |
---|
13 | is zero. |
---|
14 | |
---|
15 | RETURNS |
---|
16 | <<wcslcpy>> returns the number of wide characters in <[src]>, |
---|
17 | not including the terminating null wide character. If the |
---|
18 | return value is greater than or equal to <[siz]>, then |
---|
19 | not all wide characters were copied from <[src]> and truncation |
---|
20 | occurred. |
---|
21 | |
---|
22 | PORTABILITY |
---|
23 | No supporting OS subroutines are required. |
---|
24 | */ |
---|
25 | |
---|
26 | /* $NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ */ |
---|
27 | /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */ |
---|
28 | |
---|
29 | /* |
---|
30 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
---|
31 | * All rights reserved. |
---|
32 | * |
---|
33 | * Redistribution and use in source and binary forms, with or without |
---|
34 | * modification, are permitted provided that the following conditions |
---|
35 | * are met: |
---|
36 | * 1. Redistributions of source code must retain the above copyright |
---|
37 | * notice, this list of conditions and the following disclaimer. |
---|
38 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
39 | * notice, this list of conditions and the following disclaimer in the |
---|
40 | * documentation and/or other materials provided with the distribution. |
---|
41 | * 3. The name of the author may not be used to endorse or promote products |
---|
42 | * derived from this software without specific prior written permission. |
---|
43 | * |
---|
44 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
---|
45 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
---|
46 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
---|
47 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
48 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
49 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
---|
50 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
---|
51 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
---|
52 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
---|
53 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
54 | */ |
---|
55 | |
---|
56 | #include <_ansi.h> |
---|
57 | #include <wchar.h> |
---|
58 | |
---|
59 | /* |
---|
60 | * Copy src to string dst of size siz. At most siz-1 characters |
---|
61 | * will be copied. Always NUL terminates (unless siz == 0). |
---|
62 | * Returns wcslen(src); if retval >= siz, truncation occurred. |
---|
63 | */ |
---|
64 | size_t |
---|
65 | wcslcpy (wchar_t * dst, |
---|
66 | const wchar_t * src, |
---|
67 | size_t siz) |
---|
68 | { |
---|
69 | wchar_t *d = dst; |
---|
70 | const wchar_t *s = src; |
---|
71 | size_t n = siz; |
---|
72 | |
---|
73 | /* Copy as many bytes as will fit */ |
---|
74 | if (n != 0 && --n != 0) |
---|
75 | { |
---|
76 | do |
---|
77 | { |
---|
78 | if ((*d++ = *s++) == 0) |
---|
79 | break; |
---|
80 | } |
---|
81 | while (--n != 0); |
---|
82 | } |
---|
83 | |
---|
84 | /* Not enough room in dst, add NUL and traverse rest of src */ |
---|
85 | if (n == 0) |
---|
86 | { |
---|
87 | if (siz != 0) |
---|
88 | *d = '\0'; /* NUL-terminate dst */ |
---|
89 | while (*s++) |
---|
90 | ; |
---|
91 | } |
---|
92 | |
---|
93 | return (s - src - 1); /* count does not include NUL */ |
---|
94 | } |
---|