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