scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit a3a90734a04b065b429ab4810068a4f74386272e
parent d3f6fa971de7cee49a877f2ea8d8049cc726273d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  9 May 2019 13:41:59 +0100

[dev] Add memrchr

Diffstat:
Minclude/string.h | 1+
Msrc/libc/string/Makefile | 1+
Asrc/libc/string/memrchr.c | 13+++++++++++++
3 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/include/string.h b/include/string.h @@ -19,6 +19,7 @@ extern int strcoll(const char *s1, const char *s2); 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); diff --git a/src/libc/string/Makefile b/src/libc/string/Makefile @@ -4,6 +4,7 @@ include $(PROJECTDIR)/scripts/rules.mk include ../rules.mk OBJS = memchr.o\ + memrchr.o \ memcmp.o\ memcpy.o\ memmove.o\ diff --git a/src/libc/string/memrchr.c b/src/libc/string/memrchr.c @@ -0,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; +}