git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/5] Detect console streams more reliably on Windows

From
Stepan Kasal <kasal@ucw.cz>
Date
Jun 6, 2014, 13:42 UTC
Message-ID
<1402062173-9602-3-git-send-email-kasal@ucw.cz>
In-Reply-To
<1402062173-9602-1-git-send-email-kasal@ucw.cz>
From: Karsten Blees <blees@dcon.de>

GetStdHandle(STD_OUTPUT_HANDLE) doesn't work for stderr if stdout is redirected. Use _get_osfhandle of the FILE* instead.

_isatty() is true for all character devices (including parallel and serial ports). Check return value of GetConsoleScreenBufferInfo instead to reliably detect console handles (also don't initialize internal state from an uninitialized CONSOLE_SCREEN_BUFFER_INFO structure if the function fails).

Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
---
 compat/winansi.c | 50 ++++++++++++++++++++++++++------------------------
 1 file changed, 26 insertions(+), 24 deletions(-)
diff --git a/compat/winansi.c b/compat/winansi.c
index abe0fea..c4be401 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -25,27 +25,39 @@ static HANDLE console;
 static WORD plain_attr;
 static WORD attr;
 static int negative;
+static FILE *last_stream = NULL;
 
-static void init(void)
+static int is_console(FILE *stream)
 {
 	CONSOLE_SCREEN_BUFFER_INFO sbi;
+	HANDLE hcon;
 
 	static int initialized = 0;
-	if (initialized)
-		return;
 
-	console = GetStdHandle(STD_OUTPUT_HANDLE);
-	if (console == INVALID_HANDLE_VALUE)
-		console = NULL;
+	/* use cached value if stream hasn't changed */
+	if (stream == last_stream)
+		return console != NULL;
 
-	if (!console)
-		return;
+	last_stream = stream;
+	console = NULL;
 
-	GetConsoleScreenBufferInfo(console, &sbi);
-	attr = plain_attr = sbi.wAttributes;
-	negative = 0;
+	/* get OS handle of the stream */
+	hcon = (HANDLE) _get_osfhandle(_fileno(stream));
+	if (hcon == INVALID_HANDLE_VALUE)
+		return 0;
+
+	/* check if its a handle to a console output screen buffer */
+	if (!GetConsoleScreenBufferInfo(hcon, &sbi))
+		return 0;
+
+	if (!initialized) {
+		attr = plain_attr = sbi.wAttributes;
+		negative = 0;
+		initialized = 1;
+	}
 
-	initialized = 1;
+	console = hcon;
+	return 1;
 }
 
 static int write_console(const char *str, size_t len)
@@ -292,12 +304,7 @@ int winansi_fputs(const char *str, FILE *stream)
 {
 	int rv;
 
-	if (!isatty(fileno(stream)))
-		return fputs(str, stream);
-
-	init();
-
-	if (!console)
+	if (!is_console(stream))
 		return fputs(str, stream);
 
 	rv = ansi_emulate(str, stream);
@@ -315,12 +322,7 @@ int winansi_vfprintf(FILE *stream, const char *format, va_list list)
 	char *buf = small_buf;
 	va_list cp;
 
-	if (!isatty(fileno(stream)))
-		goto abort;
-
-	init();
-
-	if (!console)
+	if (!is_console(stream))
 		goto abort;
 
 	va_copy(cp, list);
-- 
2.0.0.9635.g0be03cb

-- 
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to msysgit@googlegroups.com
To unsubscribe from this group, send email to
msysgit+unsubscribe@googlegroups.com
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to msysgit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Previous: Stepan KasalNext: Stepan Kasal
Message 3 of 22 in “First part of Unicode console support for msysgit”
  1. 0/5 First part of Unicode console support for msysgitStepan Kasal, Jun 6, 2014
  2. 1/5 Support Unicode console output on WindowsStepan Kasal, Jun 6, 2014
  3. 2/5 Detect console streams more reliably on WindowsStepan Kasal, Jun 6, 2014
  4. 3/5 Warn if the Windows console font doesn't support UnicodeStepan Kasal, Jun 6, 2014
  5. Peter KreftingJun 6, 2014
  6. Stepan KasalJun 7, 2014
  7. 4/5 Win32: move main macro to a functionStepan Kasal, Jun 6, 2014
  8. 5/5 Win32: Thread-safe windows console outputStepan Kasal, Jun 6, 2014
  9. Peter KreftingJun 6, 2014
  10. Karsten BleesJun 6, 2014
  11. Karsten BleesJun 6, 2014
  12. Stepan KasalJun 6, 2014
  13. 0/6 First part of Unicode console support for msysgitStepan Kasal, Jun 7, 2014
  14. 1/6 Support Unicode console output on WindowsStepan Kasal, Jun 7, 2014
  15. 2/6 Detect console streams more reliably on WindowsStepan Kasal, Jun 7, 2014
  16. 3/6 Warn if the Windows console font doesn't support UnicodeStepan Kasal, Jun 7, 2014
  17. 4/6 Win32: add Unicode conversion functionsStepan Kasal, Jun 7, 2014
  18. 5/6 Win32: Thread-safe windows console outputStepan Kasal, Jun 7, 2014
  19. Johannes SixtJun 13, 2014
  20. 7/6 Win32: reliably detect console pipe handlesKarsten Blees, Jun 13, 2014
  21. 6/6 Win32: fix broken pipe detectionStepan Kasal, Jun 7, 2014
  22. Stepan KasalJun 6, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.