1 /* exclude.c -- exclude file names
3 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 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 2, or (at your option)
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; see the file COPYING.
18 If not, write to the Free Software Foundation,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Written by Paul Eggert <eggert@twinsun.com> */
40 # include "unlocked-io.h"
43 /* Non-GNU systems lack these options, so we don't need to check them. */
45 # define FNM_CASEFOLD 0
48 # define FNM_EXTMATCH 0
50 #ifndef FNM_LEADING_DIR
51 # define FNM_LEADING_DIR 0
54 verify (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
55 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
56 | FNM_CASEFOLD | FNM_EXTMATCH))
59 /* An exclude pattern-options pair. The options are fnmatch options
60 ORed with EXCLUDE_* options. */
68 /* An exclude list, of pattern-options pairs. */
72 struct patopts *exclude;
77 /* Return a newly allocated and empty exclude list. */
82 return xzalloc (sizeof *new_exclude ());
85 /* Free the storage associated with an exclude list. */
88 free_exclude (struct exclude *ex)
94 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
95 (unlike fnmatch) wildcards are disabled in PATTERN. */
98 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
100 if (! (options & FNM_LEADING_DIR))
101 return ((options & FNM_CASEFOLD)
102 ? mbscasecmp (pattern, f)
103 : strcmp (pattern, f));
104 else if (! (options & FNM_CASEFOLD))
106 size_t patlen = strlen (pattern);
107 int r = strncmp (pattern, f, patlen);
118 /* Walk through a copy of F, seeing whether P matches any prefix
121 FIXME: This is an O(N**2) algorithm; it should be O(N).
122 Also, the copy should not be necessary. However, fixing this
123 will probably involve a change to the mbs* API. */
125 char *fcopy = xstrdup (f);
128 for (p = fcopy; ; *p++ = '/')
133 r = mbscasecmp (pattern, fcopy);
143 exclude_fnmatch (char const *pattern, char const *f, int options)
145 int (*matcher) (char const *, char const *, int) =
146 (options & EXCLUDE_WILDCARDS
148 : fnmatch_no_wildcards);
149 bool matched = ((*matcher) (pattern, f, options) == 0);
152 if (! (options & EXCLUDE_ANCHORED))
153 for (p = f; *p && ! matched; p++)
154 if (*p == '/' && p[1] != '/')
155 matched = ((*matcher) (pattern, p + 1, options) == 0);
160 /* Return true if EX excludes F. */
163 excluded_file_name (struct exclude const *ex, char const *f)
165 size_t exclude_count = ex->exclude_count;
167 /* If no options are given, the default is to include. */
168 if (exclude_count == 0)
172 struct patopts const *exclude = ex->exclude;
175 /* Otherwise, the default is the opposite of the first option. */
176 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
178 /* Scan through the options, seeing whether they change F from
179 excluded to included or vice versa. */
180 for (i = 0; i < exclude_count; i++)
182 char const *pattern = exclude[i].pattern;
183 int options = exclude[i].options;
184 if (excluded == !! (options & EXCLUDE_INCLUDE))
185 excluded ^= exclude_fnmatch (pattern, f, options);
192 /* Append to EX the exclusion PATTERN with OPTIONS. */
195 add_exclude (struct exclude *ex, char const *pattern, int options)
197 struct patopts *patopts;
199 if (ex->exclude_count == ex->exclude_alloc)
200 ex->exclude = x2nrealloc (ex->exclude, &ex->exclude_alloc,
201 sizeof *ex->exclude);
203 patopts = &ex->exclude[ex->exclude_count++];
204 patopts->pattern = pattern;
205 patopts->options = options;
208 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
209 OPTIONS. LINE_END terminates each pattern in the file. If
210 LINE_END is a space character, ignore trailing spaces and empty
211 lines in FILE. Return -1 on failure, 0 on success. */
214 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
215 struct exclude *ex, char const *file_name, int options,
218 bool use_stdin = file_name[0] == '-' && !file_name[1];
224 size_t buf_alloc = 0;
225 size_t buf_count = 0;
231 else if (! (in = fopen (file_name, "r")))
234 while ((c = getc (in)) != EOF)
236 if (buf_count == buf_alloc)
237 buf = x2realloc (buf, &buf_alloc);
238 buf[buf_count++] = c;
244 if (!use_stdin && fclose (in) != 0)
247 buf = xrealloc (buf, buf_count + 1);
248 buf[buf_count] = line_end;
249 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
252 for (p = buf; p < lim; p++)
255 char *pattern_end = p;
257 if (isspace ((unsigned char) line_end))
259 for (; ; pattern_end--)
260 if (pattern_end == pattern)
262 else if (! isspace ((unsigned char) pattern_end[-1]))
267 (*add_func) (ex, pattern, options);