summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2003-06-16 04:50:09 +0000
committerManuel Novoa III <mjn3@codepoet.org>2003-06-16 04:50:09 +0000
commitb34bab24093904492a924303d4e24faffee2cd87 (patch)
treec6b72498c5425885faab8a98cedaf0cb8cc4ad2a
parent889b08e7656a5975f033bdf87ff4bf6366d03606 (diff)
Add memmem().
-rw-r--r--include/string.h2
-rw-r--r--libc/string/Makefile2
-rw-r--r--libc/string/wstring.c34
3 files changed, 35 insertions, 3 deletions
diff --git a/include/string.h b/include/string.h
index 872cccab2..8aa40f262 100644
--- a/include/string.h
+++ b/include/string.h
@@ -199,14 +199,12 @@ extern char *strtok_r (char *__restrict __s, __const char *__restrict __delim,
#endif
#ifdef __USE_GNU
-#if 0
/* Find the first occurrence of NEEDLE in HAYSTACK.
NEEDLE is NEEDLELEN bytes long;
HAYSTACK is HAYSTACKLEN bytes long. */
extern void *memmem (__const void *__haystack, size_t __haystacklen,
__const void *__needle, size_t __needlelen)
__THROW __attribute_pure__;
-#endif
/* Copy N bytes of SRC to DEST, return pointer to bytes after the
last written byte. */
diff --git a/libc/string/Makefile b/libc/string/Makefile
index fe9b64382..5ec0924d8 100644
--- a/libc/string/Makefile
+++ b/libc/string/Makefile
@@ -33,7 +33,7 @@ MOBJW= basename.o bcopy.o bzero.o dirname.o ffs.o memccpy.o memchr.o memcmp.o \
strspn.o strstr.o strtok.o strtok_r.o strerror.o _susv3_strerror_r.o \
_string_syserrmsgs.o _glibc_strerror_r.o \
_string_syssigmsgs.o sys_siglist.o strsignal.o psignal.o \
- __xpg_basename.o strlcat.o strlcpy.o sys_errlist.o
+ __xpg_basename.o strlcat.o strlcpy.o sys_errlist.o memmem.o
MOBJW2= wcscasecmp.o wcscat.o wcschrnul.o wcschr.o wcscmp.o wcscpy.o wcscspn.o \
wcsdup.o wcslen.o wcsncasecmp.o wcsncat.o wcsncmp.o wcsncpy.o \
diff --git a/libc/string/wstring.c b/libc/string/wstring.c
index 9661893d5..343e82b6c 100644
--- a/libc/string/wstring.c
+++ b/libc/string/wstring.c
@@ -1462,6 +1462,40 @@ char *_glibc_strerror_r(int errnum, char *strerrbuf, size_t buflen)
#endif
/**********************************************************************/
+#ifdef L_memmem
+
+void *memmem(const void *haystack, size_t haystacklen,
+ const void *needle, size_t needlelen)
+{
+ register const char *ph;
+ register const char *pn;
+ const char *plast;
+ size_t n;
+
+ if (needlelen == 0) {
+ return (void *) haystack;
+ }
+
+ if (haystacklen >= needlelen) {
+ ph = (const char *) haystack;
+ pn = (const char *) needle;
+ plast = ph + (haystacklen - needlelen);
+
+ do {
+ n = 0;
+ while (ph[n] == pn[n]) {
+ if (++n == needlelen) {
+ return (void *) ph;
+ }
+ }
+ } while (++ph <= plast);
+ }
+
+ return NULL;
+}
+
+#endif
+/**********************************************************************/
#ifdef L_wmempcpy
#define L_mempcpy
#define Wmempcpy wmempcpy