memmem.m4 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # memmem.m4 serial 23
  2. dnl Copyright (C) 2002-2004, 2007-2011 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.
  6. dnl Check that memmem is present and functional.
  7. AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
  8. [
  9. dnl Persuade glibc <string.h> to declare memmem().
  10. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  11. AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
  12. AC_CHECK_FUNCS([memmem])
  13. if test $ac_cv_func_memmem = yes; then
  14. HAVE_MEMMEM=1
  15. else
  16. HAVE_MEMMEM=0
  17. fi
  18. AC_CHECK_DECLS_ONCE([memmem])
  19. if test $ac_cv_have_decl_memmem = no; then
  20. HAVE_DECL_MEMMEM=0
  21. else
  22. dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
  23. dnl Also check that we handle empty needles correctly.
  24. AC_CACHE_CHECK([whether memmem works],
  25. [gl_cv_func_memmem_works_always],
  26. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  27. #include <string.h> /* for memmem */
  28. #define P "_EF_BF_BD"
  29. #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
  30. #define NEEDLE P P P P P
  31. ]], [[
  32. int result = 0;
  33. if (memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE)))
  34. result |= 1;
  35. /* Check for empty needle behavior. */
  36. {
  37. const char *haystack = "AAA";
  38. if (memmem (haystack, 3, NULL, 0) != haystack)
  39. result |= 2;
  40. }
  41. return result;
  42. ]])],
  43. [gl_cv_func_memmem_works_always=yes],
  44. [gl_cv_func_memmem_works_always=no],
  45. [dnl glibc 2.9..2.12 and cygwin 1.7.7 have issue #12092 above.
  46. dnl Also empty needles work on glibc >= 2.1 and cygwin >= 1.7.0.
  47. dnl uClibc is not affected, since it uses different source code.
  48. dnl Assume that it works on all other platforms (even if not linear).
  49. AC_EGREP_CPP([Lucky user],
  50. [
  51. #ifdef __GNU_LIBRARY__
  52. #include <features.h>
  53. #if ((__GLIBC__ == 2 && ((__GLIBC_MINOR > 0 && __GLIBC_MINOR__ < 9) \
  54. || __GLIBC_MINOR__ > 12)) \
  55. || (__GLIBC__ > 2)) \
  56. || defined __UCLIBC__
  57. Lucky user
  58. #endif
  59. #elif defined __CYGWIN__
  60. #include <cygwin/version.h>
  61. #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
  62. Lucky user
  63. #endif
  64. #else
  65. Lucky user
  66. #endif
  67. ],
  68. [gl_cv_func_memmem_works_always=yes],
  69. [gl_cv_func_memmem_works_always="guessing no"])
  70. ])
  71. ])
  72. if test "$gl_cv_func_memmem_works_always" != yes; then
  73. REPLACE_MEMMEM=1
  74. fi
  75. fi
  76. gl_PREREQ_MEMMEM
  77. ]) # gl_FUNC_MEMMEM_SIMPLE
  78. dnl Additionally, check that memmem has linear performance characteristics
  79. AC_DEFUN([gl_FUNC_MEMMEM],
  80. [
  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); }
  91. ]], [[
  92. int result = 0;
  93. size_t m = 1000000;
  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);
  99. alarm (5);
  100. /* Check for quadratic performance. */
  101. if (haystack && needle)
  102. {
  103. memset (haystack, 'A', 2 * m);
  104. haystack[2 * m] = 'B';
  105. memset (needle, 'A', m);
  106. needle[m] = 'B';
  107. if (!memmem (haystack, 2 * m + 1, needle, m + 1))
  108. result |= 1;
  109. }
  110. return result;
  111. ]])],
  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],
  116. [
  117. #include <features.h>
  118. #ifdef __GNU_LIBRARY__
  119. #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)) \
  120. && !defined __UCLIBC__
  121. Lucky user
  122. #endif
  123. #endif
  124. #ifdef __CYGWIN__
  125. #include <cygwin/version.h>
  126. #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 0)
  127. Lucky user
  128. #endif
  129. #endif
  130. ],
  131. [gl_cv_func_memmem_works_fast=yes],
  132. [gl_cv_func_memmem_works_fast="guessing no"])
  133. ])
  134. ])
  135. if test "$gl_cv_func_memmem_works_fast" != yes; then
  136. REPLACE_MEMMEM=1
  137. fi
  138. fi
  139. ]) # gl_FUNC_MEMMEM
  140. # Prerequisites of lib/memmem.c.
  141. AC_DEFUN([gl_PREREQ_MEMMEM], [:])