]> git.sur5r.net Git - cc65/blob - src/common/filetime.c
b621b9e57df14a0c637f78319a51f66162277ca9
[cc65] / src / common / filetime.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                filetime.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. The problem described here:
38  *   http://www.codeproject.com/KB/datetime/dstbugs.aspx
39  * is also true when setting file times via utime(), so we need a
40  * replacement
41  */
42
43
44
45 #if defined(__WATCOMC__) && defined(__NT__)
46 #define BUGGY_OS 1
47 #include <errno.h>
48 #include <windows.h>
49 #else
50 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__)
51 /* The Windows compilers have the file in the wrong directory */
52 #  include <sys/utime.h>
53 #else
54 #  include <sys/types.h>                /* FreeBSD needs this */
55 #  include <utime.h>
56 #endif
57 #endif
58
59
60 /* common */
61 #include "filetime.h"
62
63
64
65 /*****************************************************************************/
66 /*                                   Code                                    */
67 /*****************************************************************************/
68
69
70
71 #if defined(BUGGY_OS)
72
73
74
75 static FILETIME* UnixTimeToFileTime (time_t T, FILETIME* FT)
76 /* Calculate a FILETIME value from a time_t. FILETIME contains a 64 bit
77  * value with point zero at 1600-01-01 00:00:00 and counting 100ns intervals.
78  * time_t is in seconds since 1970-01-01 00:00:00.
79  */
80 {
81     /* Offset between 1600-01-01 and the Epoch in seconds. Watcom C has no
82      * way to express a number > 32 bit (known to me) but is able to do
83      * calculations with 64 bit integers, so we need to do it this way.
84      */
85     static const ULARGE_INTEGER Offs = { 0xB6109100UL, 0x00000020UL };
86     ULARGE_INTEGER V;
87     V.QuadPart = ((unsigned __int64) T + Offs.QuadPart) * 10000000U;
88     FT->dwLowDateTime  = V.LowPart;
89     FT->dwHighDateTime = V.HighPart;
90     return FT;
91 }
92
93
94
95 int SetFileTimes (const char* Path, time_t T)
96 /* Set the time of last modification and the time of last access of a file to
97  * the given time T. This calls utime() for system where it works, and applies
98  * workarounds for all others (which in fact means "WINDOWS").
99  */
100 {
101     HANDLE   H;
102     FILETIME FileTime;
103     int      Error = EACCES;                    /* Assume an error */
104
105
106     /* Open the file */
107     H = CreateFile (Path,
108                     GENERIC_WRITE,
109                     FILE_SHARE_READ,
110                     0,                          /* Security attributes */
111                     OPEN_EXISTING,
112                     0,                          /* File flags */
113                     0);                         /* Template file */
114     if (H != INVALID_HANDLE_VALUE) {
115         /* Set access and modification time */
116         UnixTimeToFileTime (T, &FileTime);
117         if (SetFileTime (H, 0, &FileTime, &FileTime)) {
118             /* Done */
119             Error = 0;
120         }
121
122         /* Close the handle */
123         (void) CloseHandle (H);
124     }
125
126     /* Return the error code */
127     return Error;
128 }
129
130
131
132 #else
133
134
135
136 int SetFileTimes (const char* Path, time_t T)
137 /* Set the time of last modification and the time of last access of a file to
138  * the given time T. This calls utime() for system where it works, and applies
139  * workarounds for all others (which in fact means "WINDOWS").
140  */
141 {
142     struct utimbuf U;
143
144     /* Set access and modification time */
145     U.actime  = T;
146     U.modtime = T;
147     return utime (Path, &U);
148 }
149
150
151
152 #endif
153
154
155