From 6b88dd2698760ef2e2306e3e79a0ed9fe159a12c Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 26 Nov 2016 11:37:01 +0000 Subject: file.c: home directory from system * file.c (rb_default_home_dir): resolve home directory from the system database when HOME is not set. [Feature #12695] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56902 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- file.c | 22 +++++++++++++++++++++- test/ruby/test_dir.rb | 2 -- test/ruby/test_file_exhaustive.rb | 1 - win32/file.c | 13 +++++++++++++ 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 @@ -236,6 +236,19 @@ append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc) return dst; } +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) { -- cgit v1.2.3