9d3f3021676de7c634e7b7af88770905b2fb2ea4
[platform/upstream/bash.git] / lib / sh / strindex.c
1 /* strindex.c - Find if one string appears as a substring of another string,
2                 without regard to case. */
3
4 /* Copyright (C) 2000
5    Free Software Foundation, Inc.
6
7    This file is part of GNU Bash, the Bourne Again SHell.
8
9    Bash is free software; you can redistribute it and/or modify it under
10    the terms of the GNU General Public License as published by the Free
11    Software Foundation; either version 2, or (at your option) any later
12    version.
13
14    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
15    WARRANTY; without even the implied warranty of MERCHANTABILITY or
16    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17    for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with Bash; see the file COPYING.  If not, write to the Free Software
21    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22
23 #include <config.h>
24
25 #include "bashansi.h"
26 #include <ctype.h>
27
28 #include <stdc.h>
29
30 #ifndef to_upper
31 #  define to_upper(c) (islower(c) ? toupper(c) : (c))
32 #  define to_lower(c) (isupper(c) ? tolower(c) : (c))
33 #endif
34
35 /* Determine if s2 occurs in s1.  If so, return a pointer to the
36    match in s1.  The compare is case insensitive.  This is a
37    case-insensitive strstr(3). */
38 char *
39 strindex (s1, s2)
40      const char *s1;
41      const char *s2;
42 {
43   register int i, l, len, c;
44
45   c = to_upper (s2[0]);
46   len = strlen (s1);
47   l = strlen (s2);
48   for (i = 0; (len - i) >= l; i++)
49     if ((to_upper (s1[i]) == c) && (strncasecmp (s1 + i, s2, l) == 0))
50       return ((char *)s1 + i);
51   return ((char *)0);
52 }