2 dnl Copyright (C) 2008-2022 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.
7 dnl Check that strstr works.
8 AC_DEFUN([gl_FUNC_STRSTR_SIMPLE],
10 AC_REQUIRE([gl_STRING_H_DEFAULTS])
11 AC_REQUIRE([gl_FUNC_MEMCHR])
12 if test $REPLACE_MEMCHR = 1; then
15 dnl Detect https://sourceware.org/bugzilla/show_bug.cgi?id=12092
16 dnl and https://sourceware.org/bugzilla/show_bug.cgi?id=23637.
17 AC_CACHE_CHECK([whether strstr works],
18 [gl_cv_func_strstr_works_always],
21 #include <string.h> /* for __GNU_LIBRARY__, strstr */
22 #ifdef __GNU_LIBRARY__
24 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 28
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
32 [[return !!strstr (HAYSTACK, NEEDLE);
34 [gl_cv_func_strstr_works_always=yes],
35 [gl_cv_func_strstr_works_always=no],
36 [dnl glibc 2.12 and cygwin 1.7.7 have a known bug. uClibc is not
37 dnl affected, since it uses different source code for strstr than
39 dnl Assume that it works on all other platforms, even if it is not
41 AC_EGREP_CPP([Lucky user],
43 #include <string.h> /* for __GNU_LIBRARY__ */
44 #ifdef __GNU_LIBRARY__
46 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
50 #elif defined __CYGWIN__
51 #include <cygwin/version.h>
52 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
59 [gl_cv_func_strstr_works_always="guessing yes"],
60 [gl_cv_func_strstr_works_always="$gl_cross_guess_normal"])
63 case "$gl_cv_func_strstr_works_always" in
70 ]) # gl_FUNC_STRSTR_SIMPLE
72 dnl Additionally, check that strstr is efficient.
73 AC_DEFUN([gl_FUNC_STRSTR],
75 AC_REQUIRE([gl_FUNC_STRSTR_SIMPLE])
76 if test $REPLACE_STRSTR = 0; then
77 AC_CACHE_CHECK([whether strstr works in linear time],
78 [gl_cv_func_strstr_linear],
79 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
81 /* z/OS does not deliver signals while strstr() is running (thanks to
82 restrictions on its LE runtime), which prevents us from limiting the
83 running time of this test. */
84 # error "This test does not work properly on z/OS"
86 #include <signal.h> /* for signal */
87 #include <string.h> /* for strstr */
88 #include <stdlib.h> /* for malloc */
89 #include <unistd.h> /* for alarm */
90 static void quit (int sig) { _exit (sig + 128); }
94 char *haystack = (char *) malloc (2 * m + 2);
95 char *needle = (char *) malloc (m + 2);
96 /* Failure to compile this test due to missing alarm is okay,
97 since all such platforms (mingw) also have quadratic strstr. */
98 signal (SIGALRM, quit);
100 /* Check for quadratic performance. */
101 if (haystack && needle)
103 memset (haystack, 'A', 2 * m);
104 haystack[2 * m] = 'B';
105 haystack[2 * m + 1] = 0;
106 memset (needle, 'A', m);
109 if (!strstr (haystack, needle))
112 /* Free allocated memory, in case some sanitizer is watching. */
117 [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
118 [dnl Only glibc > 2.12 on processors without SSE 4.2 instructions and
119 dnl cygwin > 1.7.7 are known to have a bug-free strstr that works in
121 AC_EGREP_CPP([Lucky user],
123 #include <features.h>
124 #ifdef __GNU_LIBRARY__
125 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
126 && !(defined __i386__ || defined __x86_64__) \
127 && !defined __UCLIBC__
132 #include <cygwin/version.h>
133 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
138 [gl_cv_func_strstr_linear="guessing yes"],
139 [gl_cv_func_strstr_linear="$gl_cross_guess_normal"])
142 case "$gl_cv_func_strstr_linear" in