--- Begin Message ---
- From: Mihail Konev <k dot mvc at ya dot ru>
- To: Mingw-w64-public <mingw-w64-public at lists dot sourceforge dot net>
- Date: Sat, 12 Nov 2016 06:58:32 +0500
- Subject: [Mingw-w64-public] [PATCH v4] Add include/iscygtty.c
- Authentication-results: mxfront2h.mail.yandex.net; spf=pass (mxfront2h.mail.yandex.net: domain of lists.sourceforge.net designates 216.34.181.88 as permitted sender) smtp.mail=mingw-w64-public-bounces at lists dot sourceforge dot net
- References: <20161110004619.GA2708@localhost> <20161112013850.GB2193@localhost>
- Reply-to: mingw-w64-public at lists dot sourceforge dot net
Applications now could call iscygtty(STDIN_FILENO)
in order to detect whether they are running from
Cygwin/MSys terminal.
Without that, they have no choice but to think that
stdin is redirected from a named pipe.
Signed-off-by: Mihail Konev <k.mvc@ya.ru>
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
---
Sorry, the v3 "\\.\" check is incorrect.
A
\\.\pipe\*-pty%d-*
should be looked for, not the
\\?\Device\NamedPipe\*-pty%d-*
.
Reverted the change, as the old way, it still works.
mingw-w64-headers/include/iscygtty.c | 69 ++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+)
create mode 100644 mingw-w64-headers/include/iscygtty.c
diff --git a/mingw-w64-headers/include/iscygtty.c b/mingw-w64-headers/include/iscygtty.c
new file mode 100644
index 000000000000..51e8b0e7319b
--- /dev/null
+++ b/mingw-w64-headers/include/iscygtty.c
@@ -0,0 +1,69 @@
+#ifndef __ISCYGTTY_C__
+#define __ISCYGTTY_C__
+
+#include <io.h>
+#include <winternl.h>
+
+static int iscygtty(int fd) {
+ intptr_t h_fd = _get_osfhandle(fd);
+
+ char ntfn_bytes[sizeof(OBJECT_NAME_INFORMATION) + 256 * sizeof(WCHAR)];
+ OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION*) ntfn_bytes;
+ NTSTATUS status;
+ ULONG ntfn_size = sizeof(ntfn_bytes);
+
+ USHORT i, l;
+ wchar_t c, *s0;
+
+ memset(ntfn, 0, ntfn_size);
+ status = NtQueryObject((HANDLE)h_fd, ObjectNameInformation,
+ ntfn, ntfn_size, &ntfn_size);
+
+ if (!NT_SUCCESS(status))
+ return 0;
+
+ l = ntfn->Name.Length;
+ s0 = ntfn->Name.Buffer;
+
+ /* Check for "\Device\NamedPipe" */
+ {
+ USHORT l1 = l;
+ wchar_t *s1 = s0;
+ wchar_t expect[] = L"\\Device\\NamedPipe\\";
+
+ if (s0[0] == L'\\' && s0[1] == L'\\' && s0[2] == L'?' && s0[3] == L'\\') {
+ l1 -= 4;
+ s1 += 4;
+ }
+
+ for (i = 0; i < l1; i++) {
+ wchar_t e = expect[i];
+ c = s1[i];
+ if (!e)
+ break;
+ if (c != e)
+ return 0;
+ }
+ }
+
+ /* Look for "-pty%d-" */
+ for (i = 0; i < l; i++) {
+ c = s0[i];
+ if (c == L'-') {
+ wchar_t *s = s0 + i + 1;
+ if (s[0] == L'p' && s[1] == L't' && s[2] == L'y' &&
+ (c = s[3]) && (c >= L'0') && (c <= L'9'))
+ {
+ s += 4;
+ while ((c = *s) && (c >= L'0') && (c <= L'9'))
+ s++;
+ if (c == L'-')
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+#endif /* __ISCYGTTY_C__ */
--
2.9.2
------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
--- End Message ---