memmem: rearrange memmem and expand memmem-simple modules
[gnulib.git] / m4 / memmem.m4
1 # memmem.m4 serial 21
2 dnl Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010 Free Software
3 dnl Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7
8 dnl Check that memmem is present and functional.
9 AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
10 [
11   dnl Persuade glibc <string.h> to declare memmem().
12   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
13
14   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
15   AC_REPLACE_FUNCS([memmem])
16   AC_CHECK_DECLS_ONCE([memmem])
17   if test $ac_cv_have_decl_memmem = no; then
18     HAVE_DECL_MEMMEM=0
19   else
20     dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
21     dnl Also check that we handle empty needles correctly.
22     AC_CACHE_CHECK([whether memmem works],
23       [gl_cv_func_memmem_works_always],
24       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
25 #include <string.h> /* for memmem */
26 #define P "_EF_BF_BD"
27 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
28 #define NEEDLE P P P P P
29 ]], [[
30     int result = 0;
31     if (memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE)))
32       result |= 1;
33     /* Check for empty needle behavior.  */
34     {
35       char* haystack="AAA";
36       if (memmem (haystack, 3, 0, 0) != haystack)
37         result |= 2;
38     }
39     return result;
40     ]])],
41         [gl_cv_func_memmem_works_always=yes],
42         [gl_cv_func_memmem_works_always=no],
43         [dnl glibc 2.12 and cygwin 1.7.7 have issue #12092 above.
44          dnl Also empty needles work on glibc >= 2.1 and cygwin >= 1.7.0
45          dnl uClibc is not affected, since it uses different source code.
46          dnl Assume that it works on all other platforms (even if not linear).
47          AC_EGREP_CPP([Lucky user],
48            [
49 #ifdef __GNU_LIBRARY__
50  #include <features.h>
51  #if ((__GLIBC__ == 2 && ((__GLIBC_MINOR > 0 && __GLIBC_MINOR__ < 9) \
52                           || __GLIBC_MINOR__ > 12)) \
53      || (__GLIBC__ > 2)) || 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], [:])