branch update for HEAD-2003021201
[reactos.git] / lib / msvcrt / sys_stat / stat.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 _stat(const char* path, struct stat* buffer)
12 {
13   HANDLE findHandle;
14   WIN32_FIND_DATAA findData;
15
16   if (!buffer)
17   {
18     __set_errno(EINVAL);
19     return -1;
20   }
21
22   if (strchr(path, '*') || strchr(path, '?'))
23   {
24     __set_errno(EINVAL);
25     return -1;
26   }
27
28   findHandle = FindFirstFileA(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 }