summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-04 15:54:04 (GMT)
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-04 15:54:04 (GMT)
commitd03315d84a0ac639f80b5c0d499c1c662be66c55 (patch)
tree8ec01d9e0742cbf5c2fc871d327eba12dd8c36b0 /dir.c
parentf20d51b0adb640c9351b3025e6dda7843c3342aa (diff)
* configure.in (dirfd): Check function.
* dir.c (dir_fileno): New method. [ruby-dev:48265] [Feature #9880] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47387 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index b915c4b..09b78c6 100644
--- a/dir.c
+++ b/dir.c
@@ -539,6 +539,37 @@ dir_inspect(VALUE dir)
return rb_funcall(dir, rb_intern("to_s"), 0, 0);
}
+#ifdef HAVE_DIRFD
+/*
+ * call-seq:
+ * dir.fileno -> integer
+ *
+ * Returns the file descriptor used in <em>dir</em>.
+ *
+ * d = Dir.new("..")
+ * d.fileno #=> 8
+ *
+ * This method uses dirfd() function defined by POSIX 2008.
+ * NotImplementedError is raised on other platforms, such as Windows,
+ * which doesn't provide the function.
+ *
+ */
+static VALUE
+dir_fileno(VALUE dir)
+{
+ struct dir_data *dirp;
+ int fd;
+
+ GetDIR(dir, dirp);
+ fd = dirfd(dirp->dir);
+ if (fd == -1)
+ rb_sys_fail("dirfd");
+ return INT2NUM(fd);
+}
+#else
+#define dir_fileno rb_f_notimplement
+#endif
+
/*
* call-seq:
* dir.path -> string or nil
@@ -2259,6 +2290,7 @@ Init_Dir(void)
rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
rb_define_method(rb_cDir,"initialize", dir_initialize, -1);
+ rb_define_method(rb_cDir,"fileno", dir_fileno, 0);
rb_define_method(rb_cDir,"path", dir_path, 0);
rb_define_method(rb_cDir,"to_path", dir_path, 0);
rb_define_method(rb_cDir,"inspect", dir_inspect, 0);