1 /* exclude.c -- exclude file names
3 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003,
4 2004 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Written by Paul Eggert <eggert@twinsun.com> */
38 #include "unlocked-io.h"
41 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
42 # define IN_CTYPE_DOMAIN(c) true
44 # define IN_CTYPE_DOMAIN(c) isascii (c)
48 is_space (unsigned char c)
50 return IN_CTYPE_DOMAIN (c) && isspace (c);
53 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
54 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
56 /* Non-GNU systems lack these options, so we don't need to check them. */
58 # define FNM_CASEFOLD 0
60 #ifndef FNM_LEADING_DIR
61 # define FNM_LEADING_DIR 0
64 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
65 (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
66 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
70 /* An exclude pattern-options pair. The options are fnmatch options
71 ORed with EXCLUDE_* options. */
79 /* An exclude list, of pattern-options pairs. */
83 struct patopts *exclude;
88 /* Return a newly allocated and empty exclude list. */
93 return xzalloc (sizeof *new_exclude ());
96 /* Free the storage associated with an exclude list. */
99 free_exclude (struct exclude *ex)
105 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
106 (unlike fnmatch) wildcards are disabled in PATTERN. */
109 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
111 if (! (options & FNM_LEADING_DIR))
112 return ((options & FNM_CASEFOLD)
113 ? strcasecmp (pattern, f)
114 : strcmp (pattern, f));
117 size_t patlen = strlen (pattern);
118 int r = ((options & FNM_CASEFOLD)
119 ? strncasecmp (pattern, f, patlen)
120 : strncmp (pattern, f, patlen));
131 /* Return true if EX excludes F. */
134 excluded_filename (struct exclude const *ex, char const *f)
136 size_t exclude_count = ex->exclude_count;
138 /* If no options are given, the default is to include. */
139 if (exclude_count == 0)
143 struct patopts const *exclude = ex->exclude;
146 /* Otherwise, the default is the opposite of the first option. */
147 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
149 /* Scan through the options, seeing whether they change F from
150 excluded to included or vice versa. */
151 for (i = 0; i < exclude_count; i++)
153 char const *pattern = exclude[i].pattern;
154 int options = exclude[i].options;
155 if (excluded == !! (options & EXCLUDE_INCLUDE))
157 int (*matcher) (char const *, char const *, int) =
158 (options & EXCLUDE_WILDCARDS
160 : fnmatch_no_wildcards);
161 bool matched = ((*matcher) (pattern, f, options) == 0);
164 if (! (options & EXCLUDE_ANCHORED))
165 for (p = f; *p && ! matched; p++)
166 if (*p == '/' && p[1] != '/')
167 matched = ((*matcher) (pattern, p + 1, options) == 0);
177 /* Append to EX the exclusion PATTERN with OPTIONS. */
180 add_exclude (struct exclude *ex, char const *pattern, int options)
182 struct patopts *patopts;
184 if (ex->exclude_count == ex->exclude_alloc)
185 ex->exclude = x2nrealloc (ex->exclude, &ex->exclude_alloc,
186 sizeof *ex->exclude);
188 patopts = &ex->exclude[ex->exclude_count++];
189 patopts->pattern = pattern;
190 patopts->options = options;
193 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
194 OPTIONS. LINE_END terminates each pattern in the file. If
195 LINE_END is a space character, ignore trailing spaces and empty
196 lines in FILE. Return -1 on failure, 0 on success. */
199 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
200 struct exclude *ex, char const *filename, int options,
203 bool use_stdin = filename[0] == '-' && !filename[1];
209 size_t buf_alloc = 0;
210 size_t buf_count = 0;
216 else if (! (in = fopen (filename, "r")))
219 while ((c = getc (in)) != EOF)
221 if (buf_count == buf_alloc)
222 buf = x2realloc (buf, &buf_alloc);
223 buf[buf_count++] = c;
229 if (!use_stdin && fclose (in) != 0)
232 buf = xrealloc (buf, buf_count + 1);
233 buf[buf_count] = line_end;
234 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
237 for (p = buf; p < lim; p++)
240 char *pattern_end = p;
242 if (is_space (line_end))
244 for (; ; pattern_end--)
245 if (pattern_end == pattern)
247 else if (! is_space (pattern_end[-1]))
252 (*add_func) (ex, pattern, options);