NaCl: Do not install <sys/mtio.h>.
[glibc.git] / wcsmbs / wcsncpy.c
blob5ee5ee6bdeb1109c48b324458d0e17cdcd277b81
1 /* Copyright (C) 1995-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <wchar.h>
21 #ifdef WCSNCPY
22 # define __wcsncpy WCSNCPY
23 #endif
25 /* Copy no more than N wide-characters of SRC to DEST. */
26 wchar_t *
27 __wcsncpy (dest, src, n)
28 wchar_t *dest;
29 const wchar_t *src;
30 size_t n;
32 wint_t c;
33 wchar_t *const s = dest;
35 --dest;
37 if (n >= 4)
39 size_t n4 = n >> 2;
41 for (;;)
43 c = *src++;
44 *++dest = c;
45 if (c == L'\0')
46 break;
47 c = *src++;
48 *++dest = c;
49 if (c == L'\0')
50 break;
51 c = *src++;
52 *++dest = c;
53 if (c == L'\0')
54 break;
55 c = *src++;
56 *++dest = c;
57 if (c == L'\0')
58 break;
59 if (--n4 == 0)
60 goto last_chars;
62 n = n - (dest - s) - 1;
63 if (n == 0)
64 return s;
65 goto zero_fill;
68 last_chars:
69 n &= 3;
70 if (n == 0)
71 return s;
75 c = *src++;
76 *++dest = c;
77 if (--n == 0)
78 return s;
80 while (c != L'\0');
82 zero_fill:
84 *++dest = L'\0';
85 while (--n > 0);
87 return s;
89 #ifndef WCSNCPY
90 weak_alias (__wcsncpy, wcsncpy)
91 #endif