memmem.c: New file, from gnulib.

* 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.

From-SVN: r145139
This commit is contained in:
Ian Lance Taylor 2009-03-27 22:38:23 +00:00 committed by Ian Lance Taylor
parent 2ac26e152e
commit 5fbd944721
7 changed files with 109 additions and 8 deletions

View File

@ -1,3 +1,13 @@
2008-03-27 Ian Lance Taylor <iant@google.com>
* 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 <jason@redhat.com>
* cp-demangle.c (d_expression): Handle pack expansion.

View File

@ -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/$@; \

View File

@ -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

4
libiberty/configure vendored
View File

@ -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 \

View File

@ -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 \

View File

@ -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})

70
libiberty/memmem.c Normal file
View File

@ -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 <config.h>
#endif
#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 (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;
}