]> pd.if.org Git - pdclib/blob - functions/string/strncmp.c
ad9e6209565508fbf6ea16d1369413096ea86741
[pdclib] / functions / string / strncmp.c
1 /* $Id$ */
2
3 /* strncmp( const char *, const char *, size_t )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <string.h>
10
11 #ifndef REGTEST
12
13 int strncmp( const char * s1, const char * s2, size_t n )
14 {
15     while ( *s1 && n && ( *s1 == *s2 ) )
16     {
17         ++s1;
18         ++s2;
19         --n;
20     }
21     if ( n == 0 )
22     {
23         return 0;
24     }
25     else
26     {
27         return ( *(unsigned char *)s1 - *(unsigned char *)s2 );
28     }
29 }
30
31 #endif
32
33 #ifdef TEST
34 #include <_PDCLIB_test.h>
35
36 int main( void )
37 {
38     char cmpabcde[] = "abcde\0f";
39     char cmpabcd_[] = "abcde\xfc";
40     char empty[] = "";
41     char x[] = "x";
42     TESTCASE( strncmp( abcde, cmpabcde, 5 ) == 0 );
43     TESTCASE( strncmp( abcde, cmpabcde, 10 ) == 0 );
44     TESTCASE( strncmp( abcde, abcdx, 5 ) < 0 );
45     TESTCASE( strncmp( abcdx, abcde, 5 ) > 0 );
46     TESTCASE( strncmp( empty, abcde, 5 ) < 0 );
47     TESTCASE( strncmp( abcde, empty, 5 ) > 0 );
48     TESTCASE( strncmp( abcde, abcdx, 4 ) == 0 );
49     TESTCASE( strncmp( abcde, x, 0 ) == 0 );
50     TESTCASE( strncmp( abcde, x, 1 ) < 0 );
51     TESTCASE( strncmp( abcde, cmpabcd_, 10 ) < 0 );
52     return TEST_RESULTS;
53 }
54 #endif