diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-25 04:06:50 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-25 04:06:50 +0000 |
commit | 7ec4a44762e63b75120ecc5154a00828c8f31b01 (patch) | |
tree | 3121babc149fe568f45db08661b1ee56e11d659c /file.c | |
parent | dda113e3ff954064f718c73a114bb01361b5d205 (diff) | |
download | ruby-7ec4a44762e63b75120ecc5154a00828c8f31b01.tar.gz |
file.c: split rb_home_dir
* dir.c (dir_s_home): use rb_home_dir_of and rb_default_home_dir.
* file.c (rb_home_dir_of): split from rb_home_dir() for the home
directry of the given user, and the user name is a VALUE, not a bare
pointer. should raise if the user does not exist.
* file.c (rb_default_home_dir): split from rb_home_dir() for the home
directry of the current user.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42160 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 65 |
1 files changed, 35 insertions, 30 deletions
@@ -2885,10 +2885,9 @@ ntfs_tail(const char *path, const char *end, rb_encoding *enc) buflen = RSTRING_LEN(result),\ pend = p + buflen) -VALUE -rb_home_dir(const char *user, VALUE result) +static VALUE +copy_home_path(VALUE result, const char *dir) { - const char *dir; char *buf; #if defined DOSISH || defined __CYGWIN__ char *p, *bend; @@ -2896,29 +2895,9 @@ rb_home_dir(const char *user, VALUE result) long dirlen; rb_encoding *enc; - if (!user || !*user) { - if (!(dir = getenv("HOME"))) { - rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'"); - } - dirlen = strlen(dir); - rb_str_resize(result, dirlen); - memcpy(buf = RSTRING_PTR(result), dir, dirlen); - } - else { -#ifdef HAVE_PWD_H - struct passwd *pwPtr = getpwnam(user); - if (!pwPtr) { - endpwent(); - return Qnil; - } - dirlen = strlen(pwPtr->pw_dir); - rb_str_resize(result, dirlen); - memcpy(buf = RSTRING_PTR(result), pwPtr->pw_dir, dirlen + 1); - endpwent(); -#else - return Qnil; -#endif - } + dirlen = strlen(dir); + rb_str_resize(result, dirlen); + memcpy(buf = RSTRING_PTR(result), dir, dirlen); enc = rb_filesystem_encoding(); rb_enc_associate(result, enc); #if defined DOSISH || defined __CYGWIN__ @@ -2931,6 +2910,33 @@ rb_home_dir(const char *user, VALUE result) return result; } +VALUE +rb_home_dir_of(VALUE user, VALUE result) +{ +#ifdef HAVE_PWD_H + struct passwd *pwPtr = getpwnam(RSTRING_PTR(user)); + 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(); +#endif + return result; +} + +VALUE +rb_default_home_dir(VALUE result) +{ + const char *dir = getenv("HOME"); + if (!dir) { + rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'"); + } + return copy_home_path(result, dir); +} + #ifndef _WIN32 static char * append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc) @@ -2980,6 +2986,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na b = 0; rb_str_set_len(result, 0); if (*++s) ++s; + rb_default_home_dir(result); } else { s = nextdirsep(b = s, fend, enc); @@ -2987,13 +2994,11 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na BUFCHECK(bdiff + userlen >= buflen); memcpy(p, b, userlen); rb_str_set_len(result, userlen); + rb_enc_associate(result, enc); + rb_home_dir_of(result, result); buf = p + 1; p += userlen; } - if (NIL_P(rb_home_dir(buf, result))) { - rb_enc_raise(enc, rb_eArgError, "%.0"PRIsVALUE"user %s doesn't exist", fname, - buf); - } if (!rb_is_absolute_path(RSTRING_PTR(result))) { if (userlen) { rb_enc_raise(enc, rb_eArgError, "non-absolute home of %.*s%.0"PRIsVALUE, |