summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2010-01-02 10:01:11 +1100
committerKarolin Seeger <kseeger@samba.org>2012-12-14 10:01:37 +0100
commit3f0c0217adae57cdc40537a34624f1c0b0179938 (patch)
tree65fa01891fd68b05851f7fc50df5697f02f7e72e
parent42cfbb83b877714b925187b7745b214fa0a2dbad (diff)
downloadsamba-3f0c0217adae57cdc40537a34624f1c0b0179938.tar.gz
libreplace: some systems don't have memmem()
added rep_memmem() and a testsuite (cherry picked from commit fef3c910da421e890925e5e61275fc457da87f6e) (cherry picked from commit 42057793ebb3ccdc4e63f59753bca8dd677e9748)
-rw-r--r--lib/replace/libreplace.m42
-rw-r--r--lib/replace/replace.c23
-rw-r--r--lib/replace/replace.h6
-rw-r--r--lib/replace/test/testsuite.c37
4 files changed, 68 insertions, 0 deletions
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 <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 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;
}