From 6aa71d4c800d11d9735007cf3b063e5ea2fc5941 Mon Sep 17 00:00:00 2001 From: knu Date: Wed, 13 Jun 2001 10:51:39 +0000 Subject: * dir.c (Init_Dir): add a new method File::fnmatch? along with File::Constants::FNM_*. While I am here, FNM_NOCASE is renamed to FNM_CASEFOLD which is commonly used by *BSD and GNU libc. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- dir.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'dir.c') diff --git a/dir.c b/dir.c index b706abc936..e778b4a922 100644 --- a/dir.c +++ b/dir.c @@ -68,7 +68,7 @@ char *strchr _((char*,char)); #define FNM_NOESCAPE 0x01 #define FNM_PATHNAME 0x02 #define FNM_PERIOD 0x04 -#define FNM_NOCASE 0x08 +#define FNM_CASEFOLD 0x08 #define FNM_NOMATCH 1 #define FNM_ERROR 2 @@ -100,7 +100,7 @@ range(pat, test, flags) int flags; { int not, ok = 0; - int nocase = flags & FNM_NOCASE; + int nocase = flags & FNM_CASEFOLD; int escape = !(flags & FNM_NOESCAPE); not = *pat == '!' || *pat == '^'; @@ -145,7 +145,7 @@ fnmatch(pat, string, flags) int escape = !(flags & FNM_NOESCAPE); int pathname = flags & FNM_PATHNAME; int period = flags & FNM_PERIOD; - int nocase = flags & FNM_NOCASE; + int nocase = flags & FNM_CASEFOLD; while (c = *pat++) { switch (c) { @@ -764,7 +764,7 @@ rb_globi(path, func, arg) void (*func)(); VALUE arg; { - glob_helper(path, FNM_PERIOD|FNM_NOCASE, func, arg); + glob_helper(path, FNM_PERIOD|FNM_CASEFOLD, func, arg); } static void push_pattern _((const char *path, VALUE ary)); @@ -921,6 +921,30 @@ dir_entries(io, dirname) return rb_ensure(rb_Array, dir, dir_close, dir); } +static VALUE +file_s_fnmatch(argc, argv, obj) + int argc; + VALUE *argv; + VALUE obj; +{ + VALUE pattern, path; + VALUE rflags; + int flags; + + if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3) + flags = NUM2INT(rflags); + else + flags = 0; + + StringValue(pattern); + StringValue(path); + + if (fnmatch(RSTRING(pattern)->ptr, RSTRING(path)->ptr, flags) == 0) + return Qtrue; + + return Qfalse; +} + void Init_Dir() { @@ -954,4 +978,12 @@ Init_Dir() rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, 1); rb_define_singleton_method(rb_cDir,"[]", dir_s_glob, 1); + + rb_define_singleton_method(rb_cFile,"fnmatch", file_s_fnmatch, -1); + rb_define_singleton_method(rb_cFile,"fnmatch?", file_s_fnmatch, -1); + + rb_file_const("FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE)); + rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME)); + rb_file_const("FNM_PERIOD", INT2FIX(FNM_PERIOD)); + rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD)); } -- cgit v1.2.3