Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / strnstr.c
blobcb5f71914185a3430568dacec79559cc06f36c8f
1 /*
2 FUNCTION
3 <<strnstr>>---find string segment
5 INDEX
6 strnstr
8 SYNOPSIS
9 #include <string.h>
10 size_t strnstr(const char *<[s1]>, const char *<[s2]>, size_t <[n]>);
12 DESCRIPTION
13 Locates the first occurrence in the string pointed to by <[s1]> of
14 the sequence of limited to the <[n]> characters in the string
15 pointed to by <[s2]>
17 RETURNS
18 Returns a pointer to the located string segment, or a null
19 pointer if the string <[s2]> is not found. If <[s2]> points to
20 a string with zero length, <[s1]> is returned.
23 PORTABILITY
24 <<strnstr>> is a BSD extension.
26 <<strnstr>> requires no supporting OS subroutines.
28 QUICKREF
29 strnstr pure
33 #define _GNU_SOURCE
34 #include <string.h>
37 * Find the first occurrence of find in s, where the search is limited to the
38 * first slen characters of s.
40 char *
41 strnstr(const char *haystack, const char *needle, size_t haystack_len)
43 size_t needle_len = strnlen(needle, haystack_len);
45 if (needle_len < haystack_len || !needle[needle_len]) {
46 char *x = memmem(haystack, haystack_len, needle, needle_len);
47 if (x && !memchr(haystack, 0, x - haystack))
48 return x;
50 return NULL;