.
[coreutils.git] / lib / fnmatch.c
blobaec3d1aba024309ebfce2e288ed3bfccba72394f
1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
4 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
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <errno.h>
25 #include <fnmatch.h>
26 #include <ctype.h>
29 /* Comment out all this code if we are using the GNU C Library, and are not
30 actually compiling the library itself. This code is part of the GNU C
31 Library, but also included in many other GNU distributions. Compiling
32 and linking in this code is a waste when using the GNU C library
33 (especially if it is a shared library). Rather than having every GNU
34 program understand `configure --with-gnu-libc' and omit the object files,
35 it is simpler to just do this in the source for each such file. */
37 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
40 #ifndef errno
41 extern int errno;
42 #endif
44 /* Match STRING against the filename pattern PATTERN, returning zero if
45 it matches, nonzero if not. */
46 int
47 fnmatch (pattern, string, flags)
48 const char *pattern;
49 const char *string;
50 int flags;
52 register const char *p = pattern, *n = string;
53 register char c;
55 /* Note that this evalutes C many times. */
56 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
58 while ((c = *p++) != '\0')
60 c = FOLD (c);
62 switch (c)
64 case '?':
65 if (*n == '\0')
66 return FNM_NOMATCH;
67 else if ((flags & FNM_FILE_NAME) && *n == '/')
68 return FNM_NOMATCH;
69 else if ((flags & FNM_PERIOD) && *n == '.' &&
70 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
71 return FNM_NOMATCH;
72 break;
74 case '\\':
75 if (!(flags & FNM_NOESCAPE))
77 c = *p++;
78 c = FOLD (c);
80 if (FOLD (*n) != c)
81 return FNM_NOMATCH;
82 break;
84 case '*':
85 if ((flags & FNM_PERIOD) && *n == '.' &&
86 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
87 return FNM_NOMATCH;
89 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
90 if (((flags & FNM_FILE_NAME) && *n == '/') ||
91 (c == '?' && *n == '\0'))
92 return FNM_NOMATCH;
94 if (c == '\0')
95 return 0;
98 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
99 c1 = FOLD (c1);
100 for (--p; *n != '\0'; ++n)
101 if ((c == '[' || FOLD (*n) == c1) &&
102 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
103 return 0;
104 return FNM_NOMATCH;
107 case '[':
109 /* Nonzero if the sense of the character class is inverted. */
110 register int not;
112 if (*n == '\0')
113 return FNM_NOMATCH;
115 if ((flags & FNM_PERIOD) && *n == '.' &&
116 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
117 return FNM_NOMATCH;
119 not = (*p == '!' || *p == '^');
120 if (not)
121 ++p;
123 c = *p++;
124 for (;;)
126 register char cstart = c, cend = c;
128 if (!(flags & FNM_NOESCAPE) && c == '\\')
129 cstart = cend = *p++;
131 cstart = cend = FOLD (cstart);
133 if (c == '\0')
134 /* [ (unterminated) loses. */
135 return FNM_NOMATCH;
137 c = *p++;
138 c = FOLD (c);
140 if ((flags & FNM_FILE_NAME) && c == '/')
141 /* [/] can never match. */
142 return FNM_NOMATCH;
144 if (c == '-' && *p != ']')
146 cend = *p++;
147 if (!(flags & FNM_NOESCAPE) && cend == '\\')
148 cend = *p++;
149 if (cend == '\0')
150 return FNM_NOMATCH;
151 cend = FOLD (cend);
153 c = *p++;
156 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
157 goto matched;
159 if (c == ']')
160 break;
162 if (!not)
163 return FNM_NOMATCH;
164 break;
166 matched:;
167 /* Skip the rest of the [...] that already matched. */
168 while (c != ']')
170 if (c == '\0')
171 /* [... (unterminated) loses. */
172 return FNM_NOMATCH;
174 c = *p++;
175 if (!(flags & FNM_NOESCAPE) && c == '\\')
176 /* XXX 1003.2d11 is unclear if this is right. */
177 ++p;
179 if (not)
180 return FNM_NOMATCH;
182 break;
184 default:
185 if (c != FOLD (*n))
186 return FNM_NOMATCH;
189 ++n;
192 if (*n == '\0')
193 return 0;
195 if ((flags & FNM_LEADING_DIR) && *n == '/')
196 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
197 return 0;
199 return FNM_NOMATCH;
202 #endif /* _LIBC or not __GNU_LIBRARY__. */