memchr.c 815 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. @deftypefn Supplemental void* memchr (const void *@var{s}, int @var{c}, @
  3. size_t @var{n})
  4. This function searches memory starting at @code{*@var{s}} for the
  5. character @var{c}. The search only ends with the first occurrence of
  6. @var{c}, or after @var{length} characters; in particular, a null
  7. character does not terminate the search. If the character @var{c} is
  8. found within @var{length} characters of @code{*@var{s}}, a pointer
  9. to the character is returned. If @var{c} is not found, then @code{NULL} is
  10. returned.
  11. @end deftypefn
  12. */
  13. #include <ansidecl.h>
  14. #include <stddef.h>
  15. PTR
  16. memchr (register const PTR src_void, int c, size_t length)
  17. {
  18. const unsigned char *src = (const unsigned char *)src_void;
  19. while (length-- > 0)
  20. {
  21. if (*src == c)
  22. return (PTR)src;
  23. src++;
  24. }
  25. return NULL;
  26. }