patch zlib check
[platform/upstream/cdrkit.git] / genisoimage / match.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)match.c  1.18 04/05/23 joerg */
14 /*
15  * 27-Mar-96: Jan-Piet Mens <jpm@mens.de>
16  * added 'match' option (-m) to specify regular expressions NOT to be included
17  * in the CD image.
18  *
19  * Re-written 13-Apr-2000 James Pearson
20  * now uses a generic set of routines
21  */
22
23 #include <mconfig.h>
24 #include <stdio.h>
25 #include <stdxlib.h>
26 #include <unixstd.h>
27 #include <strdefs.h>
28 #include <standard.h>
29 #include <schily.h>
30 #include <libport.h>
31 #include "match.h"
32
33 struct match {
34         struct match *next;
35         char     *name;
36 };
37
38 typedef struct match match;
39
40 static match *mats[MAX_MAT];
41
42 static char *mesg[MAX_MAT] = {
43         "excluded",
44         "excluded ISO-9660",
45         "excluded Joliet",
46         "hidden attribute ISO-9660",
47 #ifdef APPLE_HYB
48         "excluded HFS",
49 #endif /* APPLE_HYB */
50 };
51
52 #ifdef SORTING
53 struct sort_match {
54         struct sort_match       *next;
55         char                    *name;
56         int                     val;
57 };
58
59 typedef struct sort_match sort_match;
60
61 static sort_match       *s_mats;
62
63 int
64 add_sort_match(char *fn, int val)
65 {
66         sort_match *s_mat;
67
68         s_mat = (sort_match *)malloc(sizeof (sort_match));
69         if (s_mat == NULL) {
70 #ifdef  USE_LIBSCHILY
71                 errmsg("Can't allocate memory for sort filename\n");
72 #else
73                 fprintf(stderr, "Can't allocate memory for sort filename\n");
74 #endif
75                 return (0);
76         }
77
78         if ((s_mat->name = strdup(fn)) == NULL) {
79 #ifdef  USE_LIBSCHILY
80                 errmsg("Can't allocate memory for sort filename\n");
81 #else
82                 fprintf(stderr, "Can't allocate memory for sort filename\n");
83 #endif
84                 return (0);
85         }
86
87         /* need to reserve the minimum value for other uses */
88         if (val == NOT_SORTED)
89                 val++;
90
91         s_mat->val = val;
92         s_mat->next = s_mats;
93         s_mats = s_mat;
94
95         return (1);
96 }
97
98 void
99 add_sort_list(char *file)
100 {
101         FILE    *fp;
102         char    name[4096];
103         char    *p;
104         int     val;
105
106         if ((fp = fopen(file, "r")) == NULL) {
107 #ifdef  USE_LIBSCHILY
108                 comerr("Can't open sort file list %s\n", file);
109 #else
110                 fprintf(stderr, "Can't open hidden/exclude file list %s\n", file);
111                 exit(1);
112 #endif
113         }
114
115         while (fgets(name, sizeof (name), fp) != NULL) {
116                 /*
117                  * look for the last space or tab character
118                  */
119                 if ((p = strrchr(name, ' ')) == NULL)
120                         p = strrchr(name, '\t');
121                 else if (strrchr(p, '\t') != NULL)      /* Tab after space? */
122                         p = strrchr(p, '\t');
123
124                 if (p == NULL) {
125 #ifdef  USE_LIBSCHILY
126                         comerrno(EX_BAD, "Incorrect sort file format\n\t%s", name);
127 #else
128                         fprintf(stderr, "Incorrect sort file format\n\t%s", name);
129 #endif
130                         continue;
131                 } else {
132                         *p = '\0';
133                         val = atoi(++p);
134                 }
135                 if (!add_sort_match(name, val)) {
136                         fclose(fp);
137                         return;
138                 }
139         }
140
141         fclose(fp);
142 }
143
144 int
145 sort_matches(char *fn, int val)
146 {
147         register sort_match     *s_mat;
148
149         for (s_mat = s_mats; s_mat; s_mat = s_mat->next) {
150                 if (fnmatch(s_mat->name, fn, FNM_FILE_NAME) != FNM_NOMATCH) {
151                         return (s_mat->val); /* found sort value */
152                 }
153         }
154         return (val); /* not found - default sort value */
155 }
156
157 void
158 del_sort()
159 {
160         register sort_match * s_mat, *s_mat1;
161
162         s_mat = s_mats;
163         while (s_mat) {
164                 s_mat1 = s_mat->next;
165
166                 free(s_mat->name);
167                 free(s_mat);
168
169                 s_mat = s_mat1;
170         }
171
172         s_mats = 0;
173 }
174
175 #endif /* SORTING */
176
177
178 int
179 gen_add_match(char *fn, int n)
180 {
181         match   *mat;
182
183         if (n >= MAX_MAT)
184                 return (0);
185
186         mat = (match *)malloc(sizeof (match));
187         if (mat == NULL) {
188 #ifdef  USE_LIBSCHILY
189                 errmsg("Can't allocate memory for %s filename\n", mesg[n]);
190 #else
191                 fprintf(stderr, "Can't allocate memory for %s filename\n", mesg[n]);
192 #endif
193                 return (0);
194         }
195
196         if ((mat->name = strdup(fn)) == NULL) {
197 #ifdef  USE_LIBSCHILY
198                 errmsg("Can't allocate memory for %s filename\n", mesg[n]);
199 #else
200                 fprintf(stderr, "Can't allocate memory for %s filename\n", mesg[n]);
201 #endif
202                 return (0);
203         }
204
205         mat->next = mats[n];
206         mats[n] = mat;
207
208         return (1);
209 }
210
211 void
212 gen_add_list(char *file, int n)
213 {
214         FILE    *fp;
215         char    name[4096];
216         int     len;
217
218         if ((fp = fopen(file, "r")) == NULL) {
219 #ifdef  USE_LIBSCHILY
220                 comerr("Can't open %s file list %s\n", mesg[n], file);
221 #else
222                 fprintf(stderr, "Can't open %s file list %s\n", mesg[n], file);
223                 exit(1);
224 #endif
225         }
226
227         while (fgets(name, sizeof (name), fp) != NULL) {
228                 /*
229                  * strip of '\n'
230                  */
231                 len = strlen(name);
232                 if (name[len - 1] == '\n') {
233                         name[len - 1] = '\0';
234                 }
235                 if (!gen_add_match(name, n)) {
236                         fclose(fp);
237                         return;
238                 }
239         }
240
241         fclose(fp);
242 }
243
244 int
245 gen_matches(char *fn, int n)
246 {
247         register match * mat;
248
249         if (n >= MAX_MAT)
250                 return (0);
251
252         for (mat = mats[n]; mat; mat = mat->next) {
253                 if (fnmatch(mat->name, fn, FNM_FILE_NAME) != FNM_NOMATCH) {
254                         return (1);     /* found -> excluded filename */
255                 }
256         }
257         return (0);                     /* not found -> not excluded */
258 }
259
260 int
261 gen_ishidden(int n)
262 {
263         if (n >= MAX_MAT)
264                 return (0);
265
266         return ((int)(mats[n] != 0));
267 }
268
269 void
270 gen_del_match(int n)
271 {
272         register match  *mat;
273         register match  *mat1;
274
275         if (n >= MAX_MAT)
276                 return;
277
278         mat = mats[n];
279
280         while (mat) {
281                 mat1 = mat->next;
282
283                 free(mat->name);
284                 free(mat);
285
286                 mat = mat1;
287         }
288
289         mats[n] = 0;
290 }