1/*
2 * 27-Mar-96: Jan-Piet Mens <jpm@mens.de>
3 * added 'match' option (-m) to specify regular expressions NOT to be included
4 * in the CD image.
5 */
6
7#ifdef APPLE_HYB
8/*
9 * Added a number of routines to create lists of files to hidden from
10 * the ISO9660 and/or Joliet trees. James Pearson (j.pearson@ge.ucl.ac.uk)
11 * January 1999 (these will probably appear in mkisofs in the future)
12 */
13#endif /* APPLE_HYB */
14
15#include "config.h"
16#include <prototyp.h>
17#include <stdio.h>
18#include <fnmatch.h>
19#ifndef VMS
20#ifdef HAVE_MALLOC_H
21#include <malloc.h>
22#else
23#include <stdlib.h>
24#endif
25#endif
26#include <inttypes.h>
27#include <string.h>
28#include "match.h"
29
30#define MAXMATCH 1000
31static char *mat[MAXMATCH];
32
33int  add_match(fn)
34char * fn;
35{
36  register int i;
37
38  for (i=0; mat[i] && i<MAXMATCH; i++);
39  if (i == MAXMATCH) {
40    fprintf(stderr,"Can't exclude RE '%s' - too many entries in table\n",fn);
41    return 1;
42  }
43
44
45  mat[i] = (char *) malloc(strlen(fn)+1);
46  if (! mat[i]) {
47    fprintf(stderr,"Can't allocate memory for excluded filename\n");
48    return 1;
49  }
50
51  strcpy(mat[i],fn);
52
53  return 0;
54}
55
56int matches(fn)
57char * fn;
58{
59  /* very dumb search method ... */
60  register int i;
61
62  for (i=0; mat[i] && i<MAXMATCH; i++) {
63    if (fnmatch(mat[i], fn, FNM_PATHNAME) != FNM_NOMATCH) {
64      return 1; /* found -> excluded filenmae */
65    }
66  }
67  return 0; /* not found -> not excluded */
68}
69
70/* ISO9660/RR hide */
71
72static char *i_mat[MAXMATCH];
73
74int  i_add_match(fn)
75char * fn;
76{
77  register int i;
78
79  for (i=0; i_mat[i] && i<MAXMATCH; i++);
80  if (i == MAXMATCH) {
81    fprintf(stderr,"Can't exclude RE '%s' - too many entries in table\n",fn);
82    return 1;
83  }
84
85
86  i_mat[i] = (char *) malloc(strlen(fn)+1);
87  if (! i_mat[i]) {
88    fprintf(stderr,"Can't allocate memory for excluded filename\n");
89    return 1;
90  }
91
92  strcpy(i_mat[i],fn);
93
94  return 0;
95}
96
97int i_matches(fn)
98char * fn;
99{
100  /* very dumb search method ... */
101  register int i;
102
103  for (i=0; i_mat[i] && i<MAXMATCH; i++) {
104    if (fnmatch(i_mat[i], fn, FNM_PATHNAME) != FNM_NOMATCH) {
105      return 1; /* found -> excluded filenmae */
106    }
107  }
108  return 0; /* not found -> not excluded */
109}
110
111intptr_t i_ishidden()
112{
113  return((intptr_t)i_mat[0]);
114}
115
116/* Joliet hide */
117
118static char *j_mat[MAXMATCH];
119
120int  j_add_match(fn)
121char * fn;
122{
123  register int i;
124
125  for (i=0; j_mat[i] && i<MAXMATCH; i++);
126  if (i == MAXMATCH) {
127    fprintf(stderr,"Can't exclude RE '%s' - too many entries in table\n",fn);
128    return 1;
129  }
130
131
132  j_mat[i] = (char *) malloc(strlen(fn)+1);
133  if (! j_mat[i]) {
134    fprintf(stderr,"Can't allocate memory for excluded filename\n");
135    return 1;
136  }
137
138  strcpy(j_mat[i],fn);
139
140  return 0;
141}
142
143int j_matches(fn)
144char * fn;
145{
146  /* very dumb search method ... */
147  register int i;
148
149  for (i=0; j_mat[i] && i<MAXMATCH; i++) {
150    if (fnmatch(j_mat[i], fn, FNM_PATHNAME) != FNM_NOMATCH) {
151      return 1; /* found -> excluded filenmae */
152    }
153  }
154  return 0; /* not found -> not excluded */
155}
156
157intptr_t j_ishidden()
158{
159  return((intptr_t)j_mat[0]);
160}
161
162#ifdef APPLE_HYB
163
164/* HFS hide */
165
166static char *hfs_mat[MAXMATCH];
167
168int hfs_add_match(fn)
169char * fn;
170{
171  register int i;
172
173  for (i=0; hfs_mat[i] && i<MAXMATCH; i++);
174  if (i == MAXMATCH) {
175    fprintf(stderr,"Can't exclude RE '%s' - too many entries in table\n",fn);
176    return 1;
177  }
178
179
180  hfs_mat[i] = (char *) malloc(strlen(fn)+1);
181  if (! hfs_mat[i]) {
182    fprintf(stderr,"Can't allocate memory for excluded filename\n");
183    return 1;
184  }
185
186  strcpy(hfs_mat[i],fn);
187
188  return 0;
189}
190
191void hfs_add_list(file)
192char *file;
193{
194  FILE *fp;
195  char name[1024];
196
197  if ((fp = fopen(file, "r")) == NULL) {
198    fprintf(stderr,"Can't open hidden file list %s\n", file);
199    exit (1);
200  }
201
202  while (fscanf(fp, "%s", name) != EOF) {
203    if (hfs_add_match(name)) {
204      fclose(fp);
205      return;
206    }
207  }
208
209  fclose(fp);
210}
211
212
213int hfs_matches(fn)
214char * fn;
215{
216  /* very dumb search method ... */
217  register int i;
218
219  for (i=0; hfs_mat[i] && i<MAXMATCH; i++) {
220    if (fnmatch(hfs_mat[i], fn, FNM_PATHNAME) != FNM_NOMATCH) {
221      return 1; /* found -> excluded filenmae */
222    }
223  }
224  return 0; /* not found -> not excluded */
225}
226
227intptr_t hfs_ishidden()
228{
229  return((intptr_t)hfs_mat[0]);
230}
231#endif /* APPLE_HYB */
232
233void add_list(file)
234char *file;
235{
236  FILE *fp;
237  char name[1024];
238
239  if ((fp = fopen(file, "r")) == NULL) {
240    fprintf(stderr,"Can't open exclude file list %s\n", file);
241    exit (1);
242  }
243
244  while (fscanf(fp, "%s", name) != EOF) {
245    if (add_match(name)) {
246      fclose(fp);
247      return;
248    }
249  }
250
251  fclose(fp);
252}
253
254void i_add_list(file)
255char *file;
256{
257  FILE *fp;
258  char name[1024];
259
260  if ((fp = fopen(file, "r")) == NULL) {
261    fprintf(stderr,"Can't open hidden file list %s\n", file);
262    exit (1);
263  }
264
265  while (fscanf(fp, "%s", name) != EOF) {
266    if (i_add_match(name)) {
267      fclose(fp);
268      return;
269    }
270  }
271
272  fclose(fp);
273}
274
275void j_add_list(file)
276char *file;
277{
278  FILE *fp;
279  char name[1024];
280
281  if ((fp = fopen(file, "r")) == NULL) {
282    fprintf(stderr,"Can't open hidden file list %s\n", file);
283    exit (1);
284  }
285
286  while (fscanf(fp, "%s", name) != EOF) {
287    if (j_add_match(name)) {
288      fclose(fp);
289      return;
290    }
291  }
292
293  fclose(fp);
294}
295