xref: /illumos-gate/usr/src/lib/libc/port/gen/memrchr.c (revision 9174bfaa08ca3aa4c0a12e840c4bd4f2570237a0)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2021 Oxide Computer Company
14  */
15 
16 /*
17  * memrchr(3C) implementation. Find the first occurence of 'c' as an unsigned
18  * char in 's', by searching in reverse.
19  */
20 
21 #include <string.h>
22 
23 void *
24 memrchr(const void *s, int c, size_t len)
25 {
26 	unsigned char val = (unsigned char)c;
27 	const unsigned char *data = s;
28 
29 	for (; len > 0; len--) {
30 		size_t pos = len - 1;
31 
32 		if (data[pos] == val) {
33 			return ((void *)&data[pos]);
34 		}
35 	}
36 
37 	return (NULL);
38 }
39