ruby-changes:10409
From: nobu <ko1@a...>
Date: Mon, 2 Feb 2009 16:33:21 +0900 (JST)
Subject: [ruby-changes:10409] Ruby:r21953 (trunk): * dir.c (dir_s_home): new method.
nobu 2009-02-02 16:33:08 +0900 (Mon, 02 Feb 2009) New Revision: 21953 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21953 Log: * dir.c (dir_s_home): new method. [ruby-core:21454] Modified files: trunk/ChangeLog trunk/dir.c trunk/file.c Index: ChangeLog =================================================================== --- ChangeLog (revision 21952) +++ ChangeLog (revision 21953) @@ -1,3 +1,7 @@ +Mon Feb 2 16:33:06 2009 Nobuyoshi Nakada <nobu@r...> + + * dir.c (dir_s_home): new method. [ruby-core:21454] + Mon Feb 2 16:06:10 2009 Tanaka Akira <akr@f...> * version.h: bump up to 1.9.2 patchlevel -1. Index: dir.c =================================================================== --- dir.c (revision 21952) +++ dir.c (revision 21953) @@ -1852,7 +1852,31 @@ return Qfalse; } +VALUE rb_home_dir(const char *user, VALUE result); + /* + * call-seq: + * Dir.home() => "/home/me" + * Dir.home("root") => "/root" + * + * Returns the home directory of the current user or the named user + * if given. + */ +static VALUE +dir_s_home(int argc, VALUE *argv, VALUE obj) +{ + VALUE user; + const char *u = 0; + + rb_scan_args(argc, argv, "01", &user); + if (!NIL_P(user)) { + SafeStringValue(user); + u = StringValueCStr(user); + } + return rb_home_dir(u, rb_str_new(0, 0)); +} + +/* * Objects of class <code>Dir</code> are directory streams representing * directories in the underlying file system. They provide a variety of * ways to list directories and their contents. See also @@ -1895,6 +1919,7 @@ rb_define_singleton_method(rb_cDir,"rmdir", dir_s_rmdir, 1); rb_define_singleton_method(rb_cDir,"delete", dir_s_rmdir, 1); rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1); + rb_define_singleton_method(rb_cDir,"home", dir_s_home, -1); rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, -1); rb_define_singleton_method(rb_cDir,"[]", dir_s_aref, -1); Index: file.c =================================================================== --- file.c (revision 21952) +++ file.c (revision 21953) @@ -2600,6 +2600,46 @@ static int is_absolute_path(const char*); +VALUE +rb_home_dir(const char *user, VALUE result) +{ + const char *dir; + char *buf; + long dirlen; + + 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_GETPWENT + struct passwd *pwPtr = getpwnam(user); + if (!pwPtr) { + endpwent(); + rb_raise(rb_eArgError, "user %s doesn't exist", user); + } + dirlen = strlen(pwPtr->pw_dir); + rb_str_resize(result, dirlen); + strcpy(buf = RSTRING_PTR(result), pwPtr->pw_dir); + endpwent(); +#else + return Qnil; +#endif + } +#if defined DOSISH || defined __CYGWIN__ + for (p = buf; *p; p = CharNext(p)) { + if (*p == '\\') { + *p = '/'; + } + } +#endif + return result; +} + static VALUE file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result) { @@ -2615,51 +2655,23 @@ tainted = OBJ_TAINTED(fname); if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */ + tainted = 1; if (isdirsep(s[1]) || s[1] == '\0') { - const char *dir = getenv("HOME"); - - if (!dir) { - rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `%s'", s); - } - dirlen = strlen(dir); - BUFCHECK(dirlen > buflen); - strcpy(buf, dir); -#if defined DOSISH || defined __CYGWIN__ - for (p = buf; *p; p = CharNext(p)) { - if (*p == '\\') { - *p = '/'; - } - } -#else - p = buf + strlen(dir); -#endif - s++; - tainted = 1; - SET_EXTERNAL_ENCODING(); + buf = 0; + rb_str_set_len(result, 0); + if (*++s) ++s; } else { -#ifdef HAVE_PWD_H - struct passwd *pwPtr; - s++; -#endif s = nextdirsep(b = s); BUFCHECK(bdiff + (s-b) >= buflen); memcpy(p, b, s-b); + rb_str_set_len(result, s-b); + buf = p + 1; p += s-b; - *p = '\0'; -#ifdef HAVE_PWD_H - pwPtr = getpwnam(buf); - if (!pwPtr) { - endpwent(); - rb_raise(rb_eArgError, "user %s doesn't exist", buf); - } - dirlen = strlen(pwPtr->pw_dir); - BUFCHECK(dirlen > buflen); - strcpy(buf, pwPtr->pw_dir); - p = buf + strlen(pwPtr->pw_dir); - endpwent(); -#endif } + rb_home_dir(buf, result); + BUFINIT(); + p = pend; } #ifdef DOSISH_DRIVE_LETTER /* skip drive letter */ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/