1 /* exclude.c -- exclude file names
3 Copyright 1992, 1993, 1994, 1997, 1999, 2000, 2001 Free Software
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> */
30 typedef enum {false = 0, true = 1} bool;
39 # include <sys/types.h>
51 # include <inttypes.h>
63 # define SIZE_MAX ((size_t) -1)
66 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
67 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
69 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
70 (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
71 & (FNM_FILE_NAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
75 /* An exclude pattern-options pair. The options are fnmatch options
76 ORed with EXCLUDE_* options. */
84 /* An exclude list, of pattern-options pairs. */
88 struct patopts *exclude;
93 /* Return a newly allocated and empty exclude list. */
98 struct exclude *ex = (struct exclude *) xmalloc (sizeof *ex);
99 ex->exclude_count = 0;
100 ex->exclude_alloc = (1 << 6); /* This must be a power of 2. */
101 ex->exclude = (struct patopts *) xmalloc (ex->exclude_alloc
102 * sizeof ex->exclude[0]);
106 /* Free the storage associated with an exclude list. */
109 free_exclude (struct exclude *ex)
115 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
116 (unlike fnmatch) wildcards are disabled in PATTERN. */
119 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
121 if (! (options & FNM_CASEFOLD))
122 return (options & FNM_LEADING_DIR ? strcasecmp : strcmp) (pattern, f);
125 size_t patlen = strlen (pattern);
126 int r = ((options & FNM_LEADING_DIR ? strncasecmp : strncmp)
127 (pattern, f, patlen));
131 if (r == '/' && (options & FNM_LEADING_DIR))
138 /* Return true if EX excludes F. */
141 excluded_filename (struct exclude const *ex, char const *f)
143 size_t exclude_count = ex->exclude_count;
145 /* If no options are given, the default is to include. */
146 if (exclude_count == 0)
150 struct patopts const *exclude = ex->exclude;
153 /* Otherwise, the default is the opposite of the first option. */
154 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
156 /* Scan through the options, seeing whether they change F from
157 excluded to included or vice versa. */
158 for (i = 0; i < exclude_count; i++)
160 char const *pattern = exclude[i].pattern;
161 int options = exclude[i].options;
162 if (excluded == !! (options & EXCLUDE_INCLUDE))
164 int (*matcher) PARAMS ((char const *, char const *, int)) =
165 (options & EXCLUDE_WILDCARDS
167 : fnmatch_no_wildcards);
168 bool matched = ((*matcher) (pattern, f, options) == 0);
171 if (! (options & EXCLUDE_ANCHORED))
172 for (p = f; *p && ! matched; p++)
173 if (*p == '/' && p[1] != '/')
174 matched = ((*matcher) (pattern, p + 1, options) == 0);
184 /* Append to EX the exclusion PATTERN with OPTIONS. */
187 add_exclude (struct exclude *ex, char const *pattern, int options)
189 struct patopts *patopts;
191 if (ex->exclude_alloc <= ex->exclude_count)
193 size_t s = 2 * ex->exclude_alloc;
194 if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
196 ex->exclude_alloc = s;
197 ex->exclude = (struct patopts *) xrealloc (ex->exclude,
198 s * sizeof ex->exclude[0]);
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 FILENAME, each with
207 OPTIONS. LINE_END terminates each pattern in the file. Return -1
208 on failure, 0 on success. */
211 add_exclude_file (void (*add_func) PARAMS ((struct exclude *,
213 struct exclude *ex, char const *filename, int options,
216 bool use_stdin = filename[0] == '-' && !filename[1];
222 size_t buf_alloc = (1 << 10); /* This must be a power of two. */
223 size_t buf_count = 0;
229 else if (! (in = fopen (filename, "r")))
232 buf = xmalloc (buf_alloc);
234 while ((c = getc (in)) != EOF)
236 buf[buf_count++] = c;
237 if (buf_count == buf_alloc)
242 buf = xrealloc (buf, buf_alloc);
249 if (!use_stdin && fclose (in) != 0)
252 buf = xrealloc (buf, buf_count + 1);
254 for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++)
255 if (p < lim ? *p == line_end : buf < p && p[-1])
258 (*add_func) (ex, pattern, options);