update for HEAD-2003021201
[reactos.git] / lib / crtdll / 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
8
9 int _stat(const char* path, struct stat* buffer)
10 {
11     HANDLE fh;
12     WIN32_FIND_DATA wfd;
13
14     fh = FindFirstFile(path, &wfd);
15     if (fh == INVALID_HANDLE_VALUE) {
16         __set_errno(ENOFILE);
17         return -1;
18     }
19     if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
20         int fd = _open(path, _O_RDONLY);
21         int ret;
22         ret = fstat(fd, buffer);
23         _close(fd);
24         return ret;
25     }
26     buffer->st_ctime = FileTimeToUnixTime(&wfd.ftCreationTime,NULL);
27     buffer->st_atime = FileTimeToUnixTime(&wfd.ftLastAccessTime,NULL);
28     buffer->st_mtime = FileTimeToUnixTime(&wfd.ftLastWriteTime,NULL);
29
30     if (buffer->st_atime ==0)
31         buffer->st_atime = buffer->st_mtime;
32     if (buffer->st_ctime ==0)
33         buffer->st_ctime = buffer->st_mtime;
34
35     buffer->st_mode = S_IREAD;
36     if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
37         buffer->st_mode |= S_IFDIR;
38     else
39         buffer->st_mode |= S_IFREG;
40     if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
41         buffer->st_mode |= S_IWRITE | S_IEXEC;
42
43     buffer->st_size = wfd.nFileSizeLow; 
44     buffer->st_nlink = 1;
45     if (FindNextFile(fh, &wfd)) {
46         __set_errno(ENOFILE);
47         FindClose(fh);
48         return -1;
49     }
50     return 0;
51 }