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