]> pd.if.org Git - pdclib/blob - functions/wchar/wcsncpy.c
dos2unix
[pdclib] / functions / wchar / wcsncpy.c
1 /* wchar_t * wcsncpy( wchar_t *, const wchar_t * , size_t );
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <wchar.h>
8
9 #ifndef REGTEST
10
11 wchar_t *wcsncpy( wchar_t * _PDCLIB_restrict s1, 
12                   const wchar_t * _PDCLIB_restrict s2,
13                   size_t n )
14 {
15     wchar_t * rc = s1;
16     while ( ( n > 0 ) && ( *s1++ = *s2++ ) )
17     {
18         /* Cannot do "n--" in the conditional as size_t is unsigned and we have
19            to check it again for >0 in the next loop below, so we must not risk
20            underflow.
21         */
22         --n;
23     }
24     /* Checking against 1 as we missed the last --n in the loop above. */
25     while ( n-- > 1 )
26     {
27         *s1++ = '\0';
28     }
29     return rc;
30 }
31
32
33 #endif
34
35 #ifdef TEST
36 #include "_PDCLIB_test.h"
37
38 int main( void )
39 {
40     return TEST_RESULTS;
41 }
42
43 #endif