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