diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index a15a86005f..141246a124 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,13 @@ +2008-03-27 Ian Lance Taylor + + * memmem.c: New file, from gnulib. + * configure.ac: Add memmem to list of functions provided if they + are not available on the host. + * Makefile.in: Rebuild dependencies. + (CFILES): Add memmem.c. + (CONFIGURED_OFILES): Add memmem.o. + * configure, config.in, functions.texi: Rebuild. + 2009-03-23 Jason Merrill * cp-demangle.c (d_expression): Handle pack expansion. diff --git a/libiberty/Makefile.in b/libiberty/Makefile.in index 41a71f5ea3..29068ea876 100644 --- a/libiberty/Makefile.in +++ b/libiberty/Makefile.in @@ -135,8 +135,8 @@ CFILES = alloca.c argv.c asprintf.c atexit.c \ lbasename.c \ lrealpath.c \ make-relative-prefix.c \ - make-temp-file.c md5.c memchr.c memcmp.c memcpy.c memmove.c \ - mempcpy.c memset.c mkstemps.c \ + make-temp-file.c md5.c memchr.c memcmp.c memcpy.c memmem.c \ + memmove.c mempcpy.c memset.c mkstemps.c \ objalloc.c obstack.c \ partition.c pexecute.c \ pex-common.c pex-djgpp.c pex-msdos.c pex-one.c \ @@ -187,8 +187,8 @@ CONFIGURED_OFILES = ./asprintf.o ./atexit.o \ ./ffs.o \ ./getcwd.o ./getpagesize.o ./gettimeofday.o \ ./index.o ./insque.o \ - ./memchr.o ./memcmp.o ./memcpy.o ./memmove.o ./mempcpy.o \ - ./memset.o ./mkstemps.o \ + ./memchr.o ./memcmp.o ./memcpy.o ./memmem.o ./memmove.o \ + ./mempcpy.o ./memset.o ./mkstemps.o \ ./pex-djgpp.o ./pex-msdos.o \ ./pex-unix.o ./pex-win32.o \ ./putenv.o \ @@ -798,6 +798,12 @@ $(CONFIGURED_OFILES): stamp-picdir else true; fi $(COMPILE.c) $(srcdir)/memcpy.c $(OUTPUT_OPTION) +./memmem.o: $(srcdir)/memmem.c config.h + if [ x"$(PICFLAG)" != x ]; then \ + $(COMPILE.c) $(PICFLAG) $(srcdir)/memmem.c -o pic/$@; \ + else true; fi + $(COMPILE.c) $(srcdir)/memmem.c $(OUTPUT_OPTION) + ./memmove.o: $(srcdir)/memmove.c $(INCDIR)/ansidecl.h if [ x"$(PICFLAG)" != x ]; then \ $(COMPILE.c) $(PICFLAG) $(srcdir)/memmove.c -o pic/$@; \ diff --git a/libiberty/config.in b/libiberty/config.in index 9260d560a6..d34320c717 100644 --- a/libiberty/config.in +++ b/libiberty/config.in @@ -139,6 +139,9 @@ /* Define to 1 if you have the `memcpy' function. */ #undef HAVE_MEMCPY +/* Define to 1 if you have the `memmem' function. */ +#undef HAVE_MEMMEM + /* Define to 1 if you have the `memmove' function. */ #undef HAVE_MEMMOVE diff --git a/libiberty/configure b/libiberty/configure index 4469808cde..4d0aeae4e5 100755 --- a/libiberty/configure +++ b/libiberty/configure @@ -5420,6 +5420,7 @@ funcs="$funcs insque" funcs="$funcs memchr" funcs="$funcs memcmp" funcs="$funcs memcpy" +funcs="$funcs memmem" funcs="$funcs memmove" funcs="$funcs mempcpy" funcs="$funcs memset" @@ -5530,11 +5531,12 @@ if test "x" = "y"; then + for ac_func in asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \ getcwd getpagesize gettimeofday index insque mkstemps memchr memcmp memcpy \ - memmove mempcpy memset putenv random rename rindex sigsetmask \ + memmem memmove mempcpy memset putenv random rename rindex sigsetmask \ strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strndup strrchr strstr \ strtod strtol strtoul strverscmp tmpnam vasprintf vfprintf vprintf \ vsprintf waitpid getrusage on_exit psignal strerror strsignal \ diff --git a/libiberty/configure.ac b/libiberty/configure.ac index 00fce38481..7d167c35a8 100644 --- a/libiberty/configure.ac +++ b/libiberty/configure.ac @@ -317,6 +317,7 @@ funcs="$funcs insque" funcs="$funcs memchr" funcs="$funcs memcmp" funcs="$funcs memcpy" +funcs="$funcs memmem" funcs="$funcs memmove" funcs="$funcs mempcpy" funcs="$funcs memset" @@ -362,7 +363,7 @@ checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4 __fsetlocking" if test "x" = "y"; then AC_CHECK_FUNCS(asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \ getcwd getpagesize gettimeofday index insque mkstemps memchr memcmp memcpy \ - memmove mempcpy memset putenv random rename rindex sigsetmask \ + memmem memmove mempcpy memset putenv random rename rindex sigsetmask \ strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strndup strrchr strstr \ strtod strtol strtoul strverscmp tmpnam vasprintf vfprintf vprintf \ vsprintf waitpid getrusage on_exit psignal strerror strsignal \ diff --git a/libiberty/functions.texi b/libiberty/functions.texi index 34566d8f17..da9a320223 100644 --- a/libiberty/functions.texi +++ b/libiberty/functions.texi @@ -158,7 +158,7 @@ not recommended. @end deftypefn -@c make-temp-file.c:87 +@c make-temp-file.c:95 @deftypefn Replacement char* choose_tmpdir () Returns a pointer to a directory path suitable for creating temporary @@ -602,7 +602,7 @@ relative prefix can be found, return @code{NULL}. @end deftypefn -@c make-temp-file.c:137 +@c make-temp-file.c:163 @deftypefn Replacement char* make_temp_file (const char *@var{suffix}) Return a temporary file name (as a string) or @code{NULL} if unable to @@ -643,6 +643,15 @@ Copies @var{length} bytes from memory region @var{in} to region @end deftypefn +@c memmem.c:20 +@deftypefn Supplemental void* memmem (const void *@var{haystack}, size_t @var{haystack_len} const void *@var{needle}, size_t @var{needle_len}) + +Returns a pointer to the first occurrence of @var{needle} (length +@var{needle_len}) in @var{haystack} (length @var{haystack_len}). +Returns @code{NULL} if not found. + +@end deftypefn + @c memmove.c:6 @deftypefn Supplemental void* memmove (void *@var{from}, const void *@var{to}, size_t @var{count}) diff --git a/libiberty/memmem.c b/libiberty/memmem.c new file mode 100644 index 0000000000..5d755992b7 --- /dev/null +++ b/libiberty/memmem.c @@ -0,0 +1,70 @@ +/* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* + +@deftypefn Supplemental void* memmem (const void *@var{haystack}, size_t @var{haystack_len} const void *@var{needle}, size_t @var{needle_len}) + +Returns a pointer to the first occurrence of @var{needle} (length +@var{needle_len}) in @var{haystack} (length @var{haystack_len}). +Returns @code{NULL} if not found. + +@end deftypefn + +*/ + +#ifndef _LIBC +# include +#endif + +#include +#include + +#ifndef _LIBC +# define __builtin_expect(expr, val) (expr) +#endif + +#undef memmem + +/* Return the first occurrence of NEEDLE in HAYSTACK. */ +void * +memmem (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; +}