[PATCH 0/2] Return appropriate handle by _get_osfhandle() and GetStdHandle().

Ken Brown kbrown@cornell.edu
Mon Mar 22 17:02:46 GMT 2021


[Still CC Marco]

On 3/22/2021 7:43 AM, Corinna Vinschen via Cygwin-patches wrote:
> [CC Marco]
> 
> On Mar 22 08:07, Takashi Yano via Cygwin-patches wrote:
>> On Sun, 21 Mar 2021 17:44:27 +0900
>> Takashi Yano wrote:
>>> On Sun, 21 Mar 2021 13:01:24 +0900
>>> Takashi Yano wrote:
>>>> Takashi Yano (2):
>>>>    Cygwin: syscalls.cc: Make _get_osfhandle() return appropriate handle.
>>>>    Cygwin: pty: Add hook for GetStdHandle() to return appropriate handle.
>>>>
>>>>   winsup/cygwin/fhandler_tty.cc | 19 +++++++++++++++++++
>>>>   winsup/cygwin/syscalls.cc     | 13 ++++++++++++-
>>>>   2 files changed, 31 insertions(+), 1 deletion(-)
>>>
>>> I submitted these patches, however, I still wonder if we really
>>> need these patches. I cannot imagine the situation where handle
>>> itself is needed rather than file descriptor.
>>>
>>> However, following cygwin apps/dlls call _get_osfhandle():
>>> ccmake.exe
>>> cmake.exe
>>> cpack.exe
>>> ctest.exe
>>> ddrescue.exe
>>>
>>> And also, following cygwin apps/dlls call GetStdHandle():
>>> ccmake.exe
>>> cmake.exe
>>> cpack.exe
>>> ctest.exe
>>> run.exe
>>> cygusb0.dll
>>> tk86.dll
>>>
>>> in my installation.
>>>
>>> Therefore, some of these apps/dlls may need these patches...
>>
>> I looked into cmake source and found the patch exactly for
>> this issue. Therefore, it seems better to fix this.
>>
>> /* Get the Windows handle for a FILE stream.  */
>> static HANDLE kwsysTerminalGetStreamHandle(FILE* stream)
>> {
>>    /* Get the C-library file descriptor from the stream.  */
>>    int fd = fileno(stream);
>>
>> #  if defined(__CYGWIN__)
>>    /* Cygwin seems to have an extra pipe level.  If the file descriptor
>>       corresponds to stdout or stderr then obtain the matching windows
>>       handle directly.  */
>>    if (fd == fileno(stdout)) {
>>      return GetStdHandle(STD_OUTPUT_HANDLE);
>>    } else if (fd == fileno(stderr)) {
>>      return GetStdHandle(STD_ERROR_HANDLE);
>>    }
>> #  endif
>>
>>    /* Get the underlying Windows handle for the descriptor.  */
>>    return (HANDLE)_get_osfhandle(fd);
>> }
> 
> Why on earth is cmake using Windows functions on Cygwin at all???
> It's not as if it actually requires Windows functionality on our
> platform.

Out of curiosity, I took a quick glance at the cmake code.  It appears that this 
code is designed to support running cmake in a Console.  I don't think that 
should be needed any more, if it ever was.

> Marco, any input?  Any chance to drop this Windows stuff from the Cygwin
> code path in cmake?

I think the following might suffice (untested):

--- a/Source/kwsys/Terminal.c
+++ b/Source/kwsys/Terminal.c
@@ -10,7 +10,7 @@
  #endif

  /* Configure support for this platform.  */
-#if defined(_WIN32) || defined(__CYGWIN__)
+#if defined(_WIN32)
  #  define KWSYS_TERMINAL_SUPPORT_CONSOLE
  #endif
  #if !defined(_WIN32)

Ken


More information about the Cygwin-patches mailing list