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