aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--file.c22
-rw-r--r--test/ruby/test_dir.rb2
-rw-r--r--test/ruby/test_file_exhaustive.rb1
-rw-r--r--win32/file.c13
4 files changed, 34 insertions, 4 deletions
diff --git a/file.c b/file.c
index 170c251519..7e3cd407c2 100644
--- a/file.c
+++ b/file.c
@@ -3221,17 +3221,37 @@ rb_home_dir_of(VALUE user, VALUE result)
return result;
}
+#ifndef _WIN32
VALUE
rb_default_home_dir(VALUE result)
{
const char *dir = getenv("HOME");
+
+#if defined HAVE_PWD_H
+ if (!dir) {
+ const char *login = getlogin();
+ if (login) {
+ struct passwd *pw = getpwnam(login);
+ if (pw) {
+ copy_home_path(result, pw->pw_dir);
+ endpwent();
+ return result;
+ }
+ endpwent();
+ rb_raise(rb_eArgError, "couldn't find HOME for login `%s' -- expanding `~'",
+ login);
+ }
+ else {
+ rb_raise(rb_eArgError, "couldn't find login name -- expanding `~'");
+ }
+ }
+#endif
if (!dir) {
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
}
return copy_home_path(result, dir);
}
-#ifndef _WIN32
static VALUE
ospath_new(const char *ptr, long len, rb_encoding *fsenc)
{
diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb
index bb778e754d..d100fd7d59 100644
--- a/test/ruby/test_dir.rb
+++ b/test/ruby/test_dir.rb
@@ -289,8 +289,6 @@ class TestDir < Test::Unit::TestCase
ENV.delete("HOME")
ENV.delete("LOGDIR")
- assert_raise(ArgumentError) { Dir.home }
- assert_raise(ArgumentError) { Dir.home("") }
ENV["HOME"] = @nodir
assert_nothing_raised(ArgumentError) {
assert_equal(@nodir, Dir.home)
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index f316d3fcf3..35bbed4e61 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -827,7 +827,6 @@ class TestFileExhaustive < Test::Unit::TestCase
ENV["HOMEDRIVE"] = nil
ENV["HOMEPATH"] = nil
ENV["USERPROFILE"] = nil
- assert_raise(ArgumentError) { File.expand_path("~") }
ENV["HOME"] = "~"
assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
ENV["HOME"] = "."
diff --git a/win32/file.c b/win32/file.c
index c0efb7b091..b50c61292d 100644
--- a/win32/file.c
+++ b/win32/file.c
@@ -237,6 +237,19 @@ append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc)
}
VALUE
+rb_default_home_dir(VALUE result)
+{
+ const WCHAR *dir = rb_w32_home_dir();
+ if (!dir) {
+ rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
+ }
+ append_wstr(result, dir, -1,
+ rb_w32_filecp(), rb_filesystem_encoding());
+ xfree(dir);
+ return result;
+}
+
+VALUE
rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
{
size_t size = 0, whome_len = 0;