home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / memchr.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  1KB  |  48 lines

  1. /***
  2. *memchr.c - search block of memory for a given character
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines memchr() - search memory until a character is
  8. *       found or a limit is reached.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14.  
  15. /***
  16. *char *memchr(buf, chr, cnt) - search memory for given character.
  17. *
  18. *Purpose:
  19. *       Searches at buf for the given character, stopping when chr is
  20. *       first found or cnt bytes have been searched through.
  21. *
  22. *Entry:
  23. *       void *buf  - memory buffer to be searched
  24. *       int chr    - character to search for
  25. *       size_t cnt - max number of bytes to search
  26. *
  27. *Exit:
  28. *       returns pointer to first occurence of chr in buf
  29. *       returns NULL if chr not found in the first cnt bytes
  30. *
  31. *Exceptions:
  32. *
  33. *******************************************************************************/
  34.  
  35. void * __cdecl memchr (
  36.         const void * buf,
  37.         int chr,
  38.         size_t cnt
  39.         )
  40. {
  41.         while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
  42.                 buf = (unsigned char *)buf + 1;
  43.                 cnt--;
  44.         }
  45.  
  46.         return(cnt ? (void *)buf : NULL);
  47. }
  48.