Added config.h, miscfn.h header files
[platform/upstream/rpm.git] / misc / 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 #include <errno.h>
19 #include <ctype.h>
20
21 #include "miscfn.h"
22
23
24 /* Comment out all this code if we are using the GNU C Library, and are not
25    actually compiling the library itself.  This code is part of the GNU C
26    Library, but also included in many other GNU distributions.  Compiling
27    and linking in this code is a waste when using the GNU C library
28    (especially if it is a shared library).  Rather than having every GNU
29    program understand `configure --with-gnu-libc' and omit the object files,
30    it is simpler to just do this in the source for each such file.  */
31
32 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
33
34
35 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
36 extern int errno;
37 #endif
38
39 /* Match STRING against the filename pattern PATTERN, returning zero if
40    it matches, nonzero if not.  */
41 int
42 fnmatch (pattern, string, flags)
43      const char *pattern;
44      const char *string;
45      int flags;
46 {
47   register const char *p = pattern, *n = string;
48   register char c;
49
50 /* Note that this evalutes C many times.  */
51 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
52
53   while ((c = *p++) != '\0')
54     {
55       c = FOLD (c);
56
57       switch (c)
58         {
59         case '?':
60           if (*n == '\0')
61             return FNM_NOMATCH;
62           else if ((flags & FNM_FILE_NAME) && *n == '/')
63             return FNM_NOMATCH;
64           else if ((flags & FNM_PERIOD) && *n == '.' &&
65                    (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
66             return FNM_NOMATCH;
67           break;
68
69         case '\\':
70           if (!(flags & FNM_NOESCAPE))
71             {
72               c = *p++;
73               c = FOLD (c);
74             }
75           if (FOLD (*n) != c)
76             return FNM_NOMATCH;
77           break;
78
79         case '*':
80           if ((flags & FNM_PERIOD) && *n == '.' &&
81               (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
82             return FNM_NOMATCH;
83
84           for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
85             if (((flags & FNM_FILE_NAME) && *n == '/') ||
86                 (c == '?' && *n == '\0'))
87               return FNM_NOMATCH;
88
89           if (c == '\0')
90             return 0;
91
92           {
93             char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
94             c1 = FOLD (c1);
95             for (--p; *n != '\0'; ++n)
96               if ((c == '[' || FOLD (*n) == c1) &&
97                   fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
98                 return 0;
99             return FNM_NOMATCH;
100           }
101
102         case '[':
103           {
104             /* Nonzero if the sense of the character class is inverted.  */
105             register int not;
106
107             if (*n == '\0')
108               return FNM_NOMATCH;
109
110             if ((flags & FNM_PERIOD) && *n == '.' &&
111                 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
112               return FNM_NOMATCH;
113
114             not = (*p == '!' || *p == '^');
115             if (not)
116               ++p;
117
118             c = *p++;
119             for (;;)
120               {
121                 register char cstart = c, cend = c;
122
123                 if (!(flags & FNM_NOESCAPE) && c == '\\')
124                   cstart = cend = *p++;
125
126                 cstart = cend = FOLD (cstart);
127
128                 if (c == '\0')
129                   /* [ (unterminated) loses.  */
130                   return FNM_NOMATCH;
131
132                 c = *p++;
133                 c = FOLD (c);
134
135                 if ((flags & FNM_FILE_NAME) && c == '/')
136                   /* [/] can never match.  */
137                   return FNM_NOMATCH;
138
139                 if (c == '-' && *p != ']')
140                   {
141                     cend = *p++;
142                     if (!(flags & FNM_NOESCAPE) && cend == '\\')
143                       cend = *p++;
144                     if (cend == '\0')
145                       return FNM_NOMATCH;
146                     cend = FOLD (cend);
147
148                     c = *p++;
149                   }
150
151                 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
152                   goto matched;
153
154                 if (c == ']')
155                   break;
156               }
157             if (!not)
158               return FNM_NOMATCH;
159             break;
160
161           matched:;
162             /* Skip the rest of the [...] that already matched.  */
163             while (c != ']')
164               {
165                 if (c == '\0')
166                   /* [... (unterminated) loses.  */
167                   return FNM_NOMATCH;
168
169                 c = *p++;
170                 if (!(flags & FNM_NOESCAPE) && c == '\\')
171                   /* XXX 1003.2d11 is unclear if this is right.  */
172                   ++p;
173               }
174             if (not)
175               return FNM_NOMATCH;
176           }
177           break;
178
179         default:
180           if (c != FOLD (*n))
181             return FNM_NOMATCH;
182         }
183
184       ++n;
185     }
186
187   if (*n == '\0')
188     return 0;
189
190   if ((flags & FNM_LEADING_DIR) && *n == '/')
191     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
192     return 0;
193
194   return FNM_NOMATCH;
195 }
196
197 #endif  /* _LIBC or not __GNU_LIBRARY__.  */