- Cleanup the /lib directory, by putting more 3rd-party libs in /3rdparty, and by...
[reactos.git] / reactos / lib / sdk / crt / sys_stat / stat.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/crt/??????
5 * PURPOSE: Unknown
6 * PROGRAMER: Unknown
7 * UPDATE HISTORY:
8 * 25/11/05: Added license header
9 */
10
11 #include <precomp.h>
12 #include <sys/stat.h>
13
14 /*
15 * @implemented
16 */
17 int _stat(const char* path, struct _stat* buffer)
18 {
19 WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
20 char* ext;
21
22 if (!buffer)
23 {
24 __set_errno(EINVAL);
25 return -1;
26 }
27
28 if (strchr(path, '*') || strchr(path, '?'))
29 {
30 __set_errno(ENOENT);
31 return -1;
32 }
33
34 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fileAttributeData))
35 {
36 __set_errno(ENOENT);
37 return -1;
38 }
39
40 memset (buffer, 0, sizeof(struct stat));
41
42 buffer->st_ctime = FileTimeToUnixTime(&fileAttributeData.ftCreationTime,NULL);
43 buffer->st_atime = FileTimeToUnixTime(&fileAttributeData.ftLastAccessTime,NULL);
44 buffer->st_mtime = FileTimeToUnixTime(&fileAttributeData.ftLastWriteTime,NULL);
45
46 // statbuf->st_dev = fd;
47 buffer->st_size = fileAttributeData.nFileSizeLow;
48 buffer->st_mode = S_IREAD;
49 if (fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
50 buffer->st_mode |= S_IFDIR;
51 else
52 {
53 buffer->st_mode |= S_IFREG;
54 ext = strrchr(path, '.');
55 if (ext && (!_stricmp(ext, ".exe") ||
56 !_stricmp(ext, ".com") ||
57 !_stricmp(ext, ".bat") ||
58 !_stricmp(ext, ".cmd")))
59 buffer->st_mode |= S_IEXEC;
60 }
61 if (!(fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
62 buffer->st_mode |= S_IWRITE;
63
64 return 0;
65 }
66
67 /*
68 * @implemented
69 */
70 int _stati64 (const char *path, struct _stati64 *buffer)
71 {
72 WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
73 char* ext;
74
75 if (!buffer)
76 {
77 __set_errno(EINVAL);
78 return -1;
79 }
80
81 if(strchr(path, '*') || strchr(path, '?'))
82 {
83 __set_errno(ENOENT);
84 return -1;
85 }
86
87 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fileAttributeData))
88 {
89 __set_errno(ENOENT);
90 return -1;
91 }
92
93 memset (buffer, 0, sizeof(struct _stati64));
94
95 buffer->st_ctime = FileTimeToUnixTime(&fileAttributeData.ftCreationTime,NULL);
96 buffer->st_atime = FileTimeToUnixTime(&fileAttributeData.ftLastAccessTime,NULL);
97 buffer->st_mtime = FileTimeToUnixTime(&fileAttributeData.ftLastWriteTime,NULL);
98
99 // statbuf->st_dev = fd;
100 buffer->st_size = ((((__int64)fileAttributeData.nFileSizeHigh) << 16) << 16) +
101 fileAttributeData.nFileSizeLow;
102 buffer->st_mode = S_IREAD;
103 if (fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
104 buffer->st_mode |= S_IFDIR;
105 else
106 {
107 buffer->st_mode |= S_IFREG;
108 ext = strrchr(path, '.');
109 if (ext && (!_stricmp(ext, ".exe") ||
110 !_stricmp(ext, ".com") ||
111 !_stricmp(ext, ".bat") ||
112 !_stricmp(ext, ".cmd")))
113 buffer->st_mode |= S_IEXEC;
114 }
115 if (!(fileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
116 buffer->st_mode |= S_IWRITE;
117
118 return 0;
119 }
120