xref: /freebsd/lib/libc/string/strcasestr.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
189503316SAndrey A. Chernov /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
489503316SAndrey A. Chernov  * Copyright (c) 1990, 1993
589503316SAndrey A. Chernov  *	The Regents of the University of California.  All rights reserved.
689503316SAndrey A. Chernov  *
789503316SAndrey A. Chernov  * This code is derived from software contributed to Berkeley by
889503316SAndrey A. Chernov  * Chris Torek.
989503316SAndrey A. Chernov  *
103c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
11*5b5fa75aSEd Maste  *
123c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
133c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
143c87aa1dSDavid Chisnall  *
1589503316SAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
1689503316SAndrey A. Chernov  * modification, are permitted provided that the following conditions
1789503316SAndrey A. Chernov  * are met:
1889503316SAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
1989503316SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
2089503316SAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
2189503316SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
2289503316SAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
233fb3b97cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
2489503316SAndrey A. Chernov  *    may be used to endorse or promote products derived from this software
2589503316SAndrey A. Chernov  *    without specific prior written permission.
2689503316SAndrey A. Chernov  *
2789503316SAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2889503316SAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2989503316SAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3089503316SAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3189503316SAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3289503316SAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3389503316SAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3489503316SAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3589503316SAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3689503316SAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3789503316SAndrey A. Chernov  * SUCH DAMAGE.
3889503316SAndrey A. Chernov  */
3989503316SAndrey A. Chernov 
4089503316SAndrey A. Chernov #include <ctype.h>
4189503316SAndrey A. Chernov #include <string.h>
423c87aa1dSDavid Chisnall #include "xlocale_private.h"
4389503316SAndrey A. Chernov 
4489503316SAndrey A. Chernov /*
4589503316SAndrey A. Chernov  * Find the first occurrence of find in s, ignore case.
4689503316SAndrey A. Chernov  */
4789503316SAndrey A. Chernov char *
strcasestr_l(const char * s,const char * find,locale_t locale)483c87aa1dSDavid Chisnall strcasestr_l(const char *s, const char *find, locale_t locale)
4989503316SAndrey A. Chernov {
508fb3f3f6SDavid E. O'Brien 	char c, sc;
518fb3f3f6SDavid E. O'Brien 	size_t len;
523c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
5389503316SAndrey A. Chernov 
5489503316SAndrey A. Chernov 	if ((c = *find++) != 0) {
553c87aa1dSDavid Chisnall 		c = tolower_l((unsigned char)c, locale);
5689503316SAndrey A. Chernov 		len = strlen(find);
5789503316SAndrey A. Chernov 		do {
5889503316SAndrey A. Chernov 			do {
5989503316SAndrey A. Chernov 				if ((sc = *s++) == 0)
6089503316SAndrey A. Chernov 					return (NULL);
613c87aa1dSDavid Chisnall 			} while ((char)tolower_l((unsigned char)sc, locale) != c);
623c87aa1dSDavid Chisnall 		} while (strncasecmp_l(s, find, len, locale) != 0);
6389503316SAndrey A. Chernov 		s--;
6489503316SAndrey A. Chernov 	}
6589503316SAndrey A. Chernov 	return ((char *)s);
6689503316SAndrey A. Chernov }
673c87aa1dSDavid Chisnall char *
strcasestr(const char * s,const char * find)683c87aa1dSDavid Chisnall strcasestr(const char *s, const char *find)
693c87aa1dSDavid Chisnall {
703c87aa1dSDavid Chisnall 	return strcasestr_l(s, find, __get_locale());
713c87aa1dSDavid Chisnall }
72