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
This commit is contained in:
nobu 2016-11-26 11:37:01 +00:00
parent 58742627af
commit 6b88dd2698
4 changed files with 34 additions and 4 deletions

22
file.c
View File

@ -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)
{

View File

@ -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)

View File

@ -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"] = "."

View File

@ -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)
{