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