Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1 | /* strcasestr.c - Find if one string appears as a substring of another string, |
| 2 | without regard to case. */ |
| 3 | |
| 4 | /* Copyright (C) 2000 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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/>. |
| 20 | */ |
| 21 | |
| 22 | #include <config.h> |
| 23 | |
| 24 | #include <bashansi.h> |
| 25 | #include <chartypes.h> |
| 26 | |
| 27 | #include <stdc.h> |
| 28 | |
| 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 | strcasestr (s1, s2) |
| 34 | const char *s1; |
| 35 | const char *s2; |
| 36 | { |
| 37 | register int i, l, len, c; |
| 38 | |
| 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); |
| 46 | } |