]> git.sur5r.net Git - cc65/blob - src/common/filestat.c
Fixed an invalid offset.
[cc65] / src / common / filestat.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                filestat.c                                 */
4 /*                                                                           */
5 /*                   Replacement for buggy Microsoft code                    */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2012,      Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* This module works around bugs in the time conversion code supplied by
37  * Microsoft. See here for a description of the problem:
38  *   http://www.codeproject.com/KB/datetime/dstbugs.aspx
39  * Please let me note that I find it absolutely unacceptable to just declare
40  * buggy behaviour like this "works as designed" as Microsoft does. The
41  * problems did even make it into .NET, where the DateTime builtin data type
42  * has exactly the same problems as described in the article above.
43  */
44
45
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #if defined(__WATCOMC__) && defined(__NT__)
50 #define BUGGY_OS 1
51 #include <errno.h>
52 #include <windows.h>
53 #endif
54
55 /* common */
56 #include "filestat.h"
57
58
59
60 /*****************************************************************************/
61 /*                                   Code                                    */
62 /*****************************************************************************/
63
64
65
66 #if defined(BUGGY_OS)
67
68
69
70 static time_t FileTimeToUnixTime (const FILETIME* T)
71 /* Calculate a unix time_t value from a FILETIME. FILETIME contains a 64 bit
72  * value with point zero at 1600-01-01 00:00:00 and counting 100ns intervals.
73  * time_t is in seconds since 1970-01-01 00:00:00.
74  */
75 {
76     /* Offset between 1600-01-01 and the Epoch in seconds. Watcom C has no
77      * way to express a number > 32 bit (known to me) but is able to do
78      * calculations with 64 bit integers, so we need to do it this way.
79      */
80     static const ULARGE_INTEGER Offs = { 0xB6109100UL, 0x00000020UL };
81     ULARGE_INTEGER V;
82     V.LowPart  = T->dwLowDateTime;
83     V.HighPart = T->dwHighDateTime;
84     return (V.QuadPart / 10000000U) - Offs.QuadPart;
85 }
86
87
88
89 int FileStat (const char* Path, struct stat* Buf)
90 /* Replacement function for stat() */
91 {
92
93     HANDLE                      H;
94     BY_HANDLE_FILE_INFORMATION  Info;
95
96     /* First call stat() */
97     int Error = stat (Path, Buf);
98     if (Error != 0) {
99         return Error;
100     }
101
102     /* Open the file using backup semantics, so we won't change atime. Then
103      * retrieve the correct times in UTC and replace the ones in Buf. Return
104      * EACCES in case of errors to avoid the hassle of translating windows
105      * error codes to standard ones.
106      */
107     H = CreateFile (Path,
108                     GENERIC_READ,
109                     FILE_SHARE_READ,
110                     0,                          /* Security attributes */
111                     OPEN_EXISTING,
112                     FILE_FLAG_BACKUP_SEMANTICS,
113                     0);                         /* Template file */
114     if (H != INVALID_HANDLE_VALUE) {
115         if (GetFileInformationByHandle (H, &Info)) {
116             Buf->st_ctime = FileTimeToUnixTime (&Info.ftCreationTime);
117             Buf->st_atime = FileTimeToUnixTime (&Info.ftLastAccessTime);
118             Buf->st_mtime = FileTimeToUnixTime (&Info.ftLastWriteTime);
119         } else {
120             Error = EACCES;
121         }
122         CloseHandle (H);
123     } else {
124         Error = EACCES;
125     }
126
127     /* Done */
128     return Error;
129 }
130
131
132
133 #else
134
135
136
137 int FileStat (const char* Path, struct stat* Buf)
138 /* Replacement function for stat() */
139 {
140     /* Just call the function which works without errors */
141     return stat (Path, Buf);
142 }
143
144
145
146 #endif