Patch-ID: bash40-030
[bash.git] / lib / sh / strindex.c
blob5cb80ad3e3e1a9476176a67c7b2745863d9eb401
1 /* strindex.c - Find if one string appears as a substring of another string,
2 without regard to case. */
4 /* Copyright (C) 2000 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
22 #include <config.h>
24 #include <bashansi.h>
25 #include <chartypes.h>
27 #include <stdc.h>
29 /* Determine if s2 occurs in s1. If so, return a pointer to the
30 match in s1. The compare is case insensitive. This is a
31 case-insensitive strstr(3). */
32 char *
33 strindex (s1, s2)
34 const char *s1;
35 const char *s2;
37 register int i, l, len, c;
39 c = TOLOWER ((unsigned char)s2[0]);
40 len = strlen (s1);
41 l = strlen (s2);
42 for (i = 0; (len - i) >= l; i++)
43 if ((TOLOWER ((unsigned char)s1[i]) == c) && (strncasecmp (s1 + i, s2, l) == 0))
44 return ((char *)s1 + i);
45 return ((char *)0);