define INADDR_NONE if necessary
[rsync/rsync.git] / lib / fnmatch.c
CommitLineData
c627d613
AT
1#include "../rsync.h"
2#ifndef HAVE_FNMATCH
3
4/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
5
6NOTE: The canonical source of this file is maintained with the GNU C Library.
7Bugs can be reported to bug-glibc@prep.ai.mit.edu.
8
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23#if defined (STDC_HEADERS) || !defined (isascii)
24#define ISASCII(c) 1
25#else
26#define ISASCII(c) isascii(c)
27#endif
28
29#define ISUPPER(c) (ISASCII (c) && isupper (c))
30
31
32/* Comment out all this code if we are using the GNU C Library, and are not
33 actually compiling the library itself. This code is part of the GNU C
34 Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand `configure --with-gnu-libc' and omit the object files,
38 it is simpler to just do this in the source for each such file. */
39
40#if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
41extern int errno;
42#endif
43
44/* Match STRING against the filename pattern PATTERN, returning zero if
45 it matches, nonzero if not. */
46int
47fnmatch (pattern, string, flags)
48 const char *pattern;
49 const char *string;
50 int flags;
51{
52 register const char *p = pattern, *n = string;
53 register char c;
54
55/* Note that this evalutes C many times. */
56#define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
57
58 while ((c = *p++) != '\0')
59 {
60 c = FOLD (c);
61
62 switch (c)
63 {
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;
73
74 case '\\':
75 if (!(flags & FNM_NOESCAPE))
76 {
77 c = *p++;
78 c = FOLD (c);
79 }
80 if (FOLD (*n) != c)
81 return FNM_NOMATCH;
82 break;
83
84 case '*':
85 if ((flags & FNM_PERIOD) && *n == '.' &&
86 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
87 return FNM_NOMATCH;
88
89 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
90 if (((flags & FNM_FILE_NAME) && *n == '/') ||
91 (c == '?' && *n == '\0'))
92 return FNM_NOMATCH;
93
94 if (c == '\0')
95 return 0;
96
97 {
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;
105 }
106
107 case '[':
108 {
109 /* Nonzero if the sense of the character class is inverted. */
110 register int not;
111
112 if (*n == '\0')
113 return FNM_NOMATCH;
114
115 if ((flags & FNM_PERIOD) && *n == '.' &&
116 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
117 return FNM_NOMATCH;
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 cstart = cend = FOLD (cstart);
132
133 if (c == '\0')
134 /* [ (unterminated) loses. */
135 return FNM_NOMATCH;
136
137 c = *p++;
138 c = FOLD (c);
139
140 if ((flags & FNM_FILE_NAME) && c == '/')
141 /* [/] can never match. */
142 return FNM_NOMATCH;
143
144 if (c == '-' && *p != ']')
145 {
146 cend = *p++;
147 if (!(flags & FNM_NOESCAPE) && cend == '\\')
148 cend = *p++;
149 if (cend == '\0')
150 return FNM_NOMATCH;
151 cend = FOLD (cend);
152
153 c = *p++;
154 }
155
156 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
157 goto matched;
158
159 if (c == ']')
160 break;
161 }
162 if (!not)
163 return FNM_NOMATCH;
164 break;
165
166 matched:;
167 /* Skip the rest of the [...] that already matched. */
168 while (c != ']')
169 {
170 if (c == '\0')
171 /* [... (unterminated) loses. */
172 return FNM_NOMATCH;
173
174 c = *p++;
175 if (!(flags & FNM_NOESCAPE) && c == '\\')
176 /* XXX 1003.2d11 is unclear if this is right. */
177 ++p;
178 }
179 if (not)
180 return FNM_NOMATCH;
181 }
182 break;
183
184 default:
185 if (c != FOLD (*n))
186 return FNM_NOMATCH;
187 }
188
189 ++n;
190 }
191
192 if (*n == '\0')
193 return 0;
194
195 if ((flags & FNM_LEADING_DIR) && *n == '/')
196 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
197 return 0;
198
199 return FNM_NOMATCH;
200}
201
202#else /* HAVE_FNMATCH */
203void fnmatch_dummy(void) {}
204#endif