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