Update cross-compiling guess now that glibc 2.9 will have fast implementations.
[gnulib.git] / m4 / strstr.m4
1 # strstr.m4 serial 3
2 dnl Copyright (C) 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 strstr is efficient.
8 AC_DEFUN([gl_FUNC_STRSTR],
9 [
10   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
11   AC_CACHE_CHECK([whether strstr works in linear time],
12     [gl_cv_func_strstr_linear],
13     [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
14 #include <string.h> /* for memmem */
15 #include <stdlib.h> /* for malloc */
16 #include <unistd.h> /* for alarm */
17 ]], [[size_t m = 1000000;
18     char *haystack = (char *) malloc (2 * m + 2);
19     char *needle = (char *) malloc (m + 2);
20     void *result = 0;
21     /* Failure to compile this test due to missing alarm is okay,
22        since all such platforms (mingw) also have quadratic strstr.  */
23     alarm (5);
24     /* Check for quadratic performance.  */
25     if (haystack && needle)
26       {
27         memset (haystack, 'A', 2 * m);
28         haystack[2 * m] = 'B';
29         haystack[2 * m + 1] = 0;
30         memset (needle, 'A', m);
31         needle[m] = 'B';
32         needle[m + 1] = 0;
33         result = strstr (haystack, needle);
34       }
35     return !result;]])],
36       [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
37       [dnl Only glibc >= 2.9 is known to have an strstr that works in
38        dnl linear time.
39        AC_EGREP_CPP([Lucky GNU user],
40          [
41 #include <features.h>
42 #ifdef __GNU_LIBRARY__
43  #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)
44   Lucky GNU user
45  #endif
46 #endif
47          ],
48          [gl_cv_func_strstr_linear=yes],
49          [gl_cv_func_strstr_linear="guessing no"])
50       ])
51     ])
52   if test "$gl_cv_func_strstr_linear" != yes; then
53     REPLACE_STRSTR=1
54     AC_LIBOBJ([strstr])
55   fi
56 ]) # gl_FUNC_STRSTR