]> pd.if.org Git - pdclib/blob - functions/string/memchr.c
Added #ifdef to allow regression against system lib.
[pdclib] / functions / string / memchr.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* memchr( const void *, int, size_t )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <string.h>
12
13 #ifndef REGTEST
14
15 void * memchr( const void * s, int c, size_t n )
16 {
17     const unsigned char * p = (const unsigned char *) s;
18     while ( n-- )
19     {
20         if ( *p == (unsigned char) c )
21         {
22             return (void *) p;
23         }
24         ++p;
25     }
26     return NULL;
27 }
28
29 #endif
30
31 #ifdef TEST
32 #include <_PDCLIB_test.h>
33
34 int main()
35 {
36     BEGIN_TESTS;
37     TESTCASE( memchr( abcde, 'c', 5 ) == &abcde[2] );
38     TESTCASE( memchr( abcde, 'a', 1 ) == &abcde[0] );
39     TESTCASE( memchr( abcde, 'a', 0 ) == NULL );
40     TESTCASE( memchr( abcde, '\0', 5 ) == NULL );
41     TESTCASE( memchr( abcde, '\0', 6 ) == &abcde[5] );
42     return TEST_RESULTS;
43 }
44 #endif