add FIXME-describe comments
[gnulib.git] / lib / exclude.c
1 /* exclude.c -- exclude file names
2    Copyright 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
18
19 /* Written by Paul Eggert <eggert@twinsun.com>  */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
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>
33
34 void *xmalloc __EXCLUDE_P ((size_t));
35 void *xrealloc __EXCLUDE_P ((void *, size_t));
36
37 /* Keep track of excluded file name patterns.  */
38
39 struct exclude
40   {
41     char const **exclude;
42     int exclude_alloc;
43     int exclude_count;
44   };
45
46 /* FIXME: describe */
47
48 struct exclude *
49 new_exclude (void)
50 {
51   struct exclude *ex = (struct exclude *) xmalloc (sizeof (struct exclude));
52   ex->exclude_count = 0;
53   ex->exclude_alloc = 64;
54   ex->exclude = (char const **) xmalloc (ex->exclude_alloc * sizeof (char *));
55   return ex;
56 }
57
58 /* FIXME: describe */
59
60 int
61 excluded_filename (struct exclude const *ex, char const *f)
62 {
63   char const * const *exclude = ex->exclude;
64   int exclude_count = ex->exclude_count;
65   int i;
66
67   for (i = 0;  i < exclude_count;  i++)
68     if (fnmatch (exclude[i], f, 0) == 0)
69       return 1;
70
71   return 0;
72 }
73
74 /* FIXME: describe */
75
76 void
77 add_exclude (struct exclude *ex, char const *pattern)
78 {
79   if (ex->exclude_alloc <= ex->exclude_count)
80     ex->exclude = (char const **) xrealloc (ex->exclude,
81                                             ((ex->exclude_alloc *= 2)
82                                              * sizeof (char *)));
83
84   ex->exclude[ex->exclude_count++] = pattern;
85 }
86
87 /* FIXME: describe */
88
89 int
90 add_exclude_file (struct exclude *ex, char const *filename, char line_end)
91 {
92   int use_stdin = filename[0] == '-' && !filename[1];
93   FILE *in;
94   char *buf;
95   char *p;
96   char const *pattern;
97   char const *lim;
98   size_t buf_alloc = 1024;
99   size_t buf_count = 0;
100   int c;
101   int e = 0;
102
103   if (use_stdin)
104     in = stdin;
105   else if (! (in = fopen (filename, "r")))
106     return -1;
107
108   buf = xmalloc (buf_alloc);
109
110   while ((c = getc (in)) != EOF)
111     {
112       buf[buf_count++] = c;
113       if (buf_count == buf_alloc)
114         buf = xrealloc (buf, buf_alloc *= 2);
115     }
116
117   buf = xrealloc (buf, buf_count + 1);
118
119   if (ferror (in))
120     e = errno;
121
122   if (!use_stdin && fclose (in) != 0)
123     e = errno;
124
125   for (pattern = p = buf, lim = buf + buf_count;  p <= lim;  p++)
126     if (p < lim ? *p == line_end : buf < p && p[-1])
127       {
128         *p = '\0';
129         add_exclude (ex, pattern);
130         pattern = p + 1;
131       }
132
133   errno = e;
134   return e ? -1 : 0;
135 }