1 /* exclude.c -- exclude file names
3 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert <eggert@twinsun.com>
20 and Sergey Poznyakoff <gray@gnu.org>.
21 Thanks to Phil Proudman <phil@proudman51.freeserve.co.uk>
22 for improvement suggestions. */
44 # include "unlocked-io.h"
47 /* Non-GNU systems lack these options, so we don't need to check them. */
49 # define FNM_CASEFOLD 0
52 # define FNM_EXTMATCH 0
54 #ifndef FNM_LEADING_DIR
55 # define FNM_LEADING_DIR 0
58 verify (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
59 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
60 | FNM_CASEFOLD | FNM_EXTMATCH))
64 /* Exclusion patterns are grouped into a singly-linked list of
65 "exclusion segments". Each segment represents a set of patterns
66 that can be matches using the same algorithm. Non-wildcard
67 patterns are kept in hash tables, to speed up searches. Wildcard
68 patterns are stored as arrays of patterns. */
71 /* An exclude pattern-options pair. The options are fnmatch options
72 ORed with EXCLUDE_* options. */
80 /* An array of pattern-options pairs. */
82 struct exclude_pattern
84 struct patopts *exclude;
91 exclude_hash, /* a hash table of excluded names */
92 exclude_pattern /* an array of exclude patterns */
95 struct exclude_segment
97 struct exclude_segment *next; /* next segment in list */
98 enum exclude_type type; /* type of this segment */
99 int options; /* common options for this segment */
102 Hash_table *table; /* for type == exclude_hash */
103 struct exclude_pattern pat; /* for type == exclude_pattern */
107 /* The exclude structure keeps a singly-linked list of exclude segments */
110 struct exclude_segment *head, *tail;
113 /* Return true if str has wildcard characters */
115 fnmatch_pattern_has_wildcards (const char *str, int options)
117 const char *cset = "\\?*[]";
118 if (options & FNM_NOESCAPE)
122 size_t n = strcspn (str, cset);
125 else if (str[n] == '\\')
137 /* Return a newly allocated and empty exclude list. */
142 return xzalloc (sizeof *new_exclude ());
145 /* Calculate the hash of string. */
147 string_hasher (void const *data, size_t n_buckets)
149 char const *p = data;
150 return hash_string (p, n_buckets);
153 /* Ditto, for case-insensitive hashes */
155 string_hasher_ci (void const *data, size_t n_buckets)
157 char const *p = data;
158 mbui_iterator_t iter;
161 for (mbui_init (iter, p); mbui_avail (iter); mbui_advance (iter))
163 mbchar_t m = mbui_cur (iter);
167 wc = towlower (m.wc);
171 value = (value * 31 + wc) % n_buckets;
177 /* compare two strings for equality */
179 string_compare (void const *data1, void const *data2)
181 char const *p1 = data1;
182 char const *p2 = data2;
183 return strcmp (p1, p2) == 0;
186 /* compare two strings for equality, case-insensitive */
188 string_compare_ci (void const *data1, void const *data2)
190 char const *p1 = data1;
191 char const *p2 = data2;
192 return mbscasecmp (p1, p2) == 0;
196 string_free (void *data)
201 /* Create new exclude segment of given TYPE and OPTIONS, and attach it
202 to the tail of list in EX */
203 static struct exclude_segment *
204 new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
206 struct exclude_segment *sp = xzalloc (sizeof (struct exclude_segment));
208 sp->options = options;
211 case exclude_pattern:
215 sp->v.table = hash_initialize (0, NULL,
216 (options & FNM_CASEFOLD) ?
219 (options & FNM_CASEFOLD) ?
233 /* Free a single exclude segment */
235 free_exclude_segment (struct exclude_segment *seg)
239 case exclude_pattern:
240 free (seg->v.pat.exclude);
244 hash_free (seg->v.table);
250 /* Free the storage associated with an exclude list. */
252 free_exclude (struct exclude *ex)
254 struct exclude_segment *seg;
255 for (seg = ex->head; seg; )
257 struct exclude_segment *next = seg->next;
258 free_exclude_segment (seg);
264 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
265 (unlike fnmatch) wildcards are disabled in PATTERN. */
268 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
270 if (! (options & FNM_LEADING_DIR))
271 return ((options & FNM_CASEFOLD)
272 ? mbscasecmp (pattern, f)
273 : strcmp (pattern, f));
274 else if (! (options & FNM_CASEFOLD))
276 size_t patlen = strlen (pattern);
277 int r = strncmp (pattern, f, patlen);
288 /* Walk through a copy of F, seeing whether P matches any prefix
291 FIXME: This is an O(N**2) algorithm; it should be O(N).
292 Also, the copy should not be necessary. However, fixing this
293 will probably involve a change to the mbs* API. */
295 char *fcopy = xstrdup (f);
298 for (p = fcopy; ; *p++ = '/')
303 r = mbscasecmp (pattern, fcopy);
313 exclude_fnmatch (char const *pattern, char const *f, int options)
315 int (*matcher) (char const *, char const *, int) =
316 (options & EXCLUDE_WILDCARDS
318 : fnmatch_no_wildcards);
319 bool matched = ((*matcher) (pattern, f, options) == 0);
322 if (! (options & EXCLUDE_ANCHORED))
323 for (p = f; *p && ! matched; p++)
324 if (*p == '/' && p[1] != '/')
325 matched = ((*matcher) (pattern, p + 1, options) == 0);
330 /* Return true if the exclude_pattern segment SEG excludes F. */
333 excluded_file_pattern_p (struct exclude_segment const *seg, char const *f)
335 size_t exclude_count = seg->v.pat.exclude_count;
336 struct patopts const *exclude = seg->v.pat.exclude;
338 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
340 /* Scan through the options, until they change excluded */
341 for (i = 0; i < exclude_count; i++)
343 char const *pattern = exclude[i].pattern;
344 int options = exclude[i].options;
345 if (exclude_fnmatch (pattern, f, options))
351 /* Return true if the exclude_hash segment SEG excludes F.
352 BUFFER is an auxiliary storage of the same length as F (with nul
353 terminator included) */
355 excluded_file_name_p (struct exclude_segment const *seg, char const *f,
358 int options = seg->options;
359 bool excluded = !! (options & EXCLUDE_INCLUDE);
360 Hash_table *table = seg->v.table;
364 /* initialize the pattern */
369 if (hash_lookup (table, buffer))
371 if (options & FNM_LEADING_DIR)
373 char *p = strrchr (buffer, '/');
383 if (!(options & EXCLUDE_ANCHORED))
396 /* Return true if EX excludes F. */
399 excluded_file_name (struct exclude const *ex, char const *f)
401 struct exclude_segment *seg;
403 char *filename = NULL;
405 /* If no patterns are given, the default is to include. */
409 /* Otherwise, the default is the opposite of the first option. */
410 excluded = !! (ex->head->options & EXCLUDE_INCLUDE);
411 /* Scan through the segments, seeing whether they change status from
412 excluded to included or vice versa. */
413 for (seg = ex->head; seg; seg = seg->next)
419 case exclude_pattern:
420 rc = excluded_file_pattern_p (seg, f);
425 filename = xmalloc (strlen (f) + 1);
426 rc = excluded_file_name_p (seg, f, filename);
442 /* Append to EX the exclusion PATTERN with OPTIONS. */
445 add_exclude (struct exclude *ex, char const *pattern, int options)
447 struct exclude_segment *seg;
449 if ((options & EXCLUDE_WILDCARDS)
450 && fnmatch_pattern_has_wildcards (pattern, options))
452 struct exclude_pattern *pat;
453 struct patopts *patopts;
455 if (ex->tail && ex->tail->type == exclude_pattern
456 && ((ex->tail->options & EXCLUDE_INCLUDE) ==
457 (options & EXCLUDE_INCLUDE)))
460 seg = new_exclude_segment (ex, exclude_pattern, options);
463 if (pat->exclude_count == pat->exclude_alloc)
464 pat->exclude = x2nrealloc (pat->exclude, &pat->exclude_alloc,
465 sizeof *pat->exclude);
466 patopts = &pat->exclude[pat->exclude_count++];
467 patopts->pattern = pattern;
468 patopts->options = options;
473 #define EXCLUDE_HASH_FLAGS (EXCLUDE_INCLUDE|EXCLUDE_ANCHORED|\
474 FNM_LEADING_DIR|FNM_CASEFOLD)
475 if (ex->tail && ex->tail->type == exclude_hash
476 && ((ex->tail->options & EXCLUDE_HASH_FLAGS) ==
477 (options & EXCLUDE_HASH_FLAGS)))
480 seg = new_exclude_segment (ex, exclude_hash, options);
482 str = xstrdup (pattern);
483 p = hash_insert (seg->v.table, str);
489 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
490 OPTIONS. LINE_END terminates each pattern in the file. If
491 LINE_END is a space character, ignore trailing spaces and empty
492 lines in FILE. Return -1 on failure, 0 on success. */
495 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
496 struct exclude *ex, char const *file_name, int options,
499 bool use_stdin = file_name[0] == '-' && !file_name[1];
505 size_t buf_alloc = 0;
506 size_t buf_count = 0;
512 else if (! (in = fopen (file_name, "r")))
515 while ((c = getc (in)) != EOF)
517 if (buf_count == buf_alloc)
518 buf = x2realloc (buf, &buf_alloc);
519 buf[buf_count++] = c;
525 if (!use_stdin && fclose (in) != 0)
528 buf = xrealloc (buf, buf_count + 1);
529 buf[buf_count] = line_end;
530 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
533 for (p = buf; p < lim; p++)
536 char *pattern_end = p;
538 if (isspace ((unsigned char) line_end))
540 for (; ; pattern_end--)
541 if (pattern_end == pattern)
543 else if (! isspace ((unsigned char) pattern_end[-1]))
548 (*add_func) (ex, pattern, options);