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"
28 #define ENSC_WRAPPERS_STDLIB 1
32 MatchList_compare(struct MatchList const *list, char const *path)
35 struct MatchItem const * ptr = list->data;
36 struct MatchItem const * const end_ptr = list->data + list->count;
38 //write(1, path, strlen(path));
40 for (; ptr<end_ptr; ++ptr) {
41 if ((ptr->cmp==0 && strcmp(ptr->name, path)==0) ||
42 (ptr->cmp!=0 && (ptr->cmp)(ptr->name, path)==0)) {
44 case stINCLUDE : res = true; break;
45 case stEXCLUDE : res = false; break;
56 MatchList_init(struct MatchList *list, char const *root, size_t count)
60 list->root.l = strlen(root);
61 list->data = Emalloc(sizeof(struct MatchItem) * count);
66 String_init(&list->id);
70 fnmatchWrap(char const *a, char const *b)
72 return fnmatch(a, b, 0);
76 static MatchItemCompareFunc
77 determineCompareFunc(char const UNUSED *fname)
83 MatchList_appendFiles(struct MatchList *list, size_t idx,
84 char **files, size_t count,
87 struct MatchItem *ptr = list->data + idx;
90 assert(idx+count <= list->count);
93 for (i=0; i<count; ++i) {
94 char *file = files[i];
96 case '+' : ptr->type = stINCLUDE; ++file; break;
97 case '-' : ++file; /*@fallthrough@*/
98 default : ptr->type = stEXCLUDE; break;
100 ptr->cmp = determineCompareFunc(file);
106 for (i=0; i<count; ++i) {
107 ptr->type = stEXCLUDE;
108 ptr->name = files[i];
117 PathInfo_append(PathInfo * restrict lhs,
118 PathInfo const * restrict rhs,
122 char const * rhs_ptr = rhs->d;
123 size_t rhs_len = rhs->l;
125 while (lhs->l>1 && lhs->d[lhs->l-1]=='/') --lhs->l;
128 while (rhs->l>0 && *rhs_ptr=='/') {
133 ptr = Xmemcpy(ptr, lhs->d, lhs->l);
135 ptr = Xmemcpy(ptr, "/", 1);
137 // else if (*rhs_ptr!='/')
138 // ptr = Xmemcpy(ptr, "/", 1);
140 ptr = Xmemcpy(ptr, rhs_ptr, rhs_len+1);
147 String_init(String *str)
153 #ifdef ENSC_TESTSUITE
154 #define CHECK(LHS,RHS, EXP) \
156 PathInfo lhs = { LHS, sizeof(LHS)-1 }; \
157 PathInfo rhs = { RHS, sizeof(RHS)-1 }; \
158 char *buf = malloc(ENSC_PI_APPSZ(lhs,rhs)); \
159 assert(ENSC_PI_APPSZ(lhs,rhs)>=sizeof(EXP)); \
160 PathInfo_append(&lhs, &rhs, buf); \
161 assert(memcmp(lhs.d, EXP, sizeof(EXP))==0); \
162 assert(lhs.l == sizeof(EXP)-1); \
170 CHECK("/var", "/tmp", "/var/tmp");
171 CHECK("/var", "tmp", "/var/tmp");
172 CHECK("/var/", "/tmp", "/var/tmp");
173 CHECK("/var/", "tmp", "/var/tmp");
175 CHECK("/", "tmp", "/tmp");
176 CHECK("/", "/tmp", "/tmp");
178 CHECK("", "/tmp", "/tmp");
180 CHECK("", "tmp", "tmp");