diff options
author | Andrew Tridgell <tridge@samba.org> | 2010-01-02 10:01:11 +1100 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2010-01-02 10:08:12 +1100 |
commit | fef3c910da421e890925e5e61275fc457da87f6e (patch) | |
tree | 3884c2b98c9ccf3b13b11f2c156769014c1e5527 /lib/replace | |
parent | 00b39c70f57882a453a8d2e6b0f1f37fd39a2d2a (diff) | |
download | samba-fef3c910da421e890925e5e61275fc457da87f6e.tar.gz samba-fef3c910da421e890925e5e61275fc457da87f6e.tar.bz2 samba-fef3c910da421e890925e5e61275fc457da87f6e.zip |
libreplace: some systems don't have memmem()
added rep_memmem() and a testsuite
Diffstat (limited to 'lib/replace')
-rw-r--r-- | lib/replace/libreplace.m4 | 2 | ||||
-rw-r--r-- | lib/replace/replace.c | 23 | ||||
-rw-r--r-- | lib/replace/replace.h | 6 | ||||
-rw-r--r-- | lib/replace/test/testsuite.c | 37 |
4 files changed, 68 insertions, 0 deletions
diff --git a/lib/replace/libreplace.m4 b/lib/replace/libreplace.m4 index af8587938d..1353c1f7d2 100644 --- a/lib/replace/libreplace.m4 +++ b/lib/replace/libreplace.m4 @@ -228,6 +228,8 @@ AC_HAVE_DECL(environ, [#include <unistd.h>]) 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 fc15717349..17fd46bcc8 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 6424d10c0f..baf2368130 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 7929f11add..caa70d68e3 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; } |