* sysdeps/sparc/elf/start.S: New file.
[platform/upstream/glibc.git] / posix / fnmatch.c
1 /* Copyright (C) 1991, 1992, 1993, 1996 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 /* Enable GNU extensions in fnmatch.h.  */
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE     1
25 #endif
26
27 #include <errno.h>
28 #include <fnmatch.h>
29 #include <ctype.h>
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 (_LIBC) || !defined (__GNU_LIBRARY__)
41
42
43 #ifndef errno
44 extern int errno;
45 #endif
46
47 /* Match STRING against the filename pattern PATTERN, returning zero if
48    it matches, nonzero if not.  */
49 int
50 fnmatch (pattern, string, flags)
51      const char *pattern;
52      const char *string;
53      int flags;
54 {
55   register const char *p = pattern, *n = string;
56   register char c;
57
58 /* Note that this evalutes C many times.  */
59 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
60
61   while ((c = *p++) != '\0')
62     {
63       c = FOLD (c);
64
65       switch (c)
66         {
67         case '?':
68           if (*n == '\0')
69             return FNM_NOMATCH;
70           else if ((flags & FNM_FILE_NAME) && *n == '/')
71             return FNM_NOMATCH;
72           else if ((flags & FNM_PERIOD) && *n == '.' &&
73                    (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
74             return FNM_NOMATCH;
75           break;
76
77         case '\\':
78           if (!(flags & FNM_NOESCAPE))
79             {
80               c = *p++;
81               if (c == '\0')
82                 /* Trailing \ loses.  */
83                 return FNM_NOMATCH;
84               c = FOLD (c);
85             }
86           if (FOLD (*n) != c)
87             return FNM_NOMATCH;
88           break;
89
90         case '*':
91           if ((flags & FNM_PERIOD) && *n == '.' &&
92               (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
93             return FNM_NOMATCH;
94
95           for (c = *p++; c == '?' || c == '*'; c = *p++)
96             {
97               if ((flags & FNM_FILE_NAME) && *n == '/')
98                 /* A slash does not match a wildcard under FNM_FILE_NAME.  */
99                 return FNM_NOMATCH;
100               else if (c == '?')
101                 {
102                   /* A ? needs to match one character.  */
103                   if (*n == '\0')
104                     /* There isn't another character; no match.  */
105                     return FNM_NOMATCH;
106                   else
107                     /* One character of the string is consumed in matching
108                        this ? wildcard, so *??? won't match if there are
109                        less than three characters.  */
110                     ++n;
111                 }
112             }
113
114           if (c == '\0')
115             return 0;
116
117           {
118             char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
119             c1 = FOLD (c1);
120             for (--p; *n != '\0'; ++n)
121               if ((c == '[' || FOLD (*n) == c1) &&
122                   fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
123                 return 0;
124             return FNM_NOMATCH;
125           }
126
127         case '[':
128           {
129             /* Nonzero if the sense of the character class is inverted.  */
130             register int not;
131
132             if (*n == '\0')
133               return FNM_NOMATCH;
134
135             if ((flags & FNM_PERIOD) && *n == '.' &&
136                 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
137               return FNM_NOMATCH;
138
139             not = (*p == '!' || *p == '^');
140             if (not)
141               ++p;
142
143             c = *p++;
144             for (;;)
145               {
146                 register char cstart = c, cend = c;
147
148                 if (!(flags & FNM_NOESCAPE) && c == '\\')
149                   {
150                     if (*p == '\0')
151                       return FNM_NOMATCH;
152                     cstart = cend = *p++;
153                   }
154
155                 cstart = cend = FOLD (cstart);
156
157                 if (c == '\0')
158                   /* [ (unterminated) loses.  */
159                   return FNM_NOMATCH;
160
161                 c = *p++;
162                 c = FOLD (c);
163
164                 if ((flags & FNM_FILE_NAME) && c == '/')
165                   /* [/] can never match.  */
166                   return FNM_NOMATCH;
167
168                 if (c == '-' && *p != ']')
169                   {
170                     cend = *p++;
171                     if (!(flags & FNM_NOESCAPE) && cend == '\\')
172                       cend = *p++;
173                     if (cend == '\0')
174                       return FNM_NOMATCH;
175                     cend = FOLD (cend);
176
177                     c = *p++;
178                   }
179
180                 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
181                   goto matched;
182
183                 if (c == ']')
184                   break;
185               }
186             if (!not)
187               return FNM_NOMATCH;
188             break;
189
190           matched:;
191             /* Skip the rest of the [...] that already matched.  */
192             while (c != ']')
193               {
194                 if (c == '\0')
195                   /* [... (unterminated) loses.  */
196                   return FNM_NOMATCH;
197
198                 c = *p++;
199                 if (!(flags & FNM_NOESCAPE) && c == '\\')
200                   {
201                     if (*p == '\0')
202                       return FNM_NOMATCH;
203                     /* XXX 1003.2d11 is unclear if this is right.  */
204                     ++p;
205                   }
206               }
207             if (not)
208               return FNM_NOMATCH;
209           }
210           break;
211
212         default:
213           if (c != FOLD (*n))
214             return FNM_NOMATCH;
215         }
216
217       ++n;
218     }
219
220   if (*n == '\0')
221     return 0;
222
223   if ((flags & FNM_LEADING_DIR) && *n == '/')
224     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
225     return 0;
226
227   return FNM_NOMATCH;
228 }
229
230 #endif  /* _LIBC or not __GNU_LIBRARY__.  */