]> pd.if.org Git - pdclib/blob - functions/string/memchr.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / string / memchr.c
1 /* memchr( const void *, int, 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 void * memchr( const void * s, int c, size_t n )
12 {
13     const unsigned char * p = (const unsigned char *) s;
14     while ( n-- )
15     {
16         if ( *p == (unsigned char) c )
17         {
18             return (void *) p;
19         }
20         ++p;
21     }
22     return NULL;
23 }
24
25 #endif
26
27 #ifdef TEST
28 #include "_PDCLIB_test.h"
29
30 int main( void )
31 {
32     TESTCASE( memchr( abcde, 'c', 5 ) == &abcde[2] );
33     TESTCASE( memchr( abcde, 'a', 1 ) == &abcde[0] );
34     TESTCASE( memchr( abcde, 'a', 0 ) == NULL );
35     TESTCASE( memchr( abcde, '\0', 5 ) == NULL );
36     TESTCASE( memchr( abcde, '\0', 6 ) == &abcde[5] );
37     return TEST_RESULTS;
38 }
39
40 #endif