merge with 1.8b
[coreutils.git] / lib / fnmatch.c
blobdf1706cc0ce61d3d96497c64972f34b96d722397
1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with this library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
18 #ifdef HAVE_CONFIG_H
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22 (which it would do because it found this file in $srcdir). */
23 #include <config.h>
24 #else
25 #include "config.h"
26 #endif
27 #endif
29 #include <errno.h>
30 #include <fnmatch.h>
31 #include <ctype.h>
34 /* Comment out all this code if we are using the GNU C Library, and are not
35 actually compiling the library itself. This code is part of the GNU C
36 Library, but also included in many other GNU distributions. Compiling
37 and linking in this code is a waste when using the GNU C library
38 (especially if it is a shared library). Rather than having every GNU
39 program understand `configure --with-gnu-libc' and omit the object files,
40 it is simpler to just do this in the source for each such file. */
42 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
45 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
46 extern int errno;
47 #endif
49 /* Match STRING against the filename pattern PATTERN, returning zero if
50 it matches, nonzero if not. */
51 int
52 fnmatch (pattern, string, flags)
53 const char *pattern;
54 const char *string;
55 int flags;
57 register const char *p = pattern, *n = string;
58 register char c;
60 /* Note that this evalutes C many times. */
61 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
63 while ((c = *p++) != '\0')
65 c = FOLD (c);
67 switch (c)
69 case '?':
70 if (*n == '\0')
71 return FNM_NOMATCH;
72 else if ((flags & FNM_FILE_NAME) && *n == '/')
73 return FNM_NOMATCH;
74 else if ((flags & FNM_PERIOD) && *n == '.' &&
75 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
76 return FNM_NOMATCH;
77 break;
79 case '\\':
80 if (!(flags & FNM_NOESCAPE))
82 c = *p++;
83 c = FOLD (c);
85 if (FOLD (*n) != c)
86 return FNM_NOMATCH;
87 break;
89 case '*':
90 if ((flags & FNM_PERIOD) && *n == '.' &&
91 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
92 return FNM_NOMATCH;
94 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
95 if (((flags & FNM_FILE_NAME) && *n == '/') ||
96 (c == '?' && *n == '\0'))
97 return FNM_NOMATCH;
99 if (c == '\0')
100 return 0;
103 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
104 c1 = FOLD (c1);
105 for (--p; *n != '\0'; ++n)
106 if ((c == '[' || FOLD (*n) == c1) &&
107 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
108 return 0;
109 return FNM_NOMATCH;
112 case '[':
114 /* Nonzero if the sense of the character class is inverted. */
115 register int not;
117 if (*n == '\0')
118 return FNM_NOMATCH;
120 if ((flags & FNM_PERIOD) && *n == '.' &&
121 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
122 return FNM_NOMATCH;
124 not = (*p == '!' || *p == '^');
125 if (not)
126 ++p;
128 c = *p++;
129 for (;;)
131 register char cstart = c, cend = c;
133 if (!(flags & FNM_NOESCAPE) && c == '\\')
134 cstart = cend = *p++;
136 cstart = cend = FOLD (cstart);
138 if (c == '\0')
139 /* [ (unterminated) loses. */
140 return FNM_NOMATCH;
142 c = *p++;
143 c = FOLD (c);
145 if ((flags & FNM_FILE_NAME) && c == '/')
146 /* [/] can never match. */
147 return FNM_NOMATCH;
149 if (c == '-' && *p != ']')
151 cend = *p++;
152 if (!(flags & FNM_NOESCAPE) && cend == '\\')
153 cend = *p++;
154 if (cend == '\0')
155 return FNM_NOMATCH;
156 cend = FOLD (cend);
158 c = *p++;
161 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
162 goto matched;
164 if (c == ']')
165 break;
167 if (!not)
168 return FNM_NOMATCH;
169 break;
171 matched:;
172 /* Skip the rest of the [...] that already matched. */
173 while (c != ']')
175 if (c == '\0')
176 /* [... (unterminated) loses. */
177 return FNM_NOMATCH;
179 c = *p++;
180 if (!(flags & FNM_NOESCAPE) && c == '\\')
181 /* XXX 1003.2d11 is unclear if this is right. */
182 ++p;
184 if (not)
185 return FNM_NOMATCH;
187 break;
189 default:
190 if (c != FOLD (*n))
191 return FNM_NOMATCH;
194 ++n;
197 if (*n == '\0')
198 return 0;
200 if ((flags & FNM_LEADING_DIR) && *n == '/')
201 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
202 return 0;
204 return FNM_NOMATCH;
207 #endif /* _LIBC or not __GNU_LIBRARY__. */