branch update for HEAD-2003021201
[reactos.git] / lib / crtdll / sys_stat / fstat.c
1 /* $Id$
2  *
3  * COPYRIGHT:   See COPYING in the top level directory
4  * PROJECT:     ReactOS system libraries
5  * FILE:        lib/crtdll/sys/fstat.c
6  * PURPOSE:     Gather file information
7  * PROGRAMER:   Boudewijn Dekker
8  * UPDATE HISTORY:
9  *              28/12/98: Created
10  */
11 #include <windows.h>
12 #include <msvcrt/sys/types.h>
13 #include <msvcrt/sys/stat.h>
14 #include <msvcrt/fcntl.h>
15 #include <msvcrt/string.h>
16 #include <msvcrt/errno.h>
17 #include <msvcrt/internal/file.h>
18
19
20 int _fstat(int fd, struct stat* statbuf)
21 {
22     BY_HANDLE_FILE_INFORMATION FileInformation;
23
24     if (!statbuf) {
25         __set_errno(EINVAL);
26         return -1;
27     }
28
29     if (!GetFileInformationByHandle(_get_osfhandle(fd),&FileInformation)) {
30         __set_errno (EBADF);
31         return -1;
32     }
33     statbuf->st_ctime = FileTimeToUnixTime(&FileInformation.ftCreationTime,NULL);
34     statbuf->st_atime = FileTimeToUnixTime(&FileInformation.ftLastAccessTime,NULL);
35     statbuf->st_mtime = FileTimeToUnixTime(&FileInformation.ftLastWriteTime,NULL);
36     if (statbuf->st_atime ==0)
37         statbuf->st_atime = statbuf->st_mtime;
38     if (statbuf->st_ctime ==0)
39         statbuf->st_ctime = statbuf->st_mtime;
40
41     statbuf->st_dev = FileInformation.dwVolumeSerialNumber;
42     statbuf->st_size = FileInformation.nFileSizeLow;
43     statbuf->st_nlink = FileInformation.nNumberOfLinks;
44     statbuf->st_mode = S_IREAD;
45     if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
46         statbuf->st_mode |= S_IFDIR | S_IEXEC;
47     else
48         statbuf->st_mode |= S_IFREG;
49     if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
50         statbuf->st_mode |= S_IWRITE;
51     return 0;
52 }