2 #undef USE_WILDMATCH /* We need real fnmatch implementation here */
7 static int perf(int ac
, char **av
)
9 struct timeval tv1
, tv2
;
11 int fd
, i
, n
, flags1
= 0, flags2
= 0;
13 uint32_t usec1
, usec2
;
15 const char *file
= av
[0];
16 const char *pattern
= av
[1];
18 lang
= getenv("LANG");
19 if (lang
&& strcmp(lang
, "C"))
20 die("Please test it on C locale.");
22 if ((fd
= open(file
, O_RDONLY
)) == -1 || fstat(fd
, &st
))
23 die_errno("file open");
25 buffer
= xmalloc(st
.st_size
+ 2);
26 if (read(fd
, buffer
, st
.st_size
) != st
.st_size
)
29 buffer
[st
.st_size
] = '\0';
30 buffer
[st
.st_size
+ 1] = '\0';
31 for (i
= 0; i
< st
.st_size
; i
++)
32 if (buffer
[i
] == '\n')
36 if (av
[3] && !strcmp(av
[3], "pathname")) {
38 flags2
= FNM_PATHNAME
;
41 gettimeofday(&tv1
, NULL
);
42 for (i
= 0; i
< n
; i
++) {
43 for (p
= buffer
; *p
; p
+= strlen(p
) + 1)
44 wildmatch(pattern
, p
, flags1
, NULL
);
46 gettimeofday(&tv2
, NULL
);
48 usec1
= (uint32_t)tv2
.tv_sec
* 1000000 + tv2
.tv_usec
;
49 usec1
-= (uint32_t)tv1
.tv_sec
* 1000000 + tv1
.tv_usec
;
50 printf("wildmatch %ds %dus\n",
51 (int)(usec1
/ 1000000),
52 (int)(usec1
% 1000000));
54 gettimeofday(&tv1
, NULL
);
55 for (i
= 0; i
< n
; i
++) {
56 for (p
= buffer
; *p
; p
+= strlen(p
) + 1)
57 fnmatch(pattern
, p
, flags2
);
59 gettimeofday(&tv2
, NULL
);
61 usec2
= (uint32_t)tv2
.tv_sec
* 1000000 + tv2
.tv_usec
;
62 usec2
-= (uint32_t)tv1
.tv_sec
* 1000000 + tv1
.tv_usec
;
64 printf("fnmatch %ds %dus or %.2f%% slower\n",
65 (int)((usec2
- usec1
) / 1000000),
66 (int)((usec2
- usec1
) % 1000000),
67 (float)(usec2
- usec1
) / usec1
* 100);
69 printf("fnmatch %ds %dus or %.2f%% faster\n",
70 (int)((usec1
- usec2
) / 1000000),
71 (int)((usec1
- usec2
) % 1000000),
72 (float)(usec1
- usec2
) / usec1
* 100);
76 int main(int argc
, char **argv
)
80 if (!strcmp(argv
[1], "perf"))
81 return perf(argc
- 2, argv
+ 2);
83 for (i
= 2; i
< argc
; i
++) {
84 if (argv
[i
][0] == '/')
85 die("Forward slash is not allowed at the beginning of the\n"
86 "pattern because Windows does not like it. Use `XXX/' instead.");
87 else if (!strncmp(argv
[i
], "XXX/", 4))
90 if (!strcmp(argv
[1], "wildmatch"))
91 return !!wildmatch(argv
[3], argv
[2], WM_PATHNAME
, NULL
);
92 else if (!strcmp(argv
[1], "iwildmatch"))
93 return !!wildmatch(argv
[3], argv
[2], WM_PATHNAME
| WM_CASEFOLD
, NULL
);
94 else if (!strcmp(argv
[1], "pathmatch"))
95 return !!wildmatch(argv
[3], argv
[2], 0, NULL
);
96 else if (!strcmp(argv
[1], "fnmatch"))
97 return !!fnmatch(argv
[3], argv
[2], FNM_PATHNAME
);