1 /* isatty() replacement.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
22 /* This replacement is enabled on native Windows. */
27 /* Get declarations of the Win32 API functions. */
28 #define WIN32_LEAN_AND_MEAN
31 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
32 # include "msvc-inval.h"
35 /* Get _get_osfhandle(). */
36 #if GNULIB_MSVC_NOTHROW
37 # include "msvc-nothrow.h"
42 /* Don't assume that UNICODE is not defined. */
44 #define LoadLibrary LoadLibraryA
45 #undef QueryFullProcessImageName
46 #define QueryFullProcessImageName QueryFullProcessImageNameA
48 #if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
50 /* Avoid warnings from gcc -Wcast-function-type. */
51 # define GetProcAddress \
52 (void *) GetProcAddress
54 /* GetNamedPipeClientProcessId was introduced only in Windows Vista. */
55 typedef BOOL (WINAPI
* GetNamedPipeClientProcessIdFuncType
) (HANDLE hPipe
,
56 PULONG pClientProcessId
);
57 static GetNamedPipeClientProcessIdFuncType GetNamedPipeClientProcessIdFunc
= NULL
;
58 /* QueryFullProcessImageName was introduced only in Windows Vista. */
59 typedef BOOL (WINAPI
* QueryFullProcessImageNameFuncType
) (HANDLE hProcess
,
63 static QueryFullProcessImageNameFuncType QueryFullProcessImageNameFunc
= NULL
;
64 static BOOL initialized
= FALSE
;
69 HMODULE kernel32
= LoadLibrary ("kernel32.dll");
72 GetNamedPipeClientProcessIdFunc
=
73 (GetNamedPipeClientProcessIdFuncType
) GetProcAddress (kernel32
, "GetNamedPipeClientProcessId");
74 QueryFullProcessImageNameFunc
=
75 (QueryFullProcessImageNameFuncType
) GetProcAddress (kernel32
, "QueryFullProcessImageNameA");
82 # define GetNamedPipeClientProcessIdFunc GetNamedPipeClientProcessId
83 # define QueryFullProcessImageNameFunc QueryFullProcessImageName
87 static BOOL
IsConsoleHandle (HANDLE h
)
91 <https://docs.microsoft.com/en-us/windows/console/getconsolemode> */
92 return GetConsoleMode (h
, &mode
) != 0;
95 static BOOL
IsCygwinConsoleHandle (HANDLE h
)
97 /* A handle to a Cygwin console is in fact a named pipe whose client process
98 and server process is <CYGWIN_INSTALL_DIR>\bin\mintty.exe. */
102 #if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
107 /* GetNamedPipeClientProcessId
108 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getnamedpipeclientprocessid>
109 It requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
110 if (GetNamedPipeClientProcessIdFunc
&& QueryFullProcessImageNameFunc
111 && GetNamedPipeClientProcessIdFunc (h
, &processId
))
114 <https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openprocess> */
115 HANDLE processHandle
=
116 OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION
, FALSE
, processId
);
117 if (processHandle
!= NULL
)
120 DWORD bufsize
= sizeof (buf
);
121 /* The file name can be determined through
122 GetProcessImageFileName
123 <https://docs.microsoft.com/en-us/windows/desktop/api/psapi/nf-psapi-getprocessimagefilenamea>
125 QueryFullProcessImageName
126 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-queryfullprocessimagenamea>
127 The former returns a file name in non-standard notation (it starts
128 with '\Device\') and may require linking with psapi.dll.
129 The latter is better, but requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA
131 if (QueryFullProcessImageNameFunc (processHandle
, 0, buf
, &bufsize
))
133 if (strlen (buf
) >= 11
134 && strcmp (buf
+ strlen (buf
) - 11, "\\mintty.exe") == 0)
137 CloseHandle (processHandle
);
143 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
145 _isatty_nothrow (int fd
)
151 result
= _isatty (fd
);
162 # define _isatty_nothrow _isatty
165 /* Determine whether FD refers to a console device. Return 1 if yes.
166 Return 0 and set errno if no. (ptsname_r relies on the errno value.) */
170 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
171 if (h
== INVALID_HANDLE_VALUE
)
176 /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR.
177 But it does not set errno when it returns 0. */
178 if (_isatty_nothrow (fd
))
180 if (IsConsoleHandle (h
))
183 if (IsCygwinConsoleHandle (h
))