#include #include #include #include #include #include #define _GNU_SOURCE #include #include "fnmatchcomp.h" int fnmatch2(const char *ptr,const char *str,int flags){ return fnmatch_exec(fnmatch_compile(ptr,flags),str); } int main(int argc, char *argv[]) {int i; setlocale(LC_ALL, ""); char *ptr = argv[1]; fnmatch_state *x; int flags = atoi(argv[2]); fnmatch_state *p = fnmatch_compile(ptr, flags); int cnt = 0; char *fooptr="*foo*"; char *s1 = strdup("abcda"), *s2 =strdup("bbidaasxas"), *s3 = strdup("nabcasdxabccx"); for (i = 0; i < 3000000; i++) { s1[1]++; if (s1[1] > 126||s1[1] < 0)s1[1] = 1; s2[1]++; if (s2[1] > 126||s2[1] < 0)s2[1] = 1; s2[1]++; if (s3[1] > 126||s3[1] < 0)s3[1] = 1; #ifdef COMPARE cnt += fnmatch(ptr, s1, flags)+fnmatch(ptr, s2, flags)+fnmatch(ptr, s3, flags); #else cnt += fnmatch_exec(p, s1)+fnmatch_exec(p, s2)+fnmatch_exec(p, s3); #endif } #define TESTPREF(st,pat,str,fullstr,flag) if (fnmatch(pat,fullstr,flag)!=fnmatch_exec(st,str)) { printf ("partial %s =~ %s failed flags %i \n",pat,fullstr,flag);} for (i=0;i<32;i++){ fnmatch_state* s = fnmatch_compile(fooptr, i); fnmatch_state* home = fnmatch_prefix(s, "/home"); fnmatch_state* bar = fnmatch_prefix(s, "/bar"); fnmatch_state* baz = fnmatch_prefix(bar,"/baz"); TESTPREF(home,fooptr,"/foo","/home/foo",i); TESTPREF(bar,fooptr,"/foo","/home/bar/foo",i); TESTPREF(bar,fooptr,"/bar","/home/bar/bar",i); TESTPREF(baz,fooptr,"/blabla","/home/bar/baz/blabla",i); fnmatch_free(home); fnmatch_free(bar); fnmatch_free(s); fnmatch_free(baz); } #define TESTP(pat,str,flag) x=fnmatch_compile(pat,flag); if (fnmatch(pat,str,flag)!=fnmatch_exec(x,str)) { printf ("%s =~ %s failed flags %i \n",pat,str,flag);} fnmatch_free(x); for(i=0;i<32;i++){ TESTP("*/aa*","asdasd/aab",i); TESTP("*/[ab]a*","as/asd/aab",i); TESTP("*ads*",".asdvdf",i); TESTP("*/?as","asdasd/.asasd",i); TESTP("*/[a-z]as","asdasd/.asasd",i); TESTP("*/[a-z]a*","asdasd/aab",i); } /*m4 test of fnmatch*/ char const *Apat = 'A' < '\\' ? "[A-\\\\]" : "[\\\\-A]"; char const *apat = 'a' < '\\' ? "[a-\\\\]" : "[\\\\-a]"; static char const A_1[] = { 'A' - 1, 0 }; static char const A01[] = { 'A' + 1, 0 }; static char const a_1[] = { 'a' - 1, 0 }; static char const a01[] = { 'a' + 1, 0 }; static char const bs_1[] = { '\\' - 1, 0 }; static char const bs01[] = { '\\' + 1, 0 }; #define y TESTP #define n TESTP n ("a*", "", 0) y ("a*", "abc", 0) n ("d*/*1", "d/s/1", FNM_PATHNAME) y ("a\\bc", "abc", 0) n ("a\\bc", "abc", FNM_NOESCAPE) y ("*x", ".x", 0) n ("*x", ".x", FNM_PERIOD) y (Apat, "\\", 0) y (Apat, "A", 0) y (apat, "\\", 0) y (apat, "a", 0) n (Apat, A_1, 0) n (apat, a_1, 0) y (Apat, A01, 0) y (apat, a01, 0) y (Apat, bs_1, 0) y (apat, bs_1, 0) n (Apat, bs01, 0) n (apat, bs01, 0) y ("xxXX", "xXxX", FNM_CASEFOLD) y ("a++(x|yy)b", "a+xyyyyxb", FNM_EXTMATCH) n ("d*/*1", "d/s/1", FNM_FILE_NAME) y ("*", "x", FNM_FILE_NAME | FNM_LEADING_DIR) y ("x*", "x/y/z", FNM_FILE_NAME | FNM_LEADING_DIR) y ("*c*", "c/x", FNM_FILE_NAME | FNM_LEADING_DIR); fnmatch_free(p); return (0); }