Port to uClibc.
[gnulib.git] / m4 / strcasestr.m4
1 # strcasestr.m4 serial 17
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.  Assume that it
36            dnl works on all other platforms, even if it is not linear.
37            AC_EGREP_CPP([Lucky user],
38              [
39 #ifdef __GNU_LIBRARY__
40  #include <features.h>
41  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
42      && !defined __UCLIBC__
43   Lucky user
44  #endif
45 #elif defined __CYGWIN__
46  #include <cygwin/version.h>
47  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
48   Lucky user
49  #endif
50 #else
51   Lucky user
52 #endif
53              ],
54              [gl_cv_func_strcasestr_works_always=yes],
55              [gl_cv_func_strcasestr_works_always="guessing no"])
56           ])
57         ])
58       if test "$gl_cv_func_strcasestr_works_always" != yes; then
59         REPLACE_STRCASESTR=1
60         AC_LIBOBJ([strcasestr])
61       fi
62     fi
63   fi
64   if test $HAVE_STRCASESTR = 0 || test $REPLACE_STRCASESTR = 1; then
65     AC_LIBOBJ([strcasestr])
66     gl_PREREQ_STRCASESTR
67   fi
68 ]) # gl_FUNC_STRCASESTR_SIMPLE
69
70 dnl Additionally, check that strcasestr is efficient.
71 AC_DEFUN([gl_FUNC_STRCASESTR],
72 [
73   AC_REQUIRE([gl_FUNC_STRCASESTR_SIMPLE])
74   if test $HAVE_STRCASESTR = 1 && test $REPLACE_STRCASESTR = 0; then
75     AC_CACHE_CHECK([whether strcasestr works in linear time],
76       [gl_cv_func_strcasestr_linear],
77       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
78 #include <signal.h> /* for signal */
79 #include <string.h> /* for strcasestr */
80 #include <stdlib.h> /* for malloc */
81 #include <unistd.h> /* for alarm */
82 static void quit (int sig) { exit (sig + 128); }
83 ]], [[size_t m = 1000000;
84     char *haystack = (char *) malloc (2 * m + 2);
85     char *needle = (char *) malloc (m + 2);
86     void *result = 0;
87     /* Failure to compile this test due to missing alarm is okay,
88        since all such platforms (mingw) also lack strcasestr.  */
89     signal (SIGALRM, quit);
90     alarm (5);
91     /* Check for quadratic performance.  */
92     if (haystack && needle)
93       {
94         memset (haystack, 'A', 2 * m);
95         haystack[2 * m] = 'B';
96         haystack[2 * m + 1] = 0;
97         memset (needle, 'A', m);
98         needle[m] = 'B';
99         needle[m + 1] = 0;
100         result = strcasestr (haystack, needle);
101       }
102     return !result;]])],
103         [gl_cv_func_strcasestr_linear=yes], [gl_cv_func_strcasestr_linear=no],
104         [dnl Only glibc > 2.12 and cygwin > 1.7.7 are known to have a
105          dnl strcasestr that works in linear time.
106          AC_EGREP_CPP([Lucky user],
107            [
108 #include <features.h>
109 #ifdef __GNU_LIBRARY__
110  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
111      && !defined __UCLIBC__
112   Lucky user
113  #endif
114 #endif
115 #ifdef __CYGWIN__
116  #include <cygwin/version.h>
117  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
118   Lucky user
119  #endif
120 #endif
121            ],
122            [gl_cv_func_strcasestr_linear=yes],
123            [gl_cv_func_strcasestr_linear="guessing no"])
124         ])
125       ])
126     if test "$gl_cv_func_strcasestr_linear" != yes; then
127       REPLACE_STRCASESTR=1
128       AC_LIBOBJ([strcasestr])
129       gl_PREREQ_STRCASESTR
130     fi
131   fi
132 ]) # gl_FUNC_STRCASESTR
133
134 # Prerequisites of lib/strcasestr.c.
135 AC_DEFUN([gl_PREREQ_STRCASESTR], [
136   :
137 ])