Skip to content

Commit

Permalink
file.c: home dir fall back
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed May 8, 2016
1 parent 65bbd08 commit f0355ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
@@ -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.
Expand Down
18 changes: 13 additions & 5 deletions file.c
Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_dir.rb
Expand Up @@ -295,6 +295,10 @@ def test_home
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)}
Expand Down

0 comments on commit f0355ae

Please sign in to comment.