3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "vunify-matchlist.h"
30 MatchList_compare(struct MatchList const *list, char const *path)
33 struct MatchItem const * ptr = list->data;
34 struct MatchItem const * const end_ptr = list->data + list->count;
36 //write(1, path, strlen(path));
38 for (; ptr<end_ptr; ++ptr) {
39 if ((ptr->cmp==0 && strcmp(ptr->name, path)==0) ||
40 (ptr->cmp!=0 && (ptr->cmp)(ptr->name, path)==0)) {
42 case stINCLUDE : res = true; break;
43 case stEXCLUDE : res = false; break;
54 MatchList_init(struct MatchList *list, char const *root, size_t count)
58 list->root.l = strlen(root);
59 list->data = Emalloc(sizeof(struct MatchItem) * count);
64 String_init(&list->id);
68 fnmatchWrap(char const *a, char const *b)
70 return fnmatch(a, b, 0);
74 static MatchItemCompareFunc
75 determineCompareFunc(char const UNUSED *fname)
81 MatchList_appendFiles(struct MatchList *list, size_t idx,
82 char **files, size_t count,
85 struct MatchItem *ptr = list->data + idx;
88 assert(idx+count <= list->count);
91 for (i=0; i<count; ++i) {
92 char *file = files[i];
94 case '+' : ptr->type = stINCLUDE; ++file; break;
95 case '-' : ++file; /*@fallthrough@*/
96 default : ptr->type = stEXCLUDE; break;
98 ptr->cmp = determineCompareFunc(file);
104 for (i=0; i<count; ++i) {
105 ptr->type = stEXCLUDE;
106 ptr->name = files[i];
115 PathInfo_append(PathInfo * restrict lhs,
116 PathInfo const * restrict rhs,
120 char const * rhs_ptr = rhs->d;
121 size_t rhs_len = rhs->l;
123 while (lhs->l>1 && lhs->d[lhs->l-1]=='/') --lhs->l;
126 while (rhs->l>0 && *rhs_ptr=='/') {
131 ptr = Xmemcpy(ptr, lhs->d, lhs->l);
133 ptr = Xmemcpy(ptr, "/", 1);
135 // else if (*rhs_ptr!='/')
136 // ptr = Xmemcpy(ptr, "/", 1);
138 ptr = Xmemcpy(ptr, rhs_ptr, rhs_len+1);
145 String_init(String *str)
151 #ifdef ENSC_TESTSUITE
152 #define CHECK(LHS,RHS, EXP) \
154 PathInfo lhs = { LHS, sizeof(LHS)-1 }; \
155 PathInfo rhs = { RHS, sizeof(RHS)-1 }; \
156 char *buf = malloc(ENSC_PI_APPSZ(lhs,rhs)); \
157 assert(ENSC_PI_APPSZ(lhs,rhs)>=sizeof(EXP)); \
158 PathInfo_append(&lhs, &rhs, buf); \
159 assert(memcmp(lhs.d, EXP, sizeof(EXP))==0); \
160 assert(lhs.l == sizeof(EXP)-1); \
168 CHECK("/var", "/tmp", "/var/tmp");
169 CHECK("/var", "tmp", "/var/tmp");
170 CHECK("/var/", "/tmp", "/var/tmp");
171 CHECK("/var/", "tmp", "/var/tmp");
173 CHECK("/", "tmp", "/tmp");
174 CHECK("/", "/tmp", "/tmp");
176 CHECK("", "/tmp", "/tmp");
178 CHECK("", "tmp", "tmp");