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.
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 AC_CACHE_CHECK([whether memmem works],
22 [gl_cv_func_memmem_works_always],
23 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
24 #include <string.h> /* for memmem */
26 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
27 #define NEEDLE P P P P P
28 ]], [[return !!memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE));
30 [gl_cv_func_memmem_works_always=yes],
31 [gl_cv_func_memmem_works_always=no],
32 [dnl glibc 2.12 and cygwin 1.7.7 have a known bug. Assume that it
33 dnl works on all other platforms, even if it is not linear.
34 AC_EGREP_CPP([Lucky user],
36 #ifdef __GNU_LIBRARY__
38 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
39 && !defined __UCLIBC__
42 #elif defined __CYGWIN__
43 #include <cygwin/version.h>
44 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
51 [gl_cv_func_memmem_works_always=yes],
52 [gl_cv_func_memmem_works_always="guessing no"])
55 if test "$gl_cv_func_memmem_works_always" != yes; then
61 ]) # gl_FUNC_MEMMEM_SIMPLE
63 dnl Additionally, check that memmem is efficient and handles empty needles.
64 AC_DEFUN([gl_FUNC_MEMMEM],
66 AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
67 if test $HAVE_MEMMEM = 1 && test $REPLACE_MEMMEM = 0; then
68 AC_CACHE_CHECK([whether memmem works in linear time],
69 [gl_cv_func_memmem_works_fast],
70 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
71 #include <signal.h> /* for signal */
72 #include <string.h> /* for memmem */
73 #include <stdlib.h> /* for malloc */
74 #include <unistd.h> /* for alarm */
75 static void quit (int sig) { exit (sig + 128); }
76 ]], [[size_t m = 1000000;
77 char *haystack = (char *) malloc (2 * m + 1);
78 char *needle = (char *) malloc (m + 1);
80 /* Failure to compile this test due to missing alarm is okay,
81 since all such platforms (mingw) also lack memmem. */
82 signal (SIGALRM, quit);
84 /* Check for quadratic performance. */
85 if (haystack && needle)
87 memset (haystack, 'A', 2 * m);
88 haystack[2 * m] = 'B';
89 memset (needle, 'A', m);
91 result = memmem (haystack, 2 * m + 1, needle, m + 1);
93 /* Check for empty needle behavior. */
94 return !result || !memmem ("a", 1, 0, 0);]])],
95 [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no],
96 [dnl Only glibc > 2.12 and cygwin > 1.7.7 are known to have a
97 dnl bug-free memmem that works in linear time.
98 AC_EGREP_CPP([Lucky user],
100 #include <features.h>
101 #ifdef __GNU_LIBRARY__
102 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
103 && !defined __UCLIBC__
108 #include <cygwin/version.h>
109 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
114 [gl_cv_func_memmem_works_fast=yes],
115 [gl_cv_func_memmem_works_fast="guessing no"])
118 if test "$gl_cv_func_memmem_works_fast" != yes; then
125 # Prerequisites of lib/memmem.c.
126 AC_DEFUN([gl_PREREQ_MEMMEM], [:])