1 /*
2 Copyright (c)1999 Citrus Project,
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 SUCH DAMAGE.
25 */
26 /*
27 FUNCTION
28 <<wcsncpy>>---copy part of a wide-character string
29
30 SYNOPSIS
31 #include <wchar.h>
32 wchar_t *wcsncpy(wchar_t *__restrict <[s1]>,
33 const wchar_t *__restrict <[s2]>, size_t <[n]>);
34
35 DESCRIPTION
36 The <<wcsncpy>> function copies not more than <[n]> wide-character codes
37 (wide-character codes that follow a null wide-character code are not
38 copied) from the array pointed to by <[s2]> to the array pointed to
39 by <[s1]>. If copying takes place between objects that overlap, the
40 behaviour is undefined. Note that if <[s1]> contains more than <[n]>
41 wide characters before its terminating null, the result is not
42 null-terminated.
43
44 If the array pointed to by <[s2]> is a wide-character string that is
45 shorter than <[n]> wide-character codes, null wide-character codes are
46 appended to the copy in the array pointed to by <[s1]>, until <[n]>
47 wide-character codes in all are written.
48
49 RETURNS
50 The <<wcsncpy>> function returns <[s1]>; no return value is reserved to
51 indicate an error.
52
53 PORTABILITY
54 ISO/IEC 9899; POSIX.1.
55
56 No supporting OS subroutines are required.
57 */
58
59 #include <_ansi.h>
60 #include <wchar.h>
61
62 wchar_t *
wcsncpy(wchar_t * __restrict s1,const wchar_t * __restrict s2,size_t n)63 wcsncpy (wchar_t *__restrict s1,
64 const wchar_t *__restrict s2,
65 size_t n)
66 {
67 wchar_t *dscan=s1;
68
69 while(n > 0)
70 {
71 --n;
72 if((*dscan++ = *s2++) == L'\0') break;
73 }
74 while(n-- > 0) *dscan++ = L'\0';
75
76 return s1;
77 }
78