xref: /netbsd-src/lib/libc/string/strcasestr.c (revision 03256c6e554d05b352eb2637321a80894a532707)
1*03256c6eSchristos /*	$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $	*/
274d67c35Sjunyoung 
374d67c35Sjunyoung /*-
474d67c35Sjunyoung  * Copyright (c) 1990, 1993
574d67c35Sjunyoung  *	The Regents of the University of California.  All rights reserved.
674d67c35Sjunyoung  *
774d67c35Sjunyoung  * This code is derived from software contributed to Berkeley by
874d67c35Sjunyoung  * Chris Torek.
974d67c35Sjunyoung  *
1074d67c35Sjunyoung  * Redistribution and use in source and binary forms, with or without
1174d67c35Sjunyoung  * modification, are permitted provided that the following conditions
1274d67c35Sjunyoung  * are met:
1374d67c35Sjunyoung  * 1. Redistributions of source code must retain the above copyright
1474d67c35Sjunyoung  *    notice, this list of conditions and the following disclaimer.
1574d67c35Sjunyoung  * 2. Redistributions in binary form must reproduce the above copyright
1674d67c35Sjunyoung  *    notice, this list of conditions and the following disclaimer in the
1774d67c35Sjunyoung  *    documentation and/or other materials provided with the distribution.
1874d67c35Sjunyoung  * 3. Neither the name of the University nor the names of its contributors
1974d67c35Sjunyoung  *    may be used to endorse or promote products derived from this software
2074d67c35Sjunyoung  *    without specific prior written permission.
2174d67c35Sjunyoung  *
2274d67c35Sjunyoung  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2374d67c35Sjunyoung  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2474d67c35Sjunyoung  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2574d67c35Sjunyoung  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2674d67c35Sjunyoung  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2774d67c35Sjunyoung  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2874d67c35Sjunyoung  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2974d67c35Sjunyoung  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3074d67c35Sjunyoung  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3174d67c35Sjunyoung  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3274d67c35Sjunyoung  * SUCH DAMAGE.
3374d67c35Sjunyoung  */
3474d67c35Sjunyoung 
3574d67c35Sjunyoung #include <sys/cdefs.h>
3674d67c35Sjunyoung #if defined(LIBC_SCCS) && !defined(lint)
37*03256c6eSchristos __RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $");
3874d67c35Sjunyoung #endif /* LIBC_SCCS and not lint */
3974d67c35Sjunyoung 
40fd5cb0acSkleink #include "namespace.h"
4174d67c35Sjunyoung #include <assert.h>
4274d67c35Sjunyoung #include <ctype.h>
4374d67c35Sjunyoung #include <string.h>
4474d67c35Sjunyoung 
4574d67c35Sjunyoung /*
4674d67c35Sjunyoung  * Find the first occurrence of find in s, ignore case.
4774d67c35Sjunyoung  */
4874d67c35Sjunyoung char *
strcasestr(const char * s,const char * find)4974d67c35Sjunyoung strcasestr(const char *s, const char *find)
5074d67c35Sjunyoung {
5174d67c35Sjunyoung 	char c, sc;
5274d67c35Sjunyoung 	size_t len;
5374d67c35Sjunyoung 
5474d67c35Sjunyoung 	_DIAGASSERT(s != NULL);
5574d67c35Sjunyoung 	_DIAGASSERT(find != NULL);
5674d67c35Sjunyoung 
5774d67c35Sjunyoung 	if ((c = *find++) != 0) {
5874d67c35Sjunyoung 		c = tolower((unsigned char)c);
5974d67c35Sjunyoung 		len = strlen(find);
6074d67c35Sjunyoung 		do {
6174d67c35Sjunyoung 			do {
6274d67c35Sjunyoung 				if ((sc = *s++) == 0)
6374d67c35Sjunyoung 					return (NULL);
6474d67c35Sjunyoung 			} while ((char)tolower((unsigned char)sc) != c);
6574d67c35Sjunyoung 		} while (strncasecmp(s, find, len) != 0);
6674d67c35Sjunyoung 		s--;
6774d67c35Sjunyoung 	}
68*03256c6eSchristos 	return __UNCONST(s);
6974d67c35Sjunyoung }
70