]>
Commit | Line | Data |
---|---|---|
0ecb606c | 1 | /* Copyright (C) 1995, 1996, 1997, 2005 Free Software Foundation, Inc. |
33a934a3 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. | |
a482b5a5 | 4 | |
33a934a3 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
a482b5a5 | 9 | |
33a934a3 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
a482b5a5 | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
a482b5a5 | 19 | |
299a95b9 | 20 | #include <wchar.h> |
a482b5a5 RM |
21 | |
22 | ||
23 | /* Copy no more than N wide-characters of SRC to DEST. */ | |
24 | wchar_t * | |
0ecb606c | 25 | __wcsncpy (dest, src, n) |
299a95b9 RM |
26 | wchar_t *dest; |
27 | const wchar_t *src; | |
28 | size_t n; | |
a482b5a5 | 29 | { |
75cd5204 | 30 | wint_t c; |
299a95b9 | 31 | wchar_t *const s = dest; |
a482b5a5 RM |
32 | |
33 | --dest; | |
34 | ||
35 | if (n >= 4) | |
36 | { | |
37 | size_t n4 = n >> 2; | |
38 | ||
39 | for (;;) | |
40 | { | |
41 | c = *src++; | |
42 | *++dest = c; | |
43 | if (c == L'\0') | |
44 | break; | |
45 | c = *src++; | |
46 | *++dest = c; | |
47 | if (c == L'\0') | |
48 | break; | |
49 | c = *src++; | |
50 | *++dest = c; | |
51 | if (c == L'\0') | |
52 | break; | |
53 | c = *src++; | |
54 | *++dest = c; | |
55 | if (c == L'\0') | |
56 | break; | |
57 | if (--n4 == 0) | |
58 | goto last_chars; | |
59 | } | |
60 | n = n - (dest - s) - 1; | |
61 | if (n == 0) | |
62 | return s; | |
63 | goto zero_fill; | |
64 | } | |
65 | ||
66 | last_chars: | |
67 | n &= 3; | |
68 | if (n == 0) | |
69 | return s; | |
70 | ||
71 | do | |
72 | { | |
73 | c = *src++; | |
74 | *++dest = c; | |
75 | if (--n == 0) | |
76 | return s; | |
77 | } | |
78 | while (c != L'\0'); | |
79 | ||
80 | zero_fill: | |
81 | do | |
82 | *++dest = L'\0'; | |
83 | while (--n > 0); | |
84 | ||
85 | return s; | |
86 | } | |
0ecb606c | 87 | weak_alias (__wcsncpy, wcsncpy) |