:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / crtdll / sys_stat / stat.c
1 #include <windows.h>
2 #include <crtdll/sys/types.h>
3 #include <crtdll/sys/stat.h>
4 #include <crtdll/fcntl.h>
5 #include <crtdll/io.h>
6 #include <crtdll/errno.h>
7
8
9 int _stat( const char *path, struct stat *buffer )
10 {
11  WIN32_FIND_DATA wfd;
12  HANDLE fh;
13   fh = FindFirstFile (path,&wfd);
14   if ( fh == INVALID_HANDLE_VALUE )
15   {
16     __set_errno(ENOFILE);
17     return -1;
18   }
19   if ( ! (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
20   {
21         int fd = _open(path,_O_RDONLY);
22         int ret;
23         
24         ret = fstat(fd,buffer);
25         _close(fd);
26
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 (FindNextFile(fh,&wfd))
49   {
50     __set_errno(ENOFILE);
51     FindClose(fh);
52     return -1;
53   }
54   return 0;
55 }