libprelude/master: Update GnuLib code
[email protected] Wed, 8 Jul 2009 16:51:57 +0200 (CEST)
| Newsgroups | gmane.comp.security.ids.prelude.cvs |
|---|---|
| Message-ID | <[email protected]> |
commit faf9667a6e449e7d876ea1bf37b3e62e171e69bf Author: Yoann Vandoorselaere <[email protected]> Date: Wed Jul 8 15:52:50 2009 +0200 Update GnuLib code ======================================== libmissing/Makefile.am | 23 ++++++ libmissing/errno.in.h | 7 ++- libmissing/getpagesize.c | 39 +++++++++ libmissing/m4/errno_h.m4 | 5 +- libmissing/m4/gnulib-comp.m4 | 12 ++- libmissing/m4/lib-link.m4 | 7 +- libmissing/m4/mbrtowc.m4 | 3 +- libmissing/m4/memchr.m4 | 86 ++++++++++++++++++++ libmissing/m4/stdio_h.m4 | 5 +- libmissing/m4/strcasestr.m4 | 22 ++++-- libmissing/m4/string_h.m4 | 6 +- libmissing/memchr.c | 172 ++++++++++++++++++++++++++++++++++++++++ libmissing/memchr.valgrind | 14 +++ libmissing/stdio.in.h | 20 +++++- libmissing/strerror.c | 8 ++- libmissing/string.in.h | 17 ++++- libmissing/tests/Makefile.am | 17 ++-- libmissing/tests/getpagesize.c | 39 --------- libmissing/tests/test-memchr.c | 130 ++++++++++++++++++++++++++++++ libmissing/tests/test-stdio.c | 8 ++- libmissing/tests/test-stdlib.c | 8 ++- libmissing/tests/test-string.c | 8 ++- libmissing/tests/test-unistd.c | 6 ++ 23 files changed, 591 insertions(+), 71 deletions(-) ======================================== diff --git a/libmissing/Makefile.am b/libmissing/Makefile.am index eba4298..9ab1993 100644 --- a/libmissing/Makefile.am +++ b/libmissing/Makefile.am @@ -350,6 +350,15 @@ EXTRA_libmissing_la_SOURCES += getline.c ## end gnulib module getline +## begin gnulib module getpagesize + + +EXTRA_DIST += getpagesize.c + +EXTRA_libmissing_la_SOURCES += getpagesize.c + +## end gnulib module getpagesize + ## begin gnulib module getpass @@ -566,6 +575,15 @@ EXTRA_libmissing_la_SOURCES += mbsinit.c ## end gnulib module mbsinit +## begin gnulib module memchr + + +EXTRA_DIST += memchr.c memchr.valgrind + +EXTRA_libmissing_la_SOURCES += memchr.c + +## end gnulib module memchr + ## begin gnulib module memmem-simple @@ -892,6 +910,7 @@ stdio.h: stdio.in.h -e 's|@''GNULIB_FTELL''@|$(GNULIB_FTELL)|g' \ -e 's|@''GNULIB_FTELLO''@|$(GNULIB_FTELLO)|g' \ -e 's|@''GNULIB_FFLUSH''@|$(GNULIB_FFLUSH)|g' \ + -e 's|@''GNULIB_FPURGE''@|$(GNULIB_FPURGE)|g' \ -e 's|@''GNULIB_FCLOSE''@|$(GNULIB_FCLOSE)|g' \ -e 's|@''GNULIB_FPUTC''@|$(GNULIB_FPUTC)|g' \ -e 's|@''GNULIB_PUTC''@|$(GNULIB_PUTC)|g' \ @@ -929,6 +948,8 @@ stdio.h: stdio.in.h -e 's|@''REPLACE_FTELLO''@|$(REPLACE_FTELLO)|g' \ -e 's|@''REPLACE_FTELL''@|$(REPLACE_FTELL)|g' \ -e 's|@''REPLACE_FFLUSH''@|$(REPLACE_FFLUSH)|g' \ + -e 's|@''REPLACE_FPURGE''@|$(REPLACE_FPURGE)|g' \ + -e 's|@''HAVE_DECL_FPURGE''@|$(HAVE_DECL_FPURGE)|g' \ -e 's|@''REPLACE_FCLOSE''@|$(REPLACE_FCLOSE)|g' \ -e 's|@''HAVE_DECL_GETDELIM''@|$(HAVE_DECL_GETDELIM)|g' \ -e 's|@''HAVE_DECL_GETLINE''@|$(HAVE_DECL_GETLINE)|g' \ @@ -1083,6 +1104,7 @@ string.h: string.in.h -e 's|@''GNULIB_MBSSPN''@|$(GNULIB_MBSSPN)|g' \ -e 's|@''GNULIB_MBSSEP''@|$(GNULIB_MBSSEP)|g' \ -e 's|@''GNULIB_MBSTOK_R''@|$(GNULIB_MBSTOK_R)|g' \ + -e 's|@''GNULIB_MEMCHR''@|$(GNULIB_MEMCHR)|g' \ -e 's|@''GNULIB_MEMMEM''@|$(GNULIB_MEMMEM)|g' \ -e 's|@''GNULIB_MEMPCPY''@|$(GNULIB_MEMPCPY)|g' \ -e 's|@''GNULIB_MEMRCHR''@|$(GNULIB_MEMRCHR)|g' \ @@ -1119,6 +1141,7 @@ string.h: string.in.h -e 's|@''HAVE_DECL_STRERROR''@|$(HAVE_DECL_STRERROR)|g' \ -e 's|@''HAVE_DECL_STRSIGNAL''@|$(HAVE_DECL_STRSIGNAL)|g' \ -e 's|@''HAVE_STRVERSCMP''@|$(HAVE_STRVERSCMP)|g' \ + -e 's|@''REPLACE_MEMCHR''@|$(REPLACE_MEMCHR)|g' \ -e 's|@''REPLACE_MEMMEM''@|$(REPLACE_MEMMEM)|g' \ -e 's|@''REPLACE_STRCASESTR''@|$(REPLACE_STRCASESTR)|g' \ -e 's|@''REPLACE_STRDUP''@|$(REPLACE_STRDUP)|g' \ diff --git a/libmissing/errno.in.h b/libmissing/errno.in.h index 657f68e..a9b81d5 100644 --- a/libmissing/errno.in.h +++ b/libmissing/errno.in.h @@ -1,6 +1,6 @@ /* A POSIX-like <errno.h>. - Copyright (C) 2008 Free Software Foundation, Inc. + Copyright (C) 2008-2009 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -145,6 +145,11 @@ # define GNULIB_defined_ENOTSUP 1 # endif +# ifndef ESTALE +# define ESTALE 2009 +# define GNULIB_defined_ESTALE 1 +# endif + # ifndef ECANCELED # define ECANCELED 2008 # define GNULIB_defined_ECANCELED 1 diff --git a/libmissing/getpagesize.c b/libmissing/getpagesize.c new file mode 100644 index 0000000..82238df --- /dev/null +++ b/libmissing/getpagesize.c @@ -0,0 +1,39 @@ +/* getpagesize emulation for systems where it cannot be done in a C macro. + + Copyright (C) 2007 Free Software Foundation, Inc. + + This program 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 3 of the License, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible and Martin Lambers. */ + +#include <config.h> + +/* Specification. */ +#include <unistd.h> + +/* This implementation is only for native Win32 systems. */ +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + +# define WIN32_LEAN_AND_MEAN +# include <windows.h> + +int +getpagesize (void) +{ + SYSTEM_INFO system_info; + GetSystemInfo (&system_info); + return system_info.dwPageSize; +} + +#endif diff --git a/libmissing/m4/errno_h.m4 b/libmissing/m4/errno_h.m4 index 16188d9..4ce1ccb 100644 --- a/libmissing/m4/errno_h.m4 +++ b/libmissing/m4/errno_h.m4 @@ -1,4 +1,4 @@ -# errno_h.m4 serial 5 +# errno_h.m4 serial 6 dnl Copyright (C) 2004, 2006, 2008, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -34,6 +34,9 @@ booboo #if !defined ENOTSUP booboo #endif +#if !defined ESTALE +booboo +#endif #if !defined ECANCELED booboo #endif diff --git a/libmissing/m4/gnulib-comp.m4 b/libmissing/m4/gnulib-comp.m4 index c97f596..28c184e 100644 --- a/libmissing/m4/gnulib-comp.m4 +++ b/libmissing/m4/gnulib-comp.m4 @@ -88,6 +88,8 @@ AC_SUBST([LTALLOCA]) gl_STDIO_MODULE_INDICATOR([getdelim]) gl_FUNC_GETLINE gl_STDIO_MODULE_INDICATOR([getline]) + gl_FUNC_GETPAGESIZE + gl_UNISTD_MODULE_INDICATOR([getpagesize]) gl_FUNC_GETPASS AC_REQUIRE([gl_HEADER_SYS_SOCKET]) if test "$ac_cv_header_winsock2_h" = yes; then @@ -128,6 +130,8 @@ AC_SUBST([LTALLOCA]) gl_WCHAR_MODULE_INDICATOR([mbrtowc]) gl_FUNC_MBSINIT gl_WCHAR_MODULE_INDICATOR([mbsinit]) + gl_FUNC_MEMCHR + gl_STRING_MODULE_INDICATOR([memchr]) gl_FUNC_MEMMEM gl_FUNC_MEMMEM_SIMPLE gl_STRING_MODULE_INDICATOR([memmem]) @@ -261,8 +265,6 @@ AC_SUBST([LTALLOCA]) gt_LOCALE_FR gt_LOCALE_FR_UTF8 gl_FUNC_UNGETC_WORKS - gl_FUNC_GETPAGESIZE - gl_UNISTD_MODULE_INDICATOR([getpagesize]) gt_LOCALE_FR gt_LOCALE_FR_UTF8 gt_LOCALE_JA @@ -408,6 +410,7 @@ AC_DEFUN([gl_FILE_LIST], [ lib/getaddrinfo.c lib/getdelim.c lib/getline.c + lib/getpagesize.c lib/getpass.c lib/getpass.h lib/getsockname.c @@ -433,6 +436,8 @@ AC_DEFUN([gl_FILE_LIST], [ lib/malloc.c lib/mbrtowc.c lib/mbsinit.c + lib/memchr.c + lib/memchr.valgrind lib/memmem.c lib/minmax.h lib/mktime.c @@ -548,6 +553,7 @@ AC_DEFUN([gl_FILE_LIST], [ m4/mbrtowc.m4 m4/mbsinit.m4 m4/mbstate_t.m4 + m4/memchr.m4 m4/memmem.m4 m4/minmax.m4 m4/mktime.m4 @@ -642,6 +648,7 @@ AC_DEFUN([gl_FILE_LIST], [ tests/test-mbrtowc4.sh tests/test-mbsinit.c tests/test-mbsinit.sh + tests/test-memchr.c tests/test-memmem.c tests/test-netdb.c tests/test-netinet_in.c @@ -679,7 +686,6 @@ AC_DEFUN([gl_FILE_LIST], [ tests/test-wcrtomb.sh tests/test-wctype.c tests/zerosize-ptr.h - tests=lib/getpagesize.c tests=lib/glthread/yield.h tests=lib/sockets.c tests=lib/sockets.h diff --git a/libmissing/m4/lib-link.m4 b/libmissing/m4/lib-link.m4 index 2144203..2f8b7ff 100644 --- a/libmissing/m4/lib-link.m4 +++ b/libmissing/m4/lib-link.m4 @@ -1,4 +1,4 @@ -# lib-link.m4 serial 19 (gettext-0.18) +# lib-link.m4 serial 20 (gettext-0.18) dnl Copyright (C) 2001-2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -82,7 +82,7 @@ AC_DEFUN([AC_LIB_HAVE_LINKFLAGS], ]) if test "$ac_cv_lib[]Name" = yes; then HAVE_LIB[]NAME=yes - AC_DEFINE([HAVE_LIB]NAME, 1, [Define if you have the lib[]$1 library.]) + AC_DEFINE([HAVE_LIB]NAME, 1, [Define if you have the lib][$1 library.]) AC_MSG_CHECKING([how to link with lib[]$1]) AC_MSG_RESULT([$LIB[]NAME]) else @@ -210,6 +210,9 @@ AC_DEFUN([AC_LIB_LINKFLAGS_BODY], LTLIB[]NAME= INC[]NAME= LIB[]NAME[]_PREFIX= + dnl HAVE_LIB${NAME} is an indicator that LIB${NAME}, LTLIB${NAME} have been + dnl computed. So it has to be reset here. + HAVE_LIB[]NAME= rpathdirs= ltrpathdirs= names_already_handled= diff --git a/libmissing/m4/mbrtowc.m4 b/libmissing/m4/mbrtowc.m4 index 11d7d23..2fddcc8 100644 --- a/libmissing/m4/mbrtowc.m4 +++ b/libmissing/m4/mbrtowc.m4 @@ -1,4 +1,4 @@ -# mbrtowc.m4 serial 15 +# mbrtowc.m4 serial 16 dnl Copyright (C) 2001-2002, 2004-2005, 2008, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -156,6 +156,7 @@ changequote([,])dnl if test $LOCALE_ZH_CN != none; then AC_TRY_RUN([ #include <locale.h> +#include <stdlib.h> #include <string.h> #include <wchar.h> int main () diff --git a/libmissing/m4/memchr.m4 b/libmissing/m4/memchr.m4 new file mode 100644 index 0000000..1194bac --- /dev/null +++ b/libmissing/m4/memchr.m4 @@ -0,0 +1,86 @@ +# memchr.m4 serial 7 +dnl Copyright (C) 2002, 2003, 2004, 2009 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN_ONCE([gl_FUNC_MEMCHR], +[ + dnl Check for prerequisites for memory fence checks. + gl_FUNC_MMAP_ANON + AC_CHECK_HEADERS_ONCE([sys/mman.h]) + AC_CHECK_FUNCS_ONCE([mprotect]) + + dnl These days, we assume memchr is present. But just in case... + AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) + AC_REPLACE_FUNCS([memchr]) + if test $ac_cv_func_memchr = no; then + gl_PREREQ_MEMCHR + REPLACE_MEMCHR=1 + fi + + if test $ac_cv_func_memchr = yes; then + # Detect platform-specific bugs in some versions of glibc: + # memchr should not dereference anything with length 0 + # http://bugzilla.redhat.com/499689 + # memchr should not dereference overestimated length after a match + # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737 + # http://sourceware.org/bugzilla/show_bug.cgi?id=10162 + # Assume that memchr works on platforms that lack mprotect. + AC_CACHE_CHECK([whether memchr works], [gl_cv_func_memchr_works], + [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ +#include <string.h> +#if HAVE_SYS_MMAN_H +# include <fcntl.h> +# include <unistd.h> +# include <sys/types.h> +# include <sys/mman.h> +# ifndef MAP_FILE +# define MAP_FILE 0 +# endif +#endif +]], [[ + char *fence = NULL; +#if HAVE_SYS_MMAN_H && HAVE_MPROTECT +# if HAVE_MAP_ANONYMOUS + const int flags = MAP_ANONYMOUS | MAP_PRIVATE; + const int fd = -1; +# else /* !HAVE_MAP_ANONYMOUS */ + const int flags = MAP_FILE | MAP_PRIVATE; + int fd = open ("/dev/zero", O_RDONLY, 0666); + if (fd >= 0) +# endif + { + int pagesize = getpagesize (); + char *two_pages = + (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE, + flags, fd, 0); + if (two_pages != (char *)(-1) + && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0) + fence = two_pages + pagesize; + } +#endif + if (fence) + { + if (memchr (fence, 0, 0)) + return 1; + strcpy (fence - 9, "12345678"); + if (memchr (fence - 9, 0, 79) != fence - 1) + return 2; + } + return 0; +]])], [gl_cv_func_memchr_works=yes], [gl_cv_func_memchr_works=no], + [dnl Be pessimistic for now. + gl_cv_func_memchr_works="guessing no"])]) + if test "$gl_cv_func_memchr_works" != yes; then + gl_PREREQ_MEMCHR + REPLACE_MEMCHR=1 + AC_LIBOBJ([memchr]) + fi + fi +]) + +# Prerequisites of lib/memchr.c. +AC_DEFUN([gl_PREREQ_MEMCHR], [ + AC_CHECK_HEADERS([bp-sym.h]) +]) diff --git a/libmissing/m4/stdio_h.m4 b/libmissing/m4/stdio_h.m4 index 846b65d..fcbe68f 100644 --- a/libmissing/m4/stdio_h.m4 +++ b/libmissing/m4/stdio_h.m4 @@ -1,4 +1,4 @@ -# stdio_h.m4 serial 15 +# stdio_h.m4 serial 16 dnl Copyright (C) 2007-2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -65,6 +65,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], GNULIB_FTELL=0; AC_SUBST([GNULIB_FTELL]) GNULIB_FTELLO=0; AC_SUBST([GNULIB_FTELLO]) GNULIB_FFLUSH=0; AC_SUBST([GNULIB_FFLUSH]) + GNULIB_FPURGE=0; AC_SUBST([GNULIB_FPURGE]) GNULIB_FCLOSE=0; AC_SUBST([GNULIB_FCLOSE]) GNULIB_FPUTC=0; AC_SUBST([GNULIB_FPUTC]) GNULIB_PUTC=0; AC_SUBST([GNULIB_PUTC]) @@ -105,6 +106,8 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], REPLACE_FTELLO=0; AC_SUBST([REPLACE_FTELLO]) REPLACE_FTELL=0; AC_SUBST([REPLACE_FTELL]) REPLACE_FFLUSH=0; AC_SUBST([REPLACE_FFLUSH]) + REPLACE_FPURGE=0; AC_SUBST([REPLACE_FPURGE]) + HAVE_DECL_FPURGE=1; AC_SUBST([HAVE_DECL_FPURGE]) REPLACE_FCLOSE=0; AC_SUBST([REPLACE_FCLOSE]) HAVE_DECL_GETDELIM=1; AC_SUBST([HAVE_DECL_GETDELIM]) HAVE_DECL_GETLINE=1; AC_SUBST([HAVE_DECL_GETLINE]) diff --git a/libmissing/m4/strcasestr.m4 b/libmissing/m4/strcasestr.m4 index e1e5390..62991b1 100644 --- a/libmissing/m4/strcasestr.m4 +++ b/libmissing/m4/strcasestr.m4 @@ -1,19 +1,28 @@ -# strcasestr.m4 serial 12 -dnl Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc. +# strcasestr.m4 serial 13 +dnl Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. -dnl Check that strcasestr is present. +dnl Check that strcasestr is present and works. AC_DEFUN([gl_FUNC_STRCASESTR_SIMPLE], [ + AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) + dnl Persuade glibc <string.h> to declare strcasestr(). AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) - AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) - AC_REPLACE_FUNCS([strcasestr]) + AC_REQUIRE([gl_FUNC_MEMCHR]) + AC_CHECK_FUNCS([strcasestr]) if test $ac_cv_func_strcasestr = no; then HAVE_STRCASESTR=0 + else + if test "$gl_cv_func_memchr_works" != yes; then + REPLACE_STRCASESTR=1 + fi + fi + if test $HAVE_STRCASESTR = 0 || test $REPLACE_STRCASESTR = 1; then + AC_LIBOBJ([strcasestr]) gl_PREREQ_STRCASESTR fi ]) # gl_FUNC_STRCASESTR_SIMPLE @@ -22,7 +31,7 @@ dnl Additionally, check that strcasestr is efficient. AC_DEFUN([gl_FUNC_STRCASESTR], [ AC_REQUIRE([gl_FUNC_STRCASESTR_SIMPLE]) - if test $ac_cv_func_strcasestr = yes; then + if test $HAVE_STRCASESTR = 1 && test $REPLACE_STRCASESTR = 0; then AC_CACHE_CHECK([whether strcasestr works in linear time], [gl_cv_func_strcasestr_linear], [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ @@ -75,6 +84,7 @@ AC_DEFUN([gl_FUNC_STRCASESTR], if test "$gl_cv_func_strcasestr_linear" != yes; then REPLACE_STRCASESTR=1 AC_LIBOBJ([strcasestr]) + gl_PREREQ_STRCASESTR fi fi ]) # gl_FUNC_STRCASESTR diff --git a/libmissing/m4/string_h.m4 b/libmissing/m4/string_h.m4 index 2d5553c..11f09c8 100644 --- a/libmissing/m4/string_h.m4 +++ b/libmissing/m4/string_h.m4 @@ -1,11 +1,11 @@ # Configure a GNU-like replacement for <string.h>. -# Copyright (C) 2007, 2008 Free Software Foundation, Inc. +# Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. -# serial 6 +# serial 7 # Written by Paul Eggert. @@ -32,6 +32,7 @@ AC_DEFUN([gl_STRING_MODULE_INDICATOR], AC_DEFUN([gl_HEADER_STRING_H_DEFAULTS], [ + GNULIB_MEMCHR=0; AC_SUBST([GNULIB_MEMCHR]) GNULIB_MEMMEM=0; AC_SUBST([GNULIB_MEMMEM]) GNULIB_MEMPCPY=0; AC_SUBST([GNULIB_MEMPCPY]) GNULIB_MEMRCHR=0; AC_SUBST([GNULIB_MEMRCHR]) @@ -83,6 +84,7 @@ AC_DEFUN([gl_HEADER_STRING_H_DEFAULTS], HAVE_DECL_STRERROR=1; AC_SUBST([HAVE_DECL_STRERROR]) HAVE_DECL_STRSIGNAL=1; AC_SUBST([HAVE_DECL_STRSIGNAL]) HAVE_STRVERSCMP=1; AC_SUBST([HAVE_STRVERSCMP]) + REPLACE_MEMCHR=0; AC_SUBST([REPLACE_MEMCHR]) REPLACE_MEMMEM=0; AC_SUBST([REPLACE_MEMMEM]) REPLACE_STRDUP=0; AC_SUBST([REPLACE_STRDUP]) REPLACE_STRSTR=0; AC_SUBST([REPLACE_STRSTR]) diff --git a/libmissing/memchr.c b/libmissing/memchr.c new file mode 100644 index 0000000..3ea1d5b --- /dev/null +++ b/libmissing/memchr.c @@ -0,0 +1,172 @@ +/* Copyright (C) 1991, 1993, 1996, 1997, 1999, 2000, 2003, 2004, 2006, 2008 + Free Software Foundation, Inc. + + Based on strlen implementation by Torbjorn Granlund ([email protected]), + with help from Dan Sahlin ([email protected]) and + commentary by Jim Blandy ([email protected]); + adaptation to memchr suggested by Dick Karpinski ([email protected]), + and implemented by Roland McGrath ([email protected]). + +NOTE: The canonical source of this file is maintained with the GNU C Library. +Bugs can be reported to [email protected]. + +This program 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 3 of the License, or 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef _LIBC +# include <config.h> +#endif + +#include <string.h> + +#include <stddef.h> + +#if defined _LIBC +# include <memcopy.h> +#else +# define reg_char char +#endif + +#include <limits.h> + +#if HAVE_BP_SYM_H || defined _LIBC +# include <bp-sym.h> +#else +# define BP_SYM(sym) sym +#endif + +#undef __memchr +#ifdef _LIBC +# undef memchr +#endif + +#ifndef weak_alias +# define __memchr memchr +#endif + +/* Search no more than N bytes of S for C. */ +void * +__memchr (void const *s, int c_in, size_t n) +{ + /* On 32-bit hardware, choosing longword to be a 32-bit unsigned + long instead of a 64-bit uintmax_t tends to give better + performance. On 64-bit hardware, unsigned long is generally 64 + bits already. Change this typedef to experiment with + performance. */ + typedef unsigned long int longword; + + const unsigned char *char_ptr; + const longword *longword_ptr; + longword repeated_one; + longword repeated_c; + unsigned reg_char c; + + c = (unsigned char) c_in; + + /* Handle the first few bytes by reading one byte at a time. + Do this until CHAR_PTR is aligned on a longword boundary. */ + for (char_ptr = (const unsigned char *) s; + n > 0 && (size_t) char_ptr % sizeof (longword) != 0; + --n, ++char_ptr) + if (*char_ptr == c) + return (void *) char_ptr; + + longword_ptr = (const longword *) char_ptr; + + /* All these elucidatory comments refer to 4-byte longwords, + but the theory applies equally well to any size longwords. */ + + /* Compute auxiliary longword values: + repeated_one is a value which has a 1 in every byte. + repeated_c has c in every byte. */ + repeated_one = 0x01010101; + repeated_c = c | (c << 8); + repeated_c |= repeated_c << 16; + if (0xffffffffU < (longword) -1) + { + repeated_one |= repeated_one << 31 << 1; + repeated_c |= repeated_c << 31 << 1; + if (8 < sizeof (longword)) + { + size_t i; + + for (i = 64; i < sizeof (longword) * 8; i *= 2) + { + repeated_one |= repeated_one << i; + repeated_c |= repeated_c << i; + } + } + } + + /* Instead of the traditional loop which tests each byte, we will test a + longword at a time. The tricky part is testing if *any of the four* + bytes in the longword in question are equal to c. We first use an xor + with repeated_c. This reduces the task to testing whether *any of the + four* bytes in longword1 is zero. + + We compute tmp = + ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7). + That is, we perform the following operations: + 1. Subtract repeated_one. + 2. & ~longword1. + 3. & a mask consisting of 0x80 in every byte. + Consider what happens in each byte: + - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff, + and step 3 transforms it into 0x80. A carry can also be propagated + to more significant bytes. + - If a byte of longword1 is nonzero, let its lowest 1 bit be at + position k (0 <= k <= 7); so the lowest k bits are 0. After step 1, + the byte ends in a single bit of value 0 and k bits of value 1. + After step 2, the result is just k bits of value 1: 2^k - 1. After + step 3, the result is 0. And no carry is produced. + So, if longword1 has only non-zero bytes, tmp is zero. + Whereas if longword1 has a zero byte, call j the position of the least + significant zero byte. Then the result has a zero at positions 0, ..., + j-1 and a 0x80 at position j. We cannot predict the result at the more + significant bytes (positions j+1..3), but it does not matter since we + already have a non-zero bit at position 8*j+7. + + So, the test whether any byte in longword1 is zero is equivalent to + testing whether tmp is nonzero. */ + + while (n >= sizeof (longword)) + { + longword longword1 = *longword_ptr ^ repeated_c; + + if ((((longword1 - repeated_one) & ~longword1) + & (repeated_one << 7)) != 0) + break; + longword_ptr++; + n -= sizeof (longword); + } + + char_ptr = (const unsigned char *) longword_ptr; + + /* At this point, we know that either n < sizeof (longword), or one of the + sizeof (longword) bytes starting at char_ptr is == c. On little-endian + machines, we could determine the first such byte without any further + memory accesses, just by looking at the tmp result from the last loop + iteration. But this does not work on big-endian machines. Choose code + that works in both cases. */ + + for (; n > 0; --n, ++char_ptr) + { + if (*char_ptr == c) + return (void *) char_ptr; + } + + return NULL; +} +#ifdef weak_alias +weak_alias (__memchr, BP_SYM (memchr)) +#endif diff --git a/libmissing/memchr.valgrind b/libmissing/memchr.valgrind new file mode 100644 index 0000000..60f247e --- /dev/null +++ b/libmissing/memchr.valgrind @@ -0,0 +1,14 @@ +# Suppress a valgrind message about use of uninitialized memory in memchr(). +# POSIX states that when the character is found, memchr must not read extra +# bytes in an overestimated length (for example, where memchr is used to +# implement strnlen). However, we use a safe word read to provide a speedup. +{ + memchr-value4 + Memcheck:Value4 + fun:rpl_memchr +} +{ + memchr-value8 + Memcheck:Value8 + fun:rpl_memchr +} diff --git a/libmissing/stdio.in.h b/libmissing/stdio.in.h index 9570e50..ae681fc 100644 --- a/libmissing/stdio.in.h +++ b/libmissing/stdio.in.h @@ -1,6 +1,6 @@ /* A GNU-like <stdio.h>. - Copyright (C) 2004, 2007-2008 Free Software Foundation, Inc. + Copyright (C) 2004, 2007-2009 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -407,6 +407,24 @@ extern long rpl_ftell (FILE *fp); fflush (f)) #endif +#if @GNULIB_FPURGE@ +# if @REPLACE_FPURGE@ +# define fpurge rpl_fpurge +# endif +# if @REPLACE_FPURGE@ || !@HAVE_DECL_FPURGE@ + /* Discard all pending buffered I/O data on STREAM. + STREAM must not be wide-character oriented. + Return 0 if successful. Upon error, return -1 and set errno. */ + extern int fpurge (FILE *gl_stream); +# endif +#elif defined GNULIB_POSIXCHECK +# undef fpurge +# define fpurge(f) \ + (GL_LINK_WARNING ("fpurge is not always present - " \ + "use gnulib module fpurge for portability"), \ + fpurge (f)) +#endif + #if @GNULIB_FCLOSE@ # if @REPLACE_FCLOSE@ # define fclose rpl_fclose diff --git a/libmissing/strerror.c b/libmissing/strerror.c index 127a24b..4968f9a 100644 --- a/libmissing/strerror.c +++ b/libmissing/strerror.c @@ -313,7 +313,13 @@ rpl_strerror (int n) break; # endif -# if GNULIB_defined_ +# if GNULIB_defined_ESTALE + case ESTALE: + msg = "Stale NFS file handle"; + break; +# endif + +# if GNULIB_defined_ECANCELED case ECANCELED: msg = "Operation canceled"; break; diff --git a/libmissing/string.in.h b/libmissing/string.in.h index ca029d7..fe11425 100644 --- a/libmissing/string.in.h +++ b/libmissing/string.in.h @@ -1,6 +1,6 @@ /* A GNU-like <string.h>. - Copyright (C) 1995-1996, 2001-2008 Free Software Foundation, Inc. + Copyright (C) 1995-1996, 2001-2009 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -49,6 +49,21 @@ extern "C" { #endif +/* Return the first instance of C within N bytes of S, or NULL. */ +#if @GNULIB_MEMCHR@ +# if @REPLACE_MEMCHR@ +# define memchr rpl_memchr +extern void *memchr (void const *__s, int __c, size_t __n) + __attribute__ ((__pure__)); +# endif +#elif defined GNULIB_POSIXCHECK +# undef memchr +# define memchr(s,c,n) \ + (GL_LINK_WARNING ("memchr has platform-specific bugs - " \ + "use gnulib module memchr for portability" ), \ + memchr (s, c, n)) +#endif + /* Return the first occurrence of NEEDLE in HAYSTACK. */ #if @GNULIB_MEMMEM@ # if @REPLACE_MEMMEM@ diff --git a/libmissing/tests/Makefile.am b/libmissing/tests/Makefile.am index fd92f6b..99745fe 100644 --- a/libmissing/tests/Makefile.am +++ b/libmissing/tests/Makefile.am @@ -152,15 +152,6 @@ EXTRA_DIST += test-getline.c ## end gnulib module getline-tests -## begin gnulib module getpagesize - - -EXTRA_DIST += getpagesize.c - -EXTRA_libtests_a_SOURCES += getpagesize.c - -## end gnulib module getpagesize - ## begin gnulib module gettimeofday-tests TESTS += test-gettimeofday @@ -213,6 +204,14 @@ EXTRA_DIST += test-mbsinit.sh test-mbsinit.c ## end gnulib module mbsinit-tests +## begin gnulib module memchr-tests + +TESTS += test-memchr +check_PROGRAMS += test-memchr +EXTRA_DIST += test-memchr.c zerosize-ptr.h + +## end gnulib module memchr-tests + ## begin gnulib module memmem-tests TESTS += test-memmem diff --git a/libmissing/tests/getpagesize.c b/libmissing/tests/getpagesize.c deleted file mode 100644 index 5498aa9..0000000 --- a/libmissing/tests/getpagesize.c +++ /dev/null @@ -1,39 +0,0 @@ -/* getpagesize emulation for systems where it cannot be done in a C macro. - - Copyright (C) 2007 Free Software Foundation, Inc. - - 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */ - -/* Written by Bruno Haible and Martin Lambers. */ - -#include <config.h> - -/* Specification. */ -#include <unistd.h> - -/* This implementation is only for native Win32 systems. */ -#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ - -# define WIN32_LEAN_AND_MEAN -# include <windows.h> - -int -getpagesize (void) -{ - SYSTEM_INFO system_info; - GetSystemInfo (&system_info); - return system_info.dwPageSize; -} - -#endif diff --git a/libmissing/tests/test-memchr.c b/libmissing/tests/test-memchr.c new file mode 100644 index 0000000..1124f8b --- /dev/null +++ b/libmissing/tests/test-memchr.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2008-2009 Free Software Foundation + * Written by Eric Blake and Bruno Haible + * + * 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include <string.h> + +#include <stdio.h> +#include <stdlib.h> + +#include "zerosize-ptr.h" + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +/* Calculating void * + int is not portable, so this wrapper converts + to char * to make the tests easier to write. */ +#define MEMCHR (char *) memchr + +int +main () +{ + size_t n = 0x100000; + char *input = malloc (n); + ASSERT (input); + + input[0] = 'a'; + input[1] = 'b'; + memset (input + 2, 'c', 1024); + memset (input + 1026, 'd', n - 1028); + input[n - 2] = 'e'; + input[n - 1] = 'a'; + + /* Basic behavior tests. */ + ASSERT (MEMCHR (input, 'a', n) == input); + + ASSERT (MEMCHR (input, 'a', 0) == NULL); + ASSERT (MEMCHR (zerosize_ptr (), 'a', 0) == NULL); + + ASSERT (MEMCHR (input, 'b', n) == input + 1); + ASSERT (MEMCHR (input, 'c', n) == input + 2); + ASSERT (MEMCHR (input, 'd', n) == input + 1026); + + ASSERT (MEMCHR (input + 1, 'a', n - 1) == input + n - 1); + ASSERT (MEMCHR (input + 1, 'e', n - 1) == input + n - 2); + + ASSERT (MEMCHR (input, 'f', n) == NULL); + ASSERT (MEMCHR (input, '\0', n) == NULL); + + /* Check that a very long haystack is handled quickly if the byte is + found near the beginning. */ + { + size_t repeat = 10000; + for (; repeat > 0; repeat--) + { + ASSERT (MEMCHR (input, 'c', n) == input + 2); + } + } + + /* Alignment tests. */ + { + int i, j; + for (i = 0; i < 32; i++) + { + for (j = 0; j < 256; j++) + input[i + j] = j; + for (j = 0; j < 256; j++) + { + ASSERT (MEMCHR (input + i, j, 256) == input + i + j); + } + } + } + + /* Check that memchr() does not read past the first occurrence of the + byte being searched. See the Austin Group's clarification + <http://www.opengroup.org/austin/docs/austin_454.txt>. */ + { + char *page_boundary = (char *) zerosize_ptr (); + + if (page_boundary != NULL) + { + int n; + + for (n = 1; n <= 500; n++) + { + char *mem = page_boundary - n; + memset (mem, 'X', n); + ASSERT (MEMCHR (mem, 'U', n) == NULL); + + { + int i; + + for (i = 0; i < n; i++) + { + mem[i] = 'U'; + ASSERT (MEMCHR (mem, 'U', 4000) == mem + i); + mem[i] = 'X'; + } + } + } + } + } + + free (input); + + return 0; +} diff --git a/libmissing/tests/test-stdio.c b/libmissing/tests/test-stdio.c index a5efa32..dcfe38d 100644 --- a/libmissing/tests/test-stdio.c +++ b/libmissing/tests/test-stdio.c @@ -1,5 +1,5 @@ /* Test of <stdio.h> substitute. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2009 Free Software Foundation, Inc. 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 @@ -20,9 +20,15 @@ #include <stdio.h> +#include "verify.h" + /* Check that the various SEEK_* macros are defined. */ int sk[] = { SEEK_CUR, SEEK_END, SEEK_SET }; +/* Check that NULL can be passed through varargs as a pointer type, + per POSIX 2008. */ +verify (sizeof NULL == sizeof (void *)); + int main () { diff --git a/libmissing/tests/test-stdlib.c b/libmissing/tests/test-stdlib.c index e103d46..ae46ba1 100644 --- a/libmissing/tests/test-stdlib.c +++ b/libmissing/tests/test-stdlib.c @@ -1,5 +1,5 @@ /* Test of <stdlib.h> substitute. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2009 Free Software Foundation, Inc. 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 @@ -20,8 +20,14 @@ #include <stdlib.h> +#include "verify.h" + int exitcode; +/* Check that NULL can be passed through varargs as a pointer type, + per POSIX 2008. */ +verify (sizeof NULL == sizeof (void *)); + int main () { diff --git a/libmissing/tests/test-string.c b/libmissing/tests/test-string.c index 68014f5..fe53cd9 100644 --- a/libmissing/tests/test-string.c +++ b/libmissing/tests/test-string.c @@ -1,5 +1,5 @@ /* Test of <string.h> substitute. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2009 Free Software Foundation, Inc. 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 @@ -20,6 +20,12 @@ #include <string.h> +#include "verify.h" + +/* Check that NULL can be passed through varargs as a pointer type, + per POSIX 2008. */ +verify (sizeof NULL == sizeof (void *)); + int main () { diff --git a/libmissing/tests/test-unistd.c b/libmissing/tests/test-unistd.c index 5fed15c..129367b 100644 --- a/libmissing/tests/test-unistd.c +++ b/libmissing/tests/test-unistd.c @@ -20,6 +20,12 @@ #include <unistd.h> +#include "verify.h" + +/* Check that NULL can be passed through varargs as a pointer type, + per POSIX 2008. */ +verify (sizeof NULL == sizeof (void *)); + /* Check that the various SEEK_* macros are defined. */ int sk[] = { SEEK_CUR, SEEK_END, SEEK_SET }; _______________________________________________ Prelude-cvslog site list [email protected] http://lists.prelude-ids.org/mailman/listinfo/prelude-cvslog