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