diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-03-16 08:30:09 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-03-16 08:30:09 +0000 |
commit | 3a2611c0609fa769500b839ee43a9e3a283defd5 (patch) | |
tree | 3019fbaf784a9ab4e0a25a0bb6f41009d10730ea /dir.c | |
parent | 520858f3f1198f640c8586575163034e6647b10f (diff) | |
download | ruby-3a2611c0609fa769500b839ee43a9e3a283defd5.tar.gz ruby-3a2611c0609fa769500b839ee43a9e3a283defd5.tar.xz ruby-3a2611c0609fa769500b839ee43a9e3a283defd5.zip |
* dir.c (dir_s_chdir): block form of Dir.chdir. (RCR#U016).
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@1256 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r-- | dir.c | 49 |
1 files changed, 39 insertions, 10 deletions
@@ -389,13 +389,38 @@ dir_close(dir) return Qnil; } +static void +dir_chdir(path) + const char *path; +{ + if (chdir(path) < 0) + rb_sys_fail(path); +} + +static int chdir_blocking = 0; + +static VALUE +chdir_restore(path) + const char *path; +{ + chdir_blocking--; + dir_chdir(path); + return Qnil; +} + +#ifdef HAVE_GETCWD +#define GETCWD(path) if (getcwd(path, sizeof(path)) == 0) rb_sys_fail(path) +#else +#define GETCWD(path) if (getwd(path) == 0) rb_sys_fail(path) +#endif + static VALUE dir_s_chdir(argc, argv, obj) int argc; VALUE *argv; VALUE obj; { - VALUE path; + VALUE path = Qnil; char *dist = ""; rb_secure(2); @@ -410,8 +435,18 @@ dir_s_chdir(argc, argv, obj) } } - if (chdir(dist) < 0) - rb_sys_fail(dist); + if (chdir_blocking > 0) + rb_warn("chdir during chdir block"); + + if (rb_block_given_p()) { + char cwd[MAXPATHLEN]; + + GETCWD(cwd); + chdir_blocking++; + dir_chdir(dist); + return rb_ensure(rb_yield, path, chdir_restore, (VALUE)cwd); + } + dir_chdir(dist); return INT2FIX(0); } @@ -422,13 +457,7 @@ dir_s_getwd(dir) { char path[MAXPATHLEN]; -#ifdef HAVE_GETCWD - if (getcwd(path, sizeof(path)) == 0) rb_sys_fail(path); -#else - extern char *getwd(); - if (getwd(path) == 0) rb_sys_fail(path); -#endif - + GETCWD(path); return rb_tainted_str_new2(path); } |