Skip to content

Commit d4bbc85

Browse files
kbleesdscho
authored andcommitted
Win32: replace MSVCRT's fstat() with a Win32-based implementation
fstat() is the only stat-related CRT function for which we don't have a full replacement yet (and thus the only reason to stick with MSVCRT's 'struct stat' definition). Fully implement fstat(), in preparation of implementing a POSIX 2013 compatible 'struct stat' with nanosecond-precision file times. Signed-off-by: Karsten Blees <[email protected]>
1 parent 49dd5a9 commit d4bbc85

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

compat/mingw.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -775,18 +775,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
775775
int mingw_fstat(int fd, struct stat *buf)
776776
{
777777
HANDLE fh = (HANDLE)_get_osfhandle(fd);
778-
if (fh == INVALID_HANDLE_VALUE) {
778+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
779+
780+
switch (type) {
781+
case FILE_TYPE_DISK:
782+
return get_file_info_by_handle(fh, buf);
783+
784+
case FILE_TYPE_CHAR:
785+
case FILE_TYPE_PIPE:
786+
/* initialize stat fields */
787+
memset(buf, 0, sizeof(*buf));
788+
buf->st_nlink = 1;
789+
790+
if (type == FILE_TYPE_CHAR) {
791+
buf->st_mode = _S_IFCHR;
792+
} else {
793+
buf->st_mode = _S_IFIFO;
794+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
795+
buf->st_size = avail;
796+
}
797+
return 0;
798+
799+
default:
779800
errno = EBADF;
780801
return -1;
781802
}
782-
/* direct non-file handles to MS's fstat() */
783-
if (GetFileType(fh) != FILE_TYPE_DISK)
784-
return _fstati64(fd, buf);
785-
786-
if (!get_file_info_by_handle(fh, buf))
787-
return 0;
788-
errno = EBADF;
789-
return -1;
790803
}
791804

792805
static inline void time_t_to_filetime(time_t t, FILETIME *ft)

0 commit comments

Comments
 (0)