Skip to content

Commit

Permalink
Merge 'mingw-isatty' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
dscho committed Dec 12, 2016
2 parents 6cd98a7 + 9669059 commit 4af28e2
Show file tree
Hide file tree
Showing 2 changed files with 36 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 @@ -472,6 +472,9 @@ int mingw_raise(int sig);
* ANSI emulation wrappers
*/

int winansi_isatty(int fd);
#define isatty winansi_isatty

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

Expand Down
33 changes: 33 additions & 0 deletions compat/winansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ static void set_interactive(int fd, int bit)

#endif

/* In this file, we actually want to use Windows' own isatty(). */
#undef isatty

/*
ANSI codes used by git: m, K
Expand Down Expand Up @@ -660,6 +663,36 @@ static void detect_msys_tty(int fd)

#endif

int winansi_isatty(int fd)
{
int res = isatty(fd);

if (res) {
/*
* Make sure that /dev/null is not fooling Git into believing
* that we are connected to a terminal, as "_isatty() returns a
* nonzero value if the descriptor is associated with a
* character device."; for more information, see
*
* https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
*/
HANDLE handle = (HANDLE)_get_osfhandle(fd);
if (fd == STDIN_FILENO) {
DWORD dummy;

if (!GetConsoleMode(handle, &dummy))
res = 0;
} else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
CONSOLE_SCREEN_BUFFER_INFO dummy;

if (!GetConsoleScreenBufferInfo(handle, &dummy))
res = 0;
}
}

return res;
}

void winansi_init(void)
{
int con1, con2;
Expand Down

0 comments on commit 4af28e2

Please sign in to comment.