strcasestr.m4 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # strcasestr.m4 serial 21
  2. dnl Copyright (C) 2005, 2007-2017 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 strcasestr is present and works.
  7. AC_DEFUN([gl_FUNC_STRCASESTR_SIMPLE],
  8. [
  9. AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
  10. dnl Persuade glibc <string.h> to declare strcasestr().
  11. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  12. AC_REQUIRE([gl_FUNC_MEMCHR])
  13. AC_CHECK_FUNCS([strcasestr])
  14. if test $ac_cv_func_strcasestr = no; then
  15. HAVE_STRCASESTR=0
  16. else
  17. if test "$gl_cv_func_memchr_works" != yes; then
  18. REPLACE_STRCASESTR=1
  19. else
  20. dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
  21. AC_CACHE_CHECK([whether strcasestr works],
  22. [gl_cv_func_strcasestr_works_always],
  23. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  24. #include <string.h> /* for strcasestr */
  25. #define P "_EF_BF_BD"
  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 !!strcasestr (HAYSTACK, NEEDLE);
  29. ]])],
  30. [gl_cv_func_strcasestr_works_always=yes],
  31. [gl_cv_func_strcasestr_works_always=no],
  32. [dnl glibc 2.12 and cygwin 1.7.7 have a known bug. uClibc is not
  33. dnl affected, since it uses different source code for strcasestr
  34. dnl than glibc.
  35. dnl Assume that it works on all other platforms, even if it is not
  36. dnl linear.
  37. AC_EGREP_CPP([Lucky user],
  38. [
  39. #ifdef __GNU_LIBRARY__
  40. #include <features.h>
  41. #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
  42. || defined __UCLIBC__
  43. Lucky user
  44. #endif
  45. #elif defined __CYGWIN__
  46. #include <cygwin/version.h>
  47. #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
  48. Lucky user
  49. #endif
  50. #else
  51. Lucky user
  52. #endif
  53. ],
  54. [gl_cv_func_strcasestr_works_always="guessing yes"],
  55. [gl_cv_func_strcasestr_works_always="guessing no"])
  56. ])
  57. ])
  58. case "$gl_cv_func_strcasestr_works_always" in
  59. *yes) ;;
  60. *)
  61. REPLACE_STRCASESTR=1
  62. ;;
  63. esac
  64. fi
  65. fi
  66. ]) # gl_FUNC_STRCASESTR_SIMPLE
  67. dnl Additionally, check that strcasestr is efficient.
  68. AC_DEFUN([gl_FUNC_STRCASESTR],
  69. [
  70. AC_REQUIRE([gl_FUNC_STRCASESTR_SIMPLE])
  71. if test $HAVE_STRCASESTR = 1 && test $REPLACE_STRCASESTR = 0; then
  72. AC_CACHE_CHECK([whether strcasestr works in linear time],
  73. [gl_cv_func_strcasestr_linear],
  74. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  75. #include <signal.h> /* for signal */
  76. #include <string.h> /* for strcasestr */
  77. #include <stdlib.h> /* for malloc */
  78. #include <unistd.h> /* for alarm */
  79. static void quit (int sig) { _exit (sig + 128); }
  80. ]], [[
  81. int result = 0;
  82. size_t m = 1000000;
  83. char *haystack = (char *) malloc (2 * m + 2);
  84. char *needle = (char *) malloc (m + 2);
  85. /* Failure to compile this test due to missing alarm is okay,
  86. since all such platforms (mingw) also lack strcasestr. */
  87. signal (SIGALRM, quit);
  88. alarm (5);
  89. /* Check for quadratic performance. */
  90. if (haystack && needle)
  91. {
  92. memset (haystack, 'A', 2 * m);
  93. haystack[2 * m] = 'B';
  94. haystack[2 * m + 1] = 0;
  95. memset (needle, 'A', m);
  96. needle[m] = 'B';
  97. needle[m + 1] = 0;
  98. if (!strcasestr (haystack, needle))
  99. result |= 1;
  100. }
  101. return result;
  102. ]])],
  103. [gl_cv_func_strcasestr_linear=yes], [gl_cv_func_strcasestr_linear=no],
  104. [dnl Only glibc > 2.12 and cygwin > 1.7.7 are known to have a
  105. dnl strcasestr that works in linear time.
  106. AC_EGREP_CPP([Lucky user],
  107. [
  108. #include <features.h>
  109. #ifdef __GNU_LIBRARY__
  110. #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
  111. && !defined __UCLIBC__
  112. Lucky user
  113. #endif
  114. #endif
  115. #ifdef __CYGWIN__
  116. #include <cygwin/version.h>
  117. #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
  118. Lucky user
  119. #endif
  120. #endif
  121. ],
  122. [gl_cv_func_strcasestr_linear="guessing yes"],
  123. [gl_cv_func_strcasestr_linear="guessing no"])
  124. ])
  125. ])
  126. case "$gl_cv_func_strcasestr_linear" in
  127. *yes) ;;
  128. *)
  129. REPLACE_STRCASESTR=1
  130. ;;
  131. esac
  132. fi
  133. ]) # gl_FUNC_STRCASESTR
  134. # Prerequisites of lib/strcasestr.c.
  135. AC_DEFUN([gl_PREREQ_STRCASESTR], [
  136. :
  137. ])