3 /* strncpy( char *, const char *, size_t )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 char * strncpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n )
16 while ( ( n > 0 ) && ( *s1++ = *s2++ ) )
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
24 /* Checking against 1 as we missed the last --n in the loop above. */
35 #include <_PDCLIB_test.h>
40 TESTCASE( strncpy( s, "", 1 ) == s );
41 TESTCASE( s[0] == '\0' );
42 TESTCASE( s[1] == 'x' );
43 TESTCASE( strncpy( s, abcde, 6 ) == s );
44 TESTCASE( s[0] == 'a' );
45 TESTCASE( s[4] == 'e' );
46 TESTCASE( s[5] == '\0' );
47 TESTCASE( s[6] == 'x' );
48 TESTCASE( strncpy( s, abcde, 7 ) == s );
49 TESTCASE( s[6] == '\0' );
50 TESTCASE( strncpy( s, "xxxx", 3 ) == s );
51 TESTCASE( s[0] == 'x' );
52 TESTCASE( s[2] == 'x' );
53 TESTCASE( s[3] == 'd' );