memmem-simple: Stylistic changes.
[gnulib.git] / m4 / memmem.m4
1 # memmem.m4 serial 22
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       const char *haystack = "AAA";
36       if (memmem (haystack, 3, NULL, 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.9..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)) \
54      || defined __UCLIBC__
55   Lucky user
56  #endif
57 #elif defined __CYGWIN__
58  #include <cygwin/version.h>
59  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
60   Lucky user
61  #endif
62 #else
63   Lucky user
64 #endif
65            ],
66            [gl_cv_func_memmem_works_always=yes],
67            [gl_cv_func_memmem_works_always="guessing no"])
68         ])
69       ])
70     if test "$gl_cv_func_memmem_works_always" != yes; then
71       REPLACE_MEMMEM=1
72       AC_LIBOBJ([memmem])
73     fi
74   fi
75   gl_PREREQ_MEMMEM
76 ]) # gl_FUNC_MEMMEM_SIMPLE
77
78 dnl Additionally, check that memmem has linear performance characteristics
79 AC_DEFUN([gl_FUNC_MEMMEM],
80 [
81   AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
82   if test $HAVE_DECL_MEMMEM = 1 && test $REPLACE_MEMMEM = 0; then
83     AC_CACHE_CHECK([whether memmem works in linear time],
84       [gl_cv_func_memmem_works_fast],
85       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
86 #include <signal.h> /* for signal */
87 #include <string.h> /* for memmem */
88 #include <stdlib.h> /* for malloc */
89 #include <unistd.h> /* for alarm */
90 static void quit (int sig) { exit (sig + 128); }
91 ]], [[
92     int result = 0;
93     size_t m = 1000000;
94     char *haystack = (char *) malloc (2 * m + 1);
95     char *needle = (char *) malloc (m + 1);
96     /* Failure to compile this test due to missing alarm is okay,
97        since all such platforms (mingw) also lack memmem.  */
98     signal (SIGALRM, quit);
99     alarm (5);
100     /* Check for quadratic performance.  */
101     if (haystack && needle)
102       {
103         memset (haystack, 'A', 2 * m);
104         haystack[2 * m] = 'B';
105         memset (needle, 'A', m);
106         needle[m] = 'B';
107         if (!memmem (haystack, 2 * m + 1, needle, m + 1))
108           result |= 1;
109       }
110     return result;
111     ]])],
112         [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no],
113         [dnl Only glibc >= 2.9 and cygwin > 1.7.0 are known to have a
114          dnl memmem that works in linear time.
115          AC_EGREP_CPP([Lucky user],
116            [
117 #include <features.h>
118 #ifdef __GNU_LIBRARY__
119  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)) \
120      && !defined __UCLIBC__
121   Lucky user
122  #endif
123 #endif
124 #ifdef __CYGWIN__
125  #include <cygwin/version.h>
126  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 0)
127   Lucky user
128  #endif
129 #endif
130            ],
131            [gl_cv_func_memmem_works_fast=yes],
132            [gl_cv_func_memmem_works_fast="guessing no"])
133         ])
134       ])
135     if test "$gl_cv_func_memmem_works_fast" != yes; then
136       REPLACE_MEMMEM=1
137       AC_LIBOBJ([memmem])
138     fi
139   fi
140 ]) # gl_FUNC_MEMMEM
141
142 # Prerequisites of lib/memmem.c.
143 AC_DEFUN([gl_PREREQ_MEMMEM], [:])