d2c6502dff6c3a67fc3e17e86f38c29436ded7d0
[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 #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 "unlocked-io.h"
61 #include "xalloc.h"
62
63 #ifndef SIZE_MAX
64 # define SIZE_MAX ((size_t) -1)
65 #endif
66
67 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
68 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
69
70 /* Non-GNU systems lack these options, so we don't need to check them.  */
71 #ifndef FNM_CASEFOLD
72 # define FNM_CASEFOLD 0
73 #endif
74 #ifndef FNM_LEADING_DIR
75 # define FNM_LEADING_DIR 0
76 #endif
77
78 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
79         (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
80           & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
81              | FNM_CASEFOLD))
82          == 0));
83
84 /* An exclude pattern-options pair.  The options are fnmatch options
85    ORed with EXCLUDE_* options.  */
86
87 struct patopts
88   {
89     char const *pattern;
90     int options;
91   };
92
93 /* An exclude list, of pattern-options pairs.  */
94
95 struct exclude
96   {
97     struct patopts *exclude;
98     size_t exclude_alloc;
99     size_t exclude_count;
100   };
101
102 /* Return a newly allocated and empty exclude list.  */
103
104 struct exclude *
105 new_exclude (void)
106 {
107   struct exclude *ex = (struct exclude *) xmalloc (sizeof *ex);
108   ex->exclude_count = 0;
109   ex->exclude_alloc = (1 << 6); /* This must be a power of 2.  */
110   ex->exclude = (struct patopts *) xmalloc (ex->exclude_alloc
111                                             * sizeof ex->exclude[0]);
112   return ex;
113 }
114
115 /* Free the storage associated with an exclude list.  */
116
117 void
118 free_exclude (struct exclude *ex)
119 {
120   free (ex->exclude);
121   free (ex);
122 }
123
124 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
125    (unlike fnmatch) wildcards are disabled in PATTERN.  */
126
127 static int
128 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
129 {
130   if (! (options & FNM_LEADING_DIR))
131     return ((options & FNM_CASEFOLD)
132             ? strcasecmp (pattern, f)
133             : strcmp (pattern, f));
134   else
135     {
136       size_t patlen = strlen (pattern);
137       int r = ((options & FNM_CASEFOLD)
138                 ? strncasecmp (pattern, f, patlen)
139                 : strncmp (pattern, f, patlen));
140       if (! r)
141         {
142           r = f[patlen];
143           if (r == '/')
144             r = 0;
145         }
146       return r;
147     }
148 }
149
150 /* Return true if EX excludes F.  */
151
152 bool
153 excluded_filename (struct exclude const *ex, char const *f)
154 {
155   size_t exclude_count = ex->exclude_count;
156
157   /* If no options are given, the default is to include.  */
158   if (exclude_count == 0)
159     return false;
160   else
161     {
162       struct patopts const *exclude = ex->exclude;
163       size_t i;
164
165       /* Otherwise, the default is the opposite of the first option.  */
166       bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
167
168       /* Scan through the options, seeing whether they change F from
169          excluded to included or vice versa.  */
170       for (i = 0;  i < exclude_count;  i++)
171         {
172           char const *pattern = exclude[i].pattern;
173           int options = exclude[i].options;
174           if (excluded == !! (options & EXCLUDE_INCLUDE))
175             {
176               int (*matcher) (char const *, char const *, int) =
177                 (options & EXCLUDE_WILDCARDS
178                  ? fnmatch
179                  : fnmatch_no_wildcards);
180               bool matched = ((*matcher) (pattern, f, options) == 0);
181               char const *p;
182
183               if (! (options & EXCLUDE_ANCHORED))
184                 for (p = f; *p && ! matched; p++)
185                   if (*p == '/' && p[1] != '/')
186                     matched = ((*matcher) (pattern, p + 1, options) == 0);
187
188               excluded ^= matched;
189             }
190         }
191
192       return excluded;
193     }
194 }
195
196 /* Append to EX the exclusion PATTERN with OPTIONS.  */
197
198 void
199 add_exclude (struct exclude *ex, char const *pattern, int options)
200 {
201   struct patopts *patopts;
202
203   if (ex->exclude_alloc <= ex->exclude_count)
204     {
205       size_t s = 2 * ex->exclude_alloc;
206       if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
207         xalloc_die ();
208       ex->exclude_alloc = s;
209       ex->exclude = (struct patopts *) xrealloc (ex->exclude,
210                                                  s * sizeof ex->exclude[0]);
211     }
212
213   patopts = &ex->exclude[ex->exclude_count++];
214   patopts->pattern = pattern;
215   patopts->options = options;
216 }
217
218 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
219    OPTIONS.  LINE_END terminates each pattern in the file.  Return -1
220    on failure, 0 on success.  */
221
222 int
223 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
224                   struct exclude *ex, char const *filename, int options,
225                   char line_end)
226 {
227   bool use_stdin = filename[0] == '-' && !filename[1];
228   FILE *in;
229   char *buf;
230   char *p;
231   char const *pattern;
232   char const *lim;
233   size_t buf_alloc = (1 << 10);  /* This must be a power of two.  */
234   size_t buf_count = 0;
235   int c;
236   int e = 0;
237
238   if (use_stdin)
239     in = stdin;
240   else if (! (in = fopen (filename, "r")))
241     return -1;
242
243   buf = xmalloc (buf_alloc);
244
245   while ((c = getc (in)) != EOF)
246     {
247       buf[buf_count++] = c;
248       if (buf_count == buf_alloc)
249         {
250           buf_alloc *= 2;
251           if (! buf_alloc)
252             xalloc_die ();
253           buf = xrealloc (buf, buf_alloc);
254         }
255     }
256
257   if (ferror (in))
258     e = errno;
259
260   if (!use_stdin && fclose (in) != 0)
261     e = errno;
262
263   buf = xrealloc (buf, buf_count + 1);
264
265   for (pattern = p = buf, lim = buf + buf_count;  p <= lim;  p++)
266     if (p < lim ? *p == line_end : buf < p && p[-1])
267       {
268         *p = '\0';
269         (*add_func) (ex, pattern, options);
270         pattern = p + 1;
271       }
272
273   errno = e;
274   return e ? -1 : 0;
275 }