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