2 dnl Copyright (C) 2002, 2003, 2004, 2007, 2008 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.
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_REPLACE_FUNCS(memmem)
15 AC_CHECK_DECLS_ONCE(memmem)
16 if test $ac_cv_have_decl_memmem = no; then
20 ]) # gl_FUNC_MEMMEM_SIMPLE
22 dnl Additionally, check that memmem is efficient and handles empty needles.
23 AC_DEFUN([gl_FUNC_MEMMEM],
25 AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
26 if test $ac_cv_have_decl_memmem = yes; then
27 AC_CACHE_CHECK([whether memmem works in linear time],
28 [gl_cv_func_memmem_works],
29 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
30 #include <string.h> /* for memmem */
31 #include <stdlib.h> /* for malloc */
32 #include <unistd.h> /* for alarm */
33 ]], [[size_t m = 1000000;
34 char *haystack = (char *) malloc (2 * m + 1);
35 char *needle = (char *) malloc (m + 1);
37 /* Failure to compile this test due to missing alarm is okay,
38 since all such platforms (mingw) also lack memmem. */
40 /* Check for quadratic performance. */
41 if (haystack && needle)
43 memset (haystack, 'A', 2 * m);
44 haystack[2 * m] = 'B';
45 memset (needle, 'A', m);
47 result = memmem (haystack, 2 * m + 1, needle, m + 1);
49 /* Check for empty needle behavior. */
50 return !result || !memmem ("a", 1, 0, 0);]])],
51 [gl_cv_func_memmem_works=yes], [gl_cv_func_memmem_works=no],
52 [dnl Only glibc >= 2.9 and cygwin >= 1.7.0 are known to have a
53 dnl memmem that works in linear time.
54 AC_EGREP_CPP([Lucky user],
57 #ifdef __GNU_LIBRARY__
58 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)
63 #include <cygwin/version.h>
64 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
69 [gl_cv_func_memmem_works=yes],
70 [gl_cv_func_memmem_works="guessing no"])
73 if test "$gl_cv_func_memmem_works" != yes; then
80 # Prerequisites of lib/memmem.c.
81 AC_DEFUN([gl_PREREQ_MEMMEM], [:])