source: trunk/libs/newlib/src/newlib/libc/string/strnstr.c @ 620

Last change on this file since 620 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.1 KB
Line 
1/*
2FUNCTION
3        <<strnstr>>---find string segment
4
5INDEX
6        strnstr
7
8SYNOPSIS
9        #include <string.h>
10        size_t strnstr(const char *<[s1]>, const char *<[s2]>, size_t <[n]>);
11
12DESCRIPTION
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]>
16
17RETURNS
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.
21
22
23PORTABILITY
24<<strnstr>> is a BSD extension.
25
26<<strnstr>> requires no supporting OS subroutines.
27
28QUICKREF
29        strnstr pure
30
31*/
32
33#define _GNU_SOURCE
34#include <string.h>
35
36/*
37 * Find the first occurrence of find in s, where the search is limited to the
38 * first slen characters of s.
39 */
40char *
41strnstr(const char *haystack, const char *needle, size_t haystack_len)
42{
43  size_t needle_len = strnlen(needle, haystack_len);
44
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;
49  }
50  return NULL;
51}
Note: See TracBrowser for help on using the repository browser.