1267843Sdelphij/*	$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $	*/
2267843Sdelphij
3267843Sdelphij/*-
4267843Sdelphij * Copyright (c) 1990, 1993
5267843Sdelphij *	The Regents of the University of California.  All rights reserved.
6267843Sdelphij *
7267843Sdelphij * This code is derived from software contributed to Berkeley by
8267843Sdelphij * Chris Torek.
9267843Sdelphij *
10267843Sdelphij * Redistribution and use in source and binary forms, with or without
11267843Sdelphij * modification, are permitted provided that the following conditions
12267843Sdelphij * are met:
13267843Sdelphij * 1. Redistributions of source code must retain the above copyright
14267843Sdelphij *    notice, this list of conditions and the following disclaimer.
15267843Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
16267843Sdelphij *    notice, this list of conditions and the following disclaimer in the
17267843Sdelphij *    documentation and/or other materials provided with the distribution.
18267843Sdelphij * 3. Neither the name of the University nor the names of its contributors
19267843Sdelphij *    may be used to endorse or promote products derived from this software
20267843Sdelphij *    without specific prior written permission.
21267843Sdelphij *
22267843Sdelphij * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23267843Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24267843Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25267843Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26267843Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27267843Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28267843Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29267843Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30267843Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31267843Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32267843Sdelphij * SUCH DAMAGE.
33267843Sdelphij */
34267843Sdelphij
35267843Sdelphij#if defined(LIBC_SCCS) && !defined(lint)
36267843Sdelphij__RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $");
37267843Sdelphij__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $");
38267843Sdelphij#endif /* LIBC_SCCS and not lint */
39267843Sdelphij
40267843Sdelphij#include "file.h"
41267843Sdelphij
42267843Sdelphij#include <assert.h>
43267843Sdelphij#include <ctype.h>
44267843Sdelphij#include <string.h>
45267843Sdelphij
46267843Sdelphijstatic int
47267843Sdelphij_strncasecmp(const char *s1, const char *s2, size_t n)
48267843Sdelphij{
49267843Sdelphij	if (n != 0) {
50267843Sdelphij		const unsigned char *us1 = (const unsigned char *)s1,
51267843Sdelphij				*us2 = (const unsigned char *)s2;
52267843Sdelphij
53267843Sdelphij		do {
54267843Sdelphij			if (tolower(*us1) != tolower(*us2++))
55267843Sdelphij				return tolower(*us1) - tolower(*--us2);
56267843Sdelphij			if (*us1++ == '\0')
57267843Sdelphij				break;
58267843Sdelphij		} while (--n != 0);
59267843Sdelphij	}
60267843Sdelphij	return 0;
61267843Sdelphij}
62267843Sdelphij
63267843Sdelphij/*
64267843Sdelphij * Find the first occurrence of find in s, ignore case.
65267843Sdelphij */
66267843Sdelphijchar *
67267843Sdelphijstrcasestr(const char *s, const char *find)
68267843Sdelphij{
69267843Sdelphij	char c, sc;
70267843Sdelphij	size_t len;
71267843Sdelphij
72267843Sdelphij	if ((c = *find++) != 0) {
73267843Sdelphij		c = tolower((unsigned char)c);
74267843Sdelphij		len = strlen(find);
75267843Sdelphij		do {
76267843Sdelphij			do {
77267843Sdelphij				if ((sc = *s++) == 0)
78267843Sdelphij					return (NULL);
79267843Sdelphij			} while ((char)tolower((unsigned char)sc) != c);
80267843Sdelphij		} while (_strncasecmp(s, find, len) != 0);
81267843Sdelphij		s--;
82267843Sdelphij	}
83267843Sdelphij	return (char *)(intptr_t)(s);
84267843Sdelphij}
85