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