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