[__SVR4]: Define SVR4.
[coreutils.git] / lib / fnmatch.c
blob4bc7cd9f80339bfede67f83cc614761f7560caaf
1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C
4 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <errno.h>
25 #include <fnmatch.h>
26 #include <ctype.h>
28 #if defined (STDC_HEADERS) || !defined (isascii)
29 # define ISASCII(c) 1
30 #else
31 # define ISASCII(c) isascii(c)
32 #endif
34 #define ISUPPER(c) (ISASCII (c) && isupper (c))
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
45 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
48 # ifndef errno
49 extern int errno;
50 # endif
52 /* Match STRING against the filename pattern PATTERN, returning zero if
53 it matches, nonzero if not. */
54 int
55 fnmatch (pattern, string, flags)
56 const char *pattern;
57 const char *string;
58 int flags;
60 register const char *p = pattern, *n = string;
61 register char c;
63 /* Note that this evalutes C many times. */
64 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
66 while ((c = *p++) != '\0')
68 c = FOLD (c);
70 switch (c)
72 case '?':
73 if (*n == '\0')
74 return FNM_NOMATCH;
75 else if ((flags & FNM_FILE_NAME) && *n == '/')
76 return FNM_NOMATCH;
77 else if ((flags & FNM_PERIOD) && *n == '.' &&
78 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
79 return FNM_NOMATCH;
80 break;
82 case '\\':
83 if (!(flags & FNM_NOESCAPE))
85 c = *p++;
86 c = FOLD (c);
88 if (FOLD (*n) != c)
89 return FNM_NOMATCH;
90 break;
92 case '*':
93 if ((flags & FNM_PERIOD) && *n == '.' &&
94 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
95 return FNM_NOMATCH;
97 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
98 if (((flags & FNM_FILE_NAME) && *n == '/') ||
99 (c == '?' && *n == '\0'))
100 return FNM_NOMATCH;
102 if (c == '\0')
103 return 0;
106 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
107 c1 = FOLD (c1);
108 for (--p; *n != '\0'; ++n)
109 if ((c == '[' || FOLD (*n) == c1) &&
110 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
111 return 0;
112 return FNM_NOMATCH;
115 case '[':
117 /* Nonzero if the sense of the character class is inverted. */
118 register int not;
120 if (*n == '\0')
121 return FNM_NOMATCH;
123 if ((flags & FNM_PERIOD) && *n == '.' &&
124 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
125 return FNM_NOMATCH;
127 not = (*p == '!' || *p == '^');
128 if (not)
129 ++p;
131 c = *p++;
132 for (;;)
134 register char cstart = c, cend = c;
136 if (!(flags & FNM_NOESCAPE) && c == '\\')
137 cstart = cend = *p++;
139 cstart = cend = FOLD (cstart);
141 if (c == '\0')
142 /* [ (unterminated) loses. */
143 return FNM_NOMATCH;
145 c = *p++;
146 c = FOLD (c);
148 if ((flags & FNM_FILE_NAME) && c == '/')
149 /* [/] can never match. */
150 return FNM_NOMATCH;
152 if (c == '-' && *p != ']')
154 cend = *p++;
155 if (!(flags & FNM_NOESCAPE) && cend == '\\')
156 cend = *p++;
157 if (cend == '\0')
158 return FNM_NOMATCH;
159 cend = FOLD (cend);
161 c = *p++;
164 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
165 goto matched;
167 if (c == ']')
168 break;
170 if (!not)
171 return FNM_NOMATCH;
172 break;
174 matched:;
175 /* Skip the rest of the [...] that already matched. */
176 while (c != ']')
178 if (c == '\0')
179 /* [... (unterminated) loses. */
180 return FNM_NOMATCH;
182 c = *p++;
183 if (!(flags & FNM_NOESCAPE) && c == '\\')
184 /* XXX 1003.2d11 is unclear if this is right. */
185 ++p;
187 if (not)
188 return FNM_NOMATCH;
190 break;
192 default:
193 if (c != FOLD (*n))
194 return FNM_NOMATCH;
197 ++n;
200 if (*n == '\0')
201 return 0;
203 if ((flags & FNM_LEADING_DIR) && *n == '/')
204 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
205 return 0;
207 return FNM_NOMATCH;
209 # undef FOLD
212 #endif /* _LIBC or not __GNU_LIBRARY__. */