summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--include/ruby/win32.h4
-rw-r--r--io.c4
-rw-r--r--process.c14
-rw-r--r--win32/win32.c23
5 files changed, 34 insertions, 16 deletions
diff --git a/ChangeLog b/ChangeLog
index e69b41a..c85b9cb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Nov 28 16:15:47 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * win32/win32.c (rb_w32_dup2): extract from rb_cloexec_dup2() and
+ redirect_dup2().
+
Tue Nov 28 14:40:00 2013 Akira Matsuda <ronnie@dio.jp>
* lib/drb/ssl.rb: [Doc] Fix typo
diff --git a/include/ruby/win32.h b/include/ruby/win32.h
index 96b2f3f..070bab2 100644
--- a/include/ruby/win32.h
+++ b/include/ruby/win32.h
@@ -346,6 +346,7 @@ extern int rb_w32_access(const char *, int);
extern int rb_w32_uaccess(const char *, int);
extern char rb_w32_fd_is_text(int);
extern int rb_w32_fstati64(int, struct stati64 *);
+extern int rb_w32_dup2(int, int);
#ifdef __BORLANDC__
extern off_t _lseeki64(int, off_t, int);
@@ -734,6 +735,9 @@ extern char *rb_w32_strerror(int);
#undef times
#define times(t) rb_w32_times(t)
+
+#undef dup2
+#define dup2(o, n) rb_w32_dup2(o, n)
#endif
struct tms {
diff --git a/io.c b/io.c
index 053e8f3..9cdec94 100644
--- a/io.c
+++ b/io.c
@@ -275,10 +275,6 @@ rb_cloexec_dup2(int oldfd, int newfd)
}
#else
ret = dup2(oldfd, newfd);
-# ifdef _WIN32
- if (newfd >= 0 && newfd <= 2)
- SetStdHandle(newfd == 0 ? STD_INPUT_HANDLE : newfd == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE, (HANDLE)rb_w32_get_osfhandle(newfd));
-# endif
#endif
if (ret == -1) return -1;
}
diff --git a/process.c b/process.c
index c12182a..f1482c7 100644
--- a/process.c
+++ b/process.c
@@ -2500,28 +2500,16 @@ redirect_dup(int oldfd)
ttyprintf("dup(%d) => %d\n", oldfd, ret);
return ret;
}
-#else
-#define redirect_dup(oldfd) dup(oldfd)
-#endif
-#if defined(DEBUG_REDIRECT) || defined(_WIN32)
static int
redirect_dup2(int oldfd, int newfd)
{
int ret;
ret = dup2(oldfd, newfd);
- if (newfd >= 0 && newfd <= 2)
- SetStdHandle(newfd == 0 ? STD_INPUT_HANDLE : newfd == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE, (HANDLE)rb_w32_get_osfhandle(newfd));
-#if defined(DEBUG_REDIRECT)
ttyprintf("dup2(%d, %d)\n", oldfd, newfd);
-#endif
return ret;
}
-#else
-#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
-#endif
-#if defined(DEBUG_REDIRECT)
static int
redirect_close(int fd)
{
@@ -2541,6 +2529,8 @@ redirect_open(const char *pathname, int flags, mode_t perm)
}
#else
+#define redirect_dup(oldfd) dup(oldfd)
+#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
#define redirect_close(fd) close(fd)
#define redirect_open(pathname, flags, perm) open((pathname), (flags), (perm))
#endif
diff --git a/win32/win32.c b/win32/win32.c
index e1ac405..9cbd07f 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -79,6 +79,7 @@ static char *w32_getenv(const char *name, UINT cp);
#undef fclose
#undef close
#undef setsockopt
+#undef dup2
#if defined __BORLANDC__
# define _filbuf _fgetc
@@ -5465,6 +5466,28 @@ rb_w32_getppid(void)
return ppid;
}
+STATIC_ASSERT(std_handle, (STD_OUTPUT_HANDLE-STD_INPUT_HANDLE)==(STD_ERROR_HANDLE-STD_OUTPUT_HANDLE));
+
+/* License: Ruby's */
+#define set_new_std_handle(newfd, handle) do { \
+ if ((unsigned)(newfd) > 2) break; \
+ SetStdHandle(STD_INPUT_HANDLE+(STD_OUTPUT_HANDLE-STD_INPUT_HANDLE)*(newfd), \
+ (handle)); \
+ } while (0)
+#define set_new_std_fd(newfd) set_new_std_handle(newfd, (HANDLE)rb_w32_get_osfhandle(newfd))
+
+/* License: Ruby's */
+int
+rb_w32_dup2(int oldfd, int newfd)
+{
+ int ret;
+
+ if (oldfd == newfd) return newfd;
+ ret = dup2(oldfd, newfd);
+ set_new_std_fd(newfd);
+ return ret;
+}
+
/* License: Ruby's */
int
rb_w32_uopen(const char *file, int oflag, ...)