]> git.cworth.org Git - tar/blob - m4/fnmatch.m4
212ead57291fe6fcb88929b91250c891de3b012f
[tar] / m4 / fnmatch.m4
1 # Check for fnmatch - serial 4.
2
3 # Copyright (C) 2000-2007, 2009-2010 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
7
8 # Autoconf defines AC_FUNC_FNMATCH, but that is obsolescent.
9 # New applications should use the macros below instead.
10
11 # Request a POSIX compliant fnmatch function.
12 AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
13 [
14   m4_divert_text([DEFAULTS], [gl_fnmatch_required=POSIX])
15
16   dnl Persuade glibc <fnmatch.h> to declare FNM_CASEFOLD etc.
17   dnl This is only needed if gl_fnmatch_required = GNU. It would be possible
18   dnl to avoid this dependency for gl_FUNC_FNMATCH_POSIX by putting
19   dnl gl_FUNC_FNMATCH_GNU into a separate .m4 file.
20   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
21
22   FNMATCH_H=
23   gl_fnmatch_required_lowercase=`echo $gl_fnmatch_required | tr 'A-Z' 'a-z'`
24   gl_fnmatch_cache_var="gl_cv_func_fnmatch_${gl_fnmatch_required_lowercase}"
25   AC_CACHE_CHECK([for working $gl_fnmatch_required fnmatch],
26     [$gl_fnmatch_cache_var],
27     [dnl Some versions of Solaris, SCO, and the GNU C Library
28      dnl have a broken or incompatible fnmatch.
29      dnl So we run a test program.  If we are cross-compiling, take no chance.
30      dnl Thanks to John Oleynick, François Pinard, and Paul Eggert for this
31      dnl test.
32      if test $gl_fnmatch_required = GNU; then
33        gl_fnmatch_gnu_start=
34        gl_fnmatch_gnu_end=
35      else
36        gl_fnmatch_gnu_start='#if 0'
37        gl_fnmatch_gnu_end='#endif'
38      fi
39      AC_RUN_IFELSE(
40        [AC_LANG_PROGRAM(
41           [[#include <fnmatch.h>
42             static int
43             y (char const *pattern, char const *string, int flags)
44             {
45               return fnmatch (pattern, string, flags) == 0;
46             }
47             static int
48             n (char const *pattern, char const *string, int flags)
49             {
50               return fnmatch (pattern, string, flags) == FNM_NOMATCH;
51             }
52           ]],
53           [[char const *Apat = 'A' < '\\\\' ? "[A-\\\\\\\\]" : "[\\\\\\\\-A]";
54             char const *apat = 'a' < '\\\\' ? "[a-\\\\\\\\]" : "[\\\\\\\\-a]";
55             static char const A_1[] = { 'A' - 1, 0 };
56             static char const A01[] = { 'A' + 1, 0 };
57             static char const a_1[] = { 'a' - 1, 0 };
58             static char const a01[] = { 'a' + 1, 0 };
59             static char const bs_1[] = { '\\\\' - 1, 0 };
60             static char const bs01[] = { '\\\\' + 1, 0 };
61             return
62              !(n ("a*", "", 0)
63                && y ("a*", "abc", 0)
64                && n ("d*/*1", "d/s/1", FNM_PATHNAME)
65                && y ("a\\\\bc", "abc", 0)
66                && n ("a\\\\bc", "abc", FNM_NOESCAPE)
67                && y ("*x", ".x", 0)
68                && n ("*x", ".x", FNM_PERIOD)
69                && y (Apat, "\\\\", 0) && y (Apat, "A", 0)
70                && y (apat, "\\\\", 0) && y (apat, "a", 0)
71                && n (Apat, A_1, 0) == ('A' < '\\\\')
72                && n (apat, a_1, 0) == ('a' < '\\\\')
73                && y (Apat, A01, 0) == ('A' < '\\\\')
74                && y (apat, a01, 0) == ('a' < '\\\\')
75                && y (Apat, bs_1, 0) == ('A' < '\\\\')
76                && y (apat, bs_1, 0) == ('a' < '\\\\')
77                && n (Apat, bs01, 0) == ('A' < '\\\\')
78                && n (apat, bs01, 0) == ('a' < '\\\\')
79                $gl_fnmatch_gnu_start
80                && y ("xxXX", "xXxX", FNM_CASEFOLD)
81                && y ("a++(x|yy)b", "a+xyyyyxb", FNM_EXTMATCH)
82                && n ("d*/*1", "d/s/1", FNM_FILE_NAME)
83                && y ("*", "x", FNM_FILE_NAME | FNM_LEADING_DIR)
84                && y ("x*", "x/y/z", FNM_FILE_NAME | FNM_LEADING_DIR)
85                && y ("*c*", "c/x", FNM_FILE_NAME | FNM_LEADING_DIR)
86                $gl_fnmatch_gnu_end
87               );
88           ]])],
89        [eval "$gl_fnmatch_cache_var=yes"],
90        [eval "$gl_fnmatch_cache_var=no"],
91        [eval "$gl_fnmatch_cache_var=\"guessing no\""])
92     ])
93   eval "gl_fnmatch_result=\"\$$gl_fnmatch_cache_var\""
94   if test "$gl_fnmatch_result" = yes; then
95     dnl Not strictly necessary. Only to avoid spurious leftover files if people
96     dnl don't do "make distclean".
97     rm -f "$gl_source_base/fnmatch.h"
98   else
99     FNMATCH_H=fnmatch.h
100     AC_LIBOBJ([fnmatch])
101     dnl We must choose a different name for our function, since on ELF systems
102     dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
103     dnl compiled into a shared library.
104     AC_DEFINE_UNQUOTED([fnmatch], [${gl_fnmatch_required_lowercase}_fnmatch],
105       [Define to a replacement function name for fnmatch().])
106     dnl Prerequisites of lib/fnmatch.c.
107     AC_REQUIRE([AC_TYPE_MBSTATE_T])
108     AC_CHECK_DECLS([isblank], [], [], [#include <ctype.h>])
109     AC_CHECK_FUNCS_ONCE([btowc isblank iswctype mbsrtowcs mempcpy wmemchr wmemcpy wmempcpy])
110     AC_CHECK_HEADERS_ONCE([wctype.h])
111   fi
112   AC_SUBST([FNMATCH_H])
113 ])
114
115 # Request a POSIX compliant fnmatch function with GNU extensions.
116 AC_DEFUN([gl_FUNC_FNMATCH_GNU],
117 [
118   m4_divert_text([INIT_PREPARE], [gl_fnmatch_required=GNU])
119
120   AC_REQUIRE([gl_FUNC_FNMATCH_POSIX])
121 ])