annotate lib/isatty.c @ 40236:e3496deebf30

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.
author Bruno Haible <bruno@clisp.org>
date Thu, 14 Mar 2019 23:54:28 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* isatty() replacement.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18883
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <unistd.h>
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* This replacement is enabled on native Windows. */
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
24 #include <errno.h>
40236
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
25 #include <string.h>
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
26
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 /* Get declarations of the Win32 API functions. */
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #define WIN32_LEAN_AND_MEAN
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <windows.h>
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
18883
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
31 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
32 # include "msvc-inval.h"
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
33 #endif
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
34
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 /* Get _get_osfhandle(). */
18883
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
36 #if GNULIB_MSVC_NOTHROW
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
37 # include "msvc-nothrow.h"
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
38 #else
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
39 # include <io.h>
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
40 #endif
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
40236
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
42 /* Avoid warnings from gcc -Wcast-function-type. */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
43 #define GetProcAddress \
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
44 (void *) GetProcAddress
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
45
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
46 /* GetNamedPipeClientProcessId was introduced only in Windows Vista. */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
47 typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
48 PULONG pClientProcessId);
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
49 static GetNamedPipeClientProcessIdFuncType GetNamedPipeClientProcessIdFunc = NULL;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
50 /* QueryFullProcessImageName was introduced only in Windows Vista. */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
51 typedef BOOL (WINAPI * QueryFullProcessImageNameFuncType) (HANDLE hProcess,
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
52 DWORD dwFlags,
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
53 LPSTR lpExeName,
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
54 PDWORD pdwSize);
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
55 static QueryFullProcessImageNameFuncType QueryFullProcessImageNameFunc = NULL;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
56 static BOOL initialized = FALSE;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
57
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
58 static void
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
59 initialize (void)
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
60 {
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
61 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
62 if (kernel32 != NULL)
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
63 {
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
64 GetNamedPipeClientProcessIdFunc =
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
65 (GetNamedPipeClientProcessIdFuncType) GetProcAddress (kernel32, "GetNamedPipeClientProcessId");
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
66 QueryFullProcessImageNameFunc =
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
67 (QueryFullProcessImageNameFuncType) GetProcAddress (kernel32, "QueryFullProcessImageNameA");
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
68 }
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
69 initialized = TRUE;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
70 }
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
71
17699
36e26a0e7826 isatty: fix to work on windows 8
Michael Goffioul <michael.goffioul@gmail.com>
parents: 17576
diff changeset
72 static BOOL IsConsoleHandle (HANDLE h)
36e26a0e7826 isatty: fix to work on windows 8
Michael Goffioul <michael.goffioul@gmail.com>
parents: 17576
diff changeset
73 {
36e26a0e7826 isatty: fix to work on windows 8
Michael Goffioul <michael.goffioul@gmail.com>
parents: 17576
diff changeset
74 DWORD mode;
40236
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
75 /* GetConsoleMode
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
76 <https://docs.microsoft.com/en-us/windows/console/getconsolemode> */
17699
36e26a0e7826 isatty: fix to work on windows 8
Michael Goffioul <michael.goffioul@gmail.com>
parents: 17576
diff changeset
77 return GetConsoleMode (h, &mode) != 0;
36e26a0e7826 isatty: fix to work on windows 8
Michael Goffioul <michael.goffioul@gmail.com>
parents: 17576
diff changeset
78 }
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
40236
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
80 static BOOL IsCygwinConsoleHandle (HANDLE h)
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
81 {
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
82 /* A handle to a Cygwin console is in fact a named pipe whose client process
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
83 and server process is <CYGWIN_INSTALL_DIR>\bin\mintty.exe. */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
84 BOOL result = FALSE;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
85 ULONG processId;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
86
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
87 if (!initialized)
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
88 initialize ();
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
89
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
90 /* GetNamedPipeClientProcessId
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
91 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getnamedpipeclientprocessid>
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
92 It requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
93 if (GetNamedPipeClientProcessIdFunc && QueryFullProcessImageNameFunc
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
94 && GetNamedPipeClientProcessIdFunc (h, &processId))
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
95 {
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
96 /* OpenProcess
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
97 <https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openprocess> */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
98 HANDLE processHandle =
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
99 OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
100 if (processHandle != NULL)
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
101 {
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
102 char buf[1024];
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
103 DWORD bufsize = sizeof (buf);
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
104 /* The file name can be determined through
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
105 GetProcessImageFileName
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
106 <https://docs.microsoft.com/en-us/windows/desktop/api/psapi/nf-psapi-getprocessimagefilenamea>
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
107 or
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
108 QueryFullProcessImageName
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
109 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-queryfullprocessimagenamea>
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
110 The former returns a file name in non-standard notation (it starts
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
111 with '\Device\') and may require linking with psapi.dll.
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
112 The latter is better, but requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
113 or higher. */
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
114 if (QueryFullProcessImageNameFunc (processHandle, 0, buf, &bufsize))
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
115 {
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
116 if (strlen (buf) >= 11
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
117 && strcmp (buf + strlen (buf) - 11, "\\mintty.exe") == 0)
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
118 result = TRUE;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
119 }
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
120 CloseHandle (processHandle);
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
121 }
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
122 }
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
123 return result;
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
124 }
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
125
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
126 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16356
diff changeset
127 static int
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
128 _isatty_nothrow (int fd)
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
129 {
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
130 int result;
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
131
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
132 TRY_MSVC_INVAL
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
133 {
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
134 result = _isatty (fd);
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
135 }
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
136 CATCH_MSVC_INVAL
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
137 {
16356
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
138 result = 0;
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
139 }
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
140 DONE_MSVC_INVAL;
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
141
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
142 return result;
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
143 }
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
144 #else
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
145 # define _isatty_nothrow _isatty
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
146 #endif
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
147
16356
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
148 /* Determine whether FD refers to a console device. Return 1 if yes.
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
149 Return 0 and set errno if no. (ptsname_r relies on the errno value.) */
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 int
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 isatty (int fd)
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 {
16356
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
153 HANDLE h = (HANDLE) _get_osfhandle (fd);
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
154 if (h == INVALID_HANDLE_VALUE)
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
155 {
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
156 errno = EBADF;
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
157 return 0;
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
158 }
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
159 /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR.
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
160 But it does not set errno when it returns 0. */
16213
64bc9fe46ccd isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 16209
diff changeset
161 if (_isatty_nothrow (fd))
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 {
16356
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
163 if (IsConsoleHandle (h))
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
164 return 1;
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 }
40236
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
166 if (IsCygwinConsoleHandle (h))
e3496deebf30 isatty: Make it return true in Cygwin consoles on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
167 return 1;
16356
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
168 errno = ENOTTY;
2fa8e82affcc isatty: Fix test failure of ptsname_r on native Windows.
Bruno Haible <bruno@clisp.org>
parents: 16324
diff changeset
169 return 0;
16209
93b0d3ad505e New module 'isatty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 }