1 /* exclude.c -- exclude file names
3 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 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> */
43 # include "unlocked-io.h"
46 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
47 # define IN_CTYPE_DOMAIN(c) true
49 # define IN_CTYPE_DOMAIN(c) isascii (c)
53 is_space (unsigned char c)
55 return IN_CTYPE_DOMAIN (c) && isspace (c);
58 /* Non-GNU systems lack these options, so we don't need to check them. */
60 # define FNM_CASEFOLD 0
62 #ifndef FNM_LEADING_DIR
63 # define FNM_LEADING_DIR 0
66 verify (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
67 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
71 /* An exclude pattern-options pair. The options are fnmatch options
72 ORed with EXCLUDE_* options. */
80 /* An exclude list, of pattern-options pairs. */
84 struct patopts *exclude;
89 /* Return a newly allocated and empty exclude list. */
94 return xzalloc (sizeof *new_exclude ());
97 /* Free the storage associated with an exclude list. */
100 free_exclude (struct exclude *ex)
106 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
107 (unlike fnmatch) wildcards are disabled in PATTERN. */
110 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
112 if (! (options & FNM_LEADING_DIR))
113 return ((options & FNM_CASEFOLD)
114 ? strcasecmp (pattern, f)
115 : strcmp (pattern, f));
118 size_t patlen = strlen (pattern);
119 int r = ((options & FNM_CASEFOLD)
120 ? strncasecmp (pattern, f, patlen)
121 : strncmp (pattern, f, patlen));
133 exclude_fnmatch (char const *pattern, char const *f, int options)
135 int (*matcher) (char const *, char const *, int) =
136 (options & EXCLUDE_WILDCARDS
138 : fnmatch_no_wildcards);
139 bool matched = ((*matcher) (pattern, f, options) == 0);
142 if (! (options & EXCLUDE_ANCHORED))
143 for (p = f; *p && ! matched; p++)
144 if (*p == '/' && p[1] != '/')
145 matched = ((*matcher) (pattern, p + 1, options) == 0);
150 /* Return true if EX excludes F. */
153 excluded_file_name (struct exclude const *ex, char const *f)
155 size_t exclude_count = ex->exclude_count;
157 /* If no options are given, the default is to include. */
158 if (exclude_count == 0)
162 struct patopts const *exclude = ex->exclude;
165 /* Otherwise, the default is the opposite of the first option. */
166 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
168 /* Scan through the options, seeing whether they change F from
169 excluded to included or vice versa. */
170 for (i = 0; i < exclude_count; i++)
172 char const *pattern = exclude[i].pattern;
173 int options = exclude[i].options;
174 if (excluded == !! (options & EXCLUDE_INCLUDE))
175 excluded ^= exclude_fnmatch (pattern, f, options);
182 /* Append to EX the exclusion PATTERN with OPTIONS. */
185 add_exclude (struct exclude *ex, char const *pattern, int options)
187 struct patopts *patopts;
189 if (ex->exclude_count == ex->exclude_alloc)
190 ex->exclude = x2nrealloc (ex->exclude, &ex->exclude_alloc,
191 sizeof *ex->exclude);
193 patopts = &ex->exclude[ex->exclude_count++];
194 patopts->pattern = pattern;
195 patopts->options = options;
198 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
199 OPTIONS. LINE_END terminates each pattern in the file. If
200 LINE_END is a space character, ignore trailing spaces and empty
201 lines in FILE. Return -1 on failure, 0 on success. */
204 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
205 struct exclude *ex, char const *file_name, int options,
208 bool use_stdin = file_name[0] == '-' && !file_name[1];
214 size_t buf_alloc = 0;
215 size_t buf_count = 0;
221 else if (! (in = fopen (file_name, "r")))
224 while ((c = getc (in)) != EOF)
226 if (buf_count == buf_alloc)
227 buf = x2realloc (buf, &buf_alloc);
228 buf[buf_count++] = c;
234 if (!use_stdin && fclose (in) != 0)
237 buf = xrealloc (buf, buf_count + 1);
238 buf[buf_count] = line_end;
239 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
242 for (p = buf; p < lim; p++)
245 char *pattern_end = p;
247 if (is_space (line_end))
249 for (; ; pattern_end--)
250 if (pattern_end == pattern)
252 else if (! is_space (pattern_end[-1]))
257 (*add_func) (ex, pattern, options);