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