From 98bc4759107c54783c08bf1ecde35e96f1345dc1 Mon Sep 17 00:00:00 2001
From: Johannes Schindelin <johannes.schindelin@gmx.de>
Date: Fri, 30 Jun 2017 22:19:49 +0200
Subject: mingw: accommodate for BusyBox' assumptions about isatty()

On Windows, isatty(fd) determines whether the file descriptor refers to
a character device.

The thing is: even NUL or a printer is a character device. BusyBox
thinks, however, that isatty() only returns non-zero for an interactive
terminal.

So let's shadow isatty() by a version that answers the question BusyBox
wants to have answered.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 win32/winansi.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

(limited to 'win32')

diff --git a/win32/winansi.c b/win32/winansi.c
index 7c7e1a626..8fffc5a00 100644
--- a/win32/winansi.c
+++ b/win32/winansi.c
@@ -733,3 +733,30 @@ int winansi_getc(FILE *stream)
 
 	return rv;
 }
+
+/* Ensure that isatty(fd) returns 0 for the NUL device */
+int mingw_isatty(int fd)
+{
+	int result = _isatty(fd);
+
+	if (result) {
+		HANDLE handle = (HANDLE) _get_osfhandle(fd);
+		CONSOLE_SCREEN_BUFFER_INFO sbi;
+		DWORD mode;
+
+		if (handle == INVALID_HANDLE_VALUE)
+			return 0;
+
+		/* check if its a device (i.e. console, printer, serial port) */
+		if (GetFileType(handle) != FILE_TYPE_CHAR)
+			return 0;
+
+		if (!fd) {
+			if (!GetConsoleMode(handle, &mode))
+				return 0;
+		} else if (!GetConsoleScreenBufferInfo(handle, &sbi))
+			return 0;
+	}
+
+	return result;
+}
-- 
cgit v1.2.3-55-g6feb