From f823e1ac0b65454e10d8ff1a88bf38c2ad6e5141 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 26 Nov 2016 11:37:01 +0000 Subject: 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 --- file.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'file.c') diff --git a/file.c b/file.c index 170c251519..7e3cd407c2 100644 --- a/file.c +++ b/file.c @@ -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) { -- cgit v1.2.3