1 /* Copyright (c) 2007 Corinna Vinschen <corinna@vinschen.de> */
2 /*
3 FUNCTION
4 <<wcpncpy>>---copy part of a wide-character string returning a pointer to its end
5
6 SYNOPSIS
7 #include <wchar.h>
8 wchar_t *wcpncpy(wchar_t *__restrict <[s1]>,
9 const wchar_t *__restrict <[s2]>, size_t <[n]>);
10
11 DESCRIPTION
12 The <<wcpncpy>> function copies not more than n wide-character codes
13 (wide-character codes that follow a null wide-character code are not
14 copied) from the array pointed to by <[s2]> to the array pointed to
15 by <[s1]>. If copying takes place between objects that overlap, the
16 behaviour is undefined.
17
18 If the array pointed to by <[s2]> is a wide-character string that is
19 shorter than <[n]> wide-character codes, null wide-character codes are
20 appended to the copy in the array pointed to by <[s1]>, until <[n]>
21 wide-character codes in all are written.
22
23 RETURNS
24 The <<wcpncpy>> function returns <[s1]>; no return value is reserved to
25 indicate an error.
26
27 PORTABILITY
28 <<wcpncpy>> is ISO/IEC 9899/AMD1:1995 (ISO C).
29
30 No supporting OS subroutines are required.
31 */
32
33 #include <_ansi.h>
34 #include <wchar.h>
35
36 wchar_t *
wcpncpy(wchar_t * __restrict dst,const wchar_t * __restrict src,size_t count)37 wcpncpy (wchar_t *__restrict dst,
38 const wchar_t *__restrict src,
39 size_t count)
40 {
41 wchar_t *ret = NULL;
42
43 while (count > 0)
44 {
45 --count;
46 if ((*dst++ = *src++) == L'\0')
47 {
48 ret = dst - 1;
49 break;
50 }
51 }
52 while (count-- > 0)
53 *dst++ = L'\0';
54
55 return ret ? ret : dst;
56 }
57