1 /* MEMRCHR.C    (c) Copyright Volker Bandke, 2003                    */
2 /*              Hercules Right to Left memory scan                   */
3 
4 /*-------------------------------------------------------------------*/
5 /*      Scans the memory block and reports the last occurrence of    */
6 /*      the specified byte in the buffer.  Returns a pointer to      */
7 /*      the byte if found, or NULL if not found.                     */
8 /*-------------------------------------------------------------------*/
9 
10 #include "hstdinc.h"
11 
12 #define _MEMRCHR_C_
13 #define _HUTIL_DLL_
14 
15 #include "hercules.h"
16 
17 #if !defined( HAVE_MEMRCHR )
18 
19 #include "memrchr.h"
20 
memrchr(const void * buf,int c,size_t num)21 DLL_EXPORT void *memrchr(const void *buf, int c, size_t num)
22 {
23    unsigned char *pMem;
24    if (num == 0)
25    {
26       return NULL;
27    }
28    for (pMem = (unsigned char *) buf + num - 1; pMem >= (unsigned char *) buf; pMem--)
29    {
30       if (*pMem == (unsigned char) c) break;
31    }
32    if (pMem >= (unsigned char *) buf)
33    {
34       return ((void *) pMem);
35    }
36    return NULL;
37 }
38 
39 #endif // !defined(HAVE_MEMRCHR)
40