xref: /freebsd/lib/libc/string/strnstr.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
141036d78SMike Barcroft /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
441036d78SMike Barcroft  * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
541036d78SMike Barcroft  * Copyright (c) 1990, 1993
641036d78SMike Barcroft  *	The Regents of the University of California.  All rights reserved.
741036d78SMike Barcroft  *
841036d78SMike Barcroft  * This code is derived from software contributed to Berkeley by
941036d78SMike Barcroft  * Chris Torek.
1041036d78SMike Barcroft  *
1141036d78SMike Barcroft  * Redistribution and use in source and binary forms, with or without
1241036d78SMike Barcroft  * modification, are permitted provided that the following conditions
1341036d78SMike Barcroft  * are met:
1441036d78SMike Barcroft  * 1. Redistributions of source code must retain the above copyright
1541036d78SMike Barcroft  *    notice, this list of conditions and the following disclaimer.
1641036d78SMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
1741036d78SMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
1841036d78SMike Barcroft  *    documentation and/or other materials provided with the distribution.
193fb3b97cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
2041036d78SMike Barcroft  *    may be used to endorse or promote products derived from this software
2141036d78SMike Barcroft  *    without specific prior written permission.
2241036d78SMike Barcroft  *
2341036d78SMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2441036d78SMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2541036d78SMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2641036d78SMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2741036d78SMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2841036d78SMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2941036d78SMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3041036d78SMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3141036d78SMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3241036d78SMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3341036d78SMike Barcroft  * SUCH DAMAGE.
3441036d78SMike Barcroft  */
3541036d78SMike Barcroft 
3641036d78SMike Barcroft #include <string.h>
3741036d78SMike Barcroft 
3841036d78SMike Barcroft /*
3941036d78SMike Barcroft  * Find the first occurrence of find in s, where the search is limited to the
4041036d78SMike Barcroft  * first slen characters of s.
4141036d78SMike Barcroft  */
4241036d78SMike Barcroft char *
strnstr(const char * s,const char * find,size_t slen)43bd604b4bSDaniel Gerzo strnstr(const char *s, const char *find, size_t slen)
4441036d78SMike Barcroft {
4541036d78SMike Barcroft 	char c, sc;
4641036d78SMike Barcroft 	size_t len;
4741036d78SMike Barcroft 
4841036d78SMike Barcroft 	if ((c = *find++) != '\0') {
4941036d78SMike Barcroft 		len = strlen(find);
5041036d78SMike Barcroft 		do {
5141036d78SMike Barcroft 			do {
52029f08c2SPawel Jakub Dawidek 				if (slen-- < 1 || (sc = *s++) == '\0')
5341036d78SMike Barcroft 					return (NULL);
5441036d78SMike Barcroft 			} while (sc != c);
5541036d78SMike Barcroft 			if (len > slen)
5641036d78SMike Barcroft 				return (NULL);
5741036d78SMike Barcroft 		} while (strncmp(s, find, len) != 0);
5841036d78SMike Barcroft 		s--;
5941036d78SMike Barcroft 	}
6041036d78SMike Barcroft 	return ((char *)s);
6141036d78SMike Barcroft }
62