[MKISOFS]
[reactos.git] / reactos / sdk / tools / mkisofs / schilytools / mkisofs / match.c
1 /* @(#)match.c 1.31 16/10/10 joerg */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static UConst char sccsid[] =
5 "@(#)match.c 1.31 16/10/10 joerg";
6 #endif
7 /*
8 * 27-Mar-96: Jan-Piet Mens <jpm@mens.de>
9 * added 'match' option (-m) to specify regular expressions NOT to be included
10 * in the CD image.
11 *
12 * Re-written 13-Apr-2000 James Pearson
13 * now uses a generic set of routines
14 * Conversions to make the code more portable May 2000 .. March 2004
15 * Copyright (c) 2000-2016 J. Schilling
16 */
17
18 #include <schily/stdio.h>
19 #include <schily/stdlib.h>
20 #include <schily/unistd.h>
21 #include <schily/string.h>
22 #include <schily/standard.h>
23 #include <schily/schily.h>
24 #include <schily/libport.h>
25 #include <schily/nlsdefs.h>
26 #include "match.h"
27
28 struct match {
29 struct match *next;
30 char *name;
31 };
32
33 typedef struct match match;
34
35 static match *mats[MAX_MAT];
36
37 static char *mesg[MAX_MAT] = {
38 "excluded",
39 "excluded ISO-9660",
40 "excluded Joliet",
41 "excluded UDF",
42 "hidden attribute ISO-9660",
43 #ifdef APPLE_HYB
44 "excluded HFS",
45 #endif /* APPLE_HYB */
46 };
47
48 #ifdef SORTING
49 struct sort_match {
50 struct sort_match *next;
51 char *name;
52 int val;
53 };
54
55 typedef struct sort_match sort_match;
56
57 static sort_match *s_mats;
58
59 EXPORT int
60 add_sort_match(fn, val)
61 char *fn;
62 int val;
63 {
64 sort_match *s_mat;
65
66 s_mat = (sort_match *)malloc(sizeof (sort_match));
67 if (s_mat == NULL) {
68 errmsg(_("Can't allocate memory for sort filename\n"));
69 return (0);
70 }
71
72 if ((s_mat->name = strdup(fn)) == NULL) {
73 errmsg(_("Can't allocate memory for sort filename\n"));
74 free(s_mat);
75 return (0);
76 }
77
78 /* need to reserve the minimum value for other uses */
79 if (val == NOT_SORTED)
80 val++;
81
82 s_mat->val = val;
83 s_mat->next = s_mats;
84 s_mats = s_mat;
85
86 return (1);
87 }
88
89 EXPORT int
90 add_sort_list(file)
91 char *file;
92 {
93 FILE *fp;
94 char name[4096];
95 char *p;
96 int val;
97 extern int do_sort;
98
99 do_sort++;
100 if ((fp = fopen(file, "r")) == NULL) {
101 comerr(_("Can't open sort file list %s\n"), file);
102 }
103
104 while (fgets(name, sizeof (name), fp) != NULL) {
105 /*
106 * look for the last space or tab character
107 */
108 if ((p = strrchr(name, ' ')) == NULL)
109 p = strrchr(name, '\t');
110 else if (strrchr(p, '\t') != NULL) /* Tab after space? */
111 p = strrchr(p, '\t');
112
113 if (p == NULL) {
114 /*
115 * XXX old code did not abort here.
116 */
117 comerrno(EX_BAD, _("Incorrect sort file format\n\t%s\n"), name);
118 continue;
119 } else {
120 *p = '\0';
121 val = atoi(++p);
122 }
123 if (!add_sort_match(name, val)) {
124 fclose(fp);
125 return (-1);
126 }
127 }
128
129 fclose(fp);
130 return (1);
131 }
132
133 EXPORT int
134 sort_matches(fn, val)
135 char *fn;
136 int val;
137 {
138 register sort_match *s_mat;
139
140 for (s_mat = s_mats; s_mat; s_mat = s_mat->next) {
141 if (fnmatch(s_mat->name, fn, FNM_PATHNAME) != FNM_NOMATCH) {
142 return (s_mat->val); /* found sort value */
143 }
144 }
145 return (val); /* not found - default sort value */
146 }
147
148 EXPORT void
149 del_sort()
150 {
151 register sort_match * s_mat, *s_mat1;
152
153 s_mat = s_mats;
154 while (s_mat) {
155 s_mat1 = s_mat->next;
156
157 free(s_mat->name);
158 free(s_mat);
159
160 s_mat = s_mat1;
161 }
162
163 s_mats = 0;
164 }
165
166 #endif /* SORTING */
167
168
169 EXPORT int
170 gen_add_match(fn, n)
171 char *fn;
172 int n;
173 {
174 match *mat;
175
176 if (n >= MAX_MAT) {
177 errmsgno(EX_BAD, _("Too many patterns.\n"));
178 return (0);
179 }
180
181 mat = (match *)malloc(sizeof (match));
182 if (mat == NULL) {
183 errmsg(_("Can't allocate memory for %s filename\n"), mesg[n]);
184 return (0);
185 }
186
187 if ((mat->name = strdup(fn)) == NULL) {
188 errmsg(_("Can't allocate memory for %s filename\n"), mesg[n]);
189 free(mat);
190 return (0);
191 }
192
193 mat->next = mats[n];
194 mats[n] = mat;
195
196 return (1);
197 }
198
199 EXPORT int
200 add_match(fn)
201 char *fn;
202 {
203 int ret = gen_add_match(fn, EXCLUDE);
204
205 if (ret == 0)
206 return (-1);
207 return (1);
208 }
209
210 EXPORT int
211 i_add_match(fn)
212 char *fn;
213 {
214 int ret = gen_add_match(fn, I_HIDE);
215
216 if (ret == 0)
217 return (-1);
218 return (1);
219 }
220
221 EXPORT int
222 h_add_match(fn)
223 char *fn;
224 {
225 int ret = gen_add_match(fn, H_HIDE);
226
227 if (ret == 0)
228 return (-1);
229 return (1);
230 }
231
232 #ifdef APPLE_HYB
233 EXPORT int
234 hfs_add_match(fn)
235 char *fn;
236 {
237 int ret = gen_add_match(fn, HFS_HIDE);
238
239 if (ret == 0)
240 return (-1);
241 return (1);
242 }
243 #endif /* APPLE_HYB */
244
245 EXPORT int
246 j_add_match(fn)
247 char *fn;
248 {
249 int ret = gen_add_match(fn, J_HIDE);
250
251 if (ret == 0)
252 return (-1);
253 return (1);
254 }
255
256 EXPORT int
257 u_add_match(fn)
258 char *fn;
259 {
260 int ret = gen_add_match(fn, U_HIDE);
261
262 if (ret == 0)
263 return (-1);
264 return (1);
265 }
266
267 EXPORT void
268 gen_add_list(file, n)
269 char *file;
270 int n;
271 {
272 FILE *fp;
273 char name[4096];
274 int len;
275
276 if ((fp = fopen(file, "r")) == NULL) {
277 comerr(_("Can't open %s file list %s\n"), mesg[n], file);
278 }
279
280 while (fgets(name, sizeof (name), fp) != NULL) {
281 /*
282 * strip of '\n'
283 */
284 len = strlen(name);
285 if (name[len - 1] == '\n') {
286 name[len - 1] = '\0';
287 }
288 if (!gen_add_match(name, n)) {
289 fclose(fp);
290 return;
291 }
292 }
293
294 fclose(fp);
295 }
296
297 EXPORT int
298 add_list(fn)
299 char *fn;
300 {
301 gen_add_list(fn, EXCLUDE);
302 return (1);
303 }
304
305 EXPORT int
306 i_add_list(fn)
307 char *fn;
308 {
309 gen_add_list(fn, I_HIDE);
310 return (1);
311 }
312
313 EXPORT int
314 h_add_list(fn)
315 char *fn;
316 {
317 gen_add_list(fn, H_HIDE);
318 return (1);
319 }
320
321 EXPORT int
322 j_add_list(fn)
323 char *fn;
324 {
325 gen_add_list(fn, J_HIDE);
326 return (1);
327 }
328
329 EXPORT int
330 u_add_list(fn)
331 char *fn;
332 {
333 gen_add_list(fn, U_HIDE);
334 return (1);
335 }
336
337 #ifdef APPLE_HYB
338 EXPORT int
339 hfs_add_list(fn)
340 char *fn;
341 {
342 gen_add_list(fn, HFS_HIDE);
343 return (1);
344 }
345 #endif /* APPLE_HYB */
346
347 EXPORT int
348 gen_matches(fn, n)
349 char *fn;
350 int n;
351 {
352 register match * mat;
353
354 if (n >= MAX_MAT)
355 return (0);
356
357 for (mat = mats[n]; mat; mat = mat->next) {
358 if (fnmatch(mat->name, fn, FNM_PATHNAME) != FNM_NOMATCH) {
359 return (1); /* found -> excluded filename */
360 }
361 }
362 return (0); /* not found -> not excluded */
363 }
364
365 EXPORT int
366 gen_ishidden(n)
367 int n;
368 {
369 if (n >= MAX_MAT)
370 return (0);
371
372 return ((int)(mats[n] != 0));
373 }
374
375 EXPORT void
376 gen_del_match(n)
377 int n;
378 {
379 register match *mat;
380 register match *mat1;
381
382 if (n >= MAX_MAT)
383 return;
384
385 mat = mats[n];
386
387 while (mat) {
388 mat1 = mat->next;
389
390 free(mat->name);
391 free(mat);
392
393 mat = mat1;
394 }
395
396 mats[n] = 0;
397 }