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