chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / wcsmbs / wcsncpy.c
1 /* Copyright (C) 1995, 1996, 1997, 2005 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.
4
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.
9
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.
14
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.  */
19
20 #include <wchar.h>
21
22
23 /* Copy no more than N wide-characters of SRC to DEST.  */
24 wchar_t *
25 __wcsncpy (dest, src, n)
26      wchar_t *dest;
27      const wchar_t *src;
28      size_t n;
29 {
30   wint_t c;
31   wchar_t *const s = dest;
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 }
87 weak_alias (__wcsncpy, wcsncpy)