Add node Usage Measurements
[glibc/history.git] / posix / fnmatch.c
blob6926633dcf7c7d530f6929f3ea530860c22434d5
1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
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.
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.
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. */
18 #include <errno.h>
19 #include <fnmatch.h>
21 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
22 extern int errno;
23 #endif
25 /* Match STRING against the filename pattern PATTERN, returning zero if
26 it matches, nonzero if not. */
27 int
28 fnmatch (pattern, string, flags)
29 const char *pattern;
30 const char *string;
31 int flags;
33 register const char *p = pattern, *n = string;
34 register char c;
36 if ((flags & ~__FNM_FLAGS) != 0)
38 errno = EINVAL;
39 return -1;
42 while ((c = *p++) != '\0')
44 switch (c)
46 case '?':
47 if (*n == '\0')
48 return FNM_NOMATCH;
49 else if ((flags & FNM_PATHNAME) && *n == '/')
50 return FNM_NOMATCH;
51 else if ((flags & FNM_PERIOD) && *n == '.' &&
52 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
53 return FNM_NOMATCH;
54 break;
56 case '\\':
57 if (!(flags & FNM_NOESCAPE))
58 c = *p++;
59 if (*n != c)
60 return FNM_NOMATCH;
61 break;
63 case '*':
64 if ((flags & FNM_PERIOD) && *n == '.' &&
65 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
66 return FNM_NOMATCH;
68 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
69 if (((flags & FNM_PATHNAME) && *n == '/') ||
70 (c == '?' && *n == '\0'))
71 return FNM_NOMATCH;
73 if (c == '\0')
74 return 0;
77 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
78 for (--p; *n != '\0'; ++n)
79 if ((c == '[' || *n == c1) &&
80 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
81 return 0;
82 return FNM_NOMATCH;
85 case '[':
87 /* Nonzero if the sense of the character class is inverted. */
88 register int not;
90 if (*n == '\0')
91 return FNM_NOMATCH;
93 if ((flags & FNM_PERIOD) && *n == '.' &&
94 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
95 return FNM_NOMATCH;
97 not = (*p == '!' || *p == '^');
98 if (not)
99 ++p;
101 c = *p++;
102 for (;;)
104 register char cstart = c, cend = c;
106 if (!(flags & FNM_NOESCAPE) && c == '\\')
107 cstart = cend = *p++;
109 if (c == '\0')
110 /* [ (unterminated) loses. */
111 return FNM_NOMATCH;
113 c = *p++;
115 if ((flags & FNM_PATHNAME) && c == '/')
116 /* [/] can never match. */
117 return FNM_NOMATCH;
119 if (c == '-' && *p != ']')
121 cend = *p++;
122 if (!(flags & FNM_NOESCAPE) && cend == '\\')
123 cend = *p++;
124 if (cend == '\0')
125 return FNM_NOMATCH;
126 c = *p++;
129 if (*n >= cstart && *n <= cend)
130 goto matched;
132 if (c == ']')
133 break;
135 if (!not)
136 return FNM_NOMATCH;
137 break;
139 matched:;
140 /* Skip the rest of the [...] that already matched. */
141 while (c != ']')
143 if (c == '\0')
144 /* [... (unterminated) loses. */
145 return FNM_NOMATCH;
147 c = *p++;
148 if (!(flags & FNM_NOESCAPE) && c == '\\')
149 /* 1003.2d11 is unclear if this is right. %%% */
150 ++p;
152 if (not)
153 return FNM_NOMATCH;
155 break;
157 default:
158 if (c != *n)
159 return FNM_NOMATCH;
162 ++n;
165 if (*n == '\0')
166 return 0;
168 return FNM_NOMATCH;