xref: /dflybsd-src/lib/libc/string/memrchr.c (revision 0d5acd7467c4e95f792ef49fceb3ab8e917ce86b)
1*0d5acd74SJohn Marino /*	$OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas Exp $	*/
2*0d5acd74SJohn Marino 
3716024cdSPeter Avalos /*
4716024cdSPeter Avalos  * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5716024cdSPeter Avalos  *
6716024cdSPeter Avalos  * Permission to use, copy, modify, and distribute this software for any
7716024cdSPeter Avalos  * purpose with or without fee is hereby granted, provided that the above
8716024cdSPeter Avalos  * copyright notice and this permission notice appear in all copies.
9716024cdSPeter Avalos  *
10716024cdSPeter Avalos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11716024cdSPeter Avalos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12716024cdSPeter Avalos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13716024cdSPeter Avalos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14716024cdSPeter Avalos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15716024cdSPeter Avalos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16716024cdSPeter Avalos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17716024cdSPeter Avalos  *
18*0d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/string/memrchr.c 178051 2008-04-10 00:12:44Z delphij $
19716024cdSPeter Avalos  */
20716024cdSPeter Avalos 
21716024cdSPeter Avalos #include <string.h>
22716024cdSPeter Avalos 
23716024cdSPeter Avalos /*
24716024cdSPeter Avalos  * Reverse memchr()
25716024cdSPeter Avalos  * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
26716024cdSPeter Avalos  */
27716024cdSPeter Avalos void *
memrchr(const void * s,int c,size_t n)28716024cdSPeter Avalos memrchr(const void *s, int c, size_t n)
29716024cdSPeter Avalos {
30716024cdSPeter Avalos 	const unsigned char *cp;
31716024cdSPeter Avalos 
32716024cdSPeter Avalos 	if (n != 0) {
33716024cdSPeter Avalos 		cp = (unsigned char *)s + n;
34716024cdSPeter Avalos 		do {
35716024cdSPeter Avalos 			if (*(--cp) == (unsigned char)c)
36716024cdSPeter Avalos 				return((void *)cp);
37716024cdSPeter Avalos 		} while (--n != 0);
38716024cdSPeter Avalos 	}
39716024cdSPeter Avalos 	return(NULL);
40716024cdSPeter Avalos }
41