diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-26 18:10:47 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-26 18:10:47 +0000 |
commit | 3b363f630c067d29119707575ac3f041823e3a94 (patch) | |
tree | ca8342e5848b1c9b6b42eb3b4d38e89703be254a | |
parent | 79cfb3d908d49f10fd0c74687fd9759091cce252 (diff) | |
download | ruby-3b363f630c067d29119707575ac3f041823e3a94.tar.gz |
* io.c (io_reopen): avoid dup2() equal handles not to close itself and
to get rid of a msvcrt bug. [ruby-dev:20919]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@4177 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | io.c | 48 |
2 files changed, 31 insertions, 22 deletions
@@ -1,3 +1,8 @@ +Sun Jul 27 03:10:31 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net> + + * io.c (io_reopen): avoid dup2() equal handles not to close itself and + to get rid of a msvcrt bug. [ruby-dev:20919] + Thu Jul 17 13:42:53 2003 WATANABE Hirofumi <eban@ruby-lang.org> * lib/ftools.rb (File::makedirs): do not handle "//" as a directory. @@ -1845,7 +1845,7 @@ io_reopen(io, nfile) { OpenFile *fptr, *orig; char *mode; - int fd; + int fd, fd2; long pos; nfile = rb_io_get_io(nfile); @@ -1878,34 +1878,38 @@ io_reopen(io, nfile) mode = rb_io_mode_string(fptr); fd = fileno(fptr->f); - if (fd < 3) { - clearerr(fptr->f); - /* need to keep stdio objects */ - if (dup2(fileno(orig->f), fd) < 0) - rb_sys_fail(orig->path); - } - else { - fclose(fptr->f); - if (dup2(fileno(orig->f), fd) < 0) - rb_sys_fail(orig->path); - fptr->f = rb_fdopen(fd, mode); - } - if ((orig->mode & FMODE_READABLE) && pos >= 0) { - io_seek(fptr, pos, SEEK_SET); - io_seek(orig, pos, SEEK_SET); + fd2 = fileno(orig->f); + if (fd != fd2) { + if (fd < 3) { + clearerr(fptr->f); + /* need to keep stdio objects */ + if (dup2(fd2, fd) < 0) + rb_sys_fail(orig->path); + } + else { + fclose(fptr->f); + if (dup2(fd2, fd) < 0) + rb_sys_fail(orig->path); + fptr->f = rb_fdopen(fd, mode); + } + if ((orig->mode & FMODE_READABLE) && pos >= 0) { + io_seek(fptr, pos, SEEK_SET); + io_seek(orig, pos, SEEK_SET); + } } if (fptr->f2) { fd = fileno(fptr->f2); - fclose(fptr->f2); - if (orig->f2) { - if (dup2(fileno(orig->f2), fd) < 0) + if (!orig->f2) { + fclose(fptr->f2); + fptr->f2 = 0; + } + else if (fd != (fd2 = fileno(orig->f2))) { + fclose(fptr->f2); + if (dup2(fd2, fd) < 0) rb_sys_fail(orig->path); fptr->f2 = rb_fdopen(fd, "w"); } - else { - fptr->f2 = 0; - } } if (fptr->mode & FMODE_BINMODE) { |