1 #include <string.h>
2
3 /* $OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas Exp $ */
4 /*
5 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * $FreeBSD: head/lib/libc/string/memrchr.c 178051 2008-04-10 00:12:44Z delphij $
20 */
21
22
23 #ifdef MACOS
24 /*
25 * Reverse memchr()
26 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
27 */
28 void *
memrchr(const void * s,int c,size_t n)29 memrchr(const void *s, int c, size_t n)
30 {
31 const unsigned char *cp;
32
33 if (n != 0) {
34 cp = (unsigned char *)s + n;
35 do {
36 if (*(--cp) == (unsigned char)c)
37 return((void *)cp);
38 } while (--n != 0);
39 }
40 return(NULL);
41 }
42 #endif /* MACOS */
43
44