Index: src/OFStdIOStream.m ================================================================== --- src/OFStdIOStream.m +++ src/OFStdIOStream.m @@ -117,14 +117,25 @@ @implementation OFStdIOStream #ifndef OF_WINDOWS + (void)load { + if (self != [OFStdIOStream class]) + return; + # ifndef OF_AMIGAOS - of_stdin = [[OFStdIOStream alloc] of_initWithFileDescriptor: 0]; - of_stdout = [[OFStdIOStream alloc] of_initWithFileDescriptor: 1]; - of_stderr = [[OFStdIOStream alloc] of_initWithFileDescriptor: 2]; + int fd; + + if ((fd = fileno(stdin)) >= 0) + of_stdin = [[OFStdIOStream alloc] + of_initWithFileDescriptor: fd]; + if ((fd = fileno(stdout)) >= 0) + of_stdout = [[OFStdIOStream alloc] + of_initWithFileDescriptor: fd]; + if ((fd = fileno(stderr)) >= 0) + of_stderr = [[OFStdIOStream alloc] + of_initWithFileDescriptor: fd]; # else BPTR input, output, error; bool inputClosable = false, outputClosable = false, errorClosable = false; Index: src/OFStdIOStream_Win32Console.m ================================================================== --- src/OFStdIOStream_Win32Console.m +++ src/OFStdIOStream_Win32Console.m @@ -43,10 +43,11 @@ #include "config.h" #include #include +#include #import "OFStdIOStream_Win32Console.h" #import "OFStdIOStream+Private.h" #import "OFString.h" #import "OFData.h" @@ -60,38 +61,36 @@ #include @implementation OFStdIOStream_Win32Console + (void)load { - of_stdin = [[OFStdIOStream_Win32Console alloc] - of_initWithFileDescriptor: 0]; - of_stdout = [[OFStdIOStream_Win32Console alloc] - of_initWithFileDescriptor: 1]; - of_stderr = [[OFStdIOStream_Win32Console alloc] - of_initWithFileDescriptor: 2]; + int fd; + + if (self != [OFStdIOStream_Win32Console class]) + return; + + if ((fd = _fileno(stdin)) >= 0) + of_stdin = [[OFStdIOStream_Win32Console alloc] + of_initWithFileDescriptor: fd]; + if ((fd = _fileno(stdout)) >= 0) + of_stdout = [[OFStdIOStream_Win32Console alloc] + of_initWithFileDescriptor: fd]; + if ((fd = _fileno(stderr)) >= 0) + of_stderr = [[OFStdIOStream_Win32Console alloc] + of_initWithFileDescriptor: fd]; } - (instancetype)of_initWithFileDescriptor: (int)fd { self = [super of_initWithFileDescriptor: fd]; @try { DWORD mode; - switch (fd) { - case 0: - _handle = GetStdHandle(STD_INPUT_HANDLE); - break; - case 1: - _handle = GetStdHandle(STD_OUTPUT_HANDLE); - break; - case 2: - _handle = GetStdHandle(STD_ERROR_HANDLE); - break; - default: + _handle = (HANDLE)_get_osfhandle(fd); + if (_handle == INVALID_HANDLE_VALUE) @throw [OFInvalidArgumentException exception]; - } /* Not a console: Treat it as a regular OFStdIOStream */ if (!GetConsoleMode(_handle, &mode)) object_setClass(self, [OFStdIOStream class]); } @catch (id e) {