OSDN Git Service

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