xref: /openbsd-src/share/man/man9/strnstr.9 (revision e86eac0aa26ae70ca99c10330901a05f18155e9a)
1.\"	$OpenBSD: strnstr.9,v 1.1 2023/12/21 02:57:14 jsg Exp $
2.\"
3.\" Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
4.\" Copyright (c) 1990, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" Chris Torek and the American National Standards Committee X3,
9.\" on Information Processing Systems.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\"    notice, this list of conditions and the following disclaimer in the
18.\"    documentation and/or other materials provided with the distribution.
19.\" 3. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.Dd $Mdocdate: December 21 2023 $
36.Dt STRNSTR 9
37.Os
38.Sh NAME
39.Nm strnstr
40.Nd locate a substring in a string
41.Sh SYNOPSIS
42.In lib/libkern/libkern.h
43.Ft char *
44.Fn strnstr "const char *big" "const char *little" "size_t len"
45.Sh DESCRIPTION
46The
47.Fn strnstr
48function
49locates the first occurrence of the null-terminated string
50.Fa little
51in the string
52.Fa big ,
53where not more than
54.Fa len
55characters are searched.
56Characters that appear after a
57.Ql \e0
58character are not searched.
59.Sh RETURN VALUES
60If
61.Fa little
62is an empty string,
63.Fa big
64is returned;
65if
66.Fa little
67occurs nowhere in
68.Fa big ,
69.Dv NULL
70is returned;
71otherwise a pointer to the first character of the first occurrence of
72.Fa little
73is returned.
74.Sh EXAMPLES
75The following sets the pointer
76.Va ptr
77to
78.Dv NULL ,
79because only the first 4 characters of
80.Va largestring
81are searched:
82.Bd -literal -offset indent
83const char *largestring = "Foo Bar Baz";
84const char *smallstring = "Bar";
85char *ptr;
86
87ptr = strnstr(largestring, smallstring, 4);
88.Ed
89.Sh SEE ALSO
90.Xr memchr 9
91