summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--lib/ChangeLog5
-rw-r--r--lib/memmem.c58
-rw-r--r--lib/memmem.h32
-rw-r--r--m4/ChangeLog4
-rw-r--r--m4/memmem.m420
-rw-r--r--modules/memmem24
-rw-r--r--tests/test-memmem.c62
8 files changed, 211 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index cd8bad00ee..220e1d0f82 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2004-10-01 Simon Josefsson <jas@extundo.com>
+
+ * modules/memmem: New file.
+ * tests/test-memmem.c: New file.
+ * MODULES.html.sh (Extra functions based on ANSI C 89): Add memmem.
+
2004-10-01 Bruno Haible <bruno@clisp.org>
* MODULES.html.sh: Add strsep.
diff --git a/lib/ChangeLog b/lib/ChangeLog
index aaa22944df..a41db01d21 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2004-10-01 Simon Josefsson <jas@extundo.com>
+
+ * memmem.h: New file.
+ * memmem.c: New file, taken from glibc.
+
2004-10-03 Paul Eggert <eggert@cs.ucla.edu>
Sync from coreutils.
diff --git a/lib/memmem.c b/lib/memmem.c
new file mode 100644
index 0000000000..7a6e116fc4
--- /dev/null
+++ b/lib/memmem.c
@@ -0,0 +1,58 @@
+/* Copyright (C) 1991,92,93,94,96,97,98,2000,2004 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stddef.h>
+#include <string.h>
+
+#ifndef _LIBC
+# define __builtin_expect(expr, val) (expr)
+#endif
+
+#undef memmem
+
+/* Return the first occurrence of NEEDLE in HAYSTACK. */
+void *
+memmem (haystack, haystack_len, needle, needle_len)
+ const void *haystack;
+ size_t haystack_len;
+ const void *needle;
+ size_t needle_len;
+{
+ const char *begin;
+ const char *const last_possible
+ = (const char *) haystack + haystack_len - needle_len;
+
+ if (needle_len == 0)
+ /* The first occurrence of the empty string is deemed to occur at
+ the beginning of the string. */
+ return (void *) haystack;
+
+ /* Sanity check, otherwise the loop might search through the whole
+ memory. */
+ if (__builtin_expect (haystack_len < needle_len, 0))
+ return NULL;
+
+ for (begin = (const char *) haystack; begin <= last_possible; ++begin)
+ if (begin[0] == ((const char *) needle)[0] &&
+ !memcmp ((const void *) &begin[1],
+ (const void *) ((const char *) needle + 1),
+ needle_len - 1))
+ return (void *) begin;
+
+ return NULL;
+}
diff --git a/lib/memmem.h b/lib/memmem.h
new file mode 100644
index 0000000000..47dcbd8363
--- /dev/null
+++ b/lib/memmem.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA. */
+
+#ifndef MEMMEM_H
+# define MEMMEM_H
+
+/* Get memmem, if available. */
+# include <string.h>
+
+# if defined HAVE_DECL_MEMMEM && !HAVE_DECL_MEMMEM
+void *
+memmem (const void *haystack, size_t haystack_len,
+ const void *needle, size_t needle_len);
+# endif
+
+#endif /* MEMMEM_H */
diff --git a/m4/ChangeLog b/m4/ChangeLog
index 7e58d06dbe..bad15533df 100644
--- a/m4/ChangeLog
+++ b/m4/ChangeLog
@@ -1,3 +1,7 @@
+2004-10-01 Simon Josefsson <jas@extundo.com>
+
+ * memmem.m4: New file.
+
2004-10-01 Yoann Vandoorselaere <yoann@prelude-ids.org>
* strsep.m4: New file.
diff --git a/m4/memmem.m4 b/m4/memmem.m4
new file mode 100644
index 0000000000..2e4a20bc3c
--- /dev/null
+++ b/m4/memmem.m4
@@ -0,0 +1,20 @@
+# memmem.m4 serial 1
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License. As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FUNC_MEMMEM],
+[
+ dnl Persuade glibc <string.h> to declare memmem().
+ AC_REQUIRE([AC_GNU_SOURCE])
+
+ AC_REPLACE_FUNCS(memmem)
+ AC_CHECK_DECLS_ONCE(memmem)
+ gl_PREREQ_MEMMEM
+])
+
+# Prerequisites of lib/memmem.c.
+AC_DEFUN([gl_PREREQ_MEMMEM], [:])
diff --git a/modules/memmem b/modules/memmem
new file mode 100644
index 0000000000..72ebd21143
--- /dev/null
+++ b/modules/memmem
@@ -0,0 +1,24 @@
+Description:
+memmem() function: locate first substring in a buffer.
+
+Files:
+lib/memmem.h
+lib/memmem.c
+m4/memmem.m4
+
+Depends-on:
+
+configure.ac:
+gl_FUNC_MEMMEM
+
+Makefile.am:
+lib_SOURCES += memmem.h
+
+Include:
+"memmem.h"
+
+License:
+LGPL
+
+Maintainer:
+libc, Simon Josefsson
diff --git a/tests/test-memmem.c b/tests/test-memmem.c
new file mode 100644
index 0000000000..24ef8cc47e
--- /dev/null
+++ b/tests/test-memmem.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include "memmem.h"
+
+int
+main (int argc, char *argv[])
+{
+ const char *big = "foo-bar-baz";
+ char *p;
+
+ p = memmem (big, "foo", strlen (big));
+ if (p != big)
+ {
+ fprintf (stderr, "memmem FAILURE 1\n");
+ return 1;
+ }
+
+ p = memmem (big, "baz", strlen (big));
+ if (p != big + strlen (big) - 3)
+ {
+ fprintf (stderr, "memmem FAILURE 2\n");
+ return 1;
+
+ p = memmem (big, "-", strlen (big));
+ if (p != big + 3)
+ {
+ fprintf (stderr, "memmem FAILURE 3\n");
+ return 1;
+ }
+
+ p = memmem (big, "baz", strlen (big) - 1);
+ if (p != NULL)
+ {
+ fprintf (stderr, "memmem FAILURE 4\n");
+ return 1;
+ }
+
+ return 0;
+}