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. */
22 /* Find the first occurrence of C in S or the final NUL byte. */
24 __strchrnul (const char *s, int c)
26 const unsigned char *char_ptr;
27 const unsigned long int *longword_ptr;
28 unsigned long int longword, magic_bits, charmask;
30 c = (unsigned char) c;
32 /* Handle the first few characters by reading one character at a time.
33 Do this until CHAR_PTR is aligned on a longword boundary. */
34 for (char_ptr = (const unsigned char *)s; ((unsigned long int) char_ptr
35 & (sizeof (longword) - 1)) != 0;
37 if (*char_ptr == c || *char_ptr == '\0')
38 return (void *) char_ptr;
40 /* All these elucidatory comments refer to 4-byte longwords,
41 but the theory applies equally well to 8-byte longwords. */
43 longword_ptr = (unsigned long int *) char_ptr;
45 /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
46 the "holes." Note that there is a hole just to the left of
47 each byte, with an extra at the end:
49 bits: 01111110 11111110 11111110 11111111
50 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
52 The 1-bits make sure that carries propagate to the next 0-bit.
53 The 0-bits provide holes for carries to fall into. */
54 switch (sizeof (longword))
56 case 4: magic_bits = 0x7efefeffL; break;
57 case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break;
62 /* Set up a longword, each of whose bytes is C. */
63 charmask = c | (c << 8);
64 charmask |= charmask << 16;
65 if (sizeof (longword) > 4)
66 /* Do the shift in two steps to avoid a warning if long has 32 bits. */
67 charmask |= (charmask << 16) << 16;
68 if (sizeof (longword) > 8)
71 /* Instead of the traditional loop which tests each character,
72 we will test a longword at a time. The tricky part is testing
73 if *any of the four* bytes in the longword in question are zero. */
76 /* We tentatively exit the loop if adding MAGIC_BITS to
77 LONGWORD fails to change any of the hole bits of LONGWORD.
79 1) Is this safe? Will it catch all the zero bytes?
80 Suppose there is a byte with all zeros. Any carry bits
81 propagating from its left will fall into the hole at its
82 least significant bit and stop. Since there will be no
83 carry from its most significant bit, the LSB of the
84 byte to the left will be unchanged, and the zero will be
87 2) Is this worthwhile? Will it ignore everything except
88 zero bytes? Suppose every byte of LONGWORD has a bit set
89 somewhere. There will be a carry into bit 8. If bit 8
90 is set, this will carry into bit 16. If bit 8 is clear,
91 one of bits 9-15 must be set, so there will be a carry
92 into bit 16. Similarly, there will be a carry into bit
93 24. If one of bits 24-30 is set, there will be a carry
94 into bit 31, so all of the hole bits will be changed.
96 The one misfire occurs when bits 24-30 are clear and bit
97 31 is set; in this case, the hole at bit 31 is not
98 changed. If we had access to the processor carry flag,
99 we could close this loophole by putting the fourth hole
102 So it ignores everything except 128's, when they're aligned
105 3) But wait! Aren't we looking for C as well as zero?
106 Good point. So what we do is XOR LONGWORD with a longword,
107 each of whose bytes is C. This turns each byte that is C
110 longword = *longword_ptr++;
112 /* Add MAGIC_BITS to LONGWORD. */
113 if ((((longword + magic_bits)
115 /* Set those bits that were unchanged by the addition. */
118 /* Look at only the hole bits. If any of the hole bits
119 are unchanged, most likely one of the bytes was a
121 & ~magic_bits) != 0 ||
123 /* That caught zeroes. Now test for C. */
124 ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
127 /* Which of the bytes was C or zero?
128 If none of them were, it was a misfire; continue the search. */
130 const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
132 if (*cp == c || *cp == '\0')
134 if (*++cp == c || *cp == '\0')
136 if (*++cp == c || *cp == '\0')
138 if (*++cp == c || *cp == '\0')
140 if (sizeof (longword) > 4)
142 if (*++cp == c || *cp == '\0')
144 if (*++cp == c || *cp == '\0')
146 if (*++cp == c || *cp == '\0')
148 if (*++cp == c || *cp == '\0')
154 /* This should never happen. */
158 /* For platform which support the ISO C amendement 1 functionality we
159 support user defined character classes. */
160 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
161 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
166 /* Comment out all this code if we are using the GNU C Library, and are not
167 actually compiling the library itself. This code is part of the GNU C
168 Library, but also included in many other GNU distributions. Compiling
169 and linking in this code is a waste when using the GNU C library
170 (especially if it is a shared library). Rather than having every GNU
171 program understand `configure --with-gnu-libc' and omit the object files,
172 it is simpler to just do this in the source for each such file. */
174 #if defined _LIBC || !defined __GNU_LIBRARY__
177 # if defined STDC_HEADERS || !defined isascii
178 # define ISASCII(c) 1
180 # define ISASCII(c) isascii(c)
184 # define ISBLANK(c) (ISASCII (c) && isblank (c))
186 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
189 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
191 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
194 #define ISPRINT(c) (ISASCII (c) && isprint (c))
195 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
196 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
197 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
198 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
199 #define ISLOWER(c) (ISASCII (c) && islower (c))
200 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
201 #define ISSPACE(c) (ISASCII (c) && isspace (c))
202 #define ISUPPER(c) (ISASCII (c) && isupper (c))
203 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
205 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
207 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
208 /* The GNU C library provides support for user-defined character classes
209 and the functions from ISO C amendement 1. */
210 # ifdef CHARCLASS_NAME_MAX
211 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
213 /* This shouldn't happen but some implementation might still have this
214 problem. Use a reasonable default value. */
215 # define CHAR_CLASS_MAX_LENGTH 256
219 # define IS_CHAR_CLASS(string) __wctype (string)
221 # define IS_CHAR_CLASS(string) wctype (string)
224 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
226 # define IS_CHAR_CLASS(string) \
227 (STREQ (string, "alpha") || STREQ (string, "upper") \
228 || STREQ (string, "lower") || STREQ (string, "digit") \
229 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
230 || STREQ (string, "space") || STREQ (string, "print") \
231 || STREQ (string, "punct") || STREQ (string, "graph") \
232 || STREQ (string, "cntrl") || STREQ (string, "blank"))
235 /* Avoid depending on library functions or files
236 whose names are inconsistent. */
238 # if !defined _LIBC && !defined getenv
239 extern char *getenv ();
246 /* Match STRING against the filename pattern PATTERN, returning zero if
247 it matches, nonzero if not. */
252 internal_fnmatch (const char *pattern, const char *string,
253 int no_leading_period, int flags)
255 register const char *p = pattern, *n = string;
256 register unsigned char c;
258 /* Note that this evaluates C many times. */
260 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
262 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
265 while ((c = *p++) != '\0')
274 else if (*n == '/' && (flags & FNM_FILE_NAME))
276 else if (*n == '.' && no_leading_period
278 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
283 if (!(flags & FNM_NOESCAPE))
287 /* Trailing \ loses. */
291 if (FOLD ((unsigned char) *n) != c)
296 if (*n == '.' && no_leading_period
298 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
301 for (c = *p++; c == '?' || c == '*'; c = *p++)
303 if (*n == '/' && (flags & FNM_FILE_NAME))
304 /* A slash does not match a wildcard under FNM_FILE_NAME. */
308 /* A ? needs to match one character. */
310 /* There isn't another character; no match. */
313 /* One character of the string is consumed in matching
314 this ? wildcard, so *??? won't match if there are
315 less than three characters. */
321 /* The wildcard(s) is/are the last element of the pattern.
322 If the name is a file name and contains another slash
323 this does mean it cannot match. If the FNM_LEADING_DIR
324 flag is set and exactly one slash is following, we have
327 int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
329 if (flags & FNM_FILE_NAME)
331 const char *slashp = strchr (n, '/');
333 if (flags & FNM_LEADING_DIR)
336 && strchr (slashp + 1, '/') == NULL)
352 endp = __strchrnul (n, (flags & FNM_FILE_NAME) ? '/' : '\0');
356 int flags2 = ((flags & FNM_FILE_NAME)
357 ? flags : (flags & ~FNM_PERIOD));
359 for (--p; n < endp; ++n)
360 if (internal_fnmatch (p, n,
370 else if (c == '/' && (flags & FNM_FILE_NAME))
372 while (*n != '\0' && *n != '/')
375 && (internal_fnmatch (p, n + 1, flags & FNM_PERIOD,
381 int flags2 = ((flags & FNM_FILE_NAME)
382 ? flags : (flags & ~FNM_PERIOD));
384 if (c == '\\' && !(flags & FNM_NOESCAPE))
387 for (--p; n < endp; ++n)
388 if (FOLD ((unsigned char) *n) == c
389 && (internal_fnmatch (p, n,
400 /* If we come here no match is possible with the wildcard. */
405 /* Nonzero if the sense of the character class is inverted. */
406 static int posixly_correct;
410 if (posixly_correct == 0)
411 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
416 if (*n == '.' && no_leading_period && (n == string
422 if (*n == '/' && (flags & FNM_FILE_NAME))
423 /* `/' cannot be matched. */
426 not = (*p == '!' || (posixly_correct < 0 && *p == '^'));
433 unsigned char fn = FOLD ((unsigned char) *n);
435 if (!(flags & FNM_NOESCAPE) && c == '\\')
439 c = FOLD ((unsigned char) *p);
445 else if (c == '[' && *p == ':')
447 /* Leave room for the null. */
448 char str[CHAR_CLASS_MAX_LENGTH + 1];
450 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
453 const char *startp = p;
457 if (c1 == CHAR_CLASS_MAX_LENGTH)
458 /* The name is too long and therefore the pattern
463 if (c == ':' && p[1] == ']')
468 if (c < 'a' || c >= 'z')
470 /* This cannot possibly be a character class name.
471 Match it as a normal range. */
480 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
481 wt = IS_CHAR_CLASS (str);
483 /* Invalid character class name. */
486 if (__iswctype (__btowc ((unsigned char) *n), wt))
489 if ((STREQ (str, "alnum") && ISALNUM ((unsigned char) *n))
490 || (STREQ (str, "alpha") && ISALPHA ((unsigned char) *n))
491 || (STREQ (str, "blank") && ISBLANK ((unsigned char) *n))
492 || (STREQ (str, "cntrl") && ISCNTRL ((unsigned char) *n))
493 || (STREQ (str, "digit") && ISDIGIT ((unsigned char) *n))
494 || (STREQ (str, "graph") && ISGRAPH ((unsigned char) *n))
495 || (STREQ (str, "lower") && ISLOWER ((unsigned char) *n))
496 || (STREQ (str, "print") && ISPRINT ((unsigned char) *n))
497 || (STREQ (str, "punct") && ISPUNCT ((unsigned char) *n))
498 || (STREQ (str, "space") && ISSPACE ((unsigned char) *n))
499 || (STREQ (str, "upper") && ISUPPER ((unsigned char) *n))
500 || (STREQ (str, "xdigit") && ISXDIGIT ((unsigned char) *n)))
505 /* [ (unterminated) loses. */
517 if (c == '-' && *p != ']')
522 unsigned char cend = *p++;
523 if (!(flags & FNM_NOESCAPE) && cend == '\\')
532 if (strcoll (lo, fc) <= 0)
537 if (strcoll (fc, hi) <= 0)
554 /* Skip the rest of the [...] that already matched. */
558 /* [... (unterminated) loses. */
562 if (!(flags & FNM_NOESCAPE) && c == '\\')
566 /* XXX 1003.2d11 is unclear if this is right. */
569 else if (c == '[' && *p == ':')
574 while (*p != ':' || p[1] == ']');
585 if (c != FOLD ((unsigned char) *n))
595 if ((flags & FNM_LEADING_DIR) && *n == '/')
596 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
606 fnmatch (const char *pattern, const char *string, int flags)
608 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
611 #endif /* _LIBC or not __GNU_LIBRARY__. */