1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2016-05-08 09:51:34 +00:00
parent 65bbd0884c
commit f0355ae707
3 changed files with 24 additions and 5 deletions

View file

@ -1,3 +1,10 @@
Sun May 8 18:51:33 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@ruby-lang.org>
* configure.in: add -Wsuggest-attribute=format and suppress warnings.

18
file.c
View file

@ -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;
}

View file

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