xref: /netbsd-src/usr.sbin/mtree/excludes.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*
2  * Copyright 2000 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 static const char rcsid[] =
31   "$FreeBSD: src/usr.sbin/mtree/excludes.c,v 1.5 2000/12/29 18:04:54 ben Exp $";
32 
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 
36 #include <err.h>
37 #include <fnmatch.h>
38 #include <fts.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <util.h>
44 
45 #include "extern.h"
46 
47 /*
48  * We're assuming that there won't be a whole lot of excludes,
49  * so it's OK to use a stupid algorithm.
50  */
51 struct exclude {
52 	LIST_ENTRY(exclude) link;
53 	const char *glob;
54 	int pathname;
55 };
56 static LIST_HEAD(, exclude) excludes;
57 
58 
59 void
60 init_excludes(void)
61 {
62 
63 	LIST_INIT(&excludes);
64 }
65 
66 void
67 read_excludes_file(const char *name)
68 {
69 	FILE *fp;
70 	char *line;
71 	struct exclude *e;
72 
73 	fp = fopen(name, "r");
74 	if (fp == 0)
75 		err(1, "%s", name);
76 
77 	while ((line = fparseln(fp, NULL, NULL, NULL,
78 	    FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC))
79 	    != NULL) {
80 		if (line[0] == '\0')
81 			continue;
82 
83 		if ((e = malloc(sizeof *e)) == NULL)
84 			mtree_err("memory allocation error");
85 
86 		e->glob = line;
87 		if (strchr(e->glob, '/') != NULL)
88 			e->pathname = 1;
89 		else
90 			e->pathname = 0;
91 		LIST_INSERT_HEAD(&excludes, e, link);
92 	}
93 	fclose(fp);
94 }
95 
96 int
97 check_excludes(const char *fname, const char *path)
98 {
99 	struct exclude *e;
100 
101 	/* fnmatch(3) has a funny return value convention... */
102 #define MATCH(g, n) (fnmatch((g), (n), FNM_PATHNAME) == 0)
103 
104 	e = LIST_FIRST(&excludes);
105 	while (e) {
106 		if ((e->pathname && MATCH(e->glob, path))
107 		    || MATCH(e->glob, fname)) {
108 			return (1);
109 		}
110 		e = LIST_NEXT(e, link);
111 	}
112 	return (0);
113 }
114