xref: /netbsd-src/external/gpl2/diffutils/dist/lib/exclude.c (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: exclude.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* exclude.c -- exclude file names
4*75f6d617Schristos 
5*75f6d617Schristos    Copyright 1992, 1993, 1994, 1997, 1999, 2000, 2001 Free Software
6*75f6d617Schristos    Foundation, Inc.
7*75f6d617Schristos 
8*75f6d617Schristos    This program is free software; you can redistribute it and/or modify
9*75f6d617Schristos    it under the terms of the GNU General Public License as published by
10*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
11*75f6d617Schristos    any later version.
12*75f6d617Schristos 
13*75f6d617Schristos    This program is distributed in the hope that it will be useful,
14*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*75f6d617Schristos    GNU General Public License for more details.
17*75f6d617Schristos 
18*75f6d617Schristos    You should have received a copy of the GNU General Public License
19*75f6d617Schristos    along with this program; see the file COPYING.
20*75f6d617Schristos    If not, write to the Free Software Foundation,
21*75f6d617Schristos    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22*75f6d617Schristos 
23*75f6d617Schristos /* Written by Paul Eggert <eggert@twinsun.com>  */
24*75f6d617Schristos 
25*75f6d617Schristos #if HAVE_CONFIG_H
26*75f6d617Schristos # include <config.h>
27*75f6d617Schristos #endif
28*75f6d617Schristos 
29*75f6d617Schristos #if HAVE_STDBOOL_H
30*75f6d617Schristos # include <stdbool.h>
31*75f6d617Schristos #else
32*75f6d617Schristos typedef enum {false = 0, true = 1} bool;
33*75f6d617Schristos #endif
34*75f6d617Schristos 
35*75f6d617Schristos #include <errno.h>
36*75f6d617Schristos #ifndef errno
37*75f6d617Schristos extern int errno;
38*75f6d617Schristos #endif
39*75f6d617Schristos #include <stdio.h>
40*75f6d617Schristos #if HAVE_SYS_TYPES_H
41*75f6d617Schristos # include <sys/types.h>
42*75f6d617Schristos #endif
43*75f6d617Schristos #if HAVE_STDLIB_H
44*75f6d617Schristos # include <stdlib.h>
45*75f6d617Schristos #endif
46*75f6d617Schristos #if HAVE_STRING_H
47*75f6d617Schristos # include <string.h>
48*75f6d617Schristos #endif
49*75f6d617Schristos #if HAVE_STRINGS_H
50*75f6d617Schristos # include <strings.h>
51*75f6d617Schristos #endif
52*75f6d617Schristos #if HAVE_INTTYPES_H
53*75f6d617Schristos # include <inttypes.h>
54*75f6d617Schristos #else
55*75f6d617Schristos # if HAVE_STDINT_H
56*75f6d617Schristos #  include <stdint.h>
57*75f6d617Schristos # endif
58*75f6d617Schristos #endif
59*75f6d617Schristos 
60*75f6d617Schristos #include "exclude.h"
61*75f6d617Schristos #include "fnmatch.h"
62*75f6d617Schristos #include "unlocked-io.h"
63*75f6d617Schristos #include "xalloc.h"
64*75f6d617Schristos 
65*75f6d617Schristos #ifndef SIZE_MAX
66*75f6d617Schristos # define SIZE_MAX ((size_t) -1)
67*75f6d617Schristos #endif
68*75f6d617Schristos 
69*75f6d617Schristos /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
70*75f6d617Schristos #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
71*75f6d617Schristos 
72*75f6d617Schristos verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
73*75f6d617Schristos 	(((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
74*75f6d617Schristos 	  & (FNM_FILE_NAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
75*75f6d617Schristos 	     | FNM_CASEFOLD))
76*75f6d617Schristos 	 == 0));
77*75f6d617Schristos 
78*75f6d617Schristos /* An exclude pattern-options pair.  The options are fnmatch options
79*75f6d617Schristos    ORed with EXCLUDE_* options.  */
80*75f6d617Schristos 
81*75f6d617Schristos struct patopts
82*75f6d617Schristos   {
83*75f6d617Schristos     char const *pattern;
84*75f6d617Schristos     int options;
85*75f6d617Schristos   };
86*75f6d617Schristos 
87*75f6d617Schristos /* An exclude list, of pattern-options pairs.  */
88*75f6d617Schristos 
89*75f6d617Schristos struct exclude
90*75f6d617Schristos   {
91*75f6d617Schristos     struct patopts *exclude;
92*75f6d617Schristos     size_t exclude_alloc;
93*75f6d617Schristos     size_t exclude_count;
94*75f6d617Schristos   };
95*75f6d617Schristos 
96*75f6d617Schristos /* Return a newly allocated and empty exclude list.  */
97*75f6d617Schristos 
98*75f6d617Schristos struct exclude *
new_exclude(void)99*75f6d617Schristos new_exclude (void)
100*75f6d617Schristos {
101*75f6d617Schristos   struct exclude *ex = (struct exclude *) xmalloc (sizeof *ex);
102*75f6d617Schristos   ex->exclude_count = 0;
103*75f6d617Schristos   ex->exclude_alloc = (1 << 6); /* This must be a power of 2.  */
104*75f6d617Schristos   ex->exclude = (struct patopts *) xmalloc (ex->exclude_alloc
105*75f6d617Schristos 					    * sizeof ex->exclude[0]);
106*75f6d617Schristos   return ex;
107*75f6d617Schristos }
108*75f6d617Schristos 
109*75f6d617Schristos /* Free the storage associated with an exclude list.  */
110*75f6d617Schristos 
111*75f6d617Schristos void
free_exclude(struct exclude * ex)112*75f6d617Schristos free_exclude (struct exclude *ex)
113*75f6d617Schristos {
114*75f6d617Schristos   free (ex->exclude);
115*75f6d617Schristos   free (ex);
116*75f6d617Schristos }
117*75f6d617Schristos 
118*75f6d617Schristos /* Return zero if PATTERN matches F, obeying OPTIONS, except that
119*75f6d617Schristos    (unlike fnmatch) wildcards are disabled in PATTERN.  */
120*75f6d617Schristos 
121*75f6d617Schristos static int
fnmatch_no_wildcards(char const * pattern,char const * f,int options)122*75f6d617Schristos fnmatch_no_wildcards (char const *pattern, char const *f, int options)
123*75f6d617Schristos {
124*75f6d617Schristos   if (! (options & FNM_LEADING_DIR))
125*75f6d617Schristos     return ((options & FNM_CASEFOLD)
126*75f6d617Schristos 	    ? strcasecmp (pattern, f)
127*75f6d617Schristos 	    : strcmp (pattern, f));
128*75f6d617Schristos   else
129*75f6d617Schristos     {
130*75f6d617Schristos       size_t patlen = strlen (pattern);
131*75f6d617Schristos       int r = ((options & FNM_CASEFOLD)
132*75f6d617Schristos 		? strncasecmp (pattern, f, patlen)
133*75f6d617Schristos 		: strncmp (pattern, f, patlen));
134*75f6d617Schristos       if (! r)
135*75f6d617Schristos 	{
136*75f6d617Schristos 	  r = f[patlen];
137*75f6d617Schristos 	  if (r == '/')
138*75f6d617Schristos 	    r = 0;
139*75f6d617Schristos 	}
140*75f6d617Schristos       return r;
141*75f6d617Schristos     }
142*75f6d617Schristos }
143*75f6d617Schristos 
144*75f6d617Schristos /* Return true if EX excludes F.  */
145*75f6d617Schristos 
146*75f6d617Schristos bool
excluded_filename(struct exclude const * ex,char const * f)147*75f6d617Schristos excluded_filename (struct exclude const *ex, char const *f)
148*75f6d617Schristos {
149*75f6d617Schristos   size_t exclude_count = ex->exclude_count;
150*75f6d617Schristos 
151*75f6d617Schristos   /* If no options are given, the default is to include.  */
152*75f6d617Schristos   if (exclude_count == 0)
153*75f6d617Schristos     return false;
154*75f6d617Schristos   else
155*75f6d617Schristos     {
156*75f6d617Schristos       struct patopts const *exclude = ex->exclude;
157*75f6d617Schristos       size_t i;
158*75f6d617Schristos 
159*75f6d617Schristos       /* Otherwise, the default is the opposite of the first option.  */
160*75f6d617Schristos       bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
161*75f6d617Schristos 
162*75f6d617Schristos       /* Scan through the options, seeing whether they change F from
163*75f6d617Schristos 	 excluded to included or vice versa.  */
164*75f6d617Schristos       for (i = 0;  i < exclude_count;  i++)
165*75f6d617Schristos 	{
166*75f6d617Schristos 	  char const *pattern = exclude[i].pattern;
167*75f6d617Schristos 	  int options = exclude[i].options;
168*75f6d617Schristos 	  if (excluded == !! (options & EXCLUDE_INCLUDE))
169*75f6d617Schristos 	    {
170*75f6d617Schristos 	      int (*matcher) PARAMS ((char const *, char const *, int)) =
171*75f6d617Schristos 		(options & EXCLUDE_WILDCARDS
172*75f6d617Schristos 		 ? fnmatch
173*75f6d617Schristos 		 : fnmatch_no_wildcards);
174*75f6d617Schristos 	      bool matched = ((*matcher) (pattern, f, options) == 0);
175*75f6d617Schristos 	      char const *p;
176*75f6d617Schristos 
177*75f6d617Schristos 	      if (! (options & EXCLUDE_ANCHORED))
178*75f6d617Schristos 		for (p = f; *p && ! matched; p++)
179*75f6d617Schristos 		  if (*p == '/' && p[1] != '/')
180*75f6d617Schristos 		    matched = ((*matcher) (pattern, p + 1, options) == 0);
181*75f6d617Schristos 
182*75f6d617Schristos 	      excluded ^= matched;
183*75f6d617Schristos 	    }
184*75f6d617Schristos 	}
185*75f6d617Schristos 
186*75f6d617Schristos       return excluded;
187*75f6d617Schristos     }
188*75f6d617Schristos }
189*75f6d617Schristos 
190*75f6d617Schristos /* Append to EX the exclusion PATTERN with OPTIONS.  */
191*75f6d617Schristos 
192*75f6d617Schristos void
add_exclude(struct exclude * ex,char const * pattern,int options)193*75f6d617Schristos add_exclude (struct exclude *ex, char const *pattern, int options)
194*75f6d617Schristos {
195*75f6d617Schristos   struct patopts *patopts;
196*75f6d617Schristos 
197*75f6d617Schristos   if (ex->exclude_alloc <= ex->exclude_count)
198*75f6d617Schristos     {
199*75f6d617Schristos       size_t s = 2 * ex->exclude_alloc;
200*75f6d617Schristos       if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
201*75f6d617Schristos 	xalloc_die ();
202*75f6d617Schristos       ex->exclude_alloc = s;
203*75f6d617Schristos       ex->exclude = (struct patopts *) xrealloc (ex->exclude,
204*75f6d617Schristos 						 s * sizeof ex->exclude[0]);
205*75f6d617Schristos     }
206*75f6d617Schristos 
207*75f6d617Schristos   patopts = &ex->exclude[ex->exclude_count++];
208*75f6d617Schristos   patopts->pattern = pattern;
209*75f6d617Schristos   patopts->options = options;
210*75f6d617Schristos }
211*75f6d617Schristos 
212*75f6d617Schristos /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
213*75f6d617Schristos    OPTIONS.  LINE_END terminates each pattern in the file.  Return -1
214*75f6d617Schristos    on failure, 0 on success.  */
215*75f6d617Schristos 
216*75f6d617Schristos int
add_exclude_file(void (* add_func)PARAMS ((struct exclude *,char const *,int)),struct exclude * ex,char const * filename,int options,char line_end)217*75f6d617Schristos add_exclude_file (void (*add_func) PARAMS ((struct exclude *,
218*75f6d617Schristos 					    char const *, int)),
219*75f6d617Schristos 		  struct exclude *ex, char const *filename, int options,
220*75f6d617Schristos 		  char line_end)
221*75f6d617Schristos {
222*75f6d617Schristos   bool use_stdin = filename[0] == '-' && !filename[1];
223*75f6d617Schristos   FILE *in;
224*75f6d617Schristos   char *buf;
225*75f6d617Schristos   char *p;
226*75f6d617Schristos   char const *pattern;
227*75f6d617Schristos   char const *lim;
228*75f6d617Schristos   size_t buf_alloc = (1 << 10);  /* This must be a power of two.  */
229*75f6d617Schristos   size_t buf_count = 0;
230*75f6d617Schristos   int c;
231*75f6d617Schristos   int e = 0;
232*75f6d617Schristos 
233*75f6d617Schristos   if (use_stdin)
234*75f6d617Schristos     in = stdin;
235*75f6d617Schristos   else if (! (in = fopen (filename, "r")))
236*75f6d617Schristos     return -1;
237*75f6d617Schristos 
238*75f6d617Schristos   buf = xmalloc (buf_alloc);
239*75f6d617Schristos 
240*75f6d617Schristos   while ((c = getc (in)) != EOF)
241*75f6d617Schristos     {
242*75f6d617Schristos       buf[buf_count++] = c;
243*75f6d617Schristos       if (buf_count == buf_alloc)
244*75f6d617Schristos 	{
245*75f6d617Schristos 	  buf_alloc *= 2;
246*75f6d617Schristos 	  if (! buf_alloc)
247*75f6d617Schristos 	    xalloc_die ();
248*75f6d617Schristos 	  buf = xrealloc (buf, buf_alloc);
249*75f6d617Schristos 	}
250*75f6d617Schristos     }
251*75f6d617Schristos 
252*75f6d617Schristos   if (ferror (in))
253*75f6d617Schristos     e = errno;
254*75f6d617Schristos 
255*75f6d617Schristos   if (!use_stdin && fclose (in) != 0)
256*75f6d617Schristos     e = errno;
257*75f6d617Schristos 
258*75f6d617Schristos   buf = xrealloc (buf, buf_count + 1);
259*75f6d617Schristos 
260*75f6d617Schristos   for (pattern = p = buf, lim = buf + buf_count;  p <= lim;  p++)
261*75f6d617Schristos     if (p < lim ? *p == line_end : buf < p && p[-1])
262*75f6d617Schristos       {
263*75f6d617Schristos 	*p = '\0';
264*75f6d617Schristos 	(*add_func) (ex, pattern, options);
265*75f6d617Schristos 	pattern = p + 1;
266*75f6d617Schristos       }
267*75f6d617Schristos 
268*75f6d617Schristos   errno = e;
269*75f6d617Schristos   return e ? -1 : 0;
270*75f6d617Schristos }
271