bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

isatty: make it return true in Cygwin consoles on native Windows


From: Bruno Haible
Subject: isatty: make it return true in Cygwin consoles on native Windows
Date: Thu, 14 Mar 2019 23:59:34 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-141-generic; KDE/5.18.0; x86_64; ; )

When a native Windows program call isatty(STDOUT_FILENO) or
isatty(STDIN_FILENO), it returns true for a cmd.exe console
but false for a Cygwin console. This makes no sense, because the
Cygwin console is just as interactive as the cmd.exe console; only
the input/output encoding is different (UTF-8 vs. GetOEMCP()).

This patch fixes it.


2019-03-14  Bruno Haible  <address@hidden>

        isatty: Make it return true in Cygwin consoles on native Windows.
        * lib/isatty.c: Include <string.h>.
        (GetProcAddress): New macro.
        (GetNamedPipeClientProcessIdFuncType): New type.
        (GetNamedPipeClientProcessIdFunc): New variable.
        (QueryFullProcessImageNameFuncType): New type.
        (QueryFullProcessImageNameFunc): New variable.
        (initialized): New variable.
        (initialize): New function.
        (IsCygwinConsoleHandle): New function.
        (isatty): Invoke it.
        * doc/posix-functions/isatty.texi: Mention the issue.

diff --git a/doc/posix-functions/isatty.texi b/doc/posix-functions/isatty.texi
index 61c55da..b5e196b 100644
--- a/doc/posix-functions/isatty.texi
+++ b/doc/posix-functions/isatty.texi
@@ -12,6 +12,8 @@ Portability problems fixed by Gnulib:
 On native Windows, this function also returns true for character devices such
 as @file{NUL}.
 @item
+On native Windows, this function returns false for Cygwin consoles.
address@hidden
 This function crashes when invoked with invalid arguments on some platforms:
 MSVC 14.
 @end itemize
diff --git a/lib/isatty.c b/lib/isatty.c
index 71b32dd..085cd48 100644
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -22,6 +22,7 @@
 /* This replacement is enabled on native Windows.  */
 
 #include <errno.h>
+#include <string.h>
 
 /* Get declarations of the Win32 API functions.  */
 #define WIN32_LEAN_AND_MEAN
@@ -38,12 +39,90 @@
 # include <io.h>
 #endif
 
+/* Avoid warnings from gcc -Wcast-function-type.  */
+#define GetProcAddress \
+  (void *) GetProcAddress
+
+/* GetNamedPipeClientProcessId was introduced only in Windows Vista.  */
+typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
+                                                             PULONG 
pClientProcessId);
+static GetNamedPipeClientProcessIdFuncType GetNamedPipeClientProcessIdFunc = 
NULL;
+/* QueryFullProcessImageName was introduced only in Windows Vista.  */
+typedef BOOL (WINAPI * QueryFullProcessImageNameFuncType) (HANDLE hProcess,
+                                                           DWORD dwFlags,
+                                                           LPSTR lpExeName,
+                                                           PDWORD pdwSize);
+static QueryFullProcessImageNameFuncType QueryFullProcessImageNameFunc = NULL;
+static BOOL initialized = FALSE;
+
+static void
+initialize (void)
+{
+  HMODULE kernel32 = LoadLibrary ("kernel32.dll");
+  if (kernel32 != NULL)
+    {
+      GetNamedPipeClientProcessIdFunc =
+        (GetNamedPipeClientProcessIdFuncType) GetProcAddress (kernel32, 
"GetNamedPipeClientProcessId");
+      QueryFullProcessImageNameFunc =
+        (QueryFullProcessImageNameFuncType) GetProcAddress (kernel32, 
"QueryFullProcessImageNameA");
+    }
+  initialized = TRUE;
+}
+
 static BOOL IsConsoleHandle (HANDLE h)
 {
   DWORD mode;
+  /* GetConsoleMode
+     <https://docs.microsoft.com/en-us/windows/console/getconsolemode> */
   return GetConsoleMode (h, &mode) != 0;
 }
 
+static BOOL IsCygwinConsoleHandle (HANDLE h)
+{
+  /* A handle to a Cygwin console is in fact a named pipe whose client process
+     and server process is <CYGWIN_INSTALL_DIR>\bin\mintty.exe.  */
+  BOOL result = FALSE;
+  ULONG processId;
+
+  if (!initialized)
+    initialize ();
+
+  /* GetNamedPipeClientProcessId
+     
<https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getnamedpipeclientprocessid>
+     It requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher.  */
+  if (GetNamedPipeClientProcessIdFunc && QueryFullProcessImageNameFunc
+      && GetNamedPipeClientProcessIdFunc (h, &processId))
+    {
+      /* OpenProcess
+         
<https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openprocess>
 */
+      HANDLE processHandle =
+        OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
+      if (processHandle != NULL)
+        {
+          char buf[1024];
+          DWORD bufsize = sizeof (buf);
+          /* The file name can be determined through
+             GetProcessImageFileName
+             
<https://docs.microsoft.com/en-us/windows/desktop/api/psapi/nf-psapi-getprocessimagefilenamea>
+             or
+             QueryFullProcessImageName
+             
<https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-queryfullprocessimagenamea>
+             The former returns a file name in non-standard notation (it starts
+             with '\Device\') and may require linking with psapi.dll.
+             The latter is better, but requires 
-D_WIN32_WINNT=_WIN32_WINNT_VISTA
+             or higher.  */
+          if (QueryFullProcessImageNameFunc (processHandle, 0, buf, &bufsize))
+            {
+              if (strlen (buf) >= 11
+                  && strcmp (buf + strlen (buf) - 11, "\\mintty.exe") == 0)
+                result = TRUE;
+            }
+          CloseHandle (processHandle);
+        }
+    }
+  return result;
+}
+
 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
 static int
 _isatty_nothrow (int fd)
@@ -84,6 +163,8 @@ isatty (int fd)
       if (IsConsoleHandle (h))
         return 1;
     }
+  if (IsCygwinConsoleHandle (h))
+    return 1;
   errno = ENOTTY;
   return 0;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]