summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-26 18:10:47 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-26 18:10:47 +0000
commitf01fe1351eb46d1c13010aef245405b890efd191 (patch)
treeb636e0c12e3d0bd09a5c2a9925c6273b8f9f268d /io.c
parent2dfe646bae7d76ca1a234daf4b168499d07853bf (diff)
* 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/trunk@4177 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c53
1 files changed, 29 insertions, 24 deletions
diff --git a/io.c b/io.c
index debb94a7cd..d9db70401e 100644
--- a/io.c
+++ b/io.c
@@ -2351,7 +2351,7 @@ io_reopen(io, nfile)
{
OpenFile *fptr, *orig;
char *mode;
- int fd;
+ int fd, fd2;
off_t pos = 0;
nfile = rb_io_get_io(nfile);
@@ -2386,36 +2386,41 @@ io_reopen(io, nfile)
mode = rb_io_mode_string(fptr);
fd = fileno(fptr->f);
- if (fptr->f == stdin || fptr->f == stdout || fptr->f == stderr) {
- 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);
- }
- rb_thread_fd_close(fd);
- 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 (fptr->f == stdin || fptr->f == stdout || fptr->f == stderr) {
+ 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);
+ }
+ rb_thread_fd_close(fd);
+ 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)) {
fd = fileno(fptr->f2);
- fclose(fptr->f2);
- rb_thread_fd_close(fd);
- if (orig->f2) {
- if (dup2(fileno(orig->f2), fd) < 0)
+ if (!orig->f2) {
+ fclose(fptr->f2);
+ rb_thread_fd_close(fd);
+ fptr->f2 = 0;
+ }
+ else if (fd != (fd2 = fileno(orig->f2))) {
+ fclose(fptr->f2);
+ rb_thread_fd_close(fd);
+ 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) {