Added binary and unicode file i/o support to msvcrt.
[reactos.git] / reactos / 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 {
17 __set_errno(ENOFILE);
18 return -1;
19 }
20 if ( ! (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
21 {
22 int fd = _open(path,_O_RDONLY);
23 int ret;
24
25 ret = fstat(fd,buffer);
26 _close(fd);
27
28 return ret;
29 }
30 buffer->st_ctime = FileTimeToUnixTime( &wfd.ftCreationTime,NULL);
31 buffer->st_atime = FileTimeToUnixTime( &wfd.ftLastAccessTime,NULL);
32 buffer->st_mtime = FileTimeToUnixTime( &wfd.ftLastWriteTime,NULL);
33
34 if (buffer->st_atime ==0)
35 buffer->st_atime = buffer->st_mtime;
36 if (buffer->st_ctime ==0)
37 buffer->st_ctime = buffer->st_mtime;
38
39 buffer->st_mode = S_IREAD;
40 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
41 buffer->st_mode |= S_IFDIR;
42 else
43 buffer->st_mode |= S_IFREG;
44 if ( ! (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
45 buffer->st_mode |= S_IWRITE | S_IEXEC;
46
47 buffer->st_size = wfd.nFileSizeLow;
48 buffer->st_nlink = 1;
49 if (FindNextFile(fh,&wfd))
50 {
51 __set_errno(ENOFILE);
52 FindClose(fh);
53 return -1;
54 }
55 return 0;
56 }