Update gnulib to current upstream master
[external/binutils.git] / gdb / gnulib / import / m4 / strstr.m4
1 # strstr.m4 serial 19
2 dnl Copyright (C) 2008-2018 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 $HAVE_MEMCHR = 0 || test $REPLACE_MEMCHR = 1; then
13     REPLACE_STRSTR=1
14   else
15     dnl Detect https://sourceware.org/bugzilla/show_bug.cgi?id=12092.
16     AC_CACHE_CHECK([whether strstr works],
17       [gl_cv_func_strstr_works_always],
18       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
19 #include <string.h> /* for strstr */
20 #define P "_EF_BF_BD"
21 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
22 #define NEEDLE P P P P P
23 ]], [[return !!strstr (HAYSTACK, NEEDLE);
24     ]])],
25         [gl_cv_func_strstr_works_always=yes],
26         [gl_cv_func_strstr_works_always=no],
27         [dnl glibc 2.12 and cygwin 1.7.7 have a known bug.  uClibc is not
28          dnl affected, since it uses different source code for strstr than
29          dnl glibc.
30          dnl Assume that it works on all other platforms, even if it is not
31          dnl linear.
32          AC_EGREP_CPP([Lucky user],
33            [
34 #ifdef __GNU_LIBRARY__
35  #include <features.h>
36  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
37      || defined __UCLIBC__
38   Lucky user
39  #endif
40 #elif defined __CYGWIN__
41  #include <cygwin/version.h>
42  #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
43   Lucky user
44  #endif
45 #else
46   Lucky user
47 #endif
48            ],
49            [gl_cv_func_strstr_works_always="guessing yes"],
50            [gl_cv_func_strstr_works_always="guessing no"])
51         ])
52       ])
53     case "$gl_cv_func_strstr_works_always" in
54       *yes) ;;
55       *)
56         REPLACE_STRSTR=1
57         ;;
58     esac
59   fi
60 ]) # gl_FUNC_STRSTR_SIMPLE
61
62 dnl Additionally, check that strstr is efficient.
63 AC_DEFUN([gl_FUNC_STRSTR],
64 [
65   AC_REQUIRE([gl_FUNC_STRSTR_SIMPLE])
66   if test $REPLACE_STRSTR = 0; then
67     AC_CACHE_CHECK([whether strstr works in linear time],
68       [gl_cv_func_strstr_linear],
69       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
70 #ifdef __MVS__
71 /* z/OS does not deliver signals while strstr() is running (thanks to
72    restrictions on its LE runtime), which prevents us from limiting the
73    running time of this test.  */
74 # error "This test does not work properly on z/OS"
75 #endif
76 #include <signal.h> /* for signal */
77 #include <string.h> /* for strstr */
78 #include <stdlib.h> /* for malloc */
79 #include <unistd.h> /* for alarm */
80 static void quit (int sig) { _exit (sig + 128); }
81 ]], [[
82     int result = 0;
83     size_t m = 1000000;
84     char *haystack = (char *) malloc (2 * m + 2);
85     char *needle = (char *) malloc (m + 2);
86     /* Failure to compile this test due to missing alarm is okay,
87        since all such platforms (mingw) also have quadratic strstr.  */
88     signal (SIGALRM, quit);
89     alarm (5);
90     /* Check for quadratic performance.  */
91     if (haystack && needle)
92       {
93         memset (haystack, 'A', 2 * m);
94         haystack[2 * m] = 'B';
95         haystack[2 * m + 1] = 0;
96         memset (needle, 'A', m);
97         needle[m] = 'B';
98         needle[m + 1] = 0;
99         if (!strstr (haystack, needle))
100           result |= 1;
101       }
102     /* Free allocated memory, in case some sanitizer is watching.  */
103     free (haystack);
104     free (needle);
105     return result;
106     ]])],
107         [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
108         [dnl Only glibc > 2.12 on processors without SSE 4.2 instructions and
109          dnl cygwin > 1.7.7 are known to have a bug-free strstr that works in
110          dnl linear time.
111          AC_EGREP_CPP([Lucky user],
112            [
113 #include <features.h>
114 #ifdef __GNU_LIBRARY__
115  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
116      && !(defined __i386__ || defined __x86_64__) \
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_strstr_linear="guessing yes"],
129            [gl_cv_func_strstr_linear="guessing no"])
130         ])
131       ])
132     case "$gl_cv_func_strstr_linear" in
133       *yes) ;;
134       *)
135         REPLACE_STRSTR=1
136         ;;
137     esac
138   fi
139 ]) # gl_FUNC_STRSTR