xref: /freebsd/lib/libc/string/strcasestr.c (revision 8fb3f3f68288ae2b1b53dd65e3dd673d83c80f4c)
189503316SAndrey A. Chernov /*-
289503316SAndrey A. Chernov  * Copyright (c) 1990, 1993
389503316SAndrey A. Chernov  *	The Regents of the University of California.  All rights reserved.
489503316SAndrey A. Chernov  *
589503316SAndrey A. Chernov  * This code is derived from software contributed to Berkeley by
689503316SAndrey A. Chernov  * Chris Torek.
789503316SAndrey A. Chernov  *
889503316SAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
989503316SAndrey A. Chernov  * modification, are permitted provided that the following conditions
1089503316SAndrey A. Chernov  * are met:
1189503316SAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
1289503316SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
1389503316SAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
1489503316SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
1589503316SAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
1689503316SAndrey A. Chernov  * 3. All advertising materials mentioning features or use of this software
1789503316SAndrey A. Chernov  *    must display the following acknowledgement:
1889503316SAndrey A. Chernov  *	This product includes software developed by the University of
1989503316SAndrey A. Chernov  *	California, Berkeley and its contributors.
2089503316SAndrey A. Chernov  * 4. Neither the name of the University nor the names of its contributors
2189503316SAndrey A. Chernov  *    may be used to endorse or promote products derived from this software
2289503316SAndrey A. Chernov  *    without specific prior written permission.
2389503316SAndrey A. Chernov  *
2489503316SAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2589503316SAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2689503316SAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2789503316SAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2889503316SAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2989503316SAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3089503316SAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3189503316SAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3289503316SAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3389503316SAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3489503316SAndrey A. Chernov  * SUCH DAMAGE.
3589503316SAndrey A. Chernov  */
3689503316SAndrey A. Chernov 
3789503316SAndrey A. Chernov #include <sys/cdefs.h>
38de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
39de5fe5d5SDavid E. O'Brien 
4089503316SAndrey A. Chernov #include <ctype.h>
4189503316SAndrey A. Chernov #include <string.h>
4289503316SAndrey A. Chernov 
4389503316SAndrey A. Chernov /*
4489503316SAndrey A. Chernov  * Find the first occurrence of find in s, ignore case.
4589503316SAndrey A. Chernov  */
4689503316SAndrey A. Chernov char *
4789503316SAndrey A. Chernov strcasestr(s, find)
488fb3f3f6SDavid E. O'Brien 	const char *s, *find;
4989503316SAndrey A. Chernov {
508fb3f3f6SDavid E. O'Brien 	char c, sc;
518fb3f3f6SDavid E. O'Brien 	size_t len;
5289503316SAndrey A. Chernov 
5389503316SAndrey A. Chernov 	if ((c = *find++) != 0) {
5489503316SAndrey A. Chernov 		c = tolower((unsigned char)c);
5589503316SAndrey A. Chernov 		len = strlen(find);
5689503316SAndrey A. Chernov 		do {
5789503316SAndrey A. Chernov 			do {
5889503316SAndrey A. Chernov 				if ((sc = *s++) == 0)
5989503316SAndrey A. Chernov 					return (NULL);
6089503316SAndrey A. Chernov 			} while ((char)tolower((unsigned char)sc) != c);
6189503316SAndrey A. Chernov 		} while (strncasecmp(s, find, len) != 0);
6289503316SAndrey A. Chernov 		s--;
6389503316SAndrey A. Chernov 	}
6489503316SAndrey A. Chernov 	return ((char *)s);
6589503316SAndrey A. Chernov }
66