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> */
41 #include "unlocked-io.h"
44 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
45 # define IN_CTYPE_DOMAIN(c) true
47 # define IN_CTYPE_DOMAIN(c) isascii (c)
51 is_space (unsigned char c)
53 return IN_CTYPE_DOMAIN (c) && isspace (c);
56 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
57 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
59 /* Non-GNU systems lack these options, so we don't need to check them. */
61 # define FNM_CASEFOLD 0
63 #ifndef FNM_LEADING_DIR
64 # define FNM_LEADING_DIR 0
67 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
68 (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
69 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
73 /* An exclude pattern-options pair. The options are fnmatch options
74 ORed with EXCLUDE_* options. */
82 /* An exclude list, of pattern-options pairs. */
86 struct patopts *exclude;
91 /* Return a newly allocated and empty exclude list. */
96 struct exclude *ex = xmalloc (sizeof *ex);
97 ex->exclude_count = 0;
98 ex->exclude_alloc = 60;
99 ex->exclude = xmalloc (ex->exclude_alloc * sizeof ex->exclude[0]);
103 /* Free the storage associated with an exclude list. */
106 free_exclude (struct exclude *ex)
112 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
113 (unlike fnmatch) wildcards are disabled in PATTERN. */
116 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
118 if (! (options & FNM_LEADING_DIR))
119 return ((options & FNM_CASEFOLD)
120 ? strcasecmp (pattern, f)
121 : strcmp (pattern, f));
124 size_t patlen = strlen (pattern);
125 int r = ((options & FNM_CASEFOLD)
126 ? strncasecmp (pattern, f, patlen)
127 : strncmp (pattern, f, patlen));
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) (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 ex->exclude = xnrealloc (ex->exclude, ex->exclude_alloc,
194 2 * sizeof *ex->exclude);
195 ex->exclude_alloc *= 2;
198 patopts = &ex->exclude[ex->exclude_count++];
199 patopts->pattern = pattern;
200 patopts->options = options;
203 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
204 OPTIONS. LINE_END terminates each pattern in the file. If
205 LINE_END is a space character, ignore trailing spaces and empty
206 lines in FILE. Return -1 on failure, 0 on success. */
209 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
210 struct exclude *ex, char const *filename, int options,
213 bool use_stdin = filename[0] == '-' && !filename[1];
219 size_t buf_alloc = 1000;
220 size_t buf_count = 0;
226 else if (! (in = fopen (filename, "r")))
229 buf = xmalloc (buf_alloc);
231 while ((c = getc (in)) != EOF)
233 buf[buf_count++] = c;
234 if (buf_count == buf_alloc)
236 buf = xnrealloc (buf, buf_alloc, 2);
244 if (!use_stdin && fclose (in) != 0)
247 buf = xrealloc (buf, buf_count + 1);
248 buf[buf_count] = line_end;
249 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
252 for (p = buf; p < lim; p++)
255 char *pattern_end = p;
257 if (is_space (line_end))
259 for (; ; pattern_end--)
260 if (pattern_end == pattern)
262 else if (! is_space (pattern_end[-1]))
267 (*add_func) (ex, pattern, options);