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
21 ]) # gl_FUNC_MEMMEM_SIMPLE
23 dnl Additionally, check that memmem is efficient and handles empty needles.
24 AC_DEFUN([gl_FUNC_MEMMEM],
26 AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
27 if test $ac_cv_have_decl_memmem = yes; then
28 AC_CACHE_CHECK([whether memmem works in linear time],
29 [gl_cv_func_memmem_works],
30 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
31 #include <signal.h> /* for signal */
32 #include <string.h> /* for memmem */
33 #include <stdlib.h> /* for malloc */
34 #include <unistd.h> /* for alarm */
35 static void quit (int sig) { exit (sig + 128); }
36 ]], [[size_t m = 1000000;
37 char *haystack = (char *) malloc (2 * m + 1);
38 char *needle = (char *) malloc (m + 1);
40 /* Failure to compile this test due to missing alarm is okay,
41 since all such platforms (mingw) also lack memmem. */
42 signal (SIGALRM, quit);
44 /* Check for quadratic performance. */
45 if (haystack && needle)
47 memset (haystack, 'A', 2 * m);
48 haystack[2 * m] = 'B';
49 memset (needle, 'A', m);
51 result = memmem (haystack, 2 * m + 1, needle, m + 1);
53 /* Check for empty needle behavior. */
54 return !result || !memmem ("a", 1, 0, 0);]])],
55 [gl_cv_func_memmem_works=yes], [gl_cv_func_memmem_works=no],
56 [dnl Only glibc >= 2.9 and cygwin >= 1.7.0 are known to have a
57 dnl memmem that works in linear time.
58 AC_EGREP_CPP([Lucky user],
61 #ifdef __GNU_LIBRARY__
62 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)
67 #include <cygwin/version.h>
68 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
73 [gl_cv_func_memmem_works=yes],
74 [gl_cv_func_memmem_works="guessing no"])
77 if test "$gl_cv_func_memmem_works" != yes; then
84 # Prerequisites of lib/memmem.c.
85 AC_DEFUN([gl_PREREQ_MEMMEM], [:])