xref: /OK3568_Linux_fs/external/xserver/os/strcasestr.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*-
2*4882a593Smuzhiyun  * Copyright (c) 1990, 1993
3*4882a593Smuzhiyun  *      The Regents of the University of California.  All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This code is derived from software contributed to Berkeley by
6*4882a593Smuzhiyun  * Chris Torek.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun  * are met:
11*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
12*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
13*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
14*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
15*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
16*4882a593Smuzhiyun  * 4. Neither the name of the University nor the names of its contributors
17*4882a593Smuzhiyun  *    may be used to endorse or promote products derived from this software
18*4882a593Smuzhiyun  *    without specific prior written permission.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*4882a593Smuzhiyun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*4882a593Smuzhiyun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*4882a593Smuzhiyun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*4882a593Smuzhiyun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*4882a593Smuzhiyun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*4882a593Smuzhiyun  * SUCH DAMAGE.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
34*4882a593Smuzhiyun #include <dix-config.h>
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <ctype.h>
38*4882a593Smuzhiyun #include <string.h>
39*4882a593Smuzhiyun #include "dix.h"
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun  * Find the first occurrence of find in s, ignore case.
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun #ifndef HAVE_STRCASESTR
45*4882a593Smuzhiyun char *
xstrcasestr(const char * s,const char * find)46*4882a593Smuzhiyun xstrcasestr(const char *s, const char *find)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun     char c, sc;
49*4882a593Smuzhiyun     size_t len;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun     if ((c = *find++) != 0) {
52*4882a593Smuzhiyun         c = tolower((unsigned char) c);
53*4882a593Smuzhiyun         len = strlen(find);
54*4882a593Smuzhiyun         do {
55*4882a593Smuzhiyun             do {
56*4882a593Smuzhiyun                 if ((sc = *s++) == 0)
57*4882a593Smuzhiyun                     return NULL;
58*4882a593Smuzhiyun             } while ((char) tolower((unsigned char) sc) != c);
59*4882a593Smuzhiyun         } while (strncasecmp(s, find, len) != 0);
60*4882a593Smuzhiyun         s--;
61*4882a593Smuzhiyun     }
62*4882a593Smuzhiyun     return ((char *) s);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun #endif
65