2 * This file has been modified for the cdrkit suite.
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).
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.
13 /* @(#)match.c 1.18 04/05/23 joerg */
15 * 27-Mar-96: Jan-Piet Mens <jpm@mens.de>
16 * added 'match' option (-m) to specify regular expressions NOT to be included
19 * Re-written 13-Apr-2000 James Pearson
20 * now uses a generic set of routines
38 typedef struct match match;
40 static match *mats[MAX_MAT];
42 static char *mesg[MAX_MAT] = {
46 "hidden attribute ISO-9660",
49 #endif /* APPLE_HYB */
54 struct sort_match *next;
59 typedef struct sort_match sort_match;
61 static sort_match *s_mats;
64 add_sort_match(char *fn, int val)
68 s_mat = (sort_match *)malloc(sizeof (sort_match));
71 errmsg("Can't allocate memory for sort filename\n");
73 fprintf(stderr, "Can't allocate memory for sort filename\n");
78 if ((s_mat->name = strdup(fn)) == NULL) {
80 errmsg("Can't allocate memory for sort filename\n");
82 fprintf(stderr, "Can't allocate memory for sort filename\n");
87 /* need to reserve the minimum value for other uses */
88 if (val == NOT_SORTED)
99 add_sort_list(char *file)
106 if ((fp = fopen(file, "r")) == NULL) {
108 comerr("Can't open sort file list %s\n", file);
110 fprintf(stderr, "Can't open hidden/exclude file list %s\n", file);
115 while (fgets(name, sizeof (name), fp) != NULL) {
117 * look for the last space or tab character
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');
126 comerrno(EX_BAD, "Incorrect sort file format\n\t%s", name);
128 fprintf(stderr, "Incorrect sort file format\n\t%s", name);
135 if (!add_sort_match(name, val)) {
145 sort_matches(char *fn, int val)
147 register sort_match *s_mat;
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 */
154 return (val); /* not found - default sort value */
160 register sort_match * s_mat, *s_mat1;
164 s_mat1 = s_mat->next;
179 gen_add_match(char *fn, int n)
186 mat = (match *)malloc(sizeof (match));
189 errmsg("Can't allocate memory for %s filename\n", mesg[n]);
191 fprintf(stderr, "Can't allocate memory for %s filename\n", mesg[n]);
196 if ((mat->name = strdup(fn)) == NULL) {
198 errmsg("Can't allocate memory for %s filename\n", mesg[n]);
200 fprintf(stderr, "Can't allocate memory for %s filename\n", mesg[n]);
212 gen_add_list(char *file, int n)
218 if ((fp = fopen(file, "r")) == NULL) {
220 comerr("Can't open %s file list %s\n", mesg[n], file);
222 fprintf(stderr, "Can't open %s file list %s\n", mesg[n], file);
227 while (fgets(name, sizeof (name), fp) != NULL) {
232 if (name[len - 1] == '\n') {
233 name[len - 1] = '\0';
235 if (!gen_add_match(name, n)) {
245 gen_matches(char *fn, int n)
247 register match * mat;
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 */
257 return (0); /* not found -> not excluded */
266 return ((int)(mats[n] != 0));
273 register match *mat1;