fca4278bc42a680b247cf237d134a15cab800e0a
[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 (!GetFileInformationByHandle(_get_osfhandle(fd),&FileInformation))
30 {
31 __set_errno(EBADF);
32 return -1;
33 }
34 statbuf->st_ctime = FileTimeToUnixTime(&FileInformation.ftCreationTime,NULL);
35 statbuf->st_atime = FileTimeToUnixTime(&FileInformation.ftLastAccessTime,NULL);
36 statbuf->st_mtime = FileTimeToUnixTime(&FileInformation.ftLastWriteTime,NULL);
37
38 statbuf->st_dev = fd;
39 statbuf->st_size = FileInformation.nFileSizeLow;
40 statbuf->st_mode = S_IREAD;
41 if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) statbuf->st_mode |= S_IFDIR;
42 if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) statbuf->st_mode |= S_IWRITE;
43
44 return 0;
45 }
46
47 __int64 _fstati64 (int fd, struct _stati64* statbuf)
48 {
49 BY_HANDLE_FILE_INFORMATION FileInformation;
50
51 if (!statbuf)
52 {
53 __set_errno(EINVAL);
54 return -1;
55 }
56
57 if (!GetFileInformationByHandle(_get_osfhandle(fd),
58 &FileInformation))
59 {
60 __set_errno(EBADF);
61 return -1;
62 }
63 statbuf->st_ctime = FileTimeToUnixTime(&FileInformation.ftCreationTime,NULL);
64 statbuf->st_atime = FileTimeToUnixTime(&FileInformation.ftLastAccessTime,NULL);
65 statbuf->st_mtime = FileTimeToUnixTime(&FileInformation.ftLastWriteTime,NULL);
66
67 statbuf->st_dev = fd;
68 statbuf->st_size = (((__int64)FileInformation.nFileSizeHigh) << 32) +
69 FileInformation.nFileSizeLow;
70 statbuf->st_mode = S_IREAD;
71 if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) statbuf->st_mode |= S_IFDIR;
72 if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) statbuf->st_mode |= S_IWRITE;
73 return 0;
74 }