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
---
v5:
- stricter check (follows cygwin tty.cc)
- drop \\?\, as the name shouldn't be long

 mingw-w64-headers/include/iscygtty.c | 104 +++++++++++++++++++++++++++++++++++
 1 file changed, 104 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..1fbd8a6cba3d
--- /dev/null
+++ b/mingw-w64-headers/include/iscygtty.c
@@ -0,0 +1,104 @@
+#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" */
+    {
+        wchar_t expect[] = L"\\Device\\NamedPipe\\";
+
+        for (i = 0; i < l; i++) {
+            wchar_t e = expect[i];
+            c = s0[i];
+            if (!e)
+                break;
+            if (c != e) {
+                return 0;
+            }
+        }
+
+        l -= i;
+        s0 += i;
+    }
+
+    /* Look for "[a-z0-9]+-[a-z0-9]+-pty[0-9]+-from-master" */
+    {
+        int part = 0;
+
+        for (i = 0; i < l; i++) {
+            c = s0[i];
+
+            if (!c)
+                break;
+
+            if (c == L'-') {
+                part++;
+
+                /* handle -pty%d- */
+                if (part == 2) {
+                    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 0;
+                        }
+                        part++;
+
+                        {
+                            wchar_t expect[] = L"-from-master";
+                            int j;
+
+                            j = 0;
+                            while (1) {
+                                wchar_t e = expect[j];
+                                c = s[j];
+                                if (!e) {
+                                    return 1;
+                                }
+                                if (c != e) {
+                                    return 0;
+                                }
+                                j++;
+                            }
+                        }
+                    } else {
+                        return 0;
+                    }
+                }
+            } else if (!(c >= L'a' && c <= L'z') && !(c >= L'0' && c <= L'9')) 
{
+                return 0;
+            }
+        }
+    }
+
+    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