]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/glob/fnmatch.c
0763609021538f07dc862f60bd42853dd8bc3373
[thirdparty/bash.git] / lib / glob / fnmatch.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
18
19 #include <errno.h>
20 #include "fnmatch.h"
21
22 #if !defined (__GNU_LIBRARY__) && !defined (STDC_HEADERS)
23 # if !defined (errno)
24 extern int errno;
25 # endif /* !errno */
26 #endif
27
28 /* Match STRING against the filename pattern PATTERN, returning zero if
29 it matches, FNM_NOMATCH if not. */
30 int
31 fnmatch (pattern, string, flags)
32 char *pattern;
33 char *string;
34 int flags;
35 {
36 register char *p = pattern, *n = string;
37 register char c;
38
39 if ((flags & ~__FNM_FLAGS) != 0)
40 {
41 errno = EINVAL;
42 return (-1);
43 }
44
45 while ((c = *p++) != '\0')
46 {
47 switch (c)
48 {
49 case '?':
50 if (*n == '\0')
51 return (FNM_NOMATCH);
52 else if ((flags & FNM_PATHNAME) && *n == '/')
53 return (FNM_NOMATCH);
54 else if ((flags & FNM_PERIOD) && *n == '.' &&
55 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
56 return (FNM_NOMATCH);
57 break;
58
59 case '\\':
60 if (!(flags & FNM_NOESCAPE))
61 c = *p++;
62 if (*n != c)
63 return (FNM_NOMATCH);
64 break;
65
66 case '*':
67 if ((flags & FNM_PERIOD) && *n == '.' &&
68 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
69 return (FNM_NOMATCH);
70
71 for (c = *p++; c == '?' || c == '*'; c = *p++)
72 {
73 if (((flags & FNM_PATHNAME) && *n == '/') ||
74 (c == '?' && *n == '\0'))
75 return (FNM_NOMATCH);
76 if (c == '?')
77 n++;
78 }
79
80 if (c == '\0')
81 return (0);
82
83 {
84 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
85 for (--p; *n != '\0'; ++n)
86 if ((c == '[' || *n == c1) &&
87 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
88 return (0);
89 return (FNM_NOMATCH);
90 }
91
92 case '[':
93 {
94 /* Nonzero if the sense of the character class is inverted. */
95 register int not;
96
97 if (*n == '\0')
98 return (FNM_NOMATCH);
99
100 if ((flags & FNM_PERIOD) && *n == '.' &&
101 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
102 return (FNM_NOMATCH);
103
104 /* Make sure there is a closing `]'. If there isn't, the `['
105 is just a character to be matched. */
106 {
107 register char *np;
108
109 for (np = p; np && *np && *np != ']'; np++);
110
111 if (np && !*np)
112 {
113 if (*n != '[')
114 return (FNM_NOMATCH);
115 goto next_char;
116 }
117 }
118
119 not = (*p == '!' || *p == '^');
120 if (not)
121 ++p;
122
123 c = *p++;
124 for (;;)
125 {
126 register char cstart = c, cend = c;
127
128 if (!(flags & FNM_NOESCAPE) && c == '\\')
129 cstart = cend = *p++;
130
131 if (c == '\0')
132 /* [ (unterminated) loses. */
133 return (FNM_NOMATCH);
134
135 c = *p++;
136
137 if ((flags & FNM_PATHNAME) && c == '/')
138 /* [/] can never match. */
139 return (FNM_NOMATCH);
140
141 if (c == '-' && *p != ']')
142 {
143 cend = *p++;
144 if (!(flags & FNM_NOESCAPE) && cend == '\\')
145 cend = *p++;
146 if (cend == '\0')
147 return (FNM_NOMATCH);
148 c = *p++;
149 }
150
151 if (*n >= cstart && *n <= cend)
152 goto matched;
153
154 if (c == ']')
155 break;
156 }
157 if (!not)
158 return (FNM_NOMATCH);
159
160 next_char:
161 break;
162
163 matched:
164 /* Skip the rest of the [...] that already matched. */
165 while (c != ']')
166 {
167 if (c == '\0')
168 /* [... (unterminated) loses. */
169 return (FNM_NOMATCH);
170
171 c = *p++;
172 if (!(flags & FNM_NOESCAPE) && c == '\\')
173 /* 1003.2d11 is unclear if this is right. %%% */
174 ++p;
175 }
176 if (not)
177 return (FNM_NOMATCH);
178 }
179 break;
180
181 default:
182 if (c != *n)
183 return (FNM_NOMATCH);
184 }
185
186 ++n;
187 }
188
189 if (*n == '\0')
190 return (0);
191
192 return (FNM_NOMATCH);
193 }