:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / msvcrt / sys_stat / stat.c
1 #include <msvcrt/sys/types.h>
2 #include <msvcrt/sys/stat.h>
3 #include <msvcrt/fcntl.h>
4 #include <msvcrt/io.h>
5 #include <msvcrt/errno.h>
6
7 #include <windows.h>
8
9
10 int _stat(const char *path, struct stat *buffer)
11 {
12   HANDLE findHandle;
13   WIN32_FIND_DATAA findData;
14
15   if (!buffer)
16   {
17     __set_errno(EINVAL);
18     return -1;
19   }
20
21   if(strchr(path, '*') || strchr(path, '?'))
22   {
23     __set_errno(EINVAL);
24     return -1;
25   }
26
27   findHandle = FindFirstFileA(path, &findData);
28   if (findHandle == INVALID_HANDLE_VALUE)
29   {
30     __set_errno(ENOENT);
31     return -1;
32   }
33
34   FindClose(findHandle);
35
36   memset (buffer, 0, sizeof(struct stat));
37
38   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
39   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
40   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
41
42 //  statbuf->st_dev = fd;
43   buffer->st_size = findData.nFileSizeLow;
44   buffer->st_mode = S_IREAD;
45   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
46     buffer->st_mode |= S_IFDIR;
47   else
48     buffer->st_mode |= S_IFREG;
49   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
50     buffer->st_mode |= S_IWRITE;
51
52   return 0;
53 }
54
55 __int64 _stati64 (const char *path, struct _stati64 *buffer)
56 {
57   HANDLE findHandle;
58   WIN32_FIND_DATAA findData;
59
60   if (!buffer)
61   {
62     __set_errno(EINVAL);
63     return -1;
64   }
65
66   if(strchr(path, '*') || strchr(path, '?'))
67   {
68     __set_errno(EINVAL);
69     return -1;
70   }
71
72   findHandle = FindFirstFileA(path, &findData);
73   if (findHandle == INVALID_HANDLE_VALUE)
74   {
75     __set_errno(ENOENT);
76     return -1;
77   }
78
79   FindClose(findHandle);
80
81   memset (buffer, 0, sizeof(struct stat));
82
83   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
84   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
85   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
86
87 //  statbuf->st_dev = fd;
88   buffer->st_size = (((__int64)findData.nFileSizeHigh) << 32) +
89                      findData.nFileSizeLow;
90   buffer->st_mode = S_IREAD;
91   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
92     buffer->st_mode |= S_IFDIR;
93   else
94     buffer->st_mode |= S_IFREG;
95   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
96     buffer->st_mode |= S_IWRITE;
97
98   return 0;
99 }
100
101 int _wstat (const wchar_t *path, struct stat *buffer)
102 {
103   HANDLE findHandle;
104   WIN32_FIND_DATAW findData;
105
106   if (!buffer)
107   {
108     __set_errno(EINVAL);
109     return -1;
110   }
111
112   if(wcschr(path, L'*') || wcschr(path, L'?'))
113   {
114     __set_errno(EINVAL);
115     return -1;
116   }
117
118   findHandle = FindFirstFileW(path, &findData);
119   if (findHandle == INVALID_HANDLE_VALUE)
120   {
121     __set_errno(ENOENT);
122     return -1;
123   }
124
125   FindClose(findHandle);
126
127   memset (buffer, 0, sizeof(struct stat));
128
129   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
130   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
131   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
132
133 //  statbuf->st_dev = fd;
134   buffer->st_size = findData.nFileSizeLow;
135   buffer->st_mode = S_IREAD;
136   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
137     buffer->st_mode |= S_IFDIR;
138   else
139     buffer->st_mode |= S_IFREG;
140   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
141     buffer->st_mode |= S_IWRITE;
142
143   return 0;
144 }
145
146 __int64 _wstati64 (const wchar_t *path, struct _stati64 *buffer)
147 {
148   HANDLE findHandle;
149   WIN32_FIND_DATAW findData;
150
151   if (!buffer)
152   {
153     __set_errno(EINVAL);
154     return -1;
155   }
156
157   if(wcschr(path, L'*') || wcschr(path, L'?'))
158   {
159     __set_errno(EINVAL);
160     return -1;
161   }
162
163   findHandle = FindFirstFileW(path, &findData);
164   if (findHandle == INVALID_HANDLE_VALUE)
165   {
166     __set_errno(ENOENT);
167     return -1;
168   }
169
170   FindClose(findHandle);
171
172   memset (buffer, 0, sizeof(struct stat));
173
174   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
175   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
176   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
177
178 //  statbuf->st_dev = fd;
179   buffer->st_size = (((__int64)findData.nFileSizeHigh) << 32) +
180                      findData.nFileSizeLow;
181   buffer->st_mode = S_IREAD;
182   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
183     buffer->st_mode |= S_IFDIR;
184   else
185     buffer->st_mode |= S_IFREG;
186   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
187     buffer->st_mode |= S_IWRITE;
188
189   return 0;
190 }