summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-26 11:37:01 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-26 11:37:01 +0000
commit6b88dd2698760ef2e2306e3e79a0ed9fe159a12c (patch)
treecd3b580d1449865c3023ab6d7bdea068d8245c78 /file.c
parent58742627af428650799118179639b2793372819a (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.c22
1 files changed, 21 insertions, 1 deletions
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)
{