summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1997-12-21 11:43:34 +0000
committerJim Meyering <jim@meyering.net>1997-12-21 11:43:34 +0000
commitcbd71d8eeaa7754c287f22b7d069eee4cdd3492a (patch)
treed05e306dd5fec0398aac8a1f9d9a6fc110a94d94 /lib
parenta2c7ebd49cd6a634f99a880bc1f5f955c5f3983d (diff)
downloadcoreutils-cbd71d8eeaa7754c287f22b7d069eee4cdd3492a.tar.xz
.
Diffstat (limited to 'lib')
-rw-r--r--lib/exclude.c127
-rw-r--r--lib/exclude.h34
2 files changed, 161 insertions, 0 deletions
diff --git a/lib/exclude.c b/lib/exclude.c
new file mode 100644
index 000000000..47f8e8132
--- /dev/null
+++ b/lib/exclude.c
@@ -0,0 +1,127 @@
+/* exclude.c -- exclude file names
+ Copyright 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; see the file COPYING.
+ If not, write to the Free Software Foundation,
+ 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+/* Written by Paul Eggert <eggert@twinsun.com> */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+#include <exclude.h>
+#include <fnmatch.h>
+#include <stdio.h>
+#include <sys/types.h>
+
+void *xmalloc __EXCLUDE_P ((size_t));
+void *xrealloc __EXCLUDE_P ((void *, size_t));
+
+/* Keep track of excluded file name patterns. */
+
+struct exclude
+ {
+ char const **exclude;
+ int exclude_alloc;
+ int exclude_count;
+ };
+
+struct exclude *
+new_exclude (void)
+{
+ struct exclude *ex = (struct exclude *) xmalloc (sizeof (struct exclude));
+ ex->exclude_count = 0;
+ ex->exclude_alloc = 64;
+ ex->exclude = (char const **) xmalloc (ex->exclude_alloc * sizeof (char *));
+ return ex;
+}
+
+int
+excluded_filename (struct exclude const *ex, char const *f)
+{
+ char const * const *exclude = ex->exclude;
+ int exclude_count = ex->exclude_count;
+ int i;
+
+ for (i = 0; i < exclude_count; i++)
+ if (fnmatch (exclude[i], f, 0) == 0)
+ return 1;
+
+ return 0;
+}
+
+void
+add_exclude (struct exclude *ex, char const *pattern)
+{
+ if (ex->exclude_alloc <= ex->exclude_count)
+ ex->exclude = (char const **) xrealloc (ex->exclude,
+ ((ex->exclude_alloc *= 2)
+ * sizeof (char *)));
+
+ ex->exclude[ex->exclude_count++] = pattern;
+}
+
+int
+add_exclude_file (struct exclude *ex, char const *filename, char line_end)
+{
+ int use_stdin = filename[0] == '-' && !filename[1];
+ FILE *in;
+ char *buf;
+ char *p;
+ char const *pattern;
+ char const *lim;
+ size_t buf_alloc = 1024;
+ size_t buf_count = 0;
+ int c;
+ int e = 0;
+
+ if (use_stdin)
+ in = stdin;
+ else if (! (in = fopen (filename, "r")))
+ return -1;
+
+ buf = xmalloc (buf_alloc);
+
+ while ((c = getc (in)) != EOF)
+ {
+ buf[buf_count++] = c;
+ if (buf_count == buf_alloc)
+ buf = xrealloc (buf, buf_alloc *= 2);
+ }
+
+ buf = xrealloc (buf, buf_count + 1);
+
+ if (ferror (in))
+ e = errno;
+
+ if (!use_stdin && fclose (in) != 0)
+ e = errno;
+
+ for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++)
+ if (p < lim ? *p == line_end : buf < p && p[-1])
+ {
+ *p = '\0';
+ add_exclude (ex, pattern);
+ pattern = p + 1;
+ }
+
+ errno = e;
+ return e ? -1 : 0;
+}
diff --git a/lib/exclude.h b/lib/exclude.h
new file mode 100644
index 000000000..754ed097e
--- /dev/null
+++ b/lib/exclude.h
@@ -0,0 +1,34 @@
+/* exclude.h -- declarations for excluding file names
+ Copyright 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; see the file COPYING.
+ If not, write to the Free Software Foundation,
+ 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+/* Written by Paul Eggert <eggert@twinsun.com> */
+
+#ifndef __EXCLUDE_P
+# if defined __STDC__ || __GNUC__
+# define __EXCLUDE_P(args) args
+# else
+# define __EXCLUDE_P(args) ()
+# endif
+#endif
+
+struct exclude;
+
+struct exclude *new_exclude __EXCLUDE_P ((void));
+void add_exclude __EXCLUDE_P ((struct exclude *, char const *));
+int add_exclude_file __EXCLUDE_P ((struct exclude *, char const *, char));
+int excluded_filename __EXCLUDE_P ((struct exclude const *, char const *));