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