ref: 38237dc6cf5af3058d4935300d75410f68cf1810
parent: aa0b415d7be2602637696ad6235fb6b569c98199
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Feb 17 03:47:08 EST 2017
[libc] Add memchr()
--- a/libc/src/Makefile
+++ b/libc/src/Makefile
@@ -3,7 +3,7 @@
LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
strrchr.o strcat.o strncpy.o strncat.o \
- memset.o memcpy.o memmove.o memcmp.o
+ memset.o memcpy.o memmove.o memcmp.o memchr.o
all: libc.a
--- /dev/null
+++ b/libc/src/memchr.c
@@ -1,0 +1,13 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <string.h>
+
+void *
+memchr(const void *s, int c, size_t n)
+{
+ unsigned char *bp = (char *) s;
+
+ while (n > 0 && *bp++ != c)
+ --n;
+ return (n == 0) ? NULL : bp-1;
+}