// 字符串匹配,匹配返回 1,否则返回 0. // pattern可以通过任意个 | 字符,组合match_one中pattern的功能 int match( const char * pattern, const char * string ) { const char * or; for (;;) { or = strchr(pattern, ' | ' ); if (or == ( char *) 0 ) return match_one(pattern, strlen(pattern), string ); if (match_one(pattern, or - pattern, string )) return 1 ; pattern = or + 1 ; } } /* 对于pattern中的字符: ?:匹配任意单个字符 **:匹配任意长度字符串 *:匹配任意长度字符串,直到遇到 / */ static int match_one( const char * pattern, int patternlen, const char * string ) { const char * p; for (p = pattern; p - pattern < patternlen; ++p, ++ string ) { if (*p == ' ? ' && * string != ' \0 ' ) continue ; if (*p == ' * ' ) { int i, pl; ++ p; if (*p == ' * ' ) { /* Double-wildcard matches anything. */ ++ p; i = strlen( string ); } else /* Single-wildcard matches anything but slash. */ i = strcspn( string , " / " ); pl = patternlen - (p - pattern); for (; i >= 0 ; -- i) if (match_one(p, pl, &( string [i]))) return 1 ; return 0 ; } if (*p != * string ) return 0 ; } if (* string == ' \0 ' ) return 1 ; return 0 ; }