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