diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-26 11:37:01 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-26 11:37:01 +0000 |
commit | 6b88dd2698760ef2e2306e3e79a0ed9fe159a12c (patch) | |
tree | cd3b580d1449865c3023ab6d7bdea068d8245c78 /file.c | |
parent | 58742627af428650799118179639b2793372819a (diff) |
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
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 22 |
1 files changed, 21 insertions, 1 deletions
@@ -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) { |