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_LEADING_DIR
))
122 return ((options
& FNM_CASEFOLD
)
123 ? strcasecmp (pattern
, f
)
124 : strcmp (pattern
, f
));
127 size_t patlen
= strlen (pattern
);
128 int r
= ((options
& FNM_CASEFOLD
)
129 ? strncasecmp (pattern
, f
, patlen
)
130 : strncmp (pattern
, f
, patlen
));
141 /* Return true if EX excludes F. */
144 excluded_filename (struct exclude
const *ex
, char const *f
)
146 size_t exclude_count
= ex
->exclude_count
;
148 /* If no options are given, the default is to include. */
149 if (exclude_count
== 0)
153 struct patopts
const *exclude
= ex
->exclude
;
156 /* Otherwise, the default is the opposite of the first option. */
157 bool excluded
= !! (exclude
[0].options
& EXCLUDE_INCLUDE
);
159 /* Scan through the options, seeing whether they change F from
160 excluded to included or vice versa. */
161 for (i
= 0; i
< exclude_count
; i
++)
163 char const *pattern
= exclude
[i
].pattern
;
164 int options
= exclude
[i
].options
;
165 if (excluded
== !! (options
& EXCLUDE_INCLUDE
))
167 int (*matcher
) PARAMS ((char const *, char const *, int)) =
168 (options
& EXCLUDE_WILDCARDS
170 : fnmatch_no_wildcards
);
171 bool matched
= ((*matcher
) (pattern
, f
, options
) == 0);
174 if (! (options
& EXCLUDE_ANCHORED
))
175 for (p
= f
; *p
&& ! matched
; p
++)
176 if (*p
== '/' && p
[1] != '/')
177 matched
= ((*matcher
) (pattern
, p
+ 1, options
) == 0);
187 /* Append to EX the exclusion PATTERN with OPTIONS. */
190 add_exclude (struct exclude
*ex
, char const *pattern
, int options
)
192 struct patopts
*patopts
;
194 if (ex
->exclude_alloc
<= ex
->exclude_count
)
196 size_t s
= 2 * ex
->exclude_alloc
;
197 if (! (0 < s
&& s
<= SIZE_MAX
/ sizeof ex
->exclude
[0]))
199 ex
->exclude_alloc
= s
;
200 ex
->exclude
= (struct patopts
*) xrealloc (ex
->exclude
,
201 s
* sizeof ex
->exclude
[0]);
204 patopts
= &ex
->exclude
[ex
->exclude_count
++];
205 patopts
->pattern
= pattern
;
206 patopts
->options
= options
;
209 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
210 OPTIONS. LINE_END terminates each pattern in the file. Return -1
211 on failure, 0 on success. */
214 add_exclude_file (void (*add_func
) PARAMS ((struct exclude
*,
216 struct exclude
*ex
, char const *filename
, int options
,
219 bool use_stdin
= filename
[0] == '-' && !filename
[1];
225 size_t buf_alloc
= (1 << 10); /* This must be a power of two. */
226 size_t buf_count
= 0;
232 else if (! (in
= fopen (filename
, "r")))
235 buf
= xmalloc (buf_alloc
);
237 while ((c
= getc (in
)) != EOF
)
239 buf
[buf_count
++] = c
;
240 if (buf_count
== buf_alloc
)
245 buf
= xrealloc (buf
, buf_alloc
);
252 if (!use_stdin
&& fclose (in
) != 0)
255 buf
= xrealloc (buf
, buf_count
+ 1);
257 for (pattern
= p
= buf
, lim
= buf
+ buf_count
; p
<= lim
; p
++)
258 if (p
< lim
? *p
== line_end
: buf
< p
&& p
[-1])
261 (*add_func
) (ex
, pattern
, options
);