diff --git a/src/port/win32stat.c b/src/port/win32stat.c index 2ad8ee1..ce8505e 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) { @@ -297,6 +298,24 @@ _pgfstat64(int fileno, struct stat *buf) } /* + * Check if the fileno is a data stream. If so, unless it has been + * redirected to a file, getting information by handle will fail. Return + * the appropriate stat information instead. + */ + if ((fileno == _fileno(stdin) || + fileno == _fileno(stdout) || + fileno == _fileno(stderr)) && + !GetFinalPathNameByHandleA(hFile, path, MAX_PATH, VOLUME_NAME_NT)) + { + 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. */