Update cross-compiling guess now that glibc 2.9 will have fast implementations.
[gnulib.git] / m4 / strcasestr.m4
1 # strcasestr.m4 serial 10
2 dnl Copyright (C) 2005, 2007, 2008 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.
8 AC_DEFUN([gl_FUNC_STRCASESTR_SIMPLE],
9 [
10   dnl Persuade glibc <string.h> to declare strcasestr().
11   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
12
13   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
14   AC_REPLACE_FUNCS([strcasestr])
15   if test $ac_cv_func_strcasestr = no; then
16     HAVE_STRCASESTR=0
17     gl_PREREQ_STRCASESTR
18   fi
19 ]) # gl_FUNC_STRCASESTR_SIMPLE
20
21 dnl Additionally, check that strcasestr is efficient.
22 AC_DEFUN([gl_FUNC_STRCASESTR],
23 [
24   AC_REQUIRE([gl_FUNC_STRCASESTR_SIMPLE])
25   if test $ac_cv_func_strcasestr = yes; then
26     AC_CACHE_CHECK([whether strcasestr works in linear time],
27       [gl_cv_func_strcasestr_linear],
28       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
29 #include <string.h> /* for memmem */
30 #include <stdlib.h> /* for malloc */
31 #include <unistd.h> /* for alarm */
32 ]], [[size_t m = 1000000;
33     char *haystack = (char *) malloc (2 * m + 2);
34     char *needle = (char *) malloc (m + 2);
35     void *result = 0;
36     /* Failure to compile this test due to missing alarm is okay,
37        since all such platforms (mingw) also lack strcasestr.  */
38     alarm (5);
39     /* Check for quadratic performance.  */
40     if (haystack && needle)
41       {
42         memset (haystack, 'A', 2 * m);
43         haystack[2 * m] = 'B';
44         haystack[2 * m + 1] = 0;
45         memset (needle, 'A', m);
46         needle[m] = 'B';
47         needle[m + 1] = 0;
48         result = strcasestr (haystack, needle);
49       }
50     return !result;]])],
51         [gl_cv_func_strcasestr_linear=yes], [gl_cv_func_strcasestr_linear=no],
52         [dnl Only glibc >= 2.9 is known to have an strcasestr that works in
53          dnl linear time.
54          AC_EGREP_CPP([Lucky GNU user],
55            [
56 #include <features.h>
57 #ifdef __GNU_LIBRARY__
58  #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)
59   Lucky GNU user
60  #endif
61 #endif
62            ],
63            [gl_cv_func_strcasestr_linear=yes],
64            [gl_cv_func_strcasestr_linear="guessing no"])
65         ])
66       ])
67     if test "$gl_cv_func_strcasestr_linear" != yes; then
68       REPLACE_STRCASESTR=1
69       AC_LIBOBJ([strcasestr])
70     fi
71   fi
72 ]) # gl_FUNC_STRCASESTR
73
74 # Prerequisites of lib/strcasestr.c.
75 AC_DEFUN([gl_PREREQ_STRCASESTR], [
76   :
77 ])