ruby-changes:42873
From: nobu <ko1@a...>
Date: Sun, 8 May 2016 17:55:00 +0900 (JST)
Subject: [ruby-changes:42873] nobu:r54947 (trunk): file.c: home dir fall back
nobu 2016-05-08 18:51:34 +0900 (Sun, 08 May 2016) New Revision: 54947 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54947 Log: 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]. Modified files: trunk/ChangeLog trunk/file.c trunk/test/ruby/test_dir.rb Index: file.c =================================================================== --- file.c (revision 54946) +++ file.c (revision 54947) @@ -3178,17 +3178,25 @@ copy_home_path(VALUE result, const char https://github.com/ruby/ruby/blob/trunk/file.c#L3178 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; } Index: test/ruby/test_dir.rb =================================================================== --- test/ruby/test_dir.rb (revision 54946) +++ test/ruby/test_dir.rb (revision 54947) @@ -295,6 +295,10 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L295 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)} Index: ChangeLog =================================================================== --- ChangeLog (revision 54946) +++ ChangeLog (revision 54947) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun May 8 18:51:33 2016 Nobuyoshi Nakada <nobu@r...> + + * 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 <naruse@r...> * configure.in: add -Wsuggest-attribute=format and suppress warnings. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/