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....@ya.ru>
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
---
 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..f7344b8b20c4
--- /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] == '\\' && s0[1] == '\\' && s0[2] == '?' && s0[3] == '\\') {
+            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 == '-') {
+            wchar_t *s = s0 + i + 1;
+            c = *s;
+            if ((c == 'p' || c == 't') && s[1] == 't' && s[2] == 'y' &&
+                    (c = s[3]) && (c >= '0') && (c <= '9'))
+            {
+                s += 4;
+                while ((c = *s) && (c >= '0') && (c <= '9'))
+                    s++;
+                if (c == '-' || c == 0)
+                    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

Reply via email to