diff --git a/src/port/win32stat.c b/src/port/win32stat.c index 2ad8ee1359..3956fac060 100644 --- a/src/port/win32stat.c +++ b/src/port/win32stat.c @@ -289,6 +289,7 @@ int _pgfstat64(int fileno, struct stat *buf) { HANDLE hFile = (HANDLE) _get_osfhandle(fileno); + char path[MAX_PATH]; if (hFile == INVALID_HANDLE_VALUE || buf == NULL) { @@ -296,6 +297,25 @@ _pgfstat64(int fileno, struct stat *buf) return -1; } + /* + * Check if the fileno is a data stream. If so, unless it has been + * redirected to a file, getting information through its HANDLE will fail, + * so emulate its stat information in the most appropriate way and return + * it instead. + */ + if ((fileno == _fileno(stdin) || + fileno == _fileno(stdout) || + fileno == _fileno(stderr)) && + GetFinalPathNameByHandleA(hFile, path, MAX_PATH, VOLUME_NAME_NT) == 0) + { + memset(buf, 0, sizeof(*buf)); + buf->st_mode = _S_IFCHR; + buf->st_dev = fileno; + buf->st_rdev = fileno; + buf->st_nlink = 1; + return 0; + } + /* * Since we already have a file handle there is no need to check for * ERROR_DELETE_PENDING.