Blame


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