This source file includes following definitions.
- initialize
- IsConsoleHandle
- IsCygwinConsoleHandle
- _isatty_nothrow
- isatty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20 #include <unistd.h>
21
22
23
24 #include <errno.h>
25 #include <string.h>
26
27
28 #define WIN32_LEAN_AND_MEAN
29 #include <windows.h>
30
31 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
32 # include "msvc-inval.h"
33 #endif
34
35
36 #if GNULIB_MSVC_NOTHROW
37 # include "msvc-nothrow.h"
38 #else
39 # include <io.h>
40 #endif
41
42
43 #undef LoadLibrary
44 #define LoadLibrary LoadLibraryA
45 #undef QueryFullProcessImageName
46 #define QueryFullProcessImageName QueryFullProcessImageNameA
47
48 #if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
49
50
51 # define GetProcAddress \
52 (void *) GetProcAddress
53
54
55 typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
56 PULONG pClientProcessId);
57 static GetNamedPipeClientProcessIdFuncType GetNamedPipeClientProcessIdFunc = NULL;
58
59 typedef BOOL (WINAPI * QueryFullProcessImageNameFuncType) (HANDLE hProcess,
60 DWORD dwFlags,
61 LPSTR lpExeName,
62 PDWORD pdwSize);
63 static QueryFullProcessImageNameFuncType QueryFullProcessImageNameFunc = NULL;
64 static BOOL initialized = FALSE;
65
66 static void
67 initialize (void)
68 {
69 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
70 if (kernel32 != NULL)
71 {
72 GetNamedPipeClientProcessIdFunc =
73 (GetNamedPipeClientProcessIdFuncType) GetProcAddress (kernel32, "GetNamedPipeClientProcessId");
74 QueryFullProcessImageNameFunc =
75 (QueryFullProcessImageNameFuncType) GetProcAddress (kernel32, "QueryFullProcessImageNameA");
76 }
77 initialized = TRUE;
78 }
79
80 #else
81
82 # define GetNamedPipeClientProcessIdFunc GetNamedPipeClientProcessId
83 # define QueryFullProcessImageNameFunc QueryFullProcessImageName
84
85 #endif
86
87 static BOOL IsConsoleHandle (HANDLE h)
88 {
89 DWORD mode;
90
91
92 return GetConsoleMode (h, &mode) != 0;
93 }
94
95 static BOOL IsCygwinConsoleHandle (HANDLE h)
96 {
97
98
99 BOOL result = FALSE;
100 ULONG processId;
101
102 #if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
103 if (!initialized)
104 initialize ();
105 #endif
106
107
108
109
110 if (GetNamedPipeClientProcessIdFunc && QueryFullProcessImageNameFunc
111 && GetNamedPipeClientProcessIdFunc (h, &processId))
112 {
113
114
115 HANDLE processHandle =
116 OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
117 if (processHandle != NULL)
118 {
119 char buf[1024];
120 DWORD bufsize = sizeof (buf);
121
122
123
124
125
126
127
128
129
130
131 if (QueryFullProcessImageNameFunc (processHandle, 0, buf, &bufsize))
132 {
133 if (strlen (buf) >= 11
134 && strcmp (buf + strlen (buf) - 11, "\\mintty.exe") == 0)
135 result = TRUE;
136 }
137 CloseHandle (processHandle);
138 }
139 }
140 return result;
141 }
142
143 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
144 static int
145 _isatty_nothrow (int fd)
146 {
147 int result;
148
149 TRY_MSVC_INVAL
150 {
151 result = _isatty (fd);
152 }
153 CATCH_MSVC_INVAL
154 {
155 result = 0;
156 }
157 DONE_MSVC_INVAL;
158
159 return result;
160 }
161 #else
162 # define _isatty_nothrow _isatty
163 #endif
164
165
166
167 int
168 isatty (int fd)
169 {
170 HANDLE h = (HANDLE) _get_osfhandle (fd);
171 if (h == INVALID_HANDLE_VALUE)
172 {
173 errno = EBADF;
174 return 0;
175 }
176
177
178 if (_isatty_nothrow (fd))
179 {
180 if (IsConsoleHandle (h))
181 return 1;
182 }
183 if (IsCygwinConsoleHandle (h))
184 return 1;
185 errno = ENOTTY;
186 return 0;
187 }