]> pd.if.org Git - pdclib/blob - functions/string/strncat.c
14f6af3f3dc1145c8d8b954d2505177a9e77eb6f
[pdclib] / functions / string / strncat.c
1 /* strncat( char *, const char *, 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 <string.h>
8
9 #ifndef REGTEST
10
11 char * strncat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n )
12 {
13     char * rc = s1;
14     while ( *s1 )
15     {
16         ++s1;
17     }
18     while ( n && ( *s1++ = *s2++ ) )
19     {
20         --n;
21     }
22     if ( n == 0 )
23     {
24         *s1 = '\0';
25     }
26     return rc;
27 }
28
29 #endif
30
31 #ifdef TEST
32 #include <_PDCLIB_test.h>
33
34 int main( void )
35 {
36     char s[] = "xx\0xxxxxx";
37     TESTCASE( strncat( s, abcde, 10 ) == s );
38     TESTCASE( s[2] == 'a' );
39     TESTCASE( s[6] == 'e' );
40     TESTCASE( s[7] == '\0' );
41     TESTCASE( s[8] == 'x' );
42     s[0] = '\0';
43     TESTCASE( strncat( s, abcdx, 10 ) == s );
44     TESTCASE( s[4] == 'x' );
45     TESTCASE( s[5] == '\0' );
46     TESTCASE( strncat( s, "\0", 10 ) == s );
47     TESTCASE( s[5] == '\0' );
48     TESTCASE( s[6] == 'e' );
49     TESTCASE( strncat( s, abcde, 0 ) == s );
50     TESTCASE( s[5] == '\0' );
51     TESTCASE( s[6] == 'e' );
52     TESTCASE( strncat( s, abcde, 3 ) == s );
53     TESTCASE( s[5] == 'a' );
54     TESTCASE( s[7] == 'c' );
55     TESTCASE( s[8] == '\0' );
56     return TEST_RESULTS;
57 }
58 #endif