From 0322457e2bec0b9409a03887a8235dbe14e357f4 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 4 Apr 2016 09:42:58 -0700 Subject: [PATCH] Port redirect-debugging-output to MS-Windows Suggested by Eli Zaretskii in: http://lists.gnu.org/archive/html/emacs-devel/2016-04/msg00037.html * src/print.c [WINDOWSNT]: Include sys/socket.h. * src/w32.c (sys_dup2): Work around problem with MS-Windows _dup2. --- src/print.c | 4 ++++ src/w32.c | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/print.c b/src/print.c index db2918ff478..83edbb6bfa4 100644 --- a/src/print.c +++ b/src/print.c @@ -38,6 +38,10 @@ along with GNU Emacs. If not, see . */ #include #include +#ifdef WINDOWSNT +# include /* for F_DUPFD_CLOEXEC */ +#endif + struct terminal; /* Avoid actual stack overflow in print. */ diff --git a/src/w32.c b/src/w32.c index 3f4ac88520e..94aa7d891d5 100644 --- a/src/w32.c +++ b/src/w32.c @@ -8181,17 +8181,33 @@ sys_dup2 (int src, int dst) return -1; } - /* make sure we close the destination first if it's a pipe or socket */ - if (src != dst && fd_info[dst].flags != 0) + /* MS _dup2 seems to have weird side effect when invoked with 2 + identical arguments: an attempt to fclose the corresponding stdio + stream after that hangs (we do close standard streams in + init_ntproc). Attempt to avoid that by not calling _dup2 that + way: if SRC is valid, we know that dup2 should be a no-op, so do + nothing and return DST. */ + if (src == dst) + { + if ((HANDLE)_get_osfhandle (src) == INVALID_HANDLE_VALUE) + { + errno = EBADF; + return -1; + } + return dst; + } + + /* Make sure we close the destination first if it's a pipe or socket. */ + if (fd_info[dst].flags != 0) sys_close (dst); rc = _dup2 (src, dst); if (rc == 0) { - /* duplicate our internal info as well */ + /* Duplicate our internal info as well. */ fd_info[dst] = fd_info[src]; } - return rc; + return rc == 0 ? dst : rc; } int -- 2.39.5