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