- Wrap change of entries in an InitChange/ExitChange pair (without this,
[AROS.git] / compiler / stdc / strcasestr.c
blob6a4ed70c87ea232ee38eea107a6504d96bd7de81
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C extension function strcasestr().
6 */
8 #include <ctype.h>
10 /*****************************************************************************
12 NAME */
13 #include <string.h>
15 char * strcasestr (
17 /* SYNOPSIS */
18 const char * str,
19 const char * search)
21 /* FUNCTION
22 Searches for a string in a string.
24 INPUTS
25 str - Search this string
26 search - Look for this string
28 RESULT
29 A pointer to the first occurence of search in str or NULL if search
30 is not found in str.
32 NOTES
34 EXAMPLE
35 char buffer[64];
37 strcpy (buffer, "Hello ");
39 // This returns a pointer to the first l in buffer.
40 strcasestr (buffer, "llo ");
42 // This returns NULL
43 strcasestr (buffer, "llox");
45 BUGS
47 SEE ALSO
48 strchr(), strrchr(), strpbrk()
50 INTERNALS
52 ******************************************************************************/
54 size_t done;
55 size_t len_s = strlen (search);
56 const char * t;
60 /* If the first character matches */
61 if (tolower(*search) == tolower(*str))
63 /* How many characters to compare */
64 done = len_s;
66 /* Skip the first char (we have checked this one already) */
67 t = search + 1;
68 str ++;
71 Until all characters have been compared and the two
72 characters at the current position are equal ...
74 while ((--done) && (tolower(*t) == tolower(*str)))
76 /* Next character */
77 t ++;
78 str ++;
81 /* All character compared ? */
82 if (!done)
85 Then we have found what we were looking for. str points
86 now to the last character of the string we look for.
87 Therefore we must move it backward to the beginning of
88 the string.
90 str -= len_s;
91 return ((char *)str);
93 else
96 The strings are the same upto this character. I must
97 go back since the pattern might be something like
98 "ccbba" and the string "cccbba".
100 str -= len_s - done;
103 } while (*str++);
105 /* nothing found */
106 return(0);
107 } /* strcasestr */
109 #ifdef TEST
110 #include <stdio.h>
111 #include <string.h>
113 int main (int argc, char ** argv)
115 char * ptr;
117 if (argc != 3)
119 printf ("Usage: %s string search\n", argv[0]);
120 return 10;
123 ptr = strcasestr (argv[1], argv[2]);
125 if (!ptr)
126 printf ("%s not found in %s\n", argv[2], argv[1]);
127 else
128 printf ("%s found in %s as %s\n", argv[2], argv[1], ptr);
130 return 0;
132 #endif /* TEST */