2 dnl Copyright (C) 2002-2004, 2007-2017 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 memmem is present and functional.
8 AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
10 dnl Persuade glibc <string.h> to declare memmem().
11 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
13 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
14 AC_CHECK_FUNCS([memmem])
15 if test $ac_cv_func_memmem = yes; then
20 AC_CHECK_DECLS_ONCE([memmem])
21 if test $ac_cv_have_decl_memmem = no; then
24 dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
25 dnl Also check that we handle empty needles correctly.
26 AC_CACHE_CHECK([whether memmem works],
27 [gl_cv_func_memmem_works_always],
28 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
29 #include <string.h> /* for memmem */
31 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
32 #define NEEDLE P P P P P
35 if (memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE)))
37 /* Check for empty needle behavior. */
39 const char *haystack = "AAA";
40 if (memmem (haystack, 3, NULL, 0) != haystack)
45 [gl_cv_func_memmem_works_always=yes],
46 [gl_cv_func_memmem_works_always=no],
47 [dnl glibc 2.9..2.12 and cygwin 1.7.7 have issue #12092 above.
48 dnl Also empty needles work on glibc >= 2.1 and cygwin >= 1.7.0.
49 dnl uClibc is not affected, since it uses different source code.
50 dnl Assume that it works on all other platforms (even if not linear).
51 AC_EGREP_CPP([Lucky user],
53 #ifdef __GNU_LIBRARY__
55 #if ((__GLIBC__ == 2 && ((__GLIBC_MINOR > 0 && __GLIBC_MINOR__ < 9) \
56 || __GLIBC_MINOR__ > 12)) \
61 #elif defined __CYGWIN__
62 #include <cygwin/version.h>
63 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
70 [gl_cv_func_memmem_works_always="guessing yes"],
71 [gl_cv_func_memmem_works_always="guessing no"])
74 case "$gl_cv_func_memmem_works_always" in
82 ]) # gl_FUNC_MEMMEM_SIMPLE
84 dnl Additionally, check that memmem has linear performance characteristics
85 AC_DEFUN([gl_FUNC_MEMMEM],
87 AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
88 if test $HAVE_DECL_MEMMEM = 1 && test $REPLACE_MEMMEM = 0; then
89 AC_CACHE_CHECK([whether memmem works in linear time],
90 [gl_cv_func_memmem_works_fast],
91 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
92 #include <signal.h> /* for signal */
93 #include <string.h> /* for memmem */
94 #include <stdlib.h> /* for malloc */
95 #include <unistd.h> /* for alarm */
96 static void quit (int sig) { _exit (sig + 128); }
100 char *haystack = (char *) malloc (2 * m + 1);
101 char *needle = (char *) malloc (m + 1);
102 /* Failure to compile this test due to missing alarm is okay,
103 since all such platforms (mingw) also lack memmem. */
104 signal (SIGALRM, quit);
106 /* Check for quadratic performance. */
107 if (haystack && needle)
109 memset (haystack, 'A', 2 * m);
110 haystack[2 * m] = 'B';
111 memset (needle, 'A', m);
113 if (!memmem (haystack, 2 * m + 1, needle, m + 1))
116 /* Free allocated memory, in case some sanitizer is watching. */
121 [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no],
122 [dnl Only glibc >= 2.9 and cygwin > 1.7.0 are known to have a
123 dnl memmem that works in linear time.
124 AC_EGREP_CPP([Lucky user],
126 #include <features.h>
127 #ifdef __GNU_LIBRARY__
128 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)) \
129 && !defined __UCLIBC__
134 #include <cygwin/version.h>
135 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 0)
140 [gl_cv_func_memmem_works_fast="guessing yes"],
141 [gl_cv_func_memmem_works_fast="guessing no"])
144 case "$gl_cv_func_memmem_works_fast" in
153 # Prerequisites of lib/memmem.c.
154 AC_DEFUN([gl_PREREQ_MEMMEM], [:])