1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2007, 2010-2014
5  *	Todd C. Miller <Todd.Miller@sudo.ws>
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 
20 /*
21  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
22  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
23  */
24 
25 #include <config.h>
26 
27 #ifndef HAVE_MEMRCHR
28 
29 #include <sys/types.h>
30 
31 #include "sudo_compat.h"
32 
33 /*
34  * Reverse memchr()
35  * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
36  */
37 void *
sudo_memrchr(const void * s,int c,size_t n)38 sudo_memrchr(const void *s, int c, size_t n)
39 {
40     const unsigned char *cp;
41 
42     if (n != 0) {
43 	cp = (unsigned char *)s + n;
44 	do {
45 	    if (*(--cp) == (unsigned char)c)
46 		return (void *)cp;
47 	} while (--n != 0);
48     }
49     return (void *)0;
50 }
51 #endif /* HAVE_MEMRCHR */
52