update for HEAD-2003021201
[reactos.git] / lib / msvcrt / sys_stat / fstati64.c
1 /* $Id$
2  *
3  * COPYRIGHT:   See COPYING in the top level directory
4  * PROJECT:     ReactOS system libraries
5  * FILE:        lib/msvcrt/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 __int64 _fstati64(int fd, struct _stati64* statbuf)
21 {
22   BY_HANDLE_FILE_INFORMATION FileInformation;
23   DWORD dwFileType;
24   void *handle;
25
26   if (!statbuf)
27   {
28     __set_errno(EINVAL);
29     return -1;
30   }
31
32   if ((void*)-1 == (handle = _get_osfhandle(fd)))
33   {
34     __set_errno(EBADF);
35     return -1;
36   }
37   
38   fflush(NULL);
39
40   memset(statbuf, 0, sizeof(struct _stati64));
41
42   dwFileType = GetFileType(handle);
43
44   if (dwFileType == FILE_TYPE_DISK)
45   {
46     if (!GetFileInformationByHandle(handle,&FileInformation))
47     {
48        __set_errno(EBADF);
49        return -1;
50     }
51     statbuf->st_ctime = FileTimeToUnixTime(&FileInformation.ftCreationTime,NULL);
52     statbuf->st_atime = FileTimeToUnixTime(&FileInformation.ftLastAccessTime,NULL);
53     statbuf->st_mtime = FileTimeToUnixTime(&FileInformation.ftLastWriteTime,NULL);
54
55     statbuf->st_dev = fd;
56     statbuf->st_size = (((__int64)FileInformation.nFileSizeHigh) << 32) +
57              FileInformation.nFileSizeLow;
58     statbuf->st_mode = S_IREAD;
59     if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
60       statbuf->st_mode |= S_IFDIR;
61     else
62       statbuf->st_mode |= S_IFREG;
63     if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) statbuf->st_mode |= S_IWRITE;
64   }
65   else if (dwFileType == FILE_TYPE_CHAR)
66   {
67     statbuf->st_dev = fd;
68     statbuf->st_mode = S_IFCHR;
69   }
70   else if (dwFileType == FILE_TYPE_PIPE)
71   {
72     statbuf->st_dev = fd;
73     statbuf->st_mode = S_IFIFO;
74   }
75   else
76   {
77     // dwFileType is FILE_TYPE_UNKNOWN or has a bad value
78     __set_errno(EBADF);
79     return -1;
80   }
81   return 0;
82 }