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