Added handling for std handles.
[reactos.git] / reactos / lib / msvcrt / sys_stat / fstat.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/crtdll/sys/fstat.c
5 * PURPOSE: Gather file information
6 * PROGRAMER: Boudewijn Dekker
7 * UPDATE HISTORY:
8 * 28/12/98: Created
9 */
10 #include <windows.h>
11 #include <msvcrt/sys/types.h>
12 #include <msvcrt/sys/stat.h>
13 #include <msvcrt/fcntl.h>
14 #include <msvcrt/string.h>
15 #include <msvcrt/errno.h>
16 #include <msvcrt/internal/file.h>
17
18
19 int _fstat(int fd, struct stat *statbuf)
20 {
21 BY_HANDLE_FILE_INFORMATION FileInformation;
22
23 if (!statbuf)
24 {
25 __set_errno(EINVAL);
26 return -1;
27 }
28
29 if (fd >= 0 && fd <= 4)
30 {
31 memset(statbuf, 0, sizeof(struct stat));
32 statbuf->st_dev = fd;
33 statbuf->st_mode = S_IFCHR;
34 return 0;
35 }
36
37 if (!GetFileInformationByHandle(_get_osfhandle(fd),&FileInformation))
38 {
39 __set_errno(EBADF);
40 return -1;
41 }
42 statbuf->st_ctime = FileTimeToUnixTime(&FileInformation.ftCreationTime,NULL);
43 statbuf->st_atime = FileTimeToUnixTime(&FileInformation.ftLastAccessTime,NULL);
44 statbuf->st_mtime = FileTimeToUnixTime(&FileInformation.ftLastWriteTime,NULL);
45
46 statbuf->st_dev = fd;
47 statbuf->st_size = FileInformation.nFileSizeLow;
48 statbuf->st_mode = S_IREAD;
49 if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) statbuf->st_mode |= S_IFDIR;
50 if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) statbuf->st_mode |= S_IWRITE;
51
52 return 0;
53 }
54
55 __int64 _fstati64 (int fd, struct _stati64* statbuf)
56 {
57 BY_HANDLE_FILE_INFORMATION FileInformation;
58
59 if (!statbuf)
60 {
61 __set_errno(EINVAL);
62 return -1;
63 }
64
65 if (!GetFileInformationByHandle(_get_osfhandle(fd),
66 &FileInformation))
67 {
68 __set_errno(EBADF);
69 return -1;
70 }
71 statbuf->st_ctime = FileTimeToUnixTime(&FileInformation.ftCreationTime,NULL);
72 statbuf->st_atime = FileTimeToUnixTime(&FileInformation.ftLastAccessTime,NULL);
73 statbuf->st_mtime = FileTimeToUnixTime(&FileInformation.ftLastWriteTime,NULL);
74
75 statbuf->st_dev = fd;
76 statbuf->st_size = (((__int64)FileInformation.nFileSizeHigh) << 32) +
77 FileInformation.nFileSizeLow;
78 statbuf->st_mode = S_IREAD;
79 if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) statbuf->st_mode |= S_IFDIR;
80 if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) statbuf->st_mode |= S_IWRITE;
81 return 0;
82 }