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 36de5fe5d5SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint) 37de5fe5d5SDavid E. O'Brien static char sccsid[] = "@(#)strstr.c 8.1 (Berkeley) 6/4/93"; 38de5fe5d5SDavid E. O'Brien #endif /* LIBC_SCCS and not lint */ 3941036d78SMike Barcroft #include <sys/cdefs.h> 4041036d78SMike Barcroft __FBSDID("$FreeBSD$"); 4141036d78SMike Barcroft 4241036d78SMike Barcroft #include <string.h> 4341036d78SMike Barcroft 4441036d78SMike Barcroft /* 4541036d78SMike Barcroft * Find the first occurrence of find in s, where the search is limited to the 4641036d78SMike Barcroft * first slen characters of s. 4741036d78SMike Barcroft */ 4841036d78SMike Barcroft char * 49bd604b4bSDaniel Gerzo strnstr(const char *s, const char *find, size_t slen) 5041036d78SMike Barcroft { 5141036d78SMike Barcroft char c, sc; 5241036d78SMike Barcroft size_t len; 5341036d78SMike Barcroft 5441036d78SMike Barcroft if ((c = *find++) != '\0') { 5541036d78SMike Barcroft len = strlen(find); 5641036d78SMike Barcroft do { 5741036d78SMike Barcroft do { 58029f08c2SPawel Jakub Dawidek if (slen-- < 1 || (sc = *s++) == '\0') 5941036d78SMike Barcroft return (NULL); 6041036d78SMike Barcroft } while (sc != c); 6141036d78SMike Barcroft if (len > slen) 6241036d78SMike Barcroft return (NULL); 6341036d78SMike Barcroft } while (strncmp(s, find, len) != 0); 6441036d78SMike Barcroft s--; 6541036d78SMike Barcroft } 6641036d78SMike Barcroft return ((char *)s); 6741036d78SMike Barcroft } 68