ref: 3bcf1091863844fc22dbbdb70117c883c242dfdf
parent: 86b5e746d19539add2df370455006cfc4c679648
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu May 9 09:41:59 EDT 2019
[dev] Add memrchr
--- a/include/string.h
+++ b/include/string.h
@@ -19,6 +19,7 @@
extern int strncmp(const char *s1, const char *s2, size_t n);
extern size_t strxfrm(char * restrict s1, const char * restrict s2, size_t n);
extern void *memchr(const void *s, int c, size_t n);
+extern void *memrchr(const void *s, int c, size_t n);
extern char *strchr(const char *s, int c);
extern size_t strcspn(const char *s1, const char *s2);
extern char *strpbrk(const char *s1, const char *s2);
--- a/src/libc/string/Makefile
+++ b/src/libc/string/Makefile
@@ -4,6 +4,7 @@
include ../rules.mk
OBJS = memchr.o\
+ memrchr.o \
memcmp.o\
memcpy.o\
memmove.o\
--- /dev/null
+++ b/src/libc/string/memrchr.c
@@ -1,0 +1,13 @@
+#include <string.h>
+
+#undef memrchr
+
+void *
+memrchr(const void *s, int c, size_t n)
+{
+ unsigned char *p = s;
+
+ for (p += n-1; n > 0 && *p != c; --p)
+ n--;
+ return (n == 0) ? NULL : p;
+}