update for HEAD-2003091401
[reactos.git] / lib / msvcrt / sys_stat / wstat.c
1 #include <windows.h>
2 #include <msvcrt/sys/types.h>
3 #include <msvcrt/sys/stat.h>
4 #include <msvcrt/fcntl.h>
5 #include <msvcrt/io.h>
6 #include <msvcrt/errno.h>
7 #include <msvcrt/string.h>
8 #include <msvcrt/internal/file.h>
9
10
11 /*
12  * @implemented
13  */
14 int _wstat (const wchar_t *path, struct stat *buffer)
15 {
16   WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
17   wchar_t *ext;
18
19   if (!buffer)
20   {
21     __set_errno(EINVAL);
22     return -1;
23   }
24
25   if(wcschr(path, L'*') || wcschr(path, L'?'))
26   {
27     __set_errno(ENOENT);
28     return -1;
29   }
30
31   if (!GetFileAttributesExW(path, GetFileExInfoStandard, &fileAttributeData))
32   {
33     __set_errno(ENOENT);
34     return -1;
35   }
36
37   memset (buffer, 0, sizeof(struct stat));
38
39   buffer->st_ctime = FileTimeToUnixTime(&fileAttributeData.ftCreationTime,NULL);
40   buffer->st_atime = FileTimeToUnixTime(&fileAttributeData.ftLastAccessTime,NULL);
41   buffer->st_mtime = FileTimeToUnixTime(&fileAttributeData.ftLastWriteTime,NULL);
42
43 //  statbuf->st_dev = fd;
44   buffer->st_size = fileAttributeData.nFileSizeLow;
45   buffer->st_mode = S_IREAD;
46   if (fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
47     buffer->st_mode |= S_IFDIR;
48   else
49   {
50     buffer->st_mode |= S_IFREG;
51     ext = wcsrchr(path, L'.');
52     if (ext && (!_wcsicmp(ext, L".exe") || 
53                 !_wcsicmp(ext, L".com") || 
54                 !_wcsicmp(ext, L".bat") || 
55                 !_wcsicmp(ext, L".cmd")))
56       buffer->st_mode |= S_IEXEC;
57   }
58   if (!(fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
59     buffer->st_mode |= S_IWRITE;
60
61   return 0;
62 }
63
64 /*
65  * @implemented
66  */
67 __int64 _wstati64 (const wchar_t *path, struct _stati64 *buffer)
68 {
69   WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
70   wchar_t *ext;
71
72   if (!buffer)
73   {
74     __set_errno(EINVAL);
75     return -1;
76   }
77
78   if(wcschr(path, L'*') || wcschr(path, L'?'))
79   {
80     __set_errno(ENOENT);
81     return -1;
82   }
83
84   if (!GetFileAttributesExW(path, GetFileExInfoStandard, &fileAttributeData))
85   {
86     __set_errno(ENOENT);
87     return -1;
88   }
89
90   memset (buffer, 0, sizeof(struct _stati64));
91
92   buffer->st_ctime = FileTimeToUnixTime(&fileAttributeData.ftCreationTime,NULL);
93   buffer->st_atime = FileTimeToUnixTime(&fileAttributeData.ftLastAccessTime,NULL);
94   buffer->st_mtime = FileTimeToUnixTime(&fileAttributeData.ftLastWriteTime,NULL);
95
96 //  statbuf->st_dev = fd;
97   buffer->st_size = ((((__int64)fileAttributeData.nFileSizeHigh) << 16) << 16) +
98              fileAttributeData.nFileSizeLow;
99   buffer->st_mode = S_IREAD;
100   if (fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
101     buffer->st_mode |= S_IFDIR;
102   else
103   {
104     buffer->st_mode |= S_IFREG;
105     ext = wcsrchr(path, L'.');
106     if (ext && (!_wcsicmp(ext, L".exe") || 
107                 !_wcsicmp(ext, L".com") || 
108                 !_wcsicmp(ext, L".bat") || 
109                 !_wcsicmp(ext, L".cmd")))
110       buffer->st_mode |= S_IEXEC;
111   }
112   if (!(fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
113     buffer->st_mode |= S_IWRITE;
114
115   return 0;
116 }