1 /* exclude.c -- exclude file names
3 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005 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));
132 /* Return true if EX excludes F. */
135 excluded_file_name (struct exclude const *ex, char const *f)
137 size_t exclude_count = ex->exclude_count;
139 /* If no options are given, the default is to include. */
140 if (exclude_count == 0)
144 struct patopts const *exclude = ex->exclude;
147 /* Otherwise, the default is the opposite of the first option. */
148 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
150 /* Scan through the options, seeing whether they change F from
151 excluded to included or vice versa. */
152 for (i = 0; i < exclude_count; i++)
154 char const *pattern = exclude[i].pattern;
155 int options = exclude[i].options;
156 if (excluded == !! (options & EXCLUDE_INCLUDE))
158 int (*matcher) (char const *, char const *, int) =
159 (options & EXCLUDE_WILDCARDS
161 : fnmatch_no_wildcards);
162 bool matched = ((*matcher) (pattern, f, options) == 0);
165 if (! (options & EXCLUDE_ANCHORED))
166 for (p = f; *p && ! matched; p++)
167 if (*p == '/' && p[1] != '/')
168 matched = ((*matcher) (pattern, p + 1, options) == 0);
178 /* Append to EX the exclusion PATTERN with OPTIONS. */
181 add_exclude (struct exclude *ex, char const *pattern, int options)
183 struct patopts *patopts;
185 if (ex->exclude_count == ex->exclude_alloc)
186 ex->exclude = x2nrealloc (ex->exclude, &ex->exclude_alloc,
187 sizeof *ex->exclude);
189 patopts = &ex->exclude[ex->exclude_count++];
190 patopts->pattern = pattern;
191 patopts->options = options;
194 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
195 OPTIONS. LINE_END terminates each pattern in the file. If
196 LINE_END is a space character, ignore trailing spaces and empty
197 lines in FILE. Return -1 on failure, 0 on success. */
200 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
201 struct exclude *ex, char const *file_name, int options,
204 bool use_stdin = file_name[0] == '-' && !file_name[1];
210 size_t buf_alloc = 0;
211 size_t buf_count = 0;
217 else if (! (in = fopen (file_name, "r")))
220 while ((c = getc (in)) != EOF)
222 if (buf_count == buf_alloc)
223 buf = x2realloc (buf, &buf_alloc);
224 buf[buf_count++] = c;
230 if (!use_stdin && fclose (in) != 0)
233 buf = xrealloc (buf, buf_count + 1);
234 buf[buf_count] = line_end;
235 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
238 for (p = buf; p < lim; p++)
241 char *pattern_end = p;
243 if (is_space (line_end))
245 for (; ; pattern_end--)
246 if (pattern_end == pattern)
248 else if (! is_space (pattern_end[-1]))
253 (*add_func) (ex, pattern, options);