branch update for HEAD-2003050101
[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   char* ext;
16
17   if (!buffer)
18   {
19     __set_errno(EINVAL);
20     return -1;
21   }
22
23   if (strchr(path, '*') || strchr(path, '?'))
24   {
25     __set_errno(EINVAL);
26     return -1;
27   }
28
29   findHandle = FindFirstFileA(path, &findData);
30   if (findHandle == INVALID_HANDLE_VALUE)
31   {
32     __set_errno(ENOENT);
33     return -1;
34   }
35
36   FindClose(findHandle);
37
38   memset (buffer, 0, sizeof(struct stat));
39
40   buffer->st_ctime = FileTimeToUnixTime(&findData.ftCreationTime,NULL);
41   buffer->st_atime = FileTimeToUnixTime(&findData.ftLastAccessTime,NULL);
42   buffer->st_mtime = FileTimeToUnixTime(&findData.ftLastWriteTime,NULL);
43
44 //  statbuf->st_dev = fd;
45   buffer->st_size = findData.nFileSizeLow;
46   buffer->st_mode = S_IREAD;
47   if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
48     buffer->st_mode |= S_IFDIR;
49   else
50   {
51     buffer->st_mode |= S_IFREG;
52     ext = strrchr(path, '.');
53     if (ext && (!stricmp(ext, ".exe") || 
54                 !stricmp(ext, ".com") || 
55                 !stricmp(ext, ".bat") || 
56                 !stricmp(ext, ".cmd")))
57       buffer->st_mode |= S_IEXEC;
58   }
59   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) 
60     buffer->st_mode |= S_IWRITE;
61
62   return 0;
63 }