1
/* Copyright (C) 1991-1993, 1996-1999, 2000 Free Software Foundation, Inc.
2
This file is part of the GNU C Library.
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public License as
6
published by the Free Software Foundation; either version 2 of the
7
License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public
15
License along with this library; see the file COPYING.LIB. If not,
16
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA. */
21
/* Find the first occurrence of C in S or the final NUL byte. */
27
const unsigned char *char_ptr;
28
const unsigned long int *longword_ptr;
29
unsigned long int longword, magic_bits, charmask;
31
c = (unsigned char) c;
33
/* Handle the first few characters by reading one character at a time.
34
Do this until CHAR_PTR is aligned on a longword boundary. */
35
for (char_ptr = s; ((unsigned long int) char_ptr
36
& (sizeof (longword) - 1)) != 0;
38
if (*char_ptr == c || *char_ptr == '\0')
39
return (void *) char_ptr;
41
/* All these elucidatory comments refer to 4-byte longwords,
42
but the theory applies equally well to 8-byte longwords. */
44
longword_ptr = (unsigned long int *) char_ptr;
46
/* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
47
the "holes." Note that there is a hole just to the left of
48
each byte, with an extra at the end:
50
bits: 01111110 11111110 11111110 11111111
51
bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
53
The 1-bits make sure that carries propagate to the next 0-bit.
54
The 0-bits provide holes for carries to fall into. */
55
switch (sizeof (longword))
57
case 4: magic_bits = 0x7efefeffL; break;
58
case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break;
63
/* Set up a longword, each of whose bytes is C. */
64
charmask = c | (c << 8);
65
charmask |= charmask << 16;
66
if (sizeof (longword) > 4)
67
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
68
charmask |= (charmask << 16) << 16;
69
if (sizeof (longword) > 8)
72
/* Instead of the traditional loop which tests each character,
73
we will test a longword at a time. The tricky part is testing
74
if *any of the four* bytes in the longword in question are zero. */
77
/* We tentatively exit the loop if adding MAGIC_BITS to
78
LONGWORD fails to change any of the hole bits of LONGWORD.
80
1) Is this safe? Will it catch all the zero bytes?
81
Suppose there is a byte with all zeros. Any carry bits
82
propagating from its left will fall into the hole at its
83
least significant bit and stop. Since there will be no
84
carry from its most significant bit, the LSB of the
85
byte to the left will be unchanged, and the zero will be
88
2) Is this worthwhile? Will it ignore everything except
89
zero bytes? Suppose every byte of LONGWORD has a bit set
90
somewhere. There will be a carry into bit 8. If bit 8
91
is set, this will carry into bit 16. If bit 8 is clear,
92
one of bits 9-15 must be set, so there will be a carry
93
into bit 16. Similarly, there will be a carry into bit
94
24. If one of bits 24-30 is set, there will be a carry
95
into bit 31, so all of the hole bits will be changed.
97
The one misfire occurs when bits 24-30 are clear and bit
98
31 is set; in this case, the hole at bit 31 is not
99
changed. If we had access to the processor carry flag,
100
we could close this loophole by putting the fourth hole
103
So it ignores everything except 128's, when they're aligned
106
3) But wait! Aren't we looking for C as well as zero?
107
Good point. So what we do is XOR LONGWORD with a longword,
108
each of whose bytes is C. This turns each byte that is C
111
longword = *longword_ptr++;
113
/* Add MAGIC_BITS to LONGWORD. */
114
if ((((longword + magic_bits)
116
/* Set those bits that were unchanged by the addition. */
119
/* Look at only the hole bits. If any of the hole bits
120
are unchanged, most likely one of the bytes was a
122
& ~magic_bits) != 0 ||
124
/* That caught zeroes. Now test for C. */
125
((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
128
/* Which of the bytes was C or zero?
129
If none of them were, it was a misfire; continue the search. */
131
const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
133
if (*cp == c || *cp == '\0')
135
if (*++cp == c || *cp == '\0')
137
if (*++cp == c || *cp == '\0')
139
if (*++cp == c || *cp == '\0')
141
if (sizeof (longword) > 4)
143
if (*++cp == c || *cp == '\0')
145
if (*++cp == c || *cp == '\0')
147
if (*++cp == c || *cp == '\0')
149
if (*++cp == c || *cp == '\0')
155
/* This should never happen. */
159
/* For platform which support the ISO C amendement 1 functionality we
160
support user defined character classes. */
161
#if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
162
/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
167
/* Comment out all this code if we are using the GNU C Library, and are not
168
actually compiling the library itself. This code is part of the GNU C
169
Library, but also included in many other GNU distributions. Compiling
170
and linking in this code is a waste when using the GNU C library
171
(especially if it is a shared library). Rather than having every GNU
172
program understand `configure --with-gnu-libc' and omit the object files,
173
it is simpler to just do this in the source for each such file. */
175
#if defined _LIBC || !defined __GNU_LIBRARY__
178
# if defined STDC_HEADERS || !defined isascii
179
# define ISASCII(c) 1
181
# define ISASCII(c) isascii(c)
185
# define ISBLANK(c) (ISASCII (c) && isblank (c))
187
# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
190
# define ISGRAPH(c) (ISASCII (c) && isgraph (c))
192
# define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
195
#define ISPRINT(c) (ISASCII (c) && isprint (c))
196
#define ISDIGIT(c) (ISASCII (c) && isdigit (c))
197
#define ISALNUM(c) (ISASCII (c) && isalnum (c))
198
#define ISALPHA(c) (ISASCII (c) && isalpha (c))
199
#define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
200
#define ISLOWER(c) (ISASCII (c) && islower (c))
201
#define ISPUNCT(c) (ISASCII (c) && ispunct (c))
202
#define ISSPACE(c) (ISASCII (c) && isspace (c))
203
#define ISUPPER(c) (ISASCII (c) && isupper (c))
204
#define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
206
# define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
208
# if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
209
/* The GNU C library provides support for user-defined character classes
210
and the functions from ISO C amendement 1. */
211
# ifdef CHARCLASS_NAME_MAX
212
# define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
214
/* This shouldn't happen but some implementation might still have this
215
problem. Use a reasonable default value. */
216
# define CHAR_CLASS_MAX_LENGTH 256
220
# define IS_CHAR_CLASS(string) __wctype (string)
222
# define IS_CHAR_CLASS(string) wctype (string)
225
# define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
227
# define IS_CHAR_CLASS(string) \
228
(STREQ (string, "alpha") || STREQ (string, "upper") \
229
|| STREQ (string, "lower") || STREQ (string, "digit") \
230
|| STREQ (string, "alnum") || STREQ (string, "xdigit") \
231
|| STREQ (string, "space") || STREQ (string, "print") \
232
|| STREQ (string, "punct") || STREQ (string, "graph") \
233
|| STREQ (string, "cntrl") || STREQ (string, "blank"))
236
/* Avoid depending on library functions or files
237
whose names are inconsistent. */
239
# if !defined _LIBC && !defined getenv
240
extern char *getenv ();
247
/* Match STRING against the filename pattern PATTERN, returning zero if
248
it matches, nonzero if not. */
253
internal_fnmatch (const char *pattern, const char *string,
254
int no_leading_period, int flags)
256
register const char *p = pattern, *n = string;
257
register unsigned char c;
259
/* Note that this evaluates C many times. */
261
# define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
263
# define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
266
while ((c = *p++) != '\0')
275
else if (*n == '/' && (flags & FNM_FILE_NAME))
277
else if (*n == '.' && no_leading_period
279
|| (n[-1] == '/' && (flags & FNM_FILE_NAME))))
284
if (!(flags & FNM_NOESCAPE))
288
/* Trailing \ loses. */
292
if (FOLD ((unsigned char) *n) != c)
297
if (*n == '.' && no_leading_period
299
|| (n[-1] == '/' && (flags & FNM_FILE_NAME))))
302
for (c = *p++; c == '?' || c == '*'; c = *p++)
304
if (*n == '/' && (flags & FNM_FILE_NAME))
305
/* A slash does not match a wildcard under FNM_FILE_NAME. */
309
/* A ? needs to match one character. */
311
/* There isn't another character; no match. */
314
/* One character of the string is consumed in matching
315
this ? wildcard, so *??? won't match if there are
316
less than three characters. */
322
/* The wildcard(s) is/are the last element of the pattern.
323
If the name is a file name and contains another slash
324
this does mean it cannot match. If the FNM_LEADING_DIR
325
flag is set and exactly one slash is following, we have
328
int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
330
if (flags & FNM_FILE_NAME)
332
const char *slashp = strchr (n, '/');
334
if (flags & FNM_LEADING_DIR)
337
&& strchr (slashp + 1, '/') == NULL)
353
endp = __strchrnul (n, (flags & FNM_FILE_NAME) ? '/' : '\0');
357
int flags2 = ((flags & FNM_FILE_NAME)
358
? flags : (flags & ~FNM_PERIOD));
360
for (--p; n < endp; ++n)
361
if (internal_fnmatch (p, n,
371
else if (c == '/' && (flags & FNM_FILE_NAME))
373
while (*n != '\0' && *n != '/')
376
&& (internal_fnmatch (p, n + 1, flags & FNM_PERIOD,
382
int flags2 = ((flags & FNM_FILE_NAME)
383
? flags : (flags & ~FNM_PERIOD));
385
if (c == '\\' && !(flags & FNM_NOESCAPE))
388
for (--p; n < endp; ++n)
389
if (FOLD ((unsigned char) *n) == c
390
&& (internal_fnmatch (p, n,
401
/* If we come here no match is possible with the wildcard. */
406
/* Nonzero if the sense of the character class is inverted. */
407
static int posixly_correct;
411
if (posixly_correct == 0)
412
posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
417
if (*n == '.' && no_leading_period && (n == string
423
if (*n == '/' && (flags & FNM_FILE_NAME))
424
/* `/' cannot be matched. */
427
not = (*p == '!' || (posixly_correct < 0 && *p == '^'));
434
unsigned char fn = FOLD ((unsigned char) *n);
436
if (!(flags & FNM_NOESCAPE) && c == '\\')
440
c = FOLD ((unsigned char) *p);
446
else if (c == '[' && *p == ':')
448
/* Leave room for the null. */
449
char str[CHAR_CLASS_MAX_LENGTH + 1];
451
# if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
454
const char *startp = p;
458
if (c1 == CHAR_CLASS_MAX_LENGTH)
459
/* The name is too long and therefore the pattern
464
if (c == ':' && p[1] == ']')
469
if (c < 'a' || c >= 'z')
471
/* This cannot possibly be a character class name.
472
Match it as a normal range. */
481
# if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
482
wt = IS_CHAR_CLASS (str);
484
/* Invalid character class name. */
487
if (__iswctype (__btowc ((unsigned char) *n), wt))
490
if ((STREQ (str, "alnum") && ISALNUM ((unsigned char) *n))
491
|| (STREQ (str, "alpha") && ISALPHA ((unsigned char) *n))
492
|| (STREQ (str, "blank") && ISBLANK ((unsigned char) *n))
493
|| (STREQ (str, "cntrl") && ISCNTRL ((unsigned char) *n))
494
|| (STREQ (str, "digit") && ISDIGIT ((unsigned char) *n))
495
|| (STREQ (str, "graph") && ISGRAPH ((unsigned char) *n))
496
|| (STREQ (str, "lower") && ISLOWER ((unsigned char) *n))
497
|| (STREQ (str, "print") && ISPRINT ((unsigned char) *n))
498
|| (STREQ (str, "punct") && ISPUNCT ((unsigned char) *n))
499
|| (STREQ (str, "space") && ISSPACE ((unsigned char) *n))
500
|| (STREQ (str, "upper") && ISUPPER ((unsigned char) *n))
501
|| (STREQ (str, "xdigit") && ISXDIGIT ((unsigned char) *n)))
506
/* [ (unterminated) loses. */
518
if (c == '-' && *p != ']')
523
unsigned char cend = *p++;
524
if (!(flags & FNM_NOESCAPE) && cend == '\\')
533
if (strcoll (lo, fc) <= 0)
538
if (strcoll (fc, hi) <= 0)
555
/* Skip the rest of the [...] that already matched. */
559
/* [... (unterminated) loses. */
563
if (!(flags & FNM_NOESCAPE) && c == '\\')
567
/* XXX 1003.2d11 is unclear if this is right. */
570
else if (c == '[' && *p == ':')
575
while (*p != ':' || p[1] == ']');
586
if (c != FOLD ((unsigned char) *n))
596
if ((flags & FNM_LEADING_DIR) && *n == '/')
597
/* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
607
fnmatch (pattern, string, flags)
612
return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
615
#endif /* _LIBC or not __GNU_LIBRARY__. */