2 dnl Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
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 <string.h> /* for memmem */
32 #include <stdlib.h> /* for malloc */
33 #include <unistd.h> /* for alarm */
34 ], [[size_t m = 1000000;
35 char *haystack = (char *) malloc (2 * m + 1);
36 char *needle = (char *) malloc (m + 1);
38 /* Failure to compile this test due to missing alarm is okay,
39 since all such platforms (mingw) also lack memmem. */
41 /* Check for quadratic performance. */
42 if (haystack && needle)
44 memset (haystack, 'A', 2 * m);
45 haystack[2 * m] = 'B';
46 memset (needle, 'A', m);
48 result = memmem (haystack, 2 * m + 1, needle, m + 1);
50 /* Check for empty needle behavior. */
51 return !result || !memmem ("a", 1, 0, 0);]])],
52 [gl_cv_func_memmem_works=yes], [gl_cv_func_memmem_works=no],
53 [dnl pessimistically assume the worst, since even glibc 2.6.1
54 dnl has quadratic complexity in its memmem
55 gl_cv_func_memmem_works="guessing no"])])
56 if test "$gl_cv_func_memmem_works" != yes; then
63 # Prerequisites of lib/memmem.c.
64 AC_DEFUN([gl_PREREQ_MEMMEM], [:])