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