Re: [Mingw-w64-public] [PATH] math.h: The type of C99 INFINITY and NAN shall be float.

2016-11-13 Thread David Grayson
Patch is not attached.

--David

On Sat, Nov 12, 2016 at 7:33 PM, Nakai Yuta  wrote:

> C99 defines INFINITY and NAN macros as float.
> patch is attached.
>
>
> 
> --
> 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
>
>
--
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


Re: [Mingw-w64-public] [PATH] math.h: The type of C99 INFINITY and NAN shall be float.

2016-11-13 Thread Nakai Yuta
hmm,  it seems that sf doesnt't accept it.




差出人: David Grayson 
送信日時: 2016年11月13日 08:30
宛先: mingw-w64-public@lists.sourceforge.net
件名: Re: [Mingw-w64-public] [PATH] math.h: The type of C99 INFINITY and NAN 
shall be float.

Patch is not attached.

--David

On Sat, Nov 12, 2016 at 7:33 PM, Nakai Yuta  wrote:

> C99 defines INFINITY and NAN macros as float.
> patch is attached.
>
>
> 
> --
> 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
>
>
--
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
From 9ddfb4049e27601a690ddd1e851d1b1729f9bd86 Mon Sep 17 00:00:00 2001
From: Yuta Nakai 
Date: Sun, 13 Nov 2016 12:16:38 +0900
Subject: [PATCH] math.h: The type of C99 INFINITY and NAN shall be float.

---
 mingw-w64-headers/crt/math.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-headers/crt/math.h b/mingw-w64-headers/crt/math.h
index b06ca79..3399b93 100644
--- a/mingw-w64-headers/crt/math.h
+++ b/mingw-w64-headers/crt/math.h
@@ -353,8 +353,8 @@ _CRTIMP double __cdecl scalb (double, long);
 #ifdef __GNUC__
 #define HUGE_VALF  __builtin_huge_valf()
 #define HUGE_VALL  __builtin_huge_vall()
-#define INFINITY   __builtin_inf()
-#define NAN__builtin_nan("")
+#define INFINITY   __builtin_inff()
+#define NAN__builtin_nanf("")
 #else
 extern const float __INFF;
 #define HUGE_VALF __INFF
-- 
2.10.1

--
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


Re: [Mingw-w64-public] [PATCH v5] Add include/iscygtty.c

2016-11-13 Thread Corinna Vinschen
On Nov 12 23:10, Ray Donnelly wrote:
> MSYS2 is a software distribution with both (extremely) Cygwin-like and also
> native Windows software, all (usually) launched from mintty.

MSYS2 is a slightly tweaked Cygwin DLL in the first place.

There are applications linked against it, thus using the POSIX
emulation.  Those don't need this function at all.

And there are applications not linked against it, what we refer to as
"native" applications.  Those would profit from this function.

But that's not the major point.  The major point is:

  2. Why include a non-standard API?  Why not provide this as isatty
 function as a replacement for the system isatty?  I'd wager your
 boots on this function going mostly unused, otherwise.

This would only (correctly) affect native applications, but then *all*
of them would see mintty as a tty.


Corinna

--
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


Re: [Mingw-w64-public] [PATCH v5] Add include/iscygtty.c

2016-11-13 Thread Corinna Vinschen
On Nov 13 11:01, Corinna Vinschen wrote:
> On Nov 12 23:10, Ray Donnelly wrote:
> > MSYS2 is a software distribution with both (extremely) Cygwin-like and also
> > native Windows software, all (usually) launched from mintty.
> 
> MSYS2 is a slightly tweaked Cygwin DLL in the first place.
> 
> There are applications linked against it, thus using the POSIX
> emulation.  Those don't need this function at all.
> 
> And there are applications not linked against it, what we refer to as
> "native" applications.  Those would profit from this function.
> 
> But that's not the major point.  The major point is:
> 
>   2. Why include a non-standard API?  Why not provide this as isatty
>  function as a replacement for the system isatty?  I'd wager your
>  boots on this function going mostly unused, otherwise.
> 
> This would only (correctly) affect native applications, but then *all*
> of them would see mintty as a tty.

s/all of them/all of them using the isatty function/


Corinna


signature.asc
Description: PGP signature
--
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


[Mingw-w64-public] [PATCH] Make isatty Cygwin-compatible

2016-11-13 Thread Mihail Konev
Before, isatty() was an alias for WinAPI _isatty().
This resulted in wrong result for mintty.

Implement a pipe name check in a static isatty().
This makes io.h include wchar.h, errno.h, io.h, winternl.h, and windows.h.

Signed-off-by: Mihail Konev 
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
Adapted-from: https://cygwin.com/ml/cygwin-developers/2016-11/msg2.html
---
 mingw-w64-headers/crt/io.h | 94 +-
 1 file changed, 93 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-headers/crt/io.h b/mingw-w64-headers/crt/io.h
index c61e94ab8743..5e64352df6b4 100644
--- a/mingw-w64-headers/crt/io.h
+++ b/mingw-w64-headers/crt/io.h
@@ -9,6 +9,13 @@
 #include 
 #include 
 
+/* for cygwin-compatible isatty */
+#include 
+#include 
+#include 
+#include 
+#include 
+
 #pragma pack(push,_CRT_PACKING)
 
 #ifdef __cplusplus
@@ -322,7 +329,6 @@ _CRTIMP char* __cdecl _getcwd (char*, int);
   int __cdecl dup2(int _FileHandleSrc,int _FileHandleDst) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   int __cdecl eof(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
   long __cdecl filelength(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
-  int __cdecl isatty(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
   int __cdecl locking(int _FileHandle,int _LockMode,long _NumOfBytes) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   long __cdecl lseek(int _FileHandle,long _Offset,int _Origin) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   char *__cdecl mktemp(char *_TemplateName)  
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
@@ -335,6 +341,92 @@ _CRTIMP char* __cdecl _getcwd (char*, int);
   int __cdecl write(int _Filehandle,const void *_Buf,unsigned int 
_MaxCharCount) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
 #endif
 
+static int __cdecl isatty(int fd) {
+  typedef NTSTATUS (NTAPI proc_NtQueryObject) (HANDLE, 
OBJECT_INFORMATION_CLASS, PVOID, ULONG, PULONG);
+  proc_NtQueryObject *pNtQueryObject;
+
+  HANDLE h_fd;
+
+  /* NtQueryObject needs space for OBJECT_NAME_INFORMATION.Name->Buffer also. 
*/
+  char ntfn_bytes[sizeof(OBJECT_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR)];
+
+  OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION*) ntfn_bytes;
+  NTSTATUS status;
+  ULONG ntfn_size = sizeof(ntfn_bytes);
+
+  wchar_t c, *s;
+  USHORT i;
+
+  h_fd = (HANDLE) _get_osfhandle(fd);
+  if (!h_fd || h_fd == INVALID_HANDLE_VALUE) {
+errno = EBADF;
+return 0;
+  }
+
+  pNtQueryObject = (proc_NtQueryObject*) 
GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryObject");
+  if (!pNtQueryObject)
+goto no_tty;
+
+  memset(ntfn, 0, ntfn_size);
+  status = pNtQueryObject((HANDLE)h_fd, ObjectNameInformation,
+  ntfn, ntfn_size, &ntfn_size);
+
+  if (!NT_SUCCESS(status)) {
+/* If it is not NUL (i.e. \Device\Null, which would succeed),
+ * then normal isatty() could be consulted.
+ * */
+if (_isatty(fd))
+  return 1;
+goto no_tty;
+  }
+
+  s = ntfn->Name.Buffer;
+  s[ntfn->Name.Length / sizeof(WCHAR)] = 0;
+
+  /* Look for 
\Device\NamedPipe\(cygwin|msys)-[a-fA-F0-9]{16}-pty[0-9]{1,4}-(from-master|to-master|to-master-cyg)
 */
+
+  if (wcsncmp(s, L"\\Device\\NamedPipe\\", 18))
+goto no_tty;
+  s += 18;
+
+  if (!wcsncmp(s, L"cygwin-", 7))
+s += 7;
+  else if (!wcsncmp(s, L"msys-", 5))
+s += 5;
+  else
+goto no_tty;
+
+  for (i = 0; i < 16; i++) {
+c = *s++;
+if (!( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c 
<= '9') ))
+  goto no_tty;
+  }
+
+  if (wcsncmp(s, L"-pty", 4))
+goto no_tty;
+  s += 4;
+
+  for (i = 0; i < 4; i++, s++) {
+c = *s;
+if (!( c >= '0' && c <= '9' ))
+  break;
+  }
+
+  if (i == 0)
+goto no_tty;
+
+  if (wcscmp(s, L"-from-master") &&
+wcscmp(s, L"-to-master") &&
+wcscmp(s, L"-to-master-cyg"))
+goto no_tty;
+
+  return 1;
+
+no_tty:
+  errno = EINVAL;
+  return 0;
+}
+
 #ifndef _FILE_OFFSET_BITS_SET_LSEEK
 #define _FILE_OFFSET_BITS_SET_LSEEK
 #if (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64))
-- 
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


[Mingw-w64-public] [PATCH v2] Make isatty Cygwin-compatible

2016-11-13 Thread Mihail Konev
Before, isatty() was an alias for WinAPI _isatty().
This resulted in wrong result for mintty.

Implement a pipe name check in a static isatty().
This makes io.h include NT and Windows APIs.

The change isn't strictly standard, as it adds
'static' to the isatty() signature.

Signed-off-by: Mihail Konev 
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
Adapted-from: https://cygwin.com/ml/cygwin-developers/2016-11/msg2.html
---
v1: moved from v7
v2: edit description

 mingw-w64-headers/crt/io.h | 94 +-
 1 file changed, 93 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-headers/crt/io.h b/mingw-w64-headers/crt/io.h
index c61e94ab8743..5e64352df6b4 100644
--- a/mingw-w64-headers/crt/io.h
+++ b/mingw-w64-headers/crt/io.h
@@ -9,6 +9,13 @@
 #include 
 #include 
 
+/* for cygwin-compatible isatty */
+#include 
+#include 
+#include 
+#include 
+#include 
+
 #pragma pack(push,_CRT_PACKING)
 
 #ifdef __cplusplus
@@ -322,7 +329,6 @@ _CRTIMP char* __cdecl _getcwd (char*, int);
   int __cdecl dup2(int _FileHandleSrc,int _FileHandleDst) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   int __cdecl eof(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
   long __cdecl filelength(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
-  int __cdecl isatty(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
   int __cdecl locking(int _FileHandle,int _LockMode,long _NumOfBytes) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   long __cdecl lseek(int _FileHandle,long _Offset,int _Origin) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   char *__cdecl mktemp(char *_TemplateName)  
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
@@ -335,6 +341,92 @@ _CRTIMP char* __cdecl _getcwd (char*, int);
   int __cdecl write(int _Filehandle,const void *_Buf,unsigned int 
_MaxCharCount) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
 #endif
 
+static int __cdecl isatty(int fd) {
+  typedef NTSTATUS (NTAPI proc_NtQueryObject) (HANDLE, 
OBJECT_INFORMATION_CLASS, PVOID, ULONG, PULONG);
+  proc_NtQueryObject *pNtQueryObject;
+
+  HANDLE h_fd;
+
+  /* NtQueryObject needs space for OBJECT_NAME_INFORMATION.Name->Buffer also. 
*/
+  char ntfn_bytes[sizeof(OBJECT_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR)];
+
+  OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION*) ntfn_bytes;
+  NTSTATUS status;
+  ULONG ntfn_size = sizeof(ntfn_bytes);
+
+  wchar_t c, *s;
+  USHORT i;
+
+  h_fd = (HANDLE) _get_osfhandle(fd);
+  if (!h_fd || h_fd == INVALID_HANDLE_VALUE) {
+errno = EBADF;
+return 0;
+  }
+
+  pNtQueryObject = (proc_NtQueryObject*) 
GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryObject");
+  if (!pNtQueryObject)
+goto no_tty;
+
+  memset(ntfn, 0, ntfn_size);
+  status = pNtQueryObject((HANDLE)h_fd, ObjectNameInformation,
+  ntfn, ntfn_size, &ntfn_size);
+
+  if (!NT_SUCCESS(status)) {
+/* If it is not NUL (i.e. \Device\Null, which would succeed),
+ * then normal isatty() could be consulted.
+ * */
+if (_isatty(fd))
+  return 1;
+goto no_tty;
+  }
+
+  s = ntfn->Name.Buffer;
+  s[ntfn->Name.Length / sizeof(WCHAR)] = 0;
+
+  /* Look for 
\Device\NamedPipe\(cygwin|msys)-[a-fA-F0-9]{16}-pty[0-9]{1,4}-(from-master|to-master|to-master-cyg)
 */
+
+  if (wcsncmp(s, L"\\Device\\NamedPipe\\", 18))
+goto no_tty;
+  s += 18;
+
+  if (!wcsncmp(s, L"cygwin-", 7))
+s += 7;
+  else if (!wcsncmp(s, L"msys-", 5))
+s += 5;
+  else
+goto no_tty;
+
+  for (i = 0; i < 16; i++) {
+c = *s++;
+if (!( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c 
<= '9') ))
+  goto no_tty;
+  }
+
+  if (wcsncmp(s, L"-pty", 4))
+goto no_tty;
+  s += 4;
+
+  for (i = 0; i < 4; i++, s++) {
+c = *s;
+if (!( c >= '0' && c <= '9' ))
+  break;
+  }
+
+  if (i == 0)
+goto no_tty;
+
+  if (wcscmp(s, L"-from-master") &&
+wcscmp(s, L"-to-master") &&
+wcscmp(s, L"-to-master-cyg"))
+goto no_tty;
+
+  return 1;
+
+no_tty:
+  errno = EINVAL;
+  return 0;
+}
+
 #ifndef _FILE_OFFSET_BITS_SET_LSEEK
 #define _FILE_OFFSET_BITS_SET_LSEEK
 #if (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64))
-- 
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


Re: [Mingw-w64-public] [PATCH] Make isatty Cygwin-compatible

2016-11-13 Thread Adrien Nader
Hi,

Below I'm only looking at the code by itself, not at the overall
approach.

On Sun, Nov 13, 2016, Mihail Konev wrote:
> +  pNtQueryObject = (proc_NtQueryObject*) 
> GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryObject");
> +  if (!pNtQueryObject)
> +goto no_tty;

Curly braces prevent many mistakes.

> +  memset(ntfn, 0, ntfn_size);
> +  status = pNtQueryObject((HANDLE)h_fd, ObjectNameInformation,
> +  ntfn, ntfn_size, &ntfn_size);
> +
> +  if (!NT_SUCCESS(status)) {
> +/* If it is not NUL (i.e. \Device\Null, which would succeed),
> + * then normal isatty() could be consulted.
> + * */
> +if (_isatty(fd))
> +  return 1;

Curly braces.

> +goto no_tty;
> +  }
> +
> +  s = ntfn->Name.Buffer;
> +  s[ntfn->Name.Length / sizeof(WCHAR)] = 0;
> +
> +  /* Look for 
> \Device\NamedPipe\(cygwin|msys)-[a-fA-F0-9]{16}-pty[0-9]{1,4}-(from-master|to-master|to-master-cyg)
>  */

What's the logic? You're saying what you're doing but not the reason
for doing it.

> +
> +  if (wcsncmp(s, L"\\Device\\NamedPipe\\", 18))

18 looks like a magic number here; I guess it's related to the string
before but it's painful to count. Creating a variable and getting the
string length would be much more readable.

> +goto no_tty;

Curly braces.

> +  s += 18;
> +
> +  if (!wcsncmp(s, L"cygwin-", 7))
> +s += 7;

Curly braces.

> +  else if (!wcsncmp(s, L"msys-", 5))
> +s += 5;

Curly braces.

> +  else
> +goto no_tty;

Curly braces.

> +
> +  for (i = 0; i < 16; i++) {

Why 16?

> +c = *s++;
> +if (!( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && 
> c <= '9') ))
> +  goto no_tty;

Curly braces.

> +  }

You can probably do the same but probably more readable with wcsspn and
it'll be one less thing to worry with when it comes to encoding. Simply
ensure the value returned by wcsspn() is the string length.

By the way, what if s is smaller than 16? It is filled with null
characters?

> +
> +  if (wcsncmp(s, L"-pty", 4))

The reason I prefer explicits == 0 and != 0 for compare functions is
that I then don't have to double-check that you simply haven't forgotten
an exclamation mark at the beginning of your condition.

> +goto no_tty;

Curly braces.

> +  s += 4;
> +
> +  for (i = 0; i < 4; i++, s++) {
> +c = *s;
> +if (!( c >= '0' && c <= '9' ))
> +  break;
> +  }

Again, curly braces and handling of strings shorter than 4.

As far as I understand the goal here is to check that up to the first
four characters after "-pty" are numerical and move to the first
non-numerical characters after these. You can use wcscspn() to do the
lookup. You should also handle the cases where no numerical character is
found.

> +
> +  if (i == 0)
> +goto no_tty;

Curly braces.

> +
> +  if (wcscmp(s, L"-from-master") &&
> +wcscmp(s, L"-to-master") &&
> +wcscmp(s, L"-to-master-cyg"))
> +goto no_tty;

Curly braces.

> +
> +  return 1;
> +
> +no_tty:
> +  errno = EINVAL;
> +  return 0;
> +}
> +
>  #ifndef _FILE_OFFSET_BITS_SET_LSEEK
>  #define _FILE_OFFSET_BITS_SET_LSEEK
>  #if (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64))
> -- 
> 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


[Mingw-w64-public] [PATCH v3] Make isatty Cygwin-compatible

2016-11-13 Thread Mihail Konev
Before, isatty() was an alias for WinAPI _isatty().
This resulted in wrong result for mintty.

Implement a pipe name check in a static isatty().
This makes io.h include NT and Windows APIs.

The change isn't strictly standard, as it adds
'static' to the isatty() signature.

Signed-off-by: Mihail Konev 
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
Reference: https://cygwin.com/ml/cygwin-developers/2016-11/msg2.html
---
v3:
- use wide strlen + const strings
- brace one-line if branches
- describe the function

 mingw-w64-headers/crt/io.h | 127 -
 1 file changed, 126 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-headers/crt/io.h b/mingw-w64-headers/crt/io.h
index c61e94ab8743..0376a4ff596d 100644
--- a/mingw-w64-headers/crt/io.h
+++ b/mingw-w64-headers/crt/io.h
@@ -9,6 +9,13 @@
 #include 
 #include 
 
+/* for cygwin-compatible isatty */
+#include 
+#include 
+#include 
+#include 
+#include 
+
 #pragma pack(push,_CRT_PACKING)
 
 #ifdef __cplusplus
@@ -322,7 +329,6 @@ _CRTIMP char* __cdecl _getcwd (char*, int);
   int __cdecl dup2(int _FileHandleSrc,int _FileHandleDst) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   int __cdecl eof(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
   long __cdecl filelength(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
-  int __cdecl isatty(int _FileHandle) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
   int __cdecl locking(int _FileHandle,int _LockMode,long _NumOfBytes) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   long __cdecl lseek(int _FileHandle,long _Offset,int _Origin) 
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
   char *__cdecl mktemp(char *_TemplateName)  
__MINGW_ATTRIB_DEPRECATED_MSVC2005;
@@ -335,6 +341,125 @@ _CRTIMP char* __cdecl _getcwd (char*, int);
   int __cdecl write(int _Filehandle,const void *_Buf,unsigned int 
_MaxCharCount) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
 #endif
 
+/* Cygwin-compatible isatty.
+ *
+ * Cygwin pty is a specially-named named pipe.
+ * Fetch absolute device namespace path to fd (if any),
+ * and check it for the following pattern:
+ *
+ *   
\Device\NamedPipe\(cygwin|msys)-[a-fA-F0-9]{16}-pty[0-9]{1,4}-(from-master|to-master|to-master-cyg)
+ *
+ * [a-fA-F0-9] is the cygwin installation key, 16 characters long.
+ * pty[0-9] is the pty name. Its index is of type int, but is safe to be 
limited to 4 characters.
+ *
+ * */
+
+static int __cdecl isatty(int fd) {
+  wchar_t const expect_dev[] = L"\\Device\\NamedPipe\\";
+  wchar_t const expect_cyg[] = L"cygwin-";
+  wchar_t const expect_msy[] = L"msys-";
+  wchar_t const expect_pty[] = L"-pty";
+  wchar_t const expect_tmr[] = L"-to-master";
+  wchar_t const expect_fmc[] = L"-to-master-cyg";
+  wchar_t const expect_fmr[] = L"-from-master";
+
+  typedef NTSTATUS (NTAPI proc_NtQueryObject) (HANDLE, 
OBJECT_INFORMATION_CLASS, PVOID, ULONG, PULONG);
+  proc_NtQueryObject *pNtQueryObject;
+
+  HANDLE h_fd;
+
+  /* NtQueryObject needs space for OBJECT_NAME_INFORMATION.Name->Buffer also. 
*/
+  char ntfn_bytes[sizeof(OBJECT_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR)];
+
+  OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION*) ntfn_bytes;
+  NTSTATUS status;
+  ULONG ntfn_size = sizeof(ntfn_bytes);
+
+  wchar_t c, *s;
+  int l;
+
+  h_fd = (HANDLE) _get_osfhandle(fd);
+  if (!h_fd || h_fd == INVALID_HANDLE_VALUE) {
+errno = EBADF;
+return 0;
+  }
+
+  pNtQueryObject = (proc_NtQueryObject*) 
GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryObject");
+  if (!pNtQueryObject) {
+goto no_tty;
+  }
+
+  memset(ntfn, 0, ntfn_size);
+  status = pNtQueryObject((HANDLE)h_fd, ObjectNameInformation,
+  ntfn, ntfn_size, &ntfn_size);
+
+  if (!NT_SUCCESS(status)) {
+/* If it is not NUL (i.e. \Device\Null, which would succeed),
+ * then normal isatty() could be consulted.
+ * */
+if (_isatty(fd)) {
+  return 1;
+}
+goto no_tty;
+  }
+
+  s = ntfn->Name.Buffer;
+  s[ntfn->Name.Length / sizeof(WCHAR)] = 0;
+
+  l = wcslen(expect_dev);
+  if (wcsncmp(s, expect_dev, l) != 0) {
+goto no_tty;
+  }
+  s += l;
+
+  do {
+l = wcslen(expect_cyg);
+if (wcsncmp(s, expect_cyg, l) == 0) {
+  s += l;
+  break;
+}
+
+l = wcslen(expect_msy);
+if (wcsncmp(s, expect_msy, l) == 0) {
+  s += l;
+  break;
+}
+
+goto no_tty;
+  } while (0);
+
+  l = wcsspn(s, L"0123456789abcdefABCDEF");
+  if (l != 16) {
+goto no_tty;
+  }
+  s += l;
+
+  l = wcslen(expect_pty);
+  if (wcsncmp(s, expect_pty, l) != 0) {
+goto no_tty;
+  }
+  s += l;
+
+  l = wcsspn(s, L"0123456789");
+  if (l < 1 || l > 4) {
+goto no_tty;
+  }
+  s += l;
+
+  if (wcscmp(s, L"-from-master") != 0 &&
+wcscmp(s, L"-to-master") != 0 &&
+wcscmp(s, L"-to-master-cyg") != 0)
+  {
+goto no_tty;
+  }
+
+  return 1;
+
+no_tty:
+  errno = EINVAL;
+  return 0;
+}
+
 #ifndef _FILE_OFFSET_BITS_SET_LSEEK
 #define _FILE_OFFSET_BITS_SET_LSEEK
 #if (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64))
-- 
2.9.2

Re: [Mingw-w64-public] [PATCH v3] Make isatty Cygwin-compatible

2016-11-13 Thread Corinna Vinschen
On Nov 14 00:31, Mihail Konev wrote:
> +/* Cygwin-compatible isatty.
> + *
> + * Cygwin pty is a specially-named named pipe.
> + * Fetch absolute device namespace path to fd (if any),
> + * and check it for the following pattern:
> + *
> + *   
> \Device\NamedPipe\(cygwin|msys)-[a-fA-F0-9]{16}-pty[0-9]{1,4}-(from-master|to-master|to-master-cyg)
> + *
> + * [a-fA-F0-9] is the cygwin installation key, 16 characters long.
> + * pty[0-9] is the pty name. Its index is of type int, but is safe to be 
> limited to 4 characters.
> + *
> + * */
> +
> +static int __cdecl isatty(int fd) {
> +  wchar_t const expect_dev[] = L"\\Device\\NamedPipe\\";
> +  wchar_t const expect_cyg[] = L"cygwin-";
> +  wchar_t const expect_msy[] = L"msys-";

I'm opposed to checking the pipes for *-msys-* additionally to
*-cygwin-* for no good reason.  You're adding just another test to the
checking code and potentially introduce hard to track down backward
incompatibilities.  There's really no gain for MSYS2 to rename the
pipes, except breaking interoperability, so hopefully this will never
happen anyway.


Corinna


signature.asc
Description: PGP signature
--
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


Re: [Mingw-w64-public] [PATCH v3] Make isatty Cygwin-compatible

2016-11-13 Thread Mihail Konev
On Sun, Nov 13, 2016 at 09:44:49PM +0100, Corinna Vinschen wrote:
> On Nov 14 00:31, Mihail Konev wrote:
> 
> I'm opposed to checking the pipes for *-msys-* additionally to
> *-cygwin-* for no good reason.  You're adding just another test to the
> checking code and potentially introduce hard to track down backward
> incompatibilities.  There's really no gain for MSYS2 to rename the
> pipes, except breaking interoperability, so hopefully this will never
> happen anyway.
> 
Do you want MSYS to rename the pipes back?

https://github.com/Alexpux/MSYS2-packages/search?utf8=%E2%9C%93&q=PIPE_INTRO

Because of msys runtime 2.5.2-2, it is a necessity.

--
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