xref: /freebsd/sys/libkern/strnstr.c (revision 4211457e40e07f6f820c4171c7db81f028fd23af)
1*4211457eSGreg V /*-
2*4211457eSGreg V  * SPDX-License-Identifier: BSD-3-Clause
3*4211457eSGreg V  *
4*4211457eSGreg V  * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
5*4211457eSGreg V  * Copyright (c) 1990, 1993
6*4211457eSGreg V  *	The Regents of the University of California.  All rights reserved.
7*4211457eSGreg V  *
8*4211457eSGreg V  * This code is derived from software contributed to Berkeley by
9*4211457eSGreg V  * Chris Torek.
10*4211457eSGreg V  *
11*4211457eSGreg V  * Redistribution and use in source and binary forms, with or without
12*4211457eSGreg V  * modification, are permitted provided that the following conditions
13*4211457eSGreg V  * are met:
14*4211457eSGreg V  * 1. Redistributions of source code must retain the above copyright
15*4211457eSGreg V  *    notice, this list of conditions and the following disclaimer.
16*4211457eSGreg V  * 2. Redistributions in binary form must reproduce the above copyright
17*4211457eSGreg V  *    notice, this list of conditions and the following disclaimer in the
18*4211457eSGreg V  *    documentation and/or other materials provided with the distribution.
19*4211457eSGreg V  * 3. Neither the name of the University nor the names of its contributors
20*4211457eSGreg V  *    may be used to endorse or promote products derived from this software
21*4211457eSGreg V  *    without specific prior written permission.
22*4211457eSGreg V  *
23*4211457eSGreg V  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*4211457eSGreg V  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*4211457eSGreg V  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*4211457eSGreg V  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*4211457eSGreg V  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*4211457eSGreg V  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*4211457eSGreg V  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*4211457eSGreg V  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*4211457eSGreg V  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*4211457eSGreg V  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*4211457eSGreg V  * SUCH DAMAGE.
34*4211457eSGreg V  */
35*4211457eSGreg V 
36*4211457eSGreg V #include <sys/cdefs.h>
37*4211457eSGreg V 
38*4211457eSGreg V #include <sys/param.h>
39*4211457eSGreg V #include <sys/libkern.h>
40*4211457eSGreg V 
41*4211457eSGreg V /*
42*4211457eSGreg V  * Find the first occurrence of find in s, where the search is limited to the
43*4211457eSGreg V  * first slen characters of s.
44*4211457eSGreg V  */
45*4211457eSGreg V char *
strnstr(const char * s,const char * find,size_t slen)46*4211457eSGreg V strnstr(const char *s, const char *find, size_t slen)
47*4211457eSGreg V {
48*4211457eSGreg V 	char c, sc;
49*4211457eSGreg V 	size_t len;
50*4211457eSGreg V 
51*4211457eSGreg V 	if ((c = *find++) != '\0') {
52*4211457eSGreg V 		len = strlen(find);
53*4211457eSGreg V 		do {
54*4211457eSGreg V 			do {
55*4211457eSGreg V 				if (slen-- < 1 || (sc = *s++) == '\0')
56*4211457eSGreg V 					return (NULL);
57*4211457eSGreg V 			} while (sc != c);
58*4211457eSGreg V 			if (len > slen)
59*4211457eSGreg V 				return (NULL);
60*4211457eSGreg V 		} while (strncmp(s, find, len) != 0);
61*4211457eSGreg V 		s--;
62*4211457eSGreg V 	}
63*4211457eSGreg V 	return (__DECONST(char *, s));
64*4211457eSGreg V }
65