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 330 : static void strlist_free(struct strlist *strlist)
11 : {
12 660 : if(!strlist) return;
13 330 : if(strlist->path) free_w(&strlist->path);
14 330 : if(strlist->re) regfree(strlist->re);
15 330 : free_v((void **)&strlist->re);
16 330 : free_v((void **)&strlist);
17 : }
18 :
19 1241 : void strlists_free(struct strlist **strlist)
20 : {
21 : struct strlist *s;
22 1241 : struct strlist *shead=*strlist;
23 2812 : while(shead)
24 : {
25 330 : s=shead;
26 330 : shead=shead->next;
27 330 : strlist_free(s);
28 : }
29 1241 : *strlist=NULL;
30 1241 : }
31 :
32 330 : static struct strlist *strlist_alloc(const char *path, long flag)
33 : {
34 330 : struct strlist *slnew=NULL;
35 330 : if(!path)
36 : {
37 0 : logp("%s called with NULL path!\n", __func__);
38 0 : return NULL;
39 : }
40 330 : if(!(slnew=(struct strlist *)
41 : calloc_w(1, sizeof(struct strlist), __func__))
42 330 : || !(slnew->path=strdup_w(path, __func__)))
43 0 : return NULL;
44 330 : slnew->flag=flag;
45 330 : return slnew;
46 : }
47 :
48 330 : static int do_strlist_add(struct strlist **strlist,
49 : const char *path, long flag, int sorted)
50 : {
51 330 : struct strlist *s=NULL;
52 330 : struct strlist *slast=NULL;
53 330 : struct strlist *slnew=NULL;
54 :
55 330 : if(!(slnew=strlist_alloc(path, flag))) return -1;
56 :
57 : // Insert into a sorted position in the list, or if the sorted flag
58 : // was zero, add to the end of the list.
59 : // FIX THIS: Unsorted means that it goes through the whole list to
60 : // find the last entry. Can this be made better?
61 637 : for(s=*strlist; s; s=s->next)
62 : {
63 320 : if(sorted && pathcmp(path, s->path)<0) break;
64 307 : slast=s;
65 : }
66 330 : if(slast)
67 : {
68 156 : slnew->next=slast->next;
69 156 : slast->next=slnew;
70 : }
71 : else
72 : {
73 174 : *strlist=slnew;
74 174 : slnew->next=s;
75 : }
76 :
77 330 : return 0;
78 : }
79 :
80 231 : int strlist_add(struct strlist **strlist,
81 : const char *path, long flag)
82 : {
83 231 : return do_strlist_add(strlist, path, flag, 0 /* unsorted */);
84 : }
85 :
86 99 : int strlist_add_sorted(struct strlist **strlist,
87 : const char *path, long flag)
88 : {
89 99 : return do_strlist_add(strlist, path, flag, 1 /* sorted */);
90 : }
91 :
92 78 : int strlist_compile_regexes(struct strlist *strlist)
93 : {
94 : struct strlist *l;
95 78 : for(l=strlist; l; l=l->next) compile_regex(&l->re, l->path);
96 78 : return 0;
97 : }
98 :
99 6 : int strlist_find(struct strlist *strlist, const char *path, long flag)
100 : {
101 : struct strlist *s;
102 12 : for(s=strlist; s; s=s->next)
103 6 : if(!strcmp(path, s->path) && flag==s->flag)
104 0 : return 1;
105 6 : return 0;
106 : }
|