* io/Makefile (routines): Add mknod, xstat fxstat lxstat xmknod.
[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               c = FOLD (c);
82             }
83           if (FOLD (*n) != c)
84             return FNM_NOMATCH;
85           break;
86
87         case '*':
88           if ((flags & FNM_PERIOD) && *n == '.' &&
89               (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
90             return FNM_NOMATCH;
91
92           for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
93             if (((flags & FNM_FILE_NAME) && *n == '/') ||
94                 (c == '?' && *n == '\0'))
95               return FNM_NOMATCH;
96
97           if (c == '\0')
98             return 0;
99
100           {
101             char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
102             c1 = FOLD (c1);
103             for (--p; *n != '\0'; ++n)
104               if ((c == '[' || FOLD (*n) == c1) &&
105                   fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
106                 return 0;
107             return FNM_NOMATCH;
108           }
109
110         case '[':
111           {
112             /* Nonzero if the sense of the character class is inverted.  */
113             register int not;
114
115             if (*n == '\0')
116               return FNM_NOMATCH;
117
118             if ((flags & FNM_PERIOD) && *n == '.' &&
119                 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
120               return FNM_NOMATCH;
121
122             not = (*p == '!' || *p == '^');
123             if (not)
124               ++p;
125
126             c = *p++;
127             for (;;)
128               {
129                 register char cstart = c, cend = c;
130
131                 if (!(flags & FNM_NOESCAPE) && c == '\\')
132                   cstart = cend = *p++;
133
134                 cstart = cend = FOLD (cstart);
135
136                 if (c == '\0')
137                   /* [ (unterminated) loses.  */
138                   return FNM_NOMATCH;
139
140                 c = *p++;
141                 c = FOLD (c);
142
143                 if ((flags & FNM_FILE_NAME) && c == '/')
144                   /* [/] can never match.  */
145                   return FNM_NOMATCH;
146
147                 if (c == '-' && *p != ']')
148                   {
149                     cend = *p++;
150                     if (!(flags & FNM_NOESCAPE) && cend == '\\')
151                       cend = *p++;
152                     if (cend == '\0')
153                       return FNM_NOMATCH;
154                     cend = FOLD (cend);
155
156                     c = *p++;
157                   }
158
159                 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
160                   goto matched;
161
162                 if (c == ']')
163                   break;
164               }
165             if (!not)
166               return FNM_NOMATCH;
167             break;
168
169           matched:;
170             /* Skip the rest of the [...] that already matched.  */
171             while (c != ']')
172               {
173                 if (c == '\0')
174                   /* [... (unterminated) loses.  */
175                   return FNM_NOMATCH;
176
177                 c = *p++;
178                 if (!(flags & FNM_NOESCAPE) && c == '\\')
179                   /* XXX 1003.2d11 is unclear if this is right.  */
180                   ++p;
181               }
182             if (not)
183               return FNM_NOMATCH;
184           }
185           break;
186
187         default:
188           if (c != FOLD (*n))
189             return FNM_NOMATCH;
190         }
191
192       ++n;
193     }
194
195   if (*n == '\0')
196     return 0;
197
198   if ((flags & FNM_LEADING_DIR) && *n == '/')
199     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
200     return 0;
201
202   return FNM_NOMATCH;
203 }
204
205 #endif  /* _LIBC or not __GNU_LIBRARY__.  */