NEWS.stable: update and finalise
[gnulib.git] / m4 / memmem.m4
1 # memmem.m4 serial 22
2 dnl Copyright (C) 2002-2004, 2007-2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl Check that memmem is present and functional.
8 AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
9 [
10   dnl Persuade glibc <string.h> to declare memmem().
11   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
12
13   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
14   AC_REPLACE_FUNCS([memmem])
15   AC_CHECK_DECLS_ONCE([memmem])
16   if test $ac_cv_have_decl_memmem = no; then
17     HAVE_DECL_MEMMEM=0
18   else
19     dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
20     dnl Also check that we handle empty needles correctly.
21     AC_CACHE_CHECK([whether memmem works],
22       [gl_cv_func_memmem_works_always],
23       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
24 #include <string.h> /* for memmem */
25 #define P "_EF_BF_BD"
26 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
27 #define NEEDLE P P P P P
28 ]], [[
29     int result = 0;
30     if (memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE)))
31       result |= 1;
32     /* Check for empty needle behavior.  */
33     {
34       const char *haystack = "AAA";
35       if (memmem (haystack, 3, NULL, 0) != haystack)
36         result |= 2;
37     }
38     return result;
39     ]])],
40         [gl_cv_func_memmem_works_always=yes],
41         [gl_cv_func_memmem_works_always=no],
42         [dnl glibc 2.9..2.12 and cygwin 1.7.7 have issue #12092 above.
43          dnl Also empty needles work on glibc >= 2.1 and cygwin >= 1.7.0.
44          dnl uClibc is not affected, since it uses different source code.
45          dnl Assume that it works on all other platforms (even if not linear).
46          AC_EGREP_CPP([Lucky user],
47            [
48 #ifdef __GNU_LIBRARY__
49  #include <features.h>
50  #if ((__GLIBC__ == 2 && ((__GLIBC_MINOR > 0 && __GLIBC_MINOR__ < 9) \
51                           || __GLIBC_MINOR__ > 12)) \
52       || (__GLIBC__ > 2)) \
53      || defined __UCLIBC__
54   Lucky user
55  #endif
56 #elif defined __CYGWIN__
57  #include <cygwin/version.h>
58  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
59   Lucky user
60  #endif
61 #else
62   Lucky user
63 #endif
64            ],
65            [gl_cv_func_memmem_works_always=yes],
66            [gl_cv_func_memmem_works_always="guessing no"])
67         ])
68       ])
69     if test "$gl_cv_func_memmem_works_always" != yes; then
70       REPLACE_MEMMEM=1
71       AC_LIBOBJ([memmem])
72     fi
73   fi
74   gl_PREREQ_MEMMEM
75 ]) # gl_FUNC_MEMMEM_SIMPLE
76
77 dnl Additionally, check that memmem has linear performance characteristics
78 AC_DEFUN([gl_FUNC_MEMMEM],
79 [
80   AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
81   if test $HAVE_DECL_MEMMEM = 1 && test $REPLACE_MEMMEM = 0; then
82     AC_CACHE_CHECK([whether memmem works in linear time],
83       [gl_cv_func_memmem_works_fast],
84       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
85 #include <signal.h> /* for signal */
86 #include <string.h> /* for memmem */
87 #include <stdlib.h> /* for malloc */
88 #include <unistd.h> /* for alarm */
89 static void quit (int sig) { exit (sig + 128); }
90 ]], [[
91     int result = 0;
92     size_t m = 1000000;
93     char *haystack = (char *) malloc (2 * m + 1);
94     char *needle = (char *) malloc (m + 1);
95     /* Failure to compile this test due to missing alarm is okay,
96        since all such platforms (mingw) also lack memmem.  */
97     signal (SIGALRM, quit);
98     alarm (5);
99     /* Check for quadratic performance.  */
100     if (haystack && needle)
101       {
102         memset (haystack, 'A', 2 * m);
103         haystack[2 * m] = 'B';
104         memset (needle, 'A', m);
105         needle[m] = 'B';
106         if (!memmem (haystack, 2 * m + 1, needle, m + 1))
107           result |= 1;
108       }
109     return result;
110     ]])],
111         [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no],
112         [dnl Only glibc >= 2.9 and cygwin > 1.7.0 are known to have a
113          dnl memmem that works in linear time.
114          AC_EGREP_CPP([Lucky user],
115            [
116 #include <features.h>
117 #ifdef __GNU_LIBRARY__
118  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)) \
119      && !defined __UCLIBC__
120   Lucky user
121  #endif
122 #endif
123 #ifdef __CYGWIN__
124  #include <cygwin/version.h>
125  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 0)
126   Lucky user
127  #endif
128 #endif
129            ],
130            [gl_cv_func_memmem_works_fast=yes],
131            [gl_cv_func_memmem_works_fast="guessing no"])
132         ])
133       ])
134     if test "$gl_cv_func_memmem_works_fast" != yes; then
135       REPLACE_MEMMEM=1
136       AC_LIBOBJ([memmem])
137     fi
138   fi
139 ]) # gl_FUNC_MEMMEM
140
141 # Prerequisites of lib/memmem.c.
142 AC_DEFUN([gl_PREREQ_MEMMEM], [:])