Back to index

glibc  2.9
testfnm.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include "fnmatch.h"
00004 
00005 struct {
00006   const char *name;
00007   const char *pattern;
00008   int flags;
00009   int expected;
00010 } tests[] = {
00011   { "lib", "*LIB*", FNM_PERIOD, FNM_NOMATCH },
00012   { "lib", "*LIB*", FNM_CASEFOLD|FNM_PERIOD, 0 },
00013   { "a/b", "a[/]b", 0, 0 },
00014   { "a/b", "a[/]b", FNM_PATHNAME, FNM_NOMATCH },
00015   { "a/b", "[a-z]/[a-z]", 0, 0 },
00016   { "a/b", "*", FNM_PATHNAME, FNM_NOMATCH },
00017   { "a/b", "*[/]b", FNM_PATHNAME, FNM_NOMATCH },
00018   { "a/b", "*[b]", FNM_PATHNAME, FNM_NOMATCH },
00019   { "a/b", "[*]/b", 0, FNM_NOMATCH },
00020   { "*/b", "[*]/b", 0, 0 },
00021   { "a/b", "[?]/b", 0, FNM_NOMATCH },
00022   { "?/b", "[?]/b", 0, 0 },
00023   { "a/b", "[[a]/b", 0, 0 },
00024   { "[/b", "[[a]/b", 0, 0 },
00025   { "a/b", "\\*/b", 0, FNM_NOMATCH },
00026   { "*/b", "\\*/b", 0, 0 },
00027   { "a/b", "\\?/b", 0, FNM_NOMATCH },
00028   { "?/b", "\\?/b", 0, 0 },
00029   { "[/b", "[/b", 0, FNM_NOMATCH },
00030   { "[/b", "\\[/b", 0, 0 },
00031   { "aa/b", "?""?/b", 0, 0 },
00032   { "aa/b", "?""?""?b", 0, 0 },
00033   { "aa/b", "?""?""?b", FNM_PATHNAME, FNM_NOMATCH },
00034   { ".a/b", "?a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00035   { "a/.b", "a/?b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00036   { ".a/b", "*a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00037   { "a/.b", "a/*b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00038   { ".a/b", "[.]a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00039   { "a/.b", "a/[.]b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00040   { "a/b", "*/?", FNM_PATHNAME|FNM_PERIOD, 0 },
00041   { "a/b", "?/*", FNM_PATHNAME|FNM_PERIOD, 0 },
00042   { ".a/b", ".*/?", FNM_PATHNAME|FNM_PERIOD, 0 },
00043   { "a/.b", "*/.?", FNM_PATHNAME|FNM_PERIOD, 0 },
00044   { "a/.b", "*/*", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH },
00045   { "a/.b", "*?*/*", FNM_PERIOD, 0 },
00046   { "a./b", "*[.]/b", FNM_PATHNAME|FNM_PERIOD, 0 },
00047   { "a/b", "*[[:alpha:]]/*[[:alnum:]]", FNM_PATHNAME, 0 },
00048   { "a/b", "*[![:digit:]]*/[![:d-d]", FNM_PATHNAME, 0 },
00049   { "a/[", "*[![:digit:]]*/[[:d-d]", FNM_PATHNAME, 0 },
00050   { "a/[", "*[![:digit:]]*/[![:d-d]", FNM_PATHNAME, FNM_NOMATCH },
00051   { "a.b", "a?b", FNM_PATHNAME|FNM_PERIOD, 0 },
00052   { "a.b", "a*b", FNM_PATHNAME|FNM_PERIOD, 0 },
00053   { "a.b", "a[.]b", FNM_PATHNAME|FNM_PERIOD, 0 },
00054   { "a/b", "*a*", FNM_PATHNAME|FNM_LEADING_DIR, 0 },
00055   { "ab/c", "*a?", FNM_PATHNAME|FNM_LEADING_DIR, 0 },
00056   { "ab/c", "a?", FNM_PATHNAME|FNM_LEADING_DIR, 0 },
00057   { "a/b", "?*/?", FNM_PATHNAME, 0 },
00058   { "/b", "*/?", FNM_PATHNAME, 0 },
00059   { "/b", "**/?", FNM_PATHNAME, 0 },
00060 };
00061 
00062 int
00063 main (void)
00064 {
00065   size_t i;
00066   int errors = 0;
00067 
00068   for (i = 0; i < sizeof (tests) / sizeof (*tests); i++)
00069     {
00070       int match;
00071 
00072       match = fnmatch (tests[i].pattern, tests[i].name, tests[i].flags);
00073 
00074       printf ("[%2zd]  %s %s %s  -> %s\n", i, tests[i].pattern,
00075              match == 0 ? "matches" : "does not match",
00076              tests[i].name,
00077              match != tests[i].expected ? "FAIL" : "OK");
00078 
00079       if (match != tests[i].expected)
00080        ++errors ;
00081     }
00082 
00083   return errors != 0;
00084 }