strstr.m4 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # strstr.m4 serial 7
  2. dnl Copyright (C) 2008, 2009, 2010 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 strstr works.
  7. AC_DEFUN([gl_FUNC_STRSTR_SIMPLE],
  8. [
  9. AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
  10. AC_REQUIRE([gl_FUNC_MEMCHR])
  11. if test "$gl_cv_func_memchr_works" != yes; then
  12. REPLACE_STRSTR=1
  13. AC_LIBOBJ([strstr])
  14. fi
  15. ]) # gl_FUNC_STRSTR_SIMPLE
  16. dnl Additionally, check that strstr is efficient.
  17. AC_DEFUN([gl_FUNC_STRSTR],
  18. [
  19. AC_REQUIRE([gl_FUNC_STRSTR_SIMPLE])
  20. if test $REPLACE_STRSTR = 0; then
  21. AC_CACHE_CHECK([whether strstr works in linear time],
  22. [gl_cv_func_strstr_linear],
  23. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  24. #include <signal.h> /* for signal */
  25. #include <string.h> /* for memmem */
  26. #include <stdlib.h> /* for malloc */
  27. #include <unistd.h> /* for alarm */
  28. ]], [[size_t m = 1000000;
  29. char *haystack = (char *) malloc (2 * m + 2);
  30. char *needle = (char *) malloc (m + 2);
  31. void *result = 0;
  32. /* Failure to compile this test due to missing alarm is okay,
  33. since all such platforms (mingw) also have quadratic strstr. */
  34. signal (SIGALRM, SIG_DFL);
  35. alarm (5);
  36. /* Check for quadratic performance. */
  37. if (haystack && needle)
  38. {
  39. memset (haystack, 'A', 2 * m);
  40. haystack[2 * m] = 'B';
  41. haystack[2 * m + 1] = 0;
  42. memset (needle, 'A', m);
  43. needle[m] = 'B';
  44. needle[m + 1] = 0;
  45. result = strstr (haystack, needle);
  46. }
  47. return !result;]])],
  48. [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
  49. [dnl Only glibc >= 2.9 and cygwin >= 1.7.0 are known to have a
  50. dnl strstr that works in linear time.
  51. AC_EGREP_CPP([Lucky user],
  52. [
  53. #include <features.h>
  54. #ifdef __GNU_LIBRARY__
  55. #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)
  56. Lucky user
  57. #endif
  58. #endif
  59. #ifdef __CYGWIN__
  60. #include <cygwin/version.h>
  61. #if CYGWIN_VERSION_DLL_MAJOR >= 1007
  62. Lucky user
  63. #endif
  64. #endif
  65. ],
  66. [gl_cv_func_strstr_linear=yes],
  67. [gl_cv_func_strstr_linear="guessing no"])
  68. ])
  69. ])
  70. if test "$gl_cv_func_strstr_linear" != yes; then
  71. REPLACE_STRSTR=1
  72. fi
  73. fi
  74. if test $REPLACE_STRSTR = 1; then
  75. AC_LIBOBJ([strstr])
  76. fi
  77. ]) # gl_FUNC_STRSTR