[klibc] [PATCH] memchr and memrchr

Thayne Harbaugh tharbaugh at lnxi.com
Thu Jan 6 21:22:40 PST 2005


- memchr() needs to increment pointer or it's just a busy loop (unless c
matches the first character in s)

- add memrchr.c

--- klibc-0.194/klibc/memchr.c.orig	2005-01-06 23:08:33.703355840 -0700
+++ klibc-0.194/klibc/memchr.c	2005-01-06 23:04:26.436946048 -0700
@@ -12,6 +12,7 @@
   while ( n-- ) {
     if ( *sp == (unsigned char)c )
       return (void *)sp;
+    sp++;
   }
 
   return NULL;
--- klibc-0.194/klibc/memrchr.c.orig	2005-01-06 23:09:25.787437856 -0700
+++ klibc-0.194/klibc/memrchr.c	2005-01-06 23:05:05.772966064 -0700
@@ -0,0 +1,19 @@
+/*
+ * memrchr.c
+ */
+
+#include <stddef.h>
+#include <string.h>
+
+void *memrchr(const void *s, int c, size_t n)
+{
+  const unsigned char *sp = s + n - 1;
+
+  while ( n-- ) {
+    if ( *sp == (unsigned char)c )
+      return (void *)sp;
+    sp--;
+  }
+
+  return NULL;
+}
--- klibc-0.194/klibc/Makefile.orig	2005-01-06 23:09:57.423628424 -0700
+++ klibc-0.194/klibc/Makefile	2005-01-06 23:09:48.031056312 -0700
@@ -30,7 +30,7 @@
 	  sigaction.o sigpending.o sigprocmask.o sigsuspend.o \
 	  brk.o sbrk.o malloc.o realloc.o calloc.o mmap.o \
 	  memcpy.o memcmp.o memset.o memccpy.o memmem.o memswap.o \
-	  memmove.o memchr.o \
+	  memmove.o memchr.o memrchr.o \
 	  strcasecmp.o strncasecmp.o strndup.o strerror.o \
 	  strcat.o strchr.o strcmp.o strcpy.o strdup.o strlen.o strnlen.o \
 	  strncat.o strlcpy.o strlcat.o \

-- 
Thayne Harbaugh
Linux Networx



More information about the klibc mailing list