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