update for HEAD-2003021201
[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 int _wstat (const wchar_t *path, struct stat *buffer)
12 {
13   HANDLE findHandle;
14   WIN32_FIND_DATAW findData;
15
16   if (!buffer)
17   {
18     __set_errno(EINVAL);
19     return -1;
20   }
21
22   if(wcschr(path, L'*') || wcschr(path, L'?'))
23   {
24     __set_errno(EINVAL);
25     return -1;
26   }
27
28   findHandle = FindFirstFileW(path, &findData);
29   if (findHandle == INVALID_HANDLE_VALUE)
30   {
31     __set_errno(ENOENT);
32     return -1;
33   }
34
35   FindClose(findHandle);
36
37   memset (buffer, 0, sizeof(struct stat));
38
39   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
40   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
41   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
42
43 //  statbuf->st_dev = fd;
44   buffer->st_size = findData.nFileSizeLow;
45   buffer->st_mode = S_IREAD;
46   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
47     buffer->st_mode |= S_IFDIR;
48   else
49     buffer->st_mode |= S_IFREG;
50   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
51     buffer->st_mode |= S_IWRITE;
52
53   return 0;
54 }
55
56 __int64 _stati64 (const char *path, struct _stati64 *buffer)
57 {
58   HANDLE findHandle;
59   WIN32_FIND_DATAA findData;
60
61   if (!buffer)
62   {
63     __set_errno(EINVAL);
64     return -1;
65   }
66
67   if(strchr(path, '*') || strchr(path, '?'))
68   {
69     __set_errno(EINVAL);
70     return -1;
71   }
72
73   findHandle = FindFirstFileA(path, &findData);
74   if (findHandle == INVALID_HANDLE_VALUE)
75   {
76     __set_errno(ENOENT);
77     return -1;
78   }
79
80   FindClose(findHandle);
81
82   memset (buffer, 0, sizeof(struct stat));
83
84   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
85   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
86   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
87
88 //  statbuf->st_dev = fd;
89   buffer->st_size = (((__int64)findData.nFileSizeHigh) << 32) +
90              findData.nFileSizeLow;
91   buffer->st_mode = S_IREAD;
92   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
93     buffer->st_mode |= S_IFDIR;
94   else
95     buffer->st_mode |= S_IFREG;
96   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
97     buffer->st_mode |= S_IWRITE;
98
99   return 0;
100 }
101
102 __int64 _wstati64 (const wchar_t *path, struct _stati64 *buffer)
103 {
104   HANDLE findHandle;
105   WIN32_FIND_DATAW findData;
106
107   if (!buffer)
108   {
109     __set_errno(EINVAL);
110     return -1;
111   }
112
113   if(wcschr(path, L'*') || wcschr(path, L'?'))
114   {
115     __set_errno(EINVAL);
116     return -1;
117   }
118
119   findHandle = FindFirstFileW(path, &findData);
120   if (findHandle == INVALID_HANDLE_VALUE)
121   {
122     __set_errno(ENOENT);
123     return -1;
124   }
125
126   FindClose(findHandle);
127
128   memset (buffer, 0, sizeof(struct stat));
129
130   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
131   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
132   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
133
134 //  statbuf->st_dev = fd;
135   buffer->st_size = (((__int64)findData.nFileSizeHigh) << 32) +
136              findData.nFileSizeLow;
137   buffer->st_mode = S_IREAD;
138   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
139     buffer->st_mode |= S_IFDIR;
140   else
141     buffer->st_mode |= S_IFREG;
142   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
143     buffer->st_mode |= S_IWRITE;
144
145   return 0;
146 }