1 /* exclude.c -- exclude file names
3 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003 Free
4 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Written by Paul Eggert <eggert@twinsun.com> */
46 # include <inttypes.h>
55 #include "unlocked-io.h"
59 # define SIZE_MAX ((size_t) -1)
62 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
63 # define IN_CTYPE_DOMAIN(c) true
65 # define IN_CTYPE_DOMAIN(c) isascii (c)
69 is_space (unsigned char c)
71 return IN_CTYPE_DOMAIN (c) && isspace (c);
74 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
75 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
77 /* Non-GNU systems lack these options, so we don't need to check them. */
79 # define FNM_CASEFOLD 0
81 #ifndef FNM_LEADING_DIR
82 # define FNM_LEADING_DIR 0
85 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
86 (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
87 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
91 /* An exclude pattern-options pair. The options are fnmatch options
92 ORed with EXCLUDE_* options. */
100 /* An exclude list, of pattern-options pairs. */
104 struct patopts *exclude;
105 size_t exclude_alloc;
106 size_t exclude_count;
109 /* Return a newly allocated and empty exclude list. */
114 struct exclude *ex = xmalloc (sizeof *ex);
115 ex->exclude_count = 0;
116 ex->exclude_alloc = (1 << 6); /* This must be a power of 2. */
117 ex->exclude = xmalloc (ex->exclude_alloc * sizeof ex->exclude[0]);
121 /* Free the storage associated with an exclude list. */
124 free_exclude (struct exclude *ex)
130 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
131 (unlike fnmatch) wildcards are disabled in PATTERN. */
134 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
136 if (! (options & FNM_LEADING_DIR))
137 return ((options & FNM_CASEFOLD)
138 ? strcasecmp (pattern, f)
139 : strcmp (pattern, f));
142 size_t patlen = strlen (pattern);
143 int r = ((options & FNM_CASEFOLD)
144 ? strncasecmp (pattern, f, patlen)
145 : strncmp (pattern, f, patlen));
156 /* Return true if EX excludes F. */
159 excluded_filename (struct exclude const *ex, char const *f)
161 size_t exclude_count = ex->exclude_count;
163 /* If no options are given, the default is to include. */
164 if (exclude_count == 0)
168 struct patopts const *exclude = ex->exclude;
171 /* Otherwise, the default is the opposite of the first option. */
172 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
174 /* Scan through the options, seeing whether they change F from
175 excluded to included or vice versa. */
176 for (i = 0; i < exclude_count; i++)
178 char const *pattern = exclude[i].pattern;
179 int options = exclude[i].options;
180 if (excluded == !! (options & EXCLUDE_INCLUDE))
182 int (*matcher) (char const *, char const *, int) =
183 (options & EXCLUDE_WILDCARDS
185 : fnmatch_no_wildcards);
186 bool matched = ((*matcher) (pattern, f, options) == 0);
189 if (! (options & EXCLUDE_ANCHORED))
190 for (p = f; *p && ! matched; p++)
191 if (*p == '/' && p[1] != '/')
192 matched = ((*matcher) (pattern, p + 1, options) == 0);
202 /* Append to EX the exclusion PATTERN with OPTIONS. */
205 add_exclude (struct exclude *ex, char const *pattern, int options)
207 struct patopts *patopts;
209 if (ex->exclude_alloc <= ex->exclude_count)
211 size_t s = 2 * ex->exclude_alloc;
212 if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
214 ex->exclude_alloc = s;
215 ex->exclude = xrealloc (ex->exclude, s * sizeof ex->exclude[0]);
218 patopts = &ex->exclude[ex->exclude_count++];
219 patopts->pattern = pattern;
220 patopts->options = options;
223 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
224 OPTIONS. LINE_END terminates each pattern in the file. If
225 LINE_END is a space character, ignore trailing spaces and empty
226 lines in FILE. Return -1 on failure, 0 on success. */
229 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
230 struct exclude *ex, char const *filename, int options,
233 bool use_stdin = filename[0] == '-' && !filename[1];
239 size_t buf_alloc = (1 << 10); /* This must be a power of two. */
240 size_t buf_count = 0;
246 else if (! (in = fopen (filename, "r")))
249 buf = xmalloc (buf_alloc);
251 while ((c = getc (in)) != EOF)
253 buf[buf_count++] = c;
254 if (buf_count == buf_alloc)
259 buf = xrealloc (buf, buf_alloc);
266 if (!use_stdin && fclose (in) != 0)
269 buf = xrealloc (buf, buf_count + 1);
271 for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++)
272 if (p < lim ? *p == line_end : buf < p && p[-1])
274 if (is_space (line_end))
276 char *pattern_end = p;
277 for (; ; pattern_end--)
278 if (pattern_end == pattern)
280 else if (! is_space (pattern_end[-1]))
285 (*add_func) (ex, pattern, options);