From 3f0c0217adae57cdc40537a34624f1c0b0179938 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 2 Jan 2010 10:01:11 +1100 Subject: [PATCH] libreplace: some systems don't have memmem() added rep_memmem() and a testsuite (cherry picked from commit fef3c910da421e890925e5e61275fc457da87f6e) (cherry picked from commit 42057793ebb3ccdc4e63f59753bca8dd677e9748) --- lib/replace/libreplace.m4 | 2 ++ lib/replace/replace.c | 23 ++++++++++++++++++++++ lib/replace/replace.h | 6 ++++++ lib/replace/test/testsuite.c | 37 ++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/lib/replace/libreplace.m4 b/lib/replace/libreplace.m4 index af8587938d1..1353c1f7d26 100644 --- a/lib/replace/libreplace.m4 +++ b/lib/replace/libreplace.m4 @@ -228,6 +228,8 @@ AC_HAVE_DECL(environ, [#include ]) AC_CHECK_FUNCS(strnlen) AC_CHECK_FUNCS(strtoull __strtoull strtouq strtoll __strtoll strtoq) +AC_CHECK_FUNCS(memmem) + # this test disabled as we don't actually need __VA_ARGS__ yet AC_TRY_CPP([ #define eprintf(...) fprintf(stderr, __VA_ARGS__) diff --git a/lib/replace/replace.c b/lib/replace/replace.c index fc15717349e..17fd46bcc89 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -681,3 +681,26 @@ char *rep_realpath(const char *path, char *resolved_path) return NULL; } #endif + + +#ifndef HAVE_MEMMEM +void *rep_memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen) +{ + if (needlelen == 0) { + return discard_const(haystack); + } + while (haystacklen >= needlelen) { + char *p = memchr(haystack, *(const char *)needle, + haystacklen-(needlelen-1)); + if (!p) return NULL; + if (memcmp(p, needle, needlelen) == 0) { + return p; + } + haystack = p+1; + haystacklen -= (p - (const char *)haystack) + 1; + } + return NULL; +} +#endif + diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 6424d10c0f9..baf2368130c 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -140,6 +140,12 @@ char *rep_strdup(const char *s); void *rep_memmove(void *dest,const void *src,int size); #endif +#ifndef HAVE_MEMMEM +#define memmem rep_memmem +void *rep_memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); +#endif + #ifndef HAVE_MKTIME #define mktime rep_mktime /* prototype is in "system/time.h" */ diff --git a/lib/replace/test/testsuite.c b/lib/replace/test/testsuite.c index 7929f11addf..caa70d68e39 100644 --- a/lib/replace/test/testsuite.c +++ b/lib/replace/test/testsuite.c @@ -1015,6 +1015,42 @@ static int test_utimes(void) return true; } +static int test_memmem(void) +{ + char *s; + + printf("test: memmem\n"); + + s = memmem("foo", 3, "fo", 2); + if (strcmp(s, "foo") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + s = memmem("foo", 3, "", 0); + if (strcmp(s, "foo") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + s = memmem("foo", 4, "o", 1); + if (strcmp(s, "oo") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + s = memmem("foobarfodx", 11, "fod", 3); + if (strcmp(s, "fodx") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + printf("success: memmem\n"); + + return true; +} + + struct torture_context; bool torture_local_replace(struct torture_context *ctx) { @@ -1065,6 +1101,7 @@ bool torture_local_replace(struct torture_context *ctx) ret &= test_getifaddrs(); ret &= test_utime(); ret &= test_utimes(); + ret &= test_memmem(); return ret; } -- 2.34.1