Make .h file creation more reliable.
[gnulib.git] / m4 / fnmatch.m4
1 # Check for fnmatch.
2
3 # This is a modified version of autoconf's AC_FUNC_FNMATCH.
4 # This file should be simplified after Autoconf 2.57 is required.
5
6 # Copyright (C) 2000-2003 Free Software Foundation, Inc.
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 # 02111-1307, USA.
22
23 # _AC_FUNC_FNMATCH_IF(STANDARD = GNU | POSIX, CACHE_VAR, IF-TRUE, IF-FALSE)
24 # -------------------------------------------------------------------------
25 # If a STANDARD compliant fnmatch is found, run IF-TRUE, otherwise
26 # IF-FALSE.  Use CACHE_VAR.
27 AC_DEFUN([_AC_FUNC_FNMATCH_IF],
28 [AC_CACHE_CHECK(
29    [for working $1 fnmatch],
30    [$2],
31   [# Some versions of Solaris, SCO, and the GNU C Library
32    # have a broken or incompatible fnmatch.
33    # So we run a test program.  If we are cross-compiling, take no chance.
34    # Thanks to John Oleynick, Franc,ois Pinard, and Paul Eggert for this test.
35    AC_RUN_IFELSE(
36       [AC_LANG_PROGRAM(
37          [
38 #          include <stdlib.h>
39 #          include <fnmatch.h>
40 #          define y(a, b, c) (fnmatch (a, b, c) == 0)
41 #          define n(a, b, c) (fnmatch (a, b, c) == FNM_NOMATCH)
42          ],
43          [exit
44            (!(y ("a*", "abc", 0)
45               && n ("d*/*1", "d/s/1", FNM_PATHNAME)
46               && y ("a\\\\bc", "abc", 0)
47               && n ("a\\\\bc", "abc", FNM_NOESCAPE)
48               && y ("*x", ".x", 0)
49               && n ("*x", ".x", FNM_PERIOD)
50               && m4_if([$1], [GNU],
51                    [y ("xxXX", "xXxX", FNM_CASEFOLD)
52                     && y ("a++(x|yy)b", "a+xyyyyxb", FNM_EXTMATCH)
53                     && n ("d*/*1", "d/s/1", FNM_FILE_NAME)
54                     && y ("*", "x", FNM_FILE_NAME | FNM_LEADING_DIR)
55                     && y ("x*", "x/y/z", FNM_FILE_NAME | FNM_LEADING_DIR)
56                     && y ("*c*", "c/x", FNM_FILE_NAME | FNM_LEADING_DIR)],
57                    1)));])],
58       [$2=yes],
59       [$2=no],
60       [$2=cross])])
61 AS_IF([test $$2 = yes], [$3], [$4])
62 ])# _AC_FUNC_FNMATCH_IF
63
64
65 # _AC_LIBOBJ_FNMATCH
66 # ------------------
67 # Prepare the replacement of fnmatch.
68 AC_DEFUN([_AC_LIBOBJ_FNMATCH],
69 [AC_REQUIRE([AC_C_CONST])dnl
70 AC_REQUIRE([AC_FUNC_ALLOCA])dnl
71 AC_REQUIRE([AC_TYPE_MBSTATE_T])dnl
72 AC_CHECK_DECLS([getenv])
73 AC_CHECK_FUNCS([btowc mbsrtowcs mempcpy wmempcpy])
74 AC_CHECK_HEADERS([wchar.h wctype.h])
75 AC_LIBOBJ([fnmatch])
76 FNMATCH_H=fnmatch.h
77 AC_DEFINE(fnmatch, rpl_fnmatch,
78           [Define to rpl_fnmatch if the replacement function should be used.])
79 ])# _AC_LIBOBJ_FNMATCH
80
81
82 # Additional prerequisites of lib/fnmatch.c, not part of _AC_LIBOBJ_FNMATCH.
83 AC_DEFUN([gl_PREREQ_FNMATCH_EXTRA],
84 [
85   AC_REQUIRE([AC_HEADER_STDC])
86   AC_CHECK_HEADERS_ONCE(string.h strings.h)
87 ])
88
89
90 AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
91 [
92   FNMATCH_H=
93   _AC_FUNC_FNMATCH_IF([POSIX], [ac_cv_func_fnmatch_posix],
94                       [rm -f lib/fnmatch.h],
95                       [_AC_LIBOBJ_FNMATCH])
96   if test $ac_cv_func_fnmatch_posix != yes; then
97     gl_PREREQ_FNMATCH_EXTRA
98     dnl We must choose a different name for our function, since on ELF systems
99     dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
100     dnl compiled into a shared library.
101     AC_DEFINE([fnmatch], [posix_fnmatch],
102       [Define to a replacement function name for fnmatch().])
103   fi
104   AC_SUBST([FNMATCH_H])
105 ])
106
107
108 AC_DEFUN([gl_FUNC_FNMATCH_GNU],
109 [
110   dnl Persuade glibc <fnmatch.h> to declare FNM_CASEFOLD etc.
111   AC_REQUIRE([AC_GNU_SOURCE])
112
113   FNMATCH_H=
114   _AC_FUNC_FNMATCH_IF([GNU], [ac_cv_func_fnmatch_gnu],
115                       [rm -f lib/fnmatch.h],
116                       [_AC_LIBOBJ_FNMATCH])
117   if test $ac_cv_func_fnmatch_gnu != yes; then
118     gl_PREREQ_FNMATCH_EXTRA
119     dnl We must choose a different name for our function, since on ELF systems
120     dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
121     dnl compiled into a shared library.
122     AC_DEFINE([fnmatch], [gnu_fnmatch],
123       [Define to a replacement function name for fnmatch().])
124   fi
125   AC_SUBST([FNMATCH_H])
126 ])