2 dnl Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010 Free Software
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl Check that memmem is present and functional.
9 AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
11 dnl Persuade glibc <string.h> to declare memmem().
12 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
14 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
15 AC_REPLACE_FUNCS([memmem])
16 AC_CHECK_DECLS_ONCE([memmem])
17 if test $ac_cv_have_decl_memmem = no; then
20 dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
21 dnl Also check that we handle empty needles correctly.
22 AC_CACHE_CHECK([whether memmem works],
23 [gl_cv_func_memmem_works_always],
24 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
25 #include <string.h> /* for memmem */
27 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
28 #define NEEDLE P P P P P
31 if (memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE)))
33 /* Check for empty needle behavior. */
35 const char *haystack = "AAA";
36 if (memmem (haystack, 3, NULL, 0) != haystack)
41 [gl_cv_func_memmem_works_always=yes],
42 [gl_cv_func_memmem_works_always=no],
43 [dnl glibc 2.9..2.12 and cygwin 1.7.7 have issue #12092 above.
44 dnl Also empty needles work on glibc >= 2.1 and cygwin >= 1.7.0.
45 dnl uClibc is not affected, since it uses different source code.
46 dnl Assume that it works on all other platforms (even if not linear).
47 AC_EGREP_CPP([Lucky user],
49 #ifdef __GNU_LIBRARY__
51 #if ((__GLIBC__ == 2 && ((__GLIBC_MINOR > 0 && __GLIBC_MINOR__ < 9) \
52 || __GLIBC_MINOR__ > 12)) \
57 #elif defined __CYGWIN__
58 #include <cygwin/version.h>
59 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
66 [gl_cv_func_memmem_works_always=yes],
67 [gl_cv_func_memmem_works_always="guessing no"])
70 if test "$gl_cv_func_memmem_works_always" != yes; then
76 ]) # gl_FUNC_MEMMEM_SIMPLE
78 dnl Additionally, check that memmem has linear performance characteristics
79 AC_DEFUN([gl_FUNC_MEMMEM],
81 AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
82 if test $HAVE_DECL_MEMMEM = 1 && test $REPLACE_MEMMEM = 0; then
83 AC_CACHE_CHECK([whether memmem works in linear time],
84 [gl_cv_func_memmem_works_fast],
85 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
86 #include <signal.h> /* for signal */
87 #include <string.h> /* for memmem */
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 + 1);
95 char *needle = (char *) malloc (m + 1);
96 /* Failure to compile this test due to missing alarm is okay,
97 since all such platforms (mingw) also lack memmem. */
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 memset (needle, 'A', m);
107 if (!memmem (haystack, 2 * m + 1, needle, m + 1))
112 [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no],
113 [dnl Only glibc >= 2.9 and cygwin > 1.7.0 are known to have a
114 dnl memmem that works in linear time.
115 AC_EGREP_CPP([Lucky user],
117 #include <features.h>
118 #ifdef __GNU_LIBRARY__
119 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)) \
120 && !defined __UCLIBC__
125 #include <cygwin/version.h>
126 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 0)
131 [gl_cv_func_memmem_works_fast=yes],
132 [gl_cv_func_memmem_works_fast="guessing no"])
135 if test "$gl_cv_func_memmem_works_fast" != yes; then
142 # Prerequisites of lib/memmem.c.
143 AC_DEFUN([gl_PREREQ_MEMMEM], [:])