From 21f81885ab310f564fb60bfe7dcde816eeb11994 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 28 Nov 2013 07:15:56 +0000 Subject: [PATCH] win32.c: rb_w32_dup2 * win32/win32.c (rb_w32_dup2): extract from rb_cloexec_dup2() and redirect_dup2(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ include/ruby/win32.h | 4 ++++ io.c | 4 ---- process.c | 14 ++------------ win32/win32.c | 23 +++++++++++++++++++++++ 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index e69b41a382..c85b9cbc7c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Nov 28 16:15:47 2013 Nobuyoshi Nakada + + * win32/win32.c (rb_w32_dup2): extract from rb_cloexec_dup2() and + redirect_dup2(). + Tue Nov 28 14:40:00 2013 Akira Matsuda * lib/drb/ssl.rb: [Doc] Fix typo diff --git a/include/ruby/win32.h b/include/ruby/win32.h index 96b2f3f8a2..070bab28f9 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 053e8f3b0e..9cdec945bf 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 c12182abfe..f1482c7e98 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 e1ac4054fd..9cbd07f669 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, ...)