Remove K&R cruft.
[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 <ctype.h>
30 #include <errno.h>
31 #ifndef errno
32 extern int errno;
33 #endif
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #if HAVE_INTTYPES_H
39 # include <inttypes.h>
40 #else
41 # if HAVE_STDINT_H
42 #  include <stdint.h>
43 # endif
44 #endif
45
46 #include "exclude.h"
47 #include "fnmatch.h"
48 #include "unlocked-io.h"
49 #include "xalloc.h"
50
51 #ifndef SIZE_MAX
52 # define SIZE_MAX ((size_t) -1)
53 #endif
54
55 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
56 # define IN_CTYPE_DOMAIN(c) true
57 #else
58 # define IN_CTYPE_DOMAIN(c) isascii (c)
59 #endif
60
61 static inline bool
62 is_space (unsigned char c)
63 {
64   return IN_CTYPE_DOMAIN (c) && isspace (c);
65 }
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 = xmalloc (sizeof *ex);
108   ex->exclude_count = 0;
109   ex->exclude_alloc = (1 << 6); /* This must be a power of 2.  */
110   ex->exclude = xmalloc (ex->exclude_alloc * sizeof ex->exclude[0]);
111   return ex;
112 }
113
114 /* Free the storage associated with an exclude list.  */
115
116 void
117 free_exclude (struct exclude *ex)
118 {
119   free (ex->exclude);
120   free (ex);
121 }
122
123 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
124    (unlike fnmatch) wildcards are disabled in PATTERN.  */
125
126 static int
127 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
128 {
129   if (! (options & FNM_LEADING_DIR))
130     return ((options & FNM_CASEFOLD)
131             ? strcasecmp (pattern, f)
132             : strcmp (pattern, f));
133   else
134     {
135       size_t patlen = strlen (pattern);
136       int r = ((options & FNM_CASEFOLD)
137                 ? strncasecmp (pattern, f, patlen)
138                 : strncmp (pattern, f, patlen));
139       if (! r)
140         {
141           r = f[patlen];
142           if (r == '/')
143             r = 0;
144         }
145       return r;
146     }
147 }
148
149 /* Return true if EX excludes F.  */
150
151 bool
152 excluded_filename (struct exclude const *ex, char const *f)
153 {
154   size_t exclude_count = ex->exclude_count;
155
156   /* If no options are given, the default is to include.  */
157   if (exclude_count == 0)
158     return false;
159   else
160     {
161       struct patopts const *exclude = ex->exclude;
162       size_t i;
163
164       /* Otherwise, the default is the opposite of the first option.  */
165       bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
166
167       /* Scan through the options, seeing whether they change F from
168          excluded to included or vice versa.  */
169       for (i = 0;  i < exclude_count;  i++)
170         {
171           char const *pattern = exclude[i].pattern;
172           int options = exclude[i].options;
173           if (excluded == !! (options & EXCLUDE_INCLUDE))
174             {
175               int (*matcher) (char const *, char const *, int) =
176                 (options & EXCLUDE_WILDCARDS
177                  ? fnmatch
178                  : fnmatch_no_wildcards);
179               bool matched = ((*matcher) (pattern, f, options) == 0);
180               char const *p;
181
182               if (! (options & EXCLUDE_ANCHORED))
183                 for (p = f; *p && ! matched; p++)
184                   if (*p == '/' && p[1] != '/')
185                     matched = ((*matcher) (pattern, p + 1, options) == 0);
186
187               excluded ^= matched;
188             }
189         }
190
191       return excluded;
192     }
193 }
194
195 /* Append to EX the exclusion PATTERN with OPTIONS.  */
196
197 void
198 add_exclude (struct exclude *ex, char const *pattern, int options)
199 {
200   struct patopts *patopts;
201
202   if (ex->exclude_alloc <= ex->exclude_count)
203     {
204       size_t s = 2 * ex->exclude_alloc;
205       if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
206         xalloc_die ();
207       ex->exclude_alloc = s;
208       ex->exclude = xrealloc (ex->exclude, s * sizeof ex->exclude[0]);
209     }
210
211   patopts = &ex->exclude[ex->exclude_count++];
212   patopts->pattern = pattern;
213   patopts->options = options;
214 }
215
216 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
217    OPTIONS.  LINE_END terminates each pattern in the file.  If
218    LINE_END is a space character, ignore trailing spaces and empty
219    lines in FILE.  Return -1 on failure, 0 on success.  */
220
221 int
222 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
223                   struct exclude *ex, char const *filename, int options,
224                   char line_end)
225 {
226   bool use_stdin = filename[0] == '-' && !filename[1];
227   FILE *in;
228   char *buf;
229   char *p;
230   char const *pattern;
231   char const *lim;
232   size_t buf_alloc = (1 << 10);  /* This must be a power of two.  */
233   size_t buf_count = 0;
234   int c;
235   int e = 0;
236
237   if (use_stdin)
238     in = stdin;
239   else if (! (in = fopen (filename, "r")))
240     return -1;
241
242   buf = xmalloc (buf_alloc);
243
244   while ((c = getc (in)) != EOF)
245     {
246       buf[buf_count++] = c;
247       if (buf_count == buf_alloc)
248         {
249           buf_alloc *= 2;
250           if (! buf_alloc)
251             xalloc_die ();
252           buf = xrealloc (buf, buf_alloc);
253         }
254     }
255
256   if (ferror (in))
257     e = errno;
258
259   if (!use_stdin && fclose (in) != 0)
260     e = errno;
261
262   buf = xrealloc (buf, buf_count + 1);
263   buf[buf_count] = line_end;
264   lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
265   pattern = buf;
266
267   for (p = buf; p < lim; p++)
268     if (*p == line_end)
269       {
270         char *pattern_end = p;
271
272         if (is_space (line_end))
273           {
274             for (; ; pattern_end--)
275               if (pattern_end == pattern)
276                 goto next_pattern;
277               else if (! is_space (pattern_end[-1]))
278                 break;
279           }
280
281         *pattern_end = '\0';
282         (*add_func) (ex, pattern, options);
283
284       next_pattern:
285         pattern = p + 1;
286       }
287
288   errno = e;
289   return e ? -1 : 0;
290 }