summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-06-13 10:51:39 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-06-13 10:51:39 +0000
commit6aa71d4c800d11d9735007cf3b063e5ea2fc5941 (patch)
treecc69dd031427b3a0c11ceaa8b725d7a155511817
parent68ebfef615c0f4cda0aaa6837c92b03d47a6c8db (diff)
* 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
-rw-r--r--ChangeLog6
-rw-r--r--dir.c40
-rw-r--r--doc/NEWS9
3 files changed, 51 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index cbbf0cfbd0..748eaf6995 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Jun 13 19:34:59 2001 Akinori MUSHA <knu@iDaemons.org>
+
+ * 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.
+
Tue Jun 12 14:21:28 2001 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp>
* lib/mkmf.rb: target_prefix is only for installation, not for
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));
}
diff --git a/doc/NEWS b/doc/NEWS
index 6db42b8714..9bf0473298 100644
--- a/doc/NEWS
+++ b/doc/NEWS
@@ -1,3 +1,12 @@
+: File#fnmatch, File::Constants::FNM_*
+
+ Added. Refer to the fnmatch(3) manpage for details.
+
+ e.g.
+
+ # exclude files matching "*.bak".
+ files.reject! { |fn| File::fnmatch?("*.bak", fn) }
+
: Method#==
Added.