]> git.ozlabs.org Git - ccan/commitdiff
memmem: Trivial module to provide memmem() function
authorDavid Gibson <david@gibson.dropbear.id.au>
Sat, 14 Jun 2014 16:11:25 +0000 (02:11 +1000)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 16 Jun 2014 11:42:43 +0000 (21:12 +0930)
glibc includes a memmem() function which, by analogy with strstr()
searches for a byte sequence within a larger byte sequence.  The function
isn't standard, however, so other C libraries may not include it.

This adds a trivial module providing the memmem() function, if the C
library doesn't already do so.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Makefile-ccan
ccan/memmem/LICENSE [new symlink]
ccan/memmem/_info [new file with mode: 0644]
ccan/memmem/memmem.c [new file with mode: 0644]
ccan/memmem/memmem.h [new file with mode: 0644]
ccan/memmem/test/run.c [new file with mode: 0644]

index c9115368f6faadcddf6a6f70fe50bd8217e988de..6410301d1e5d763835c50bc22156f83da6adb42d 100644 (file)
@@ -68,6 +68,7 @@ MODS_WITH_SRC := antithread \
        likely \
        list \
        md4 \
+       memmem \
        net \
        nfs \
        noerr \
diff --git a/ccan/memmem/LICENSE b/ccan/memmem/LICENSE
new file mode 120000 (symlink)
index 0000000..b7951da
--- /dev/null
@@ -0,0 +1 @@
+../../licenses/CC0
\ No newline at end of file
diff --git a/ccan/memmem/_info b/ccan/memmem/_info
new file mode 100644 (file)
index 0000000..5c736a9
--- /dev/null
@@ -0,0 +1,28 @@
+#include <string.h>
+#include "config.h"
+
+/**
+ * memmem - Trivial module providing a memmem() implementation
+ *
+ * This code implements memmem() if it's not alreayd available in the
+ * C library.
+ *
+ * License: CC0
+ */
+int main(int argc, char *argv[])
+{
+       /* Expect exactly one argument */
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               return 0;
+       }
+
+       if (strcmp(argv[1], "testdepends") == 0) {
+               printf("ccan/array_size");
+               return 0;
+       }
+
+       return 1;
+}
diff --git a/ccan/memmem/memmem.c b/ccan/memmem/memmem.c
new file mode 100644 (file)
index 0000000..4d3c2e6
--- /dev/null
@@ -0,0 +1,25 @@
+/* CC0 (Public domain) - see LICENSE file for details */
+
+#include <string.h>
+#include <ccan/memmem/memmem.h>
+
+#if !HAVE_MEMMEM
+void *memmem(const void *haystack, size_t haystacklen,
+            const void *needle, size_t needlelen)
+{
+       const char *p;
+
+       if (needlelen > haystacklen)
+               return NULL;
+
+       p = haystack;
+
+       for (p = haystack;
+            (p + needlelen) <= (haystack + haystacklen);
+            p++)
+               if (memcmp(p, needle, needlelen) == 0)
+                       return (void *)p;
+
+       return NULL;
+}
+#endif
diff --git a/ccan/memmem/memmem.h b/ccan/memmem/memmem.h
new file mode 100644 (file)
index 0000000..4da5394
--- /dev/null
@@ -0,0 +1,14 @@
+/* CC0 (Public domain) - see LICENSE file for details */
+#ifndef CCAN_MEMMEM_H
+#define CCAN_MEMMEM_H
+
+#include "config.h"
+
+#include <string.h>
+
+#if !HAVE_MEMMEM
+void *memmem(const void *haystack, size_t haystacklen,
+            const void *needle, size_t needlelen);
+#endif
+
+#endif /* CCAN_MEMMEM_H */
diff --git a/ccan/memmem/test/run.c b/ccan/memmem/test/run.c
new file mode 100644 (file)
index 0000000..af9aac5
--- /dev/null
@@ -0,0 +1,20 @@
+#include <ccan/array_size/array_size.h>
+#include <ccan/memmem/memmem.h>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       char haystack1[] = "abcd\0efgh";
+       char needle1[] = "ab";
+       char needle2[] = "d\0e";
+
+       /* This is how many tests you plan to run */
+       plan_tests(3);
+
+       ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
+       ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
+       ok1(memmem(haystack1, sizeof(haystack1), needle2, 3) == (haystack1 + 3));
+
+       /* This exits depending on whether all tests passed */
+       return exit_status();
+}