xref: /freebsd/lib/libc/string/strnstr.c (revision 029f08c23c243660e5f61d813a657c7bf2499178)
141036d78SMike Barcroft /*-
241036d78SMike Barcroft  * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
341036d78SMike Barcroft  * Copyright (c) 1990, 1993
441036d78SMike Barcroft  *	The Regents of the University of California.  All rights reserved.
541036d78SMike Barcroft  *
641036d78SMike Barcroft  * This code is derived from software contributed to Berkeley by
741036d78SMike Barcroft  * Chris Torek.
841036d78SMike Barcroft  *
941036d78SMike Barcroft  * Redistribution and use in source and binary forms, with or without
1041036d78SMike Barcroft  * modification, are permitted provided that the following conditions
1141036d78SMike Barcroft  * are met:
1241036d78SMike Barcroft  * 1. Redistributions of source code must retain the above copyright
1341036d78SMike Barcroft  *    notice, this list of conditions and the following disclaimer.
1441036d78SMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
1541036d78SMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
1641036d78SMike Barcroft  *    documentation and/or other materials provided with the distribution.
1741036d78SMike Barcroft  * 3. All advertising materials mentioning features or use of this software
1841036d78SMike Barcroft  *    must display the following acknowledgement:
1941036d78SMike Barcroft  *	This product includes software developed by the University of
2041036d78SMike Barcroft  *	California, Berkeley and its contributors.
2141036d78SMike Barcroft  * 4. Neither the name of the University nor the names of its contributors
2241036d78SMike Barcroft  *    may be used to endorse or promote products derived from this software
2341036d78SMike Barcroft  *    without specific prior written permission.
2441036d78SMike Barcroft  *
2541036d78SMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2641036d78SMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2741036d78SMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2841036d78SMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2941036d78SMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3041036d78SMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3141036d78SMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3241036d78SMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3341036d78SMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3441036d78SMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3541036d78SMike Barcroft  * SUCH DAMAGE.
3641036d78SMike Barcroft  */
3741036d78SMike Barcroft 
38de5fe5d5SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
39de5fe5d5SDavid E. O'Brien static char sccsid[] = "@(#)strstr.c	8.1 (Berkeley) 6/4/93";
40de5fe5d5SDavid E. O'Brien #endif /* LIBC_SCCS and not lint */
4141036d78SMike Barcroft #include <sys/cdefs.h>
4241036d78SMike Barcroft __FBSDID("$FreeBSD$");
4341036d78SMike Barcroft 
4441036d78SMike Barcroft #include <string.h>
4541036d78SMike Barcroft 
4641036d78SMike Barcroft /*
4741036d78SMike Barcroft  * Find the first occurrence of find in s, where the search is limited to the
4841036d78SMike Barcroft  * first slen characters of s.
4941036d78SMike Barcroft  */
5041036d78SMike Barcroft char *
5141036d78SMike Barcroft strnstr(s, find, slen)
5241036d78SMike Barcroft 	const char *s;
5341036d78SMike Barcroft 	const char *find;
5441036d78SMike Barcroft 	size_t slen;
5541036d78SMike Barcroft {
5641036d78SMike Barcroft 	char c, sc;
5741036d78SMike Barcroft 	size_t len;
5841036d78SMike Barcroft 
5941036d78SMike Barcroft 	if ((c = *find++) != '\0') {
6041036d78SMike Barcroft 		len = strlen(find);
6141036d78SMike Barcroft 		do {
6241036d78SMike Barcroft 			do {
63029f08c2SPawel Jakub Dawidek 				if (slen-- < 1 || (sc = *s++) == '\0')
6441036d78SMike Barcroft 					return (NULL);
6541036d78SMike Barcroft 			} while (sc != c);
6641036d78SMike Barcroft 			if (len > slen)
6741036d78SMike Barcroft 				return (NULL);
6841036d78SMike Barcroft 		} while (strncmp(s, find, len) != 0);
6941036d78SMike Barcroft 		s--;
7041036d78SMike Barcroft 	}
7141036d78SMike Barcroft 	return ((char *)s);
7241036d78SMike Barcroft }
73