summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-02 07:33:08 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-02 07:33:08 +0000
commit043f665274006e4f040e681e0a1aee4975cf30b5 (patch)
treef838c55fe4361b3774d0e298f4ad064fe3a1711c /dir.c
parent4072b83916774ea0280bd74d18882620c1c7282c (diff)
* dir.c (dir_s_home): new method. [ruby-core:21454]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21953 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index f5e8cf72e8..f938fae721 100644
--- a/dir.c
+++ b/dir.c
@@ -1852,6 +1852,30 @@ file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
return Qfalse;
}
+VALUE rb_home_dir(const char *user, VALUE result);
+
+/*
+ * call-seq:
+ * Dir.home() => "/home/me"
+ * Dir.home("root") => "/root"
+ *
+ * Returns the home directory of the current user or the named user
+ * if given.
+ */
+static VALUE
+dir_s_home(int argc, VALUE *argv, VALUE obj)
+{
+ VALUE user;
+ const char *u = 0;
+
+ rb_scan_args(argc, argv, "01", &user);
+ if (!NIL_P(user)) {
+ SafeStringValue(user);
+ u = StringValueCStr(user);
+ }
+ return rb_home_dir(u, rb_str_new(0, 0));
+}
+
/*
* Objects of class <code>Dir</code> are directory streams representing
* directories in the underlying file system. They provide a variety of
@@ -1895,6 +1919,7 @@ Init_Dir(void)
rb_define_singleton_method(rb_cDir,"rmdir", dir_s_rmdir, 1);
rb_define_singleton_method(rb_cDir,"delete", dir_s_rmdir, 1);
rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1);
+ rb_define_singleton_method(rb_cDir,"home", dir_s_home, -1);
rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, -1);
rb_define_singleton_method(rb_cDir,"[]", dir_s_aref, -1);