]> Pileus Git - ~andy/gtk/blob - gtk/fnmatch.c
Added check for inline in configure and updated files to use it.
[~andy/gtk] / gtk / fnmatch.c
1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
2
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.
7
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.
12
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.  */
17
18 #ifdef HAVE_CONFIG_H
19 #include "../config.h"
20 #endif
21
22 #include <errno.h>
23
24 /* Added for GTK. We need to make sure that all constants are defined
25  * to properly compile this file */
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE
28 #endif
29 #include "fnmatch.h"
30
31 #include <ctype.h>
32
33
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.  */
41
42 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
43
44
45 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
46 extern int errno;
47 #endif
48
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;
56 {
57   register const char *p = pattern, *n = string;
58   register char c;
59
60 /* Note that this evalutes C many times.  */
61 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
62
63   while ((c = *p++) != '\0')
64     {
65       c = FOLD (c);
66
67       switch (c)
68         {
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;
78
79         case '\\':
80           if (!(flags & FNM_NOESCAPE))
81             {
82               c = *p++;
83               c = FOLD (c);
84             }
85           if (FOLD (*n) != c)
86             return FNM_NOMATCH;
87           break;
88
89         case '*':
90           if ((flags & FNM_PERIOD) && *n == '.' &&
91               (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
92             return FNM_NOMATCH;
93
94           for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
95             if (((flags & FNM_FILE_NAME) && *n == '/') ||
96                 (c == '?' && *n == '\0'))
97               return FNM_NOMATCH;
98
99           if (c == '\0')
100             return 0;
101
102           {
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;
110           }
111
112         case '[':
113           {
114             /* Nonzero if the sense of the character class is inverted.  */
115             register int not;
116
117             if (*n == '\0')
118               return FNM_NOMATCH;
119
120             if ((flags & FNM_PERIOD) && *n == '.' &&
121                 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
122               return FNM_NOMATCH;
123
124             not = (*p == '!' || *p == '^');
125             if (not)
126               ++p;
127
128             c = *p++;
129             for (;;)
130               {
131                 register char cstart = c, cend = c;
132
133                 if (!(flags & FNM_NOESCAPE) && c == '\\')
134                   cstart = cend = *p++;
135
136                 cstart = cend = FOLD (cstart);
137
138                 if (c == '\0')
139                   /* [ (unterminated) loses.  */
140                   return FNM_NOMATCH;
141
142                 c = *p++;
143                 c = FOLD (c);
144
145                 if ((flags & FNM_FILE_NAME) && c == '/')
146                   /* [/] can never match.  */
147                   return FNM_NOMATCH;
148
149                 if (c == '-' && *p != ']')
150                   {
151                     cend = *p++;
152                     if (!(flags & FNM_NOESCAPE) && cend == '\\')
153                       cend = *p++;
154                     if (cend == '\0')
155                       return FNM_NOMATCH;
156                     cend = FOLD (cend);
157
158                     c = *p++;
159                   }
160
161                 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
162                   goto matched;
163
164                 if (c == ']')
165                   break;
166               }
167             if (!not)
168               return FNM_NOMATCH;
169             break;
170
171           matched:;
172             /* Skip the rest of the [...] that already matched.  */
173             while (c != ']')
174               {
175                 if (c == '\0')
176                   /* [... (unterminated) loses.  */
177                   return FNM_NOMATCH;
178
179                 c = *p++;
180                 if (!(flags & FNM_NOESCAPE) && c == '\\')
181                   /* XXX 1003.2d11 is unclear if this is right.  */
182                   ++p;
183               }
184             if (not)
185               return FNM_NOMATCH;
186           }
187           break;
188
189         default:
190           if (c != FOLD (*n))
191             return FNM_NOMATCH;
192         }
193
194       ++n;
195     }
196
197   if (*n == '\0')
198     return 0;
199
200   if ((flags & FNM_LEADING_DIR) && *n == '/')
201     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
202     return 0;
203
204   return FNM_NOMATCH;
205 }
206
207 #endif  /* _LIBC or not __GNU_LIBRARY__.  */