*** empty log message ***
[coreutils.git] / lib / exclude.c
blobebb599cfc14e16f3b28777ebbb539eab0ec10bd1
1 /* exclude.c -- exclude file names
2 Copyright 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING.
16 If not, write to the Free Software Foundation,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Paul Eggert <eggert@twinsun.com> */
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <errno.h>
26 #ifndef errno
27 extern int errno;
28 #endif
29 #include <exclude.h>
30 #include <fnmatch.h>
31 #include <stdio.h>
32 #include <sys/types.h>
34 void *xmalloc PARAMS ((size_t));
35 void *xrealloc PARAMS ((void *, size_t));
37 /* Keep track of excluded file name patterns. */
39 struct exclude
41 char const **exclude;
42 int exclude_alloc;
43 int exclude_count;
46 struct exclude *
47 new_exclude (void)
49 struct exclude *ex = (struct exclude *) xmalloc (sizeof (struct exclude));
50 ex->exclude_count = 0;
51 ex->exclude_alloc = 64;
52 ex->exclude = (char const **) xmalloc (ex->exclude_alloc * sizeof (char *));
53 return ex;
56 int
57 excluded_filename (struct exclude const *ex, char const *f)
59 char const * const *exclude = ex->exclude;
60 int exclude_count = ex->exclude_count;
61 int i;
63 for (i = 0; i < exclude_count; i++)
64 if (fnmatch (exclude[i], f, 0) == 0)
65 return 1;
67 return 0;
70 void
71 add_exclude (struct exclude *ex, char const *pattern)
73 if (ex->exclude_alloc <= ex->exclude_count)
74 ex->exclude = (char const **) xrealloc (ex->exclude,
75 ((ex->exclude_alloc *= 2)
76 * sizeof (char *)));
78 ex->exclude[ex->exclude_count++] = pattern;
81 int
82 add_exclude_file (struct exclude *ex, char const *filename, char line_end)
84 int use_stdin = filename[0] == '-' && !filename[1];
85 FILE *in;
86 char *buf;
87 char *p;
88 char const *pattern;
89 char const *lim;
90 size_t buf_alloc = 1024;
91 size_t buf_count = 0;
92 int c;
93 int e = 0;
95 if (use_stdin)
96 in = stdin;
97 else if (! (in = fopen (filename, "r")))
98 return -1;
100 buf = xmalloc (buf_alloc);
102 while ((c = getc (in)) != EOF)
104 buf[buf_count++] = c;
105 if (buf_count == buf_alloc)
106 buf = xrealloc (buf, buf_alloc *= 2);
109 buf = xrealloc (buf, buf_count + 1);
111 if (ferror (in))
112 e = errno;
114 if (!use_stdin && fclose (in) != 0)
115 e = errno;
117 for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++)
118 if (p < lim ? *p == line_end : buf < p && p[-1])
120 *p = '\0';
121 add_exclude (ex, pattern);
122 pattern = p + 1;
125 errno = e;
126 return e ? -1 : 0;