(new_exclude, add_exclude): Remove casts that are
[gnulib.git] / lib / exclude.c
1 /* exclude.c -- exclude file names
2
3    Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003 Free
4    Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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.  */
20
21 /* Written by Paul Eggert <eggert@twinsun.com>  */
22
23 #if HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdbool.h>
28
29 #include <errno.h>
30 #ifndef errno
31 extern int errno;
32 #endif
33 #include <stddef.h>
34 #include <stdio.h>
35 #if HAVE_STDLIB_H
36 # include <stdlib.h>
37 #endif
38 #if HAVE_STRING_H
39 # include <string.h>
40 #endif
41 #if HAVE_STRINGS_H
42 # include <strings.h>
43 #endif
44 #if HAVE_INTTYPES_H
45 # include <inttypes.h>
46 #else
47 # if HAVE_STDINT_H
48 #  include <stdint.h>
49 # endif
50 #endif
51
52 #include "exclude.h"
53 #include "fnmatch.h"
54 #include "unlocked-io.h"
55 #include "xalloc.h"
56
57 #ifndef SIZE_MAX
58 # define SIZE_MAX ((size_t) -1)
59 #endif
60
61 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
62 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
63
64 /* Non-GNU systems lack these options, so we don't need to check them.  */
65 #ifndef FNM_CASEFOLD
66 # define FNM_CASEFOLD 0
67 #endif
68 #ifndef FNM_LEADING_DIR
69 # define FNM_LEADING_DIR 0
70 #endif
71
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
75              | FNM_CASEFOLD))
76          == 0));
77
78 /* An exclude pattern-options pair.  The options are fnmatch options
79    ORed with EXCLUDE_* options.  */
80
81 struct patopts
82   {
83     char const *pattern;
84     int options;
85   };
86
87 /* An exclude list, of pattern-options pairs.  */
88
89 struct exclude
90   {
91     struct patopts *exclude;
92     size_t exclude_alloc;
93     size_t exclude_count;
94   };
95
96 /* Return a newly allocated and empty exclude list.  */
97
98 struct exclude *
99 new_exclude (void)
100 {
101   struct exclude *ex = xmalloc (sizeof *ex);
102   ex->exclude_count = 0;
103   ex->exclude_alloc = (1 << 6); /* This must be a power of 2.  */
104   ex->exclude = xmalloc (ex->exclude_alloc * sizeof ex->exclude[0]);
105   return ex;
106 }
107
108 /* Free the storage associated with an exclude list.  */
109
110 void
111 free_exclude (struct exclude *ex)
112 {
113   free (ex->exclude);
114   free (ex);
115 }
116
117 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
118    (unlike fnmatch) wildcards are disabled in PATTERN.  */
119
120 static int
121 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
122 {
123   if (! (options & FNM_LEADING_DIR))
124     return ((options & FNM_CASEFOLD)
125             ? strcasecmp (pattern, f)
126             : strcmp (pattern, f));
127   else
128     {
129       size_t patlen = strlen (pattern);
130       int r = ((options & FNM_CASEFOLD)
131                 ? strncasecmp (pattern, f, patlen)
132                 : strncmp (pattern, f, patlen));
133       if (! r)
134         {
135           r = f[patlen];
136           if (r == '/')
137             r = 0;
138         }
139       return r;
140     }
141 }
142
143 /* Return true if EX excludes F.  */
144
145 bool
146 excluded_filename (struct exclude const *ex, char const *f)
147 {
148   size_t exclude_count = ex->exclude_count;
149
150   /* If no options are given, the default is to include.  */
151   if (exclude_count == 0)
152     return false;
153   else
154     {
155       struct patopts const *exclude = ex->exclude;
156       size_t i;
157
158       /* Otherwise, the default is the opposite of the first option.  */
159       bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
160
161       /* Scan through the options, seeing whether they change F from
162          excluded to included or vice versa.  */
163       for (i = 0;  i < exclude_count;  i++)
164         {
165           char const *pattern = exclude[i].pattern;
166           int options = exclude[i].options;
167           if (excluded == !! (options & EXCLUDE_INCLUDE))
168             {
169               int (*matcher) (char const *, char const *, int) =
170                 (options & EXCLUDE_WILDCARDS
171                  ? fnmatch
172                  : fnmatch_no_wildcards);
173               bool matched = ((*matcher) (pattern, f, options) == 0);
174               char const *p;
175
176               if (! (options & EXCLUDE_ANCHORED))
177                 for (p = f; *p && ! matched; p++)
178                   if (*p == '/' && p[1] != '/')
179                     matched = ((*matcher) (pattern, p + 1, options) == 0);
180
181               excluded ^= matched;
182             }
183         }
184
185       return excluded;
186     }
187 }
188
189 /* Append to EX the exclusion PATTERN with OPTIONS.  */
190
191 void
192 add_exclude (struct exclude *ex, char const *pattern, int options)
193 {
194   struct patopts *patopts;
195
196   if (ex->exclude_alloc <= ex->exclude_count)
197     {
198       size_t s = 2 * ex->exclude_alloc;
199       if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
200         xalloc_die ();
201       ex->exclude_alloc = s;
202       ex->exclude = xrealloc (ex->exclude, s * sizeof ex->exclude[0]);
203     }
204
205   patopts = &ex->exclude[ex->exclude_count++];
206   patopts->pattern = pattern;
207   patopts->options = options;
208 }
209
210 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
211    OPTIONS.  LINE_END terminates each pattern in the file.  Return -1
212    on failure, 0 on success.  */
213
214 int
215 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
216                   struct exclude *ex, char const *filename, int options,
217                   char line_end)
218 {
219   bool use_stdin = filename[0] == '-' && !filename[1];
220   FILE *in;
221   char *buf;
222   char *p;
223   char const *pattern;
224   char const *lim;
225   size_t buf_alloc = (1 << 10);  /* This must be a power of two.  */
226   size_t buf_count = 0;
227   int c;
228   int e = 0;
229
230   if (use_stdin)
231     in = stdin;
232   else if (! (in = fopen (filename, "r")))
233     return -1;
234
235   buf = xmalloc (buf_alloc);
236
237   while ((c = getc (in)) != EOF)
238     {
239       buf[buf_count++] = c;
240       if (buf_count == buf_alloc)
241         {
242           buf_alloc *= 2;
243           if (! buf_alloc)
244             xalloc_die ();
245           buf = xrealloc (buf, buf_alloc);
246         }
247     }
248
249   if (ferror (in))
250     e = errno;
251
252   if (!use_stdin && fclose (in) != 0)
253     e = errno;
254
255   buf = xrealloc (buf, buf_count + 1);
256
257   for (pattern = p = buf, lim = buf + buf_count;  p <= lim;  p++)
258     if (p < lim ? *p == line_end : buf < p && p[-1])
259       {
260         *p = '\0';
261         (*add_func) (ex, pattern, options);
262         pattern = p + 1;
263       }
264
265   errno = e;
266   return e ? -1 : 0;
267 }