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 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> */
38 # include "unlocked-io.h"
41 /* Non-GNU systems lack these options, so we don't need to check them. */
43 # define FNM_CASEFOLD 0
46 # define FNM_EXTMATCH 0
48 #ifndef FNM_LEADING_DIR
49 # define FNM_LEADING_DIR 0
52 verify (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
53 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
54 | FNM_CASEFOLD | FNM_EXTMATCH))
57 /* An exclude pattern-options pair. The options are fnmatch options
58 ORed with EXCLUDE_* options. */
66 /* An exclude list, of pattern-options pairs. */
70 struct patopts *exclude;
75 /* Return a newly allocated and empty exclude list. */
80 return xzalloc (sizeof *new_exclude ());
83 /* Free the storage associated with an exclude list. */
86 free_exclude (struct exclude *ex)
92 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
93 (unlike fnmatch) wildcards are disabled in PATTERN. */
96 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
98 if (! (options & FNM_LEADING_DIR))
99 return ((options & FNM_CASEFOLD)
100 ? mbscasecmp (pattern, f)
101 : strcmp (pattern, f));
102 else if (! (options & FNM_CASEFOLD))
104 size_t patlen = strlen (pattern);
105 int r = strncmp (pattern, f, patlen);
116 /* Walk through a copy of F, seeing whether P matches any prefix
119 FIXME: This is an O(N**2) algorithm; it should be O(N).
120 Also, the copy should not be necessary. However, fixing this
121 will probably involve a change to the mbs* API. */
123 char *fcopy = xstrdup (f);
126 for (p = fcopy; ; *p++ = '/')
131 r = mbscasecmp (pattern, fcopy);
141 exclude_fnmatch (char const *pattern, char const *f, int options)
143 int (*matcher) (char const *, char const *, int) =
144 (options & EXCLUDE_WILDCARDS
146 : fnmatch_no_wildcards);
147 bool matched = ((*matcher) (pattern, f, options) == 0);
150 if (! (options & EXCLUDE_ANCHORED))
151 for (p = f; *p && ! matched; p++)
152 if (*p == '/' && p[1] != '/')
153 matched = ((*matcher) (pattern, p + 1, options) == 0);
158 /* Return true if EX excludes F. */
161 excluded_file_name (struct exclude const *ex, char const *f)
163 size_t exclude_count = ex->exclude_count;
165 /* If no options are given, the default is to include. */
166 if (exclude_count == 0)
170 struct patopts const *exclude = ex->exclude;
173 /* Otherwise, the default is the opposite of the first option. */
174 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
176 /* Scan through the options, seeing whether they change F from
177 excluded to included or vice versa. */
178 for (i = 0; i < exclude_count; i++)
180 char const *pattern = exclude[i].pattern;
181 int options = exclude[i].options;
182 if (excluded == !! (options & EXCLUDE_INCLUDE))
183 excluded ^= exclude_fnmatch (pattern, f, options);
190 /* Append to EX the exclusion PATTERN with OPTIONS. */
193 add_exclude (struct exclude *ex, char const *pattern, int options)
195 struct patopts *patopts;
197 if (ex->exclude_count == ex->exclude_alloc)
198 ex->exclude = x2nrealloc (ex->exclude, &ex->exclude_alloc,
199 sizeof *ex->exclude);
201 patopts = &ex->exclude[ex->exclude_count++];
202 patopts->pattern = pattern;
203 patopts->options = options;
206 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
207 OPTIONS. LINE_END terminates each pattern in the file. If
208 LINE_END is a space character, ignore trailing spaces and empty
209 lines in FILE. Return -1 on failure, 0 on success. */
212 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
213 struct exclude *ex, char const *file_name, int options,
216 bool use_stdin = file_name[0] == '-' && !file_name[1];
222 size_t buf_alloc = 0;
223 size_t buf_count = 0;
229 else if (! (in = fopen (file_name, "r")))
232 while ((c = getc (in)) != EOF)
234 if (buf_count == buf_alloc)
235 buf = x2realloc (buf, &buf_alloc);
236 buf[buf_count++] = c;
242 if (!use_stdin && fclose (in) != 0)
245 buf = xrealloc (buf, buf_count + 1);
246 buf[buf_count] = line_end;
247 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
250 for (p = buf; p < lim; p++)
253 char *pattern_end = p;
255 if (isspace ((unsigned char) line_end))
257 for (; ; pattern_end--)
258 if (pattern_end == pattern)
260 else if (! isspace ((unsigned char) pattern_end[-1]))
265 (*add_func) (ex, pattern, options);