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