Skip to content

Commit a2c8900

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
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 166189d commit a2c8900

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
@@ -470,6 +470,18 @@ static void die_lasterr(const char *fmt, ...)
470470
va_end(params);
471471
}
472472

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

0 commit comments

Comments
 (0)