Mercurial > hg > octave-kai > gnulib-hg
annotate lib/exclude.c @ 2108:57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Wed, 12 Jan 2000 06:37:30 +0000 (2000-01-12) |
parents | 5ff7080eb4ba |
children | 1beac6d79e51 |
rev | line source |
---|---|
1171 | 1 /* exclude.c -- exclude file names |
2108
57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
Jim Meyering <jim@meyering.net>
parents:
1190
diff
changeset
|
2 Copyright 1992, 1993, 1994, 1997, 1999, 2000 Free Software Foundation, Inc. |
1171 | 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 | |
1190
5ff7080eb4ba
Use PARAMS, not __EXCLUDE_P.
Jim Meyering <jim@meyering.net>
parents:
1174
diff
changeset
|
34 void *xmalloc PARAMS ((size_t)); |
5ff7080eb4ba
Use PARAMS, not __EXCLUDE_P.
Jim Meyering <jim@meyering.net>
parents:
1174
diff
changeset
|
35 void *xrealloc PARAMS ((void *, size_t)); |
1171 | 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 struct exclude * | |
47 new_exclude (void) | |
48 { | |
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; | |
54 } | |
55 | |
56 int | |
2108
57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
Jim Meyering <jim@meyering.net>
parents:
1190
diff
changeset
|
57 excluded_filename (struct exclude const *ex, char const *f, int options) |
1171 | 58 { |
59 char const * const *exclude = ex->exclude; | |
60 int exclude_count = ex->exclude_count; | |
61 int i; | |
62 | |
63 for (i = 0; i < exclude_count; i++) | |
2108
57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
Jim Meyering <jim@meyering.net>
parents:
1190
diff
changeset
|
64 if (fnmatch (exclude[i], f, options) == 0) |
1171 | 65 return 1; |
66 | |
67 return 0; | |
68 } | |
69 | |
70 void | |
71 add_exclude (struct exclude *ex, char const *pattern) | |
72 { | |
73 if (ex->exclude_alloc <= ex->exclude_count) | |
74 ex->exclude = (char const **) xrealloc (ex->exclude, | |
75 ((ex->exclude_alloc *= 2) | |
76 * sizeof (char *))); | |
77 | |
78 ex->exclude[ex->exclude_count++] = pattern; | |
79 } | |
80 | |
81 int | |
2108
57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
Jim Meyering <jim@meyering.net>
parents:
1190
diff
changeset
|
82 add_exclude_file (void (*add_func) PARAMS ((struct exclude *, char const *)), |
57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
Jim Meyering <jim@meyering.net>
parents:
1190
diff
changeset
|
83 struct exclude *ex, char const *filename, char line_end) |
1171 | 84 { |
85 int use_stdin = filename[0] == '-' && !filename[1]; | |
86 FILE *in; | |
87 char *buf; | |
88 char *p; | |
89 char const *pattern; | |
90 char const *lim; | |
91 size_t buf_alloc = 1024; | |
92 size_t buf_count = 0; | |
93 int c; | |
94 int e = 0; | |
95 | |
96 if (use_stdin) | |
97 in = stdin; | |
98 else if (! (in = fopen (filename, "r"))) | |
99 return -1; | |
100 | |
101 buf = xmalloc (buf_alloc); | |
102 | |
103 while ((c = getc (in)) != EOF) | |
104 { | |
105 buf[buf_count++] = c; | |
106 if (buf_count == buf_alloc) | |
107 buf = xrealloc (buf, buf_alloc *= 2); | |
108 } | |
109 | |
110 buf = xrealloc (buf, buf_count + 1); | |
111 | |
112 if (ferror (in)) | |
113 e = errno; | |
114 | |
115 if (!use_stdin && fclose (in) != 0) | |
116 e = errno; | |
117 | |
118 for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++) | |
119 if (p < lim ? *p == line_end : buf < p && p[-1]) | |
120 { | |
121 *p = '\0'; | |
2108
57cf4f9be1e9
Sync to the slightly more general version of GNU tar.
Jim Meyering <jim@meyering.net>
parents:
1190
diff
changeset
|
122 (*add_func) (ex, pattern); |
1171 | 123 pattern = p + 1; |
124 } | |
125 | |
126 errno = e; | |
127 return e ? -1 : 0; | |
128 } |