xref: /freebsd/lib/libc/string/strcasestr.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
189503316SAndrey A. Chernov /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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
113c87aa1dSDavid Chisnall  * All rights reserved.
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 <sys/cdefs.h>
41de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
42de5fe5d5SDavid E. O'Brien 
4389503316SAndrey A. Chernov #include <ctype.h>
4489503316SAndrey A. Chernov #include <string.h>
453c87aa1dSDavid Chisnall #include "xlocale_private.h"
4689503316SAndrey A. Chernov 
4789503316SAndrey A. Chernov /*
4889503316SAndrey A. Chernov  * Find the first occurrence of find in s, ignore case.
4989503316SAndrey A. Chernov  */
5089503316SAndrey A. Chernov char *
513c87aa1dSDavid Chisnall strcasestr_l(const char *s, const char *find, locale_t locale)
5289503316SAndrey A. Chernov {
538fb3f3f6SDavid E. O'Brien 	char c, sc;
548fb3f3f6SDavid E. O'Brien 	size_t len;
553c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
5689503316SAndrey A. Chernov 
5789503316SAndrey A. Chernov 	if ((c = *find++) != 0) {
583c87aa1dSDavid Chisnall 		c = tolower_l((unsigned char)c, locale);
5989503316SAndrey A. Chernov 		len = strlen(find);
6089503316SAndrey A. Chernov 		do {
6189503316SAndrey A. Chernov 			do {
6289503316SAndrey A. Chernov 				if ((sc = *s++) == 0)
6389503316SAndrey A. Chernov 					return (NULL);
643c87aa1dSDavid Chisnall 			} while ((char)tolower_l((unsigned char)sc, locale) != c);
653c87aa1dSDavid Chisnall 		} while (strncasecmp_l(s, find, len, locale) != 0);
6689503316SAndrey A. Chernov 		s--;
6789503316SAndrey A. Chernov 	}
6889503316SAndrey A. Chernov 	return ((char *)s);
6989503316SAndrey A. Chernov }
703c87aa1dSDavid Chisnall char *
713c87aa1dSDavid Chisnall strcasestr(const char *s, const char *find)
723c87aa1dSDavid Chisnall {
733c87aa1dSDavid Chisnall 	return strcasestr_l(s, find, __get_locale());
743c87aa1dSDavid Chisnall }
75