Put more information about failed tests into the test return codes.
[gnulib.git] / m4 / strstr.m4
1 # strstr.m4 serial 12
2 dnl Copyright (C) 2008, 2009, 2010 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 strstr works.
8 AC_DEFUN([gl_FUNC_STRSTR_SIMPLE],
9 [
10   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_MEMCHR])
12   if test "$gl_cv_func_memchr_works" != yes; then
13     REPLACE_STRSTR=1
14     AC_LIBOBJ([strstr])
15   else
16     dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
17     AC_CACHE_CHECK([whether strstr works],
18       [gl_cv_func_strstr_works_always],
19       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
20 #include <string.h> /* for strstr */
21 #define P "_EF_BF_BD"
22 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
23 #define NEEDLE P P P P P
24 ]], [[return !!strstr (HAYSTACK, NEEDLE);
25     ]])],
26         [gl_cv_func_strstr_works_always=yes],
27         [gl_cv_func_strstr_works_always=no],
28         [dnl glibc 2.12 and cygwin 1.7.7 have a known bug.  Assume that it
29          dnl works on all other platforms, even if it is not linear.
30          AC_EGREP_CPP([Lucky user],
31            [
32 #ifdef __GNU_LIBRARY__
33  #include <features.h>
34  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
35      && !defined __UCLIBC__
36   Lucky user
37  #endif
38 #elif defined __CYGWIN__
39  #include <cygwin/version.h>
40  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
41   Lucky user
42  #endif
43 #else
44   Lucky user
45 #endif
46            ],
47            [gl_cv_func_strstr_works_always=yes],
48            [gl_cv_func_strstr_works_always="guessing no"])
49         ])
50       ])
51     if test "$gl_cv_func_strstr_works_always" != yes; then
52       REPLACE_STRSTR=1
53       AC_LIBOBJ([strstr])
54     fi
55   fi
56 ]) # gl_FUNC_STRSTR_SIMPLE
57
58 dnl Additionally, check that strstr is efficient.
59 AC_DEFUN([gl_FUNC_STRSTR],
60 [
61   AC_REQUIRE([gl_FUNC_STRSTR_SIMPLE])
62   if test $REPLACE_STRSTR = 0; then
63     AC_CACHE_CHECK([whether strstr works in linear time],
64       [gl_cv_func_strstr_linear],
65       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
66 #include <signal.h> /* for signal */
67 #include <string.h> /* for strstr */
68 #include <stdlib.h> /* for malloc */
69 #include <unistd.h> /* for alarm */
70 static void quit (int sig) { exit (sig + 128); }
71 ]], [[
72     int result = 0;
73     size_t m = 1000000;
74     char *haystack = (char *) malloc (2 * m + 2);
75     char *needle = (char *) malloc (m + 2);
76     /* Failure to compile this test due to missing alarm is okay,
77        since all such platforms (mingw) also have quadratic strstr.  */
78     signal (SIGALRM, quit);
79     alarm (5);
80     /* Check for quadratic performance.  */
81     if (haystack && needle)
82       {
83         memset (haystack, 'A', 2 * m);
84         haystack[2 * m] = 'B';
85         haystack[2 * m + 1] = 0;
86         memset (needle, 'A', m);
87         needle[m] = 'B';
88         needle[m + 1] = 0;
89         if (!strstr (haystack, needle))
90           result |= 1;
91       }
92     return result;
93     ]])],
94         [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
95         [dnl Only glibc > 2.12 and cygwin > 1.7.7 are known to have a
96          dnl bug-free strstr that works in linear time.
97          AC_EGREP_CPP([Lucky user],
98            [
99 #include <features.h>
100 #ifdef __GNU_LIBRARY__
101  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
102      && !defined __UCLIBC__
103   Lucky user
104  #endif
105 #endif
106 #ifdef __CYGWIN__
107  #include <cygwin/version.h>
108  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
109   Lucky user
110  #endif
111 #endif
112            ],
113            [gl_cv_func_strstr_linear=yes],
114            [gl_cv_func_strstr_linear="guessing no"])
115         ])
116       ])
117     if test "$gl_cv_func_strstr_linear" != yes; then
118       REPLACE_STRSTR=1
119     fi
120   fi
121   if test $REPLACE_STRSTR = 1; then
122     AC_LIBOBJ([strstr])
123   fi
124 ]) # gl_FUNC_STRSTR