rpm  5.4.15
fnmatch.c
Go to the documentation of this file.
1 /*@-bounds@*/
2 /*@-retalias@*/
3 /*@-shiftimplementation@*/
4 /*@-temptrans@*/
5 /*@-unreachable@*/
6 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2007
7  Free Software Foundation, Inc.
8  This file is part of the GNU C Library.
9 
10  The GNU C Library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  The GNU C Library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with the GNU C Library; if not, write to the Free
22  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  02111-1307 USA. */
24 
25 #include "system.h"
26 
27 #include <string.h>
28 
29 #include "debug.h"
30 
31 #if ! defined __builtin_expect && __GNUC__ < 3
32 # define __builtin_expect(expr, expected) (expr)
33 #elif ! defined __builtin_expect && defined(__SUNPRO_C)
34 # define __builtin_expect(expr, expected) (expr)
35 #endif
36 
37 /* XXX Don't bother with wide and multibyte characters ... */
38 #undef HAVE_WCTYPE_H
39 #undef HAVE_WCHAR_H
40 #undef HAVE_MBSTATE_T
41 #undef HAVE_MBSRTOWCS
42 
43 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
44 #define NO_LEADING_PERIOD(flags) \
45  ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
46 
47 /* Comment out all this code if we are using the GNU C Library, and are not
48  actually compiling the library itself. This code is part of the GNU C
49  Library, but also included in many other GNU distributions. Compiling
50  and linking in this code is a waste when using the GNU C library
51  (especially if it is a shared library). Rather than having every GNU
52  program understand `configure --with-gnu-libc' and omit the object files,
53  it is simpler to just do this in the source for each such file. */
54 
55 #if defined _LIBC || !defined __GNU_LIBRARY__
56 
57 
58 # if defined STDC_HEADERS || !defined isascii
59 # define ISASCII(c) 1
60 # else
61 # define ISASCII(c) isascii(c)
62 # endif
63 
64 # ifdef isblank
65 # define ISBLANK(c) (ISASCII (c) && isblank (c))
66 # else
67 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
68 # endif
69 # ifdef isgraph
70 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
71 # else
72 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
73 # endif
74 
75 # define ISPRINT(c) (ISASCII (c) && isprint (c))
76 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
77 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
78 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
79 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
80 # define ISLOWER(c) (ISASCII (c) && islower (c))
81 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
82 # define ISSPACE(c) (ISASCII (c) && isspace (c))
83 # define ISUPPER(c) (ISASCII (c) && isupper (c))
84 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
85 
86 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
87 
88 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
89 /* The GNU C library provides support for user-defined character classes
90  and the functions from ISO C amendement 1. */
91 # ifdef CHARCLASS_NAME_MAX
92 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
93 # else
94 /* This shouldn't happen but some implementation might still have this
95  problem. Use a reasonable default value. */
96 # define CHAR_CLASS_MAX_LENGTH 256
97 # endif
98 
99 # ifdef _LIBC
100 # define IS_CHAR_CLASS(string) __wctype (string)
101 # else
102 # define IS_CHAR_CLASS(string) wctype (string)
103 # endif
104 
105 # ifdef _LIBC
106 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
107 # else
108 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
109 # endif
110 
111 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
112 /* In this case we are implementing the multibyte character handling. */
113 # define HANDLE_MULTIBYTE 1
114 # endif
115 
116 # else
117 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
118 
119 # define IS_CHAR_CLASS(string) \
120  (STREQ (string, "alpha") || STREQ (string, "upper") \
121  || STREQ (string, "lower") || STREQ (string, "digit") \
122  || STREQ (string, "alnum") || STREQ (string, "xdigit") \
123  || STREQ (string, "space") || STREQ (string, "print") \
124  || STREQ (string, "punct") || STREQ (string, "graph") \
125  || STREQ (string, "cntrl") || STREQ (string, "blank"))
126 # endif
127 
128 /* Avoid depending on library functions or files
129  whose names are inconsistent. */
130 
131 # if defined __linux__ && (!defined _LIBC && !defined getenv)
132 extern char *getenv ();
133 # endif
134 
135 # ifndef errno
136 extern int errno;
137 # endif
138 
139 /* Global variable. */
140 static int posixly_correct;
141 
142 # ifndef internal_function
143 /* Inside GNU libc we mark some function in a special way. In other
144  environments simply ignore the marking. */
145 # define internal_function
146 # endif
147 
148 /* Note that this evaluates C many times. */
149 # ifdef _LIBC
150 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
151 # else
152 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
153 # endif
154 # define CHAR char
155 # define UCHAR unsigned char
156 # define INT int
157 # define FCT internal_fnmatch
158 # define EXT ext_match
159 # define END end_pattern
160 # define STRUCT fnmatch_struct
161 # define L(CS) CS
162 # ifdef _LIBC
163 # define BTOWC(C) __btowc (C)
164 # else
165 # define BTOWC(C) btowc (C)
166 # endif
167 # define STRLEN(S) strlen (S)
168 # define STRCAT(D, S) strcat (D, S)
169 # if defined HAVE_MEMPCPY
170 # define MEMPCPY(D, S, N) mempcpy (D, S, N)
171 #else
172 # define MEMPCPY(D, S, N) __fnmatch_mempcpy (D, S, N)
173 static void *__fnmatch_mempcpy(void *, const void *, size_t);
174 static void *__fnmatch_mempcpy(void *dest, const void *src, size_t n)
175 {
176  return (void *)((char *)memcpy(dest, src, n) + n);
177 }
178 #endif
179 # define MEMCHR(S, C, N) memchr (S, C, N)
180 # define STRCOLL(S1, S2) strcoll (S1, S2)
181 # include "fnmatch_loop.c"
182 
183 
184 # if HANDLE_MULTIBYTE
185 /* Note that this evaluates C many times. */
186 # ifdef _LIBC
187 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
188 # else
189 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
190 # endif
191 # define CHAR wchar_t
192 # define UCHAR wint_t
193 # define INT wint_t
194 # define FCT internal_fnwmatch
195 # define EXT ext_wmatch
196 # define END end_wpattern
197 # define STRUCT fnwmatch_struct
198 # define L(CS) L##CS
199 # define BTOWC(C) (C)
200 # define STRLEN(S) __wcslen (S)
201 # define STRCAT(D, S) __wcscat (D, S)
202 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
203 # define MEMCHR(S, C, N) wmemchr (S, C, N)
204 # define STRCOLL(S1, S2) wcscoll (S1, S2)
205 # define WIDE_CHAR_VERSION 1
206 
207 # undef IS_CHAR_CLASS
208 /* We have to convert the wide character string in a multibyte string. But
209  we know that the character class names consist of alphanumeric characters
210  from the portable character set, and since the wide character encoding
211  for a member of the portable character set is the same code point as
212  its single-byte encoding, we can use a simplified method to convert the
213  string to a multibyte character string. */
214 static wctype_t
215 is_char_class (const wchar_t *wcs)
216 {
217  char s[CHAR_CLASS_MAX_LENGTH + 1];
218  char *cp = s;
219 
220  do
221  {
222  /* Test for a printable character from the portable character set. */
223 # ifdef _LIBC
224  if (*wcs < 0x20 || *wcs > 0x7e
225  || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
226  return (wctype_t) 0;
227 # else
228  switch (*wcs)
229  {
230  case L' ': case L'!': case L'"': case L'#': case L'%':
231  case L'&': case L'\'': case L'(': case L')': case L'*':
232  case L'+': case L',': case L'-': case L'.': case L'/':
233  case L'0': case L'1': case L'2': case L'3': case L'4':
234  case L'5': case L'6': case L'7': case L'8': case L'9':
235  case L':': case L';': case L'<': case L'=': case L'>':
236  case L'?':
237  case L'A': case L'B': case L'C': case L'D': case L'E':
238  case L'F': case L'G': case L'H': case L'I': case L'J':
239  case L'K': case L'L': case L'M': case L'N': case L'O':
240  case L'P': case L'Q': case L'R': case L'S': case L'T':
241  case L'U': case L'V': case L'W': case L'X': case L'Y':
242  case L'Z':
243  case L'[': case L'\\': case L']': case L'^': case L'_':
244  case L'a': case L'b': case L'c': case L'd': case L'e':
245  case L'f': case L'g': case L'h': case L'i': case L'j':
246  case L'k': case L'l': case L'm': case L'n': case L'o':
247  case L'p': case L'q': case L'r': case L's': case L't':
248  case L'u': case L'v': case L'w': case L'x': case L'y':
249  case L'z': case L'{': case L'|': case L'}': case L'~':
250  break;
251  default:
252  return (wctype_t) 0;
253  }
254 # endif
255 
256  /* Avoid overrunning the buffer. */
257  if (cp == s + CHAR_CLASS_MAX_LENGTH)
258  return (wctype_t) 0;
259 
260  *cp++ = (char) *wcs++;
261  }
262  while (*wcs != L'\0');
263 
264  *cp = '\0';
265 
266 # ifdef _LIBC
267  return __wctype (s);
268 # else
269  return wctype (s);
270 # endif
271 }
272 # define IS_CHAR_CLASS(string) is_char_class (string)
273 
274 # include "fnmatch_loop.c"
275 # endif
276 
277 
278 int
280  const char *pattern;
281  const char *string;
282  int flags;
283 {
284 # if HANDLE_MULTIBYTE
285  if (__builtin_expect (MB_CUR_MAX, 1) != 1)
286  {
287  mbstate_t ps;
288  size_t n;
289  const char *p;
290  wchar_t *wpattern;
291  wchar_t *wstring;
292 
293  /* Convert the strings into wide characters. */
294  memset (&ps, '\0', sizeof (ps));
295  p = pattern;
296 #ifdef _LIBC
297  n = strnlen (pattern, 1024);
298 #else
299  n = strlen (pattern);
300 #endif
301  if (__builtin_expect (n < 1024, 1))
302  {
303  wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
304  n = mbsrtowcs (wpattern, &p, n + 1, &ps);
305  if (__builtin_expect (n == (size_t) -1, 0))
306  /* Something wrong.
307  XXX Do we have to set `errno' to something which mbsrtows hasn't
308  already done? */
309  return -1;
310  if (p)
311  {
312  memset (&ps, '\0', sizeof (ps));
313  goto prepare_wpattern;
314  }
315  }
316  else
317  {
318  prepare_wpattern:
319  n = mbsrtowcs (NULL, &pattern, 0, &ps);
320  if (__builtin_expect (n == (size_t) -1, 0))
321  /* Something wrong.
322  XXX Do we have to set `errno' to something which mbsrtows hasn't
323  already done? */
324  return -1;
325  wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
326  assert (mbsinit (&ps));
327  (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
328  }
329 
330  assert (mbsinit (&ps));
331 #ifdef _LIBC
332  n = strnlen (string, 1024);
333 #else
334  n = strlen (string);
335 #endif
336  p = string;
337  if (__builtin_expect (n < 1024, 1))
338  {
339  wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
340  n = mbsrtowcs (wstring, &p, n + 1, &ps);
341  if (__builtin_expect (n == (size_t) -1, 0))
342  /* Something wrong.
343  XXX Do we have to set `errno' to something which mbsrtows hasn't
344  already done? */
345  return -1;
346  if (p)
347  {
348  memset (&ps, '\0', sizeof (ps));
349  goto prepare_wstring;
350  }
351  }
352  else
353  {
354  prepare_wstring:
355  n = mbsrtowcs (NULL, &string, 0, &ps);
356  if (__builtin_expect (n == (size_t) -1, 0))
357  /* Something wrong.
358  XXX Do we have to set `errno' to something which mbsrtows hasn't
359  already done? */
360  return -1;
361  wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
362  assert (mbsinit (&ps));
363  (void) mbsrtowcs (wstring, &string, n + 1, &ps);
364  }
365 
366  return internal_fnwmatch (wpattern, wstring, wstring + n,
367  flags & FNM_PERIOD, flags, NULL);
368  }
369 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
370 
371  return internal_fnmatch (pattern, string, string + strlen (string),
372  flags & FNM_PERIOD, flags, NULL);
373 }
374 
375 # ifdef _LIBC
376 # undef fnmatch
377 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
378 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
379 strong_alias (__fnmatch, __fnmatch_old)
380 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
381 # endif
382 libc_hidden_ver (__fnmatch, fnmatch)
383 # endif
384 
385 #endif /* _LIBC or not __GNU_LIBRARY__. */
386 /*@=unreachable@*/
387 /*@=temptrans@*/
388 /*@=shiftimplementation@*/
389 /*@=retalias@*/
390 /*@=bounds@*/
#define CHAR_CLASS_MAX_LENGTH
Definition: fnmatch.c:117
int errno
char * getenv(const char *name)
char * alloca()
#define __builtin_expect(expr, expected)
Definition: fnmatch.c:32
#define FNM_PERIOD
Definition: fnmatch.h:43
const char const bson int mongo_write_concern int flags
Definition: mongo.h:485
static void * __fnmatch_mempcpy(void *, const void *, size_t)
Definition: fnmatch.c:174
#define L(CS)
Definition: fnmatch.c:161
static int posixly_correct
Definition: fnmatch.c:140
const char const char * pattern
Definition: bson.h:971
int fnmatch(char *pattern, const char *string, int flags) const
Definition: fnmatch.c:279