memchr.c revision 169695
1284345Ssjg/*
2284345Ssjg
3284345Ssjg@deftypefn Supplemental void* memchr (const void *@var{s}, int @var{c}, size_t @var{n})
4284345Ssjg
5284345SsjgThis function searches memory starting at @code{*@var{s}} for the
6284345Ssjgcharacter @var{c}.  The search only ends with the first occurrence of
7284345Ssjg@var{c}, or after @var{length} characters; in particular, a null
8284345Ssjgcharacter does not terminate the search.  If the character @var{c} is
9284345Ssjgfound within @var{length} characters of @code{*@var{s}}, a pointer
10284345Ssjgto the character is returned.  If @var{c} is not found, then @code{NULL} is
11284345Ssjgreturned.
12284345Ssjg
13284345Ssjg@end deftypefn
14284345Ssjg
15284345Ssjg*/
16284345Ssjg
17284345Ssjg#include <ansidecl.h>
18284345Ssjg#include <stddef.h>
19
20PTR
21memchr (register const PTR src_void, int c, size_t length)
22{
23  const unsigned char *src = (const unsigned char *)src_void;
24
25  while (length-- > 0)
26  {
27    if (*src == c)
28     return (PTR)src;
29    src++;
30  }
31  return NULL;
32}
33