xref: /openbsd-src/gnu/lib/libiberty/src/memchr.c (revision 150b7e42cfa21e6546d96ae514ca23e80d970ac7)
100bf4279Sespie /*
200bf4279Sespie 
39588ddcfSespie @deftypefn Supplemental void* memchr (const void *@var{s}, int @var{c}, size_t @var{n})
400bf4279Sespie 
59588ddcfSespie This function searches memory starting at @code{*@var{s}} for the
69588ddcfSespie character @var{c}.  The search only ends with the first occurrence of
79588ddcfSespie @var{c}, or after @var{length} characters; in particular, a null
89588ddcfSespie character does not terminate the search.  If the character @var{c} is
99588ddcfSespie found within @var{length} characters of @code{*@var{s}}, a pointer
109588ddcfSespie to the character is returned.  If @var{c} is not found, then @code{NULL} is
119588ddcfSespie returned.
1200bf4279Sespie 
139588ddcfSespie @end deftypefn
1400bf4279Sespie 
1500bf4279Sespie */
1600bf4279Sespie 
1700bf4279Sespie #include <ansidecl.h>
1800bf4279Sespie #include <stddef.h>
1900bf4279Sespie 
2000bf4279Sespie PTR
memchr(register const PTR src_void,int c,size_t length)21*150b7e42Smiod memchr (register const PTR src_void, int c, size_t length)
2200bf4279Sespie {
2300bf4279Sespie   const unsigned char *src = (const unsigned char *)src_void;
2400bf4279Sespie 
259588ddcfSespie   while (length-- > 0)
2600bf4279Sespie   {
2700bf4279Sespie     if (*src == c)
2800bf4279Sespie      return (PTR)src;
2900bf4279Sespie     src++;
3000bf4279Sespie   }
3100bf4279Sespie   return NULL;
3200bf4279Sespie }
33