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