Skip to content

Commit a84b7f4

Browse files
committed
mingw: fix isatty() after dup2()
We newly handle isatty() by special-casing the stdin/stdout/stderr file descriptors, caching the return value. However, we missed the case where dup2() overrides the respective file descriptor. That poses a problem e.g. where the `show` builtin asks for a pager very early, the `setup_pager()` function sets the pager depending on the return value of `isatty()` and then redirects stdout. Subsequently, `cmd_log_init_finish()` calls `setup_pager()` *again*. What should happen now is that `isatty()` reports that stdout is *not* a TTY and consequently stdout should be left alone. Let's override dup2() to handle this appropriately. This fixes #1077 Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ee9c051 commit a84b7f4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ int mingw_raise(int sig);
475475
int winansi_isatty(int fd);
476476
#define isatty winansi_isatty
477477

478+
int winansi_dup2(int oldfd, int newfd);
479+
#define dup2 winansi_dup2
480+
478481
void winansi_init(void);
479482
HANDLE winansi_get_osfhandle(int fd);
480483

compat/winansi.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,18 @@ static void die_lasterr(const char *fmt, ...)
469469
va_end(params);
470470
}
471471

472+
#undef dup2
473+
int winansi_dup2(int oldfd, int newfd)
474+
{
475+
int ret = dup2(oldfd, newfd);
476+
477+
if (!ret && newfd >= 0 && newfd <= 2)
478+
fd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ?
479+
0 : fd_is_interactive[oldfd];
480+
481+
return ret;
482+
}
483+
472484
static HANDLE duplicate_handle(HANDLE hnd)
473485
{
474486
HANDLE hresult, hproc = GetCurrentProcess();

0 commit comments

Comments
 (0)