1# Check for fnmatch - serial 15.  -*- coding: utf-8 -*-
2
3# Copyright (C) 2000-2007, 2009-2021 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.
12AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
13[
14  m4_divert_text([DEFAULTS], [gl_fnmatch_required=POSIX])
15
16  AC_REQUIRE([gl_FNMATCH_H])
17  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
18  gl_fnmatch_required_lowercase=`
19    echo $gl_fnmatch_required | LC_ALL=C tr '[[A-Z]]' '[[a-z]]'
20  `
21  AC_CHECK_FUNCS_ONCE([fnmatch])
22  if test $ac_cv_func_fnmatch = no; then
23    HAVE_FNMATCH=0
24  else
25    gl_fnmatch_cache_var="gl_cv_func_fnmatch_${gl_fnmatch_required_lowercase}"
26    AC_CACHE_CHECK([for working $gl_fnmatch_required fnmatch],
27      [$gl_fnmatch_cache_var],
28      [dnl Some versions of Solaris, SCO, and the GNU C Library
29       dnl have a broken or incompatible fnmatch.
30       dnl So we run a test program.  If we are cross-compiling, take no chance.
31       dnl Thanks to John Oleynick, François Pinard, and Paul Eggert for this
32       dnl test.
33       if test $gl_fnmatch_required = GNU; then
34         gl_fnmatch_gnu_start=
35         gl_fnmatch_gnu_end=
36       else
37         gl_fnmatch_gnu_start='#if 0'
38         gl_fnmatch_gnu_end='#endif'
39       fi
40       AC_RUN_IFELSE(
41         [AC_LANG_PROGRAM(
42            [[#include <fnmatch.h>
43              static int
44              y (char const *pattern, char const *string, int flags)
45              {
46                return fnmatch (pattern, string, flags) == 0;
47              }
48              static int
49              n (char const *pattern, char const *string, int flags)
50              {
51                return fnmatch (pattern, string, flags) == FNM_NOMATCH;
52              }
53            ]],
54            [[char const *Apat = 'A' < '\\\\' ? "[A-\\\\\\\\]" : "[\\\\\\\\-A]";
55              char const *apat = 'a' < '\\\\' ? "[a-\\\\\\\\]" : "[\\\\\\\\-a]";
56              static char const A_1[] = { 'A' - 1, 0 };
57              static char const A01[] = { 'A' + 1, 0 };
58              static char const a_1[] = { 'a' - 1, 0 };
59              static char const a01[] = { 'a' + 1, 0 };
60              static char const bs_1[] = { '\\\\' - 1, 0 };
61              static char const bs01[] = { '\\\\' + 1, 0 };
62              int result = 0;
63              if (!n ("a*", "", 0))
64                return 1;
65              if (!y ("a*", "abc", 0))
66                return 1;
67              if (!y ("[/b", "[/b", 0)) /*"]]"*/ /* glibc Bugzilla bug 12378 */
68                return 1;
69              if (!n ("d*/*1", "d/s/1", FNM_PATHNAME))
70                return 2;
71              if (!y ("a\\\\bc", "abc", 0))
72                return 3;
73              if (!n ("a\\\\bc", "abc", FNM_NOESCAPE))
74                return 3;
75              if (!y ("*x", ".x", 0))
76                return 4;
77              if (!n ("*x", ".x", FNM_PERIOD))
78                return 4;
79              if (!y (Apat, "\\\\", 0))
80                return 5;
81              if (!y (Apat, "A", 0))
82                return 5;
83              if (!y (apat, "\\\\", 0))
84                return 5;
85              if (!y (apat, "a", 0))
86                return 5;
87              if (!(n (Apat, A_1, 0) == ('A' < '\\\\')))
88                return 5;
89              if (!(n (apat, a_1, 0) == ('a' < '\\\\')))
90                return 5;
91              if (!(y (Apat, A01, 0) == ('A' < '\\\\')))
92                return 5;
93              if (!(y (apat, a01, 0) == ('a' < '\\\\')))
94                return 5;
95              if (!(y (Apat, bs_1, 0) == ('A' < '\\\\')))
96                return 5;
97              if (!(y (apat, bs_1, 0) == ('a' < '\\\\')))
98                return 5;
99              if (!(n (Apat, bs01, 0) == ('A' < '\\\\')))
100                return 5;
101              if (!(n (apat, bs01, 0) == ('a' < '\\\\')))
102                return 5;
103              $gl_fnmatch_gnu_start
104              if (!y ("xxXX", "xXxX", FNM_CASEFOLD))
105                result |= 8;
106              if (!y ("a++(x|yy)b", "a+xyyyyxb", FNM_EXTMATCH))
107                result |= 16;
108              if (!n ("d*/*1", "d/s/1", FNM_FILE_NAME))
109                result |= 32;
110              if (!y ("*", "x", FNM_FILE_NAME | FNM_LEADING_DIR))
111                result |= 64;
112              if (!y ("x*", "x/y/z", FNM_FILE_NAME | FNM_LEADING_DIR))
113                result |= 64;
114              if (!y ("*c*", "c/x", FNM_FILE_NAME | FNM_LEADING_DIR))
115                result |= 64;
116              $gl_fnmatch_gnu_end
117              return result;
118            ]])],
119         [eval "$gl_fnmatch_cache_var=yes"],
120         [eval "$gl_fnmatch_cache_var=no"],
121         [case "$host_os" in
122                     # Guess yes on musl systems.
123            *-musl*) eval "$gl_fnmatch_cache_var=\"guessing yes\"" ;;
124                     # Guess no otherwise, even on glibc systems.
125            *)       eval "$gl_fnmatch_cache_var=\"guessing no\"" ;;
126          esac
127         ])
128      ])
129    eval "gl_fnmatch_result=\"\$$gl_fnmatch_cache_var\""
130    case "$gl_fnmatch_result" in
131      *yes) ;;
132      *) REPLACE_FNMATCH=1 ;;
133    esac
134  fi
135  if test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1; then
136    gl_REPLACE_FNMATCH_H
137  fi
138])
139
140# Request a POSIX compliant fnmatch function with GNU extensions.
141AC_DEFUN([gl_FUNC_FNMATCH_GNU],
142[
143  m4_divert_text([INIT_PREPARE], [gl_fnmatch_required=GNU])
144
145  AC_REQUIRE([gl_FUNC_FNMATCH_POSIX])
146])
147
148AC_DEFUN([gl_PREREQ_FNMATCH],
149[
150  dnl Prerequisites of lib/fnmatch.c.
151  AC_REQUIRE([AC_TYPE_MBSTATE_T])
152  AC_CHECK_FUNCS_ONCE([mbsrtowcs])
153])
154