]> pd.if.org Git - pdclib/blob - functions/string/strstr.c
461283ac49f81fbf62fe929d2ba8a3bb73652881
[pdclib] / functions / string / strstr.c
1 /* $Id$ */
2
3 /* strstr( const char *, const char * )
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 char * strstr( const char * s1, const char * s2 )
14 {
15     const char * p1 = s1;
16     const char * p2;
17     while ( *s1 )
18     {
19         p2 = s2;
20         while ( *p2 && ( *p1 == *p2 ) )
21         {
22             ++p1;
23             ++p2;
24         }
25         if ( ! *p2 )
26         {
27             return (char *) s1;
28         }
29         ++s1;
30         p1 = s1;
31     }
32     return NULL;
33 }
34
35 #endif
36
37 #ifdef TEST
38 #include <_PDCLIB_test.h>
39
40 int main( void )
41 {
42     char s[] = "abcabcabcdabcde";
43     TESTCASE( strstr( s, "x" ) == NULL );
44     TESTCASE( strstr( s, "xyz" ) == NULL );
45     TESTCASE( strstr( s, "a" ) == &s[0] );
46     TESTCASE( strstr( s, "abc" ) == &s[0] );
47     TESTCASE( strstr( s, "abcd" ) == &s[6] );
48     TESTCASE( strstr( s, "abcde" ) == &s[10] );
49     return TEST_RESULTS;
50 }
51 #endif