Line data Source code
1 : #include "burp.h"
2 : #include "pathcmp.h"
3 :
4 : // Return a number indicating the number of directories matched.
5 : // 0 if it is not a sub-directory.
6 : // Two paths the same counts as a subdirectory.
7 225 : int is_subdir(const char *dir, const char *sub)
8 : {
9 225 : int count=1;
10 225 : const char *d=NULL;
11 225 : const char *s=NULL;
12 225 : const char *dl=NULL;
13 225 : const char *sl=NULL;
14 225 : if(!sub || !dir) return 0;
15 7369 : for(s=sl=sub, dl=d=dir; *s && *d; s++, d++)
16 : {
17 7401 : if(*s!=*d) break;
18 7369 : sl=s;
19 7369 : dl=d;
20 7369 : if(*s=='/') count++;
21 : }
22 222 : if(!*d && !*s) return count; // Paths were exactly the same.
23 197 : if(!*d && *s=='/')
24 : return count; // 'dir' ended without a slash, for example:
25 : // dir=/bin sub=/bin/bash
26 69 : if(*dl=='/' && *sl=='/' && *(sl+1) && !*(dl+1)) return count;
27 : return 0;
28 : }
29 :
30 673303 : int pathcmp(const char *a, const char *b)
31 : {
32 : // This should have used 'unsigned chars', but now its too late and
33 : // everybody has backups with odd sorting. Will have to live with it.
34 673303 : const char *x=NULL;
35 673303 : const char *y=NULL;
36 673303 : if(!a && !b)
37 : return 0; // equal
38 595578 : if( a && !b)
39 : return 1; // a is longer
40 593491 : if(!a && b)
41 : return -1; // b is longer
42 10828688 : for(x=a, y=b; *x && *y ; x++, y++)
43 : {
44 11355279 : if(*x==*y)
45 : continue;
46 526591 : if(*x=='/' && *y!='/')
47 : return -1;
48 526561 : if(*x!='/' && *y=='/')
49 : return 1;
50 526560 : if(*x<*y)
51 : return -1;
52 269860 : if(*x>*y)
53 : return 1;
54 : }
55 66899 : if(!*x && !*y)
56 : return 0; // equal
57 132 : if( *x && !*y)
58 : return 1; // x is longer
59 9 : return -1; // y is longer
60 : }
|