From f0355ae7078c8539aaec5991411802c6fd055533 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 8 May 2016 09:51:34 +0000 Subject: file.c: home dir fall back * file.c (rb_home_dir_of): return the default home path if the user name is the current user name, on platforms where struct pwd is not supported. a temporary measure against [Bug #12226]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54947 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ file.c | 18 +++++++++++++----- test/ruby/test_dir.rb | 4 ++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 854e23ced8..32e7cdfa80 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sun May 8 18:51:33 2016 Nobuyoshi Nakada + + * file.c (rb_home_dir_of): return the default home path if the + user name is the current user name, on platforms where struct + pwd is not supported. a temporary measure against + [Bug #12226]. + Sun May 8 08:51:38 2016 NARUSE, Yui * configure.in: add -Wsuggest-attribute=format and suppress warnings. diff --git a/file.c b/file.c index fab1b343c1..cc12c650ca 100644 --- a/file.c +++ b/file.c @@ -3178,17 +3178,25 @@ copy_home_path(VALUE result, const char *dir) VALUE rb_home_dir_of(VALUE user, VALUE result) { + const char *dir, *username = RSTRING_PTR(user); #ifdef HAVE_PWD_H - struct passwd *pwPtr = getpwnam(RSTRING_PTR(user)); + struct passwd *pwPtr = getpwnam(username); +#else + extern char *getlogin(void); + const char *pwPtr = 0; + # define endpwent() ((void)0) + if (strcasecmp(username, getlogin()) == 0) + dir = pwPtr = getenv("HOME"); +#endif if (!pwPtr) { endpwent(); -#endif rb_raise(rb_eArgError, "user %"PRIsVALUE" doesn't exist", user); -#ifdef HAVE_PWD_H } - copy_home_path(result, pwPtr->pw_dir); - endpwent(); +#ifdef HAVE_PWD_H + dir = pwPtr->pw_dir; #endif + copy_home_path(result, dir); + endpwent(); return result; } diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 2685bea2a4..b188f4da60 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -295,6 +295,10 @@ class TestDir < Test::Unit::TestCase assert_nothing_raised(ArgumentError) { assert_equal(@nodir, Dir.home) assert_equal(@nodir, Dir.home("")) + if user = ENV["USER"] + ENV["HOME"] = env_home + assert_equal(File.expand_path(env_home), Dir.home(user)) + end } %W[no:such:user \u{7559 5b88}:\u{756a}].each do |user| assert_raise_with_message(ArgumentError, /#{user}/) {Dir.home(user)} -- cgit v1.2.3