1131554Stjr/* exclude.c -- exclude file names 2131554Stjr Copyright 1992, 1993, 1994, 1997, 1999, 2000 Free Software Foundation, Inc. 3131554Stjr 4131554Stjr This program is free software; you can redistribute it and/or modify 5131554Stjr it under the terms of the GNU General Public License as published by 6131554Stjr the Free Software Foundation; either version 2, or (at your option) 7131554Stjr any later version. 8131554Stjr 9131554Stjr This program is distributed in the hope that it will be useful, 10131554Stjr but WITHOUT ANY WARRANTY; without even the implied warranty of 11131554Stjr MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12131554Stjr GNU General Public License for more details. 13131554Stjr 14131554Stjr You should have received a copy of the GNU General Public License 15131554Stjr along with this program; see the file COPYING. 16131554Stjr If not, write to the Free Software Foundation, 17131554Stjr 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18131554Stjr 19131554Stjr/* Written by Paul Eggert <eggert@twinsun.com> */ 20131554Stjr 21131554Stjr#if HAVE_CONFIG_H 22131554Stjr# include <config.h> 23131554Stjr#endif 24131554Stjr 25131554Stjr#include <errno.h> 26131554Stjr#ifndef errno 27131554Stjrextern int errno; 28131554Stjr#endif 29131554Stjr#include <exclude.h> 30131554Stjr#include <fnmatch.h> 31131554Stjr#include <stdio.h> 32131554Stjr#include <sys/types.h> 33131554Stjr 34131554Stjrvoid *xmalloc PARAMS ((size_t)); 35131554Stjrvoid *xrealloc PARAMS ((void *, size_t)); 36131554Stjr 37131554Stjr/* Keep track of excluded file name patterns. */ 38131554Stjr 39131554Stjrstruct exclude 40131554Stjr { 41131554Stjr char const **exclude; 42131554Stjr int exclude_alloc; 43131554Stjr int exclude_count; 44131554Stjr }; 45131554Stjr 46131554Stjrstruct exclude * 47131554Stjrnew_exclude (void) 48131554Stjr{ 49131554Stjr struct exclude *ex = (struct exclude *) xmalloc (sizeof (struct exclude)); 50131554Stjr ex->exclude_count = 0; 51131554Stjr ex->exclude_alloc = 64; 52131554Stjr ex->exclude = (char const **) xmalloc (ex->exclude_alloc * sizeof (char *)); 53131554Stjr return ex; 54131554Stjr} 55131554Stjr 56131554Stjrint 57131554Stjrexcluded_filename (struct exclude const *ex, char const *f, int options) 58131554Stjr{ 59131554Stjr char const * const *exclude = ex->exclude; 60131554Stjr int exclude_count = ex->exclude_count; 61131554Stjr int i; 62131554Stjr 63131554Stjr for (i = 0; i < exclude_count; i++) 64131554Stjr if (fnmatch (exclude[i], f, options) == 0) 65131554Stjr return 1; 66131554Stjr 67131554Stjr return 0; 68131554Stjr} 69131554Stjr 70131554Stjrvoid 71131554Stjradd_exclude (struct exclude *ex, char const *pattern) 72131554Stjr{ 73131554Stjr if (ex->exclude_alloc <= ex->exclude_count) 74131554Stjr ex->exclude = (char const **) xrealloc (ex->exclude, 75131554Stjr ((ex->exclude_alloc *= 2) 76131554Stjr * sizeof (char *))); 77131554Stjr 78131554Stjr ex->exclude[ex->exclude_count++] = pattern; 79131554Stjr} 80131554Stjr 81131554Stjrint 82131554Stjradd_exclude_file (void (*add_func) PARAMS ((struct exclude *, char const *)), 83131554Stjr struct exclude *ex, char const *filename, char line_end) 84131554Stjr{ 85131554Stjr int use_stdin = filename[0] == '-' && !filename[1]; 86131554Stjr FILE *in; 87131554Stjr char *buf; 88131554Stjr char *p; 89131554Stjr char const *pattern; 90131554Stjr char const *lim; 91131554Stjr size_t buf_alloc = 1024; 92131554Stjr size_t buf_count = 0; 93131554Stjr int c; 94131554Stjr int e = 0; 95131554Stjr 96131554Stjr if (use_stdin) 97131554Stjr in = stdin; 98131554Stjr else if (! (in = fopen (filename, "r"))) 99131554Stjr return -1; 100131554Stjr 101131554Stjr buf = xmalloc (buf_alloc); 102131554Stjr 103131554Stjr while ((c = getc (in)) != EOF) 104131554Stjr { 105131554Stjr buf[buf_count++] = c; 106131554Stjr if (buf_count == buf_alloc) 107131554Stjr buf = xrealloc (buf, buf_alloc *= 2); 108131554Stjr } 109131554Stjr 110131554Stjr buf = xrealloc (buf, buf_count + 1); 111131554Stjr 112131554Stjr if (ferror (in)) 113131554Stjr e = errno; 114131554Stjr 115131554Stjr if (!use_stdin && fclose (in) != 0) 116131554Stjr e = errno; 117131554Stjr 118131554Stjr for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++) 119131554Stjr if (p < lim ? *p == line_end : buf < p && p[-1]) 120131554Stjr { 121131554Stjr *p = '\0'; 122131554Stjr (*add_func) (ex, pattern); 123131554Stjr pattern = p + 1; 124131554Stjr } 125131554Stjr 126131554Stjr errno = e; 127131554Stjr return e ? -1 : 0; 128131554Stjr} 129