Line data Source code
1 : #include "burp.h"
2 : #include "alloc.h"
3 : #include "log.h"
4 : #include "pathcmp.h"
5 : #include "regexp.h"
6 : #include "strlist.h"
7 :
8 : // Maybe rename this stuff to 'struct pathlist'.
9 :
10 385 : static void strlist_free(struct strlist *strlist)
11 : {
12 770 : if(!strlist) return;
13 385 : regex_free(&strlist->re);
14 385 : free_w(&strlist->path);
15 385 : free_v((void **)&strlist);
16 : }
17 :
18 3741 : void strlists_free(struct strlist **strlist)
19 : {
20 : struct strlist *s;
21 : struct strlist *shead;
22 7482 : if(!strlist) return;
23 3729 : shead=*strlist;
24 7843 : while(shead)
25 : {
26 385 : s=shead;
27 385 : shead=shead->next;
28 385 : strlist_free(s);
29 : }
30 3729 : *strlist=NULL;
31 : }
32 :
33 385 : static struct strlist *strlist_alloc(const char *path, long flag)
34 : {
35 385 : struct strlist *slnew=NULL;
36 385 : if(!path)
37 : {
38 0 : logp("%s called with NULL path!\n", __func__);
39 0 : return NULL;
40 : }
41 385 : if(!(slnew=(struct strlist *)
42 : calloc_w(1, sizeof(struct strlist), __func__))
43 385 : || !(slnew->path=strdup_w(path, __func__)))
44 : return NULL;
45 385 : slnew->flag=flag;
46 385 : return slnew;
47 : }
48 :
49 385 : static int do_strlist_add(struct strlist **strlist,
50 : const char *path, long flag, int sorted)
51 : {
52 385 : struct strlist *s=NULL;
53 385 : struct strlist *slast=NULL;
54 385 : struct strlist *slnew=NULL;
55 :
56 385 : if(!(slnew=strlist_alloc(path, flag))) return -1;
57 :
58 : // Insert into a sorted position in the list, or if the sorted flag
59 : // was zero, add to the end of the list.
60 : // FIX THIS: Unsorted means that it goes through the whole list to
61 : // find the last entry. Can this be made better?
62 709 : for(s=*strlist; s; s=s->next)
63 : {
64 338 : if(sorted && pathcmp(path, s->path)<0) break;
65 324 : slast=s;
66 : }
67 385 : if(slast)
68 : {
69 171 : slnew->next=slast->next;
70 171 : slast->next=slnew;
71 : }
72 : else
73 : {
74 214 : *strlist=slnew;
75 214 : slnew->next=s;
76 : }
77 :
78 : return 0;
79 : }
80 :
81 267 : int strlist_add(struct strlist **strlist,
82 : const char *path, long flag)
83 : {
84 267 : return do_strlist_add(strlist, path, flag, 0 /* unsorted */);
85 : }
86 :
87 118 : int strlist_add_sorted(struct strlist **strlist,
88 : const char *path, long flag)
89 : {
90 118 : return do_strlist_add(strlist, path, flag, 1 /* sorted */);
91 : }
92 :
93 146 : int strlist_compile_regexes(struct strlist *strlist)
94 : {
95 : struct strlist *l;
96 : // FIX THIS: when the regex does not compile, should remove the
97 : // strlist entry completely.
98 148 : for(l=strlist; l; l=l->next)
99 2 : if(!(l->re=regex_compile(l->path)))
100 0 : logp("unable to compile regex: %s\n", l->path);
101 146 : return 0;
102 : }
103 :
104 6 : int strlist_find(struct strlist *strlist, const char *path, long flag)
105 : {
106 : struct strlist *s;
107 12 : for(s=strlist; s; s=s->next)
108 6 : if(!strcmp(path, s->path) && flag==s->flag)
109 : return 1;
110 : return 0;
111 : }
|