ruby-changes:44829
From: nobu <ko1@a...>
Date: Sat, 26 Nov 2016 20:37:07 +0900 (JST)
Subject: [ruby-changes:44829] nobu:r56902 (trunk): file.c: home directory from system
nobu 2016-11-26 20:37:01 +0900 (Sat, 26 Nov 2016) New Revision: 56902 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56902 Log: 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] Modified files: trunk/file.c trunk/test/ruby/test_dir.rb trunk/test/ruby/test_file_exhaustive.rb trunk/win32/file.c Index: test/ruby/test_file_exhaustive.rb =================================================================== --- test/ruby/test_file_exhaustive.rb (revision 56901) +++ test/ruby/test_file_exhaustive.rb (revision 56902) @@ -827,7 +827,6 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L827 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"] = "." Index: test/ruby/test_dir.rb =================================================================== --- test/ruby/test_dir.rb (revision 56901) +++ test/ruby/test_dir.rb (revision 56902) @@ -289,8 +289,6 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L289 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) Index: win32/file.c =================================================================== --- win32/file.c (revision 56901) +++ win32/file.c (revision 56902) @@ -237,6 +237,19 @@ append_wstr(VALUE dst, const WCHAR *ws, https://github.com/ruby/ruby/blob/trunk/win32/file.c#L237 } 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; Index: file.c =================================================================== --- file.c (revision 56901) +++ file.c (revision 56902) @@ -3221,17 +3221,37 @@ rb_home_dir_of(VALUE user, VALUE result) https://github.com/ruby/ruby/blob/trunk/file.c#L3221 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) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/