Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

  1. /***
  2. *memchr.c - search block of memory for a given character
  3. *
  4. * Copyright (c) 1985-2001, 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. *Revision History:
  11. * 05-31-89 JCR C version created.
  12. * 02-27-90 GJF Fixed calling type, #include <cruntime.h>, fixed
  13. * copyright.
  14. * 08-14-90 SBM Compiles cleanly with -W3, removed now redundant
  15. * #include <stddef.h>
  16. * 10-01-90 GJF New-style function declarator. Also, rewrote expr. to
  17. * avoid using cast as an lvalue.
  18. * 04-26-91 SRW Removed level 3 warnings
  19. * 09-01-93 GJF Replaced _CALLTYPE1 with __cdecl.
  20. * 03-15-95 GJF Unified PMAC and Win32 versions, elimating bug in
  21. * PMAC version in the process
  22. *
  23. *******************************************************************************/
  24. #include <cruntime.h>
  25. #include <string.h>
  26. /***
  27. *char *memchr(buf, chr, cnt) - search memory for given character.
  28. *
  29. *Purpose:
  30. * Searches at buf for the given character, stopping when chr is
  31. * first found or cnt bytes have been searched through.
  32. *
  33. *Entry:
  34. * void *buf - memory buffer to be searched
  35. * int chr - character to search for
  36. * size_t cnt - max number of bytes to search
  37. *
  38. *Exit:
  39. * returns pointer to first occurence of chr in buf
  40. * returns NULL if chr not found in the first cnt bytes
  41. *
  42. *Exceptions:
  43. *
  44. *******************************************************************************/
  45. void * __cdecl memchr (
  46. const void * buf,
  47. int chr,
  48. size_t cnt
  49. )
  50. {
  51. while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
  52. buf = (unsigned char *)buf + 1;
  53. cnt--;
  54. }
  55. return(cnt ? (void *)buf : NULL);
  56. }