fnmatch.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <fnmatch.h>
  2. #include <stdio.h>
  3. void test(char* pattern, char* input, int flags) {
  4. if (!fnmatch(pattern, input, flags)) {
  5. printf("\"%s\" matches \"%s\"\n", pattern, input);
  6. } else {
  7. printf("\"%s\" doesn't match \"%s\"\n", pattern, input);
  8. }
  9. }
  10. int main() {
  11. puts("Should succeed:");
  12. test("*World", "Hello World", 0);
  13. test("*World", "World", 0);
  14. test("Hello*", "Hello World", 0);
  15. test("H[ae]llo?World", "Hallo+World", 0);
  16. test("H[ae]llo?World", "Hello_World", 0);
  17. test("[0-9][!a]", "1b", 0);
  18. test("/a/*/d", "/a/b/c/d", 0);
  19. test("/a/*/d", "/a/bc/d", FNM_PATHNAME);
  20. test("*hello", ".hello", 0);
  21. test("/*hello", "/.hello", FNM_PERIOD);
  22. test("[a!][a!]", "!a", 0);
  23. test("[\\]]", "]", 0);
  24. test("[\\\\]", "\\", 0);
  25. test("hello[/+]world", "hello/world", 0);
  26. test("hello world", "HELLO WORLD", FNM_CASEFOLD);
  27. puts("");
  28. puts("Should fail:");
  29. test("*World", "Hello Potato", 0);
  30. test("*World", "Potato", 0);
  31. test("H[ae]llo?World", "Hillo+World", 0);
  32. test("H[ae]llo?World", "Hello__World", 0);
  33. test("[0-9][!a]", "ab", 0);
  34. test("[0-9][!a]", "2a", 0);
  35. test("/a/*/d", "/a/b/c/d", FNM_PATHNAME);
  36. test("/a/*/d", "/a/bc/d/", FNM_PATHNAME);
  37. test("*hello", ".hello", FNM_PERIOD);
  38. test("/*hello", "/.hello", FNM_PERIOD | FNM_PATHNAME);
  39. test("[a!][a!]", "ab", 0);
  40. test("hello[/+]world", "hello/world", FNM_PATHNAME);
  41. test("hello world", "HELLO WORLD", 0);
  42. }