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