xref: /freebsd/contrib/file/src/strcasestr.c (revision 246e7a2b6494cd991b08ac669ed761ecea0cc98c)
1*b6cee71dSXin LI /*	$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $	*/
2*b6cee71dSXin LI 
3*b6cee71dSXin LI /*-
4*b6cee71dSXin LI  * Copyright (c) 1990, 1993
5*b6cee71dSXin LI  *	The Regents of the University of California.  All rights reserved.
6*b6cee71dSXin LI  *
7*b6cee71dSXin LI  * This code is derived from software contributed to Berkeley by
8*b6cee71dSXin LI  * Chris Torek.
9*b6cee71dSXin LI  *
10*b6cee71dSXin LI  * Redistribution and use in source and binary forms, with or without
11*b6cee71dSXin LI  * modification, are permitted provided that the following conditions
12*b6cee71dSXin LI  * are met:
13*b6cee71dSXin LI  * 1. Redistributions of source code must retain the above copyright
14*b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer.
15*b6cee71dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
16*b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer in the
17*b6cee71dSXin LI  *    documentation and/or other materials provided with the distribution.
18*b6cee71dSXin LI  * 3. Neither the name of the University nor the names of its contributors
19*b6cee71dSXin LI  *    may be used to endorse or promote products derived from this software
20*b6cee71dSXin LI  *    without specific prior written permission.
21*b6cee71dSXin LI  *
22*b6cee71dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*b6cee71dSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*b6cee71dSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*b6cee71dSXin LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*b6cee71dSXin LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*b6cee71dSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*b6cee71dSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*b6cee71dSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*b6cee71dSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*b6cee71dSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*b6cee71dSXin LI  * SUCH DAMAGE.
33*b6cee71dSXin LI  */
34*b6cee71dSXin LI 
35*b6cee71dSXin LI #if defined(LIBC_SCCS) && !defined(lint)
36*b6cee71dSXin LI __RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $");
37*b6cee71dSXin LI __RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $");
38*b6cee71dSXin LI #endif /* LIBC_SCCS and not lint */
39*b6cee71dSXin LI 
40*b6cee71dSXin LI #include "file.h"
41*b6cee71dSXin LI 
42*b6cee71dSXin LI #include <assert.h>
43*b6cee71dSXin LI #include <ctype.h>
44*b6cee71dSXin LI #include <string.h>
45*b6cee71dSXin LI 
46*b6cee71dSXin LI static int
_strncasecmp(const char * s1,const char * s2,size_t n)47*b6cee71dSXin LI _strncasecmp(const char *s1, const char *s2, size_t n)
48*b6cee71dSXin LI {
49*b6cee71dSXin LI 	if (n != 0) {
50*b6cee71dSXin LI 		const unsigned char *us1 = (const unsigned char *)s1,
51*b6cee71dSXin LI 				*us2 = (const unsigned char *)s2;
52*b6cee71dSXin LI 
53*b6cee71dSXin LI 		do {
54*b6cee71dSXin LI 			if (tolower(*us1) != tolower(*us2++))
55*b6cee71dSXin LI 				return tolower(*us1) - tolower(*--us2);
56*b6cee71dSXin LI 			if (*us1++ == '\0')
57*b6cee71dSXin LI 				break;
58*b6cee71dSXin LI 		} while (--n != 0);
59*b6cee71dSXin LI 	}
60*b6cee71dSXin LI 	return 0;
61*b6cee71dSXin LI }
62*b6cee71dSXin LI 
63*b6cee71dSXin LI /*
64*b6cee71dSXin LI  * Find the first occurrence of find in s, ignore case.
65*b6cee71dSXin LI  */
66*b6cee71dSXin LI char *
strcasestr(const char * s,const char * find)67*b6cee71dSXin LI strcasestr(const char *s, const char *find)
68*b6cee71dSXin LI {
69*b6cee71dSXin LI 	char c, sc;
70*b6cee71dSXin LI 	size_t len;
71*b6cee71dSXin LI 
72*b6cee71dSXin LI 	if ((c = *find++) != 0) {
73*b6cee71dSXin LI 		c = tolower((unsigned char)c);
74*b6cee71dSXin LI 		len = strlen(find);
75*b6cee71dSXin LI 		do {
76*b6cee71dSXin LI 			do {
77*b6cee71dSXin LI 				if ((sc = *s++) == 0)
78*b6cee71dSXin LI 					return (NULL);
79*b6cee71dSXin LI 			} while ((char)tolower((unsigned char)sc) != c);
80*b6cee71dSXin LI 		} while (_strncasecmp(s, find, len) != 0);
81*b6cee71dSXin LI 		s--;
82*b6cee71dSXin LI 	}
83*b6cee71dSXin LI 	return (char *)(intptr_t)(s);
84*b6cee71dSXin LI }
85