Skip to content

Commit 4af28e2

Browse files
committed
Merge 'mingw-isatty' into HEAD
2 parents 6cd98a7 + 9669059 commit 4af28e2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ int mingw_raise(int sig);
472472
* ANSI emulation wrappers
473473
*/
474474

475+
int winansi_isatty(int fd);
476+
#define isatty winansi_isatty
477+
475478
void winansi_init(void);
476479
HANDLE winansi_get_osfhandle(int fd);
477480

compat/winansi.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ static void set_interactive(int fd, int bit)
2424

2525
#endif
2626

27+
/* In this file, we actually want to use Windows' own isatty(). */
28+
#undef isatty
29+
2730
/*
2831
ANSI codes used by git: m, K
2932
@@ -660,6 +663,36 @@ static void detect_msys_tty(int fd)
660663

661664
#endif
662665

666+
int winansi_isatty(int fd)
667+
{
668+
int res = isatty(fd);
669+
670+
if (res) {
671+
/*
672+
* Make sure that /dev/null is not fooling Git into believing
673+
* that we are connected to a terminal, as "_isatty() returns a
674+
* nonzero value if the descriptor is associated with a
675+
* character device."; for more information, see
676+
*
677+
* https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
678+
*/
679+
HANDLE handle = (HANDLE)_get_osfhandle(fd);
680+
if (fd == STDIN_FILENO) {
681+
DWORD dummy;
682+
683+
if (!GetConsoleMode(handle, &dummy))
684+
res = 0;
685+
} else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
686+
CONSOLE_SCREEN_BUFFER_INFO dummy;
687+
688+
if (!GetConsoleScreenBufferInfo(handle, &dummy))
689+
res = 0;
690+
}
691+
}
692+
693+
return res;
694+
}
695+
663696
void winansi_init(void)
664697
{
665698
int con1, con2;

0 commit comments

Comments
 (0)