Skip to content

Commit

Permalink
mingw: fix isatty() after dup2()
Browse files Browse the repository at this point in the history
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 <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Mar 20, 2017
1 parent b8abe30 commit c2474dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compat/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ int mingw_raise(int sig);
int winansi_isatty(int fd);
#define isatty winansi_isatty

int winansi_dup2(int oldfd, int newfd);
#define dup2 winansi_dup2

void winansi_init(void);
HANDLE winansi_get_osfhandle(int fd);

Expand Down
12 changes: 12 additions & 0 deletions compat/winansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,18 @@ static void die_lasterr(const char *fmt, ...)
va_end(params);
}

#undef dup2
int winansi_dup2(int oldfd, int newfd)
{
int ret = dup2(oldfd, newfd);

if (!ret && newfd >= 0 && newfd <= 2)
fd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ?
0 : fd_is_interactive[oldfd];

return ret;
}

static HANDLE duplicate_handle(HANDLE hnd)
{
HANDLE hresult, hproc = GetCurrentProcess();
Expand Down

0 comments on commit c2474dc

Please sign in to comment.