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