-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpathcmp.c
92 lines (76 loc) · 1.67 KB
/
pathcmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#ifdef __APPLE__
#define USE_ASCII 1
#endif /* __APPLE__ */
#include <sys/param.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include "pathcmp.h"
#include "code.h"
int
pathcasecmp( const char *p1, const char *p2,
int case_sensitive )
{
int rc;
do {
if ( case_sensitive ) {
rc = ( (unsigned char)*p1 - (unsigned char)*p2 );
} else {
rc = ( tolower( *p1 ) - tolower( *p2 ));
}
if ( rc != 0 ) {
if (( *p2 != '\0' ) && ( *p1 == '/' )) {
return( -1 );
} else if (( *p1 != '\0' ) && ( *p2 == '/' )) {
return( 1 );
} else {
return( rc );
}
}
p2++;
} while ( *p1++ != '\0' );
return( 0 );
}
/* Just like strcmp(), but pays attention to the meaning of '/'. */
int
pathcmp( const char *p1, const char *p2 )
{
return( pathcasecmp( p1, p2, 1 ));
}
int
ischildcase( const char *child, const char *parent, int
case_sensitive )
{
int rc;
size_t parentlen;
if ( parent == NULL ) {
return( 1 );
}
parentlen = strlen( parent );
if ( parentlen > strlen( child )) {
return( 0 );
}
if (( 1 == parentlen ) && ( '/' == *parent )) {
return( '/' == *child );
}
if ( case_sensitive ) {
rc = strncmp( parent, child, parentlen );
} else {
rc = strncasecmp( parent, child, parentlen );
}
if (( rc == 0 ) && (( '/' == child[ parentlen ] ) ||
( '\0' == child[ parentlen ] ))) {
return( 1 );
}
return( 0 );
}
int
ischild( const char *child, const char *parent )
{
return( ischildcase( child, parent, 1 ));
}