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> */
45 # include <inttypes.h>
54 #include "unlocked-io.h"
58 # define SIZE_MAX ((size_t) -1)
61 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
62 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
64 /* Non-GNU systems lack these options, so we don't need to check them. */
66 # define FNM_CASEFOLD 0
68 #ifndef FNM_LEADING_DIR
69 # define FNM_LEADING_DIR 0
72 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
73 (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
74 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
78 /* An exclude pattern-options pair. The options are fnmatch options
79 ORed with EXCLUDE_* options. */
87 /* An exclude list, of pattern-options pairs. */
91 struct patopts *exclude;
96 /* Return a newly allocated and empty exclude list. */
101 struct exclude *ex = (struct exclude *) xmalloc (sizeof *ex);
102 ex->exclude_count = 0;
103 ex->exclude_alloc = (1 << 6); /* This must be a power of 2. */
104 ex->exclude = (struct patopts *) xmalloc (ex->exclude_alloc
105 * sizeof ex->exclude[0]);
109 /* Free the storage associated with an exclude list. */
112 free_exclude (struct exclude *ex)
118 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
119 (unlike fnmatch) wildcards are disabled in PATTERN. */
122 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
124 if (! (options & FNM_LEADING_DIR))
125 return ((options & FNM_CASEFOLD)
126 ? strcasecmp (pattern, f)
127 : strcmp (pattern, f));
130 size_t patlen = strlen (pattern);
131 int r = ((options & FNM_CASEFOLD)
132 ? strncasecmp (pattern, f, patlen)
133 : strncmp (pattern, f, patlen));
144 /* Return true if EX excludes F. */
147 excluded_filename (struct exclude const *ex, char const *f)
149 size_t exclude_count = ex->exclude_count;
151 /* If no options are given, the default is to include. */
152 if (exclude_count == 0)
156 struct patopts const *exclude = ex->exclude;
159 /* Otherwise, the default is the opposite of the first option. */
160 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
162 /* Scan through the options, seeing whether they change F from
163 excluded to included or vice versa. */
164 for (i = 0; i < exclude_count; i++)
166 char const *pattern = exclude[i].pattern;
167 int options = exclude[i].options;
168 if (excluded == !! (options & EXCLUDE_INCLUDE))
170 int (*matcher) (char const *, char const *, int) =
171 (options & EXCLUDE_WILDCARDS
173 : fnmatch_no_wildcards);
174 bool matched = ((*matcher) (pattern, f, options) == 0);
177 if (! (options & EXCLUDE_ANCHORED))
178 for (p = f; *p && ! matched; p++)
179 if (*p == '/' && p[1] != '/')
180 matched = ((*matcher) (pattern, p + 1, options) == 0);
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_alloc <= ex->exclude_count)
199 size_t s = 2 * ex->exclude_alloc;
200 if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
202 ex->exclude_alloc = s;
203 ex->exclude = (struct patopts *) xrealloc (ex->exclude,
204 s * sizeof ex->exclude[0]);
207 patopts = &ex->exclude[ex->exclude_count++];
208 patopts->pattern = pattern;
209 patopts->options = options;
212 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
213 OPTIONS. LINE_END terminates each pattern in the file. Return -1
214 on failure, 0 on success. */
217 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
218 struct exclude *ex, char const *filename, int options,
221 bool use_stdin = filename[0] == '-' && !filename[1];
227 size_t buf_alloc = (1 << 10); /* This must be a power of two. */
228 size_t buf_count = 0;
234 else if (! (in = fopen (filename, "r")))
237 buf = xmalloc (buf_alloc);
239 while ((c = getc (in)) != EOF)
241 buf[buf_count++] = c;
242 if (buf_count == buf_alloc)
247 buf = xrealloc (buf, buf_alloc);
254 if (!use_stdin && fclose (in) != 0)
257 buf = xrealloc (buf, buf_count + 1);
259 for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++)
260 if (p < lim ? *p == line_end : buf < p && p[-1])
263 (*add_func) (ex, pattern, options);