Skip to content

Latest commit

 

History

History
1097 lines (881 loc) · 30.6 KB

windows.c

File metadata and controls

1097 lines (881 loc) · 30.6 KB
 
Aug 23, 2001
Aug 23, 2001
1
/*
Mar 11, 2007
Mar 11, 2007
2
* Windows support routines for PhysicsFS.
Aug 23, 2001
Aug 23, 2001
3
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Aug 23, 2001
Aug 23, 2001
5
*
Apr 13, 2002
Apr 13, 2002
6
* This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
Aug 23, 2001
Aug 23, 2001
7
8
*/
Mar 11, 2007
Mar 11, 2007
9
10
11
12
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_WINDOWS
Jul 10, 2002
Jul 10, 2002
13
Aug 23, 2001
Aug 23, 2001
14
15
#include <windows.h>
#include <stdio.h>
Aug 29, 2001
Aug 29, 2001
16
#include <stdlib.h>
Jun 29, 2002
Jun 29, 2002
17
18
#include <string.h>
#include <errno.h>
Aug 29, 2001
Aug 29, 2001
19
#include <ctype.h>
Jun 7, 2002
Jun 7, 2002
20
#include <time.h>
May 6, 2002
May 6, 2002
21
Aug 23, 2001
Aug 23, 2001
22
23
#include "physfs_internal.h"
Dec 11, 2002
Dec 11, 2002
24
25
#if (defined _MSC_VER)
#define alloca(x) _alloca(x)
Sep 6, 2005
Sep 6, 2005
26
#elif (defined __MINGW32__) /* scary...hopefully this is okay. */
Dec 11, 2002
Dec 11, 2002
27
#define alloca(x) __builtin_alloca(x)
Jun 29, 2002
Jun 29, 2002
28
29
#endif
Dec 8, 2003
Dec 8, 2003
30
31
32
33
#define LOWORDER_UINT64(pos) (PHYSFS_uint32) \
(pos & 0x00000000FFFFFFFF)
#define HIGHORDER_UINT64(pos) (PHYSFS_uint32) \
(((pos & 0xFFFFFFFF00000000) >> 32) & 0x00000000FFFFFFFF)
Apr 3, 2002
Apr 3, 2002
34
Jun 29, 2002
Jun 29, 2002
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* GetUserProfileDirectory() is only available on >= NT4 (no 9x/ME systems!) */
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
HANDLE hToken,
LPTSTR lpProfileDir,
LPDWORD lpcchSize);
/* GetFileAttributesEx() is only available on >= Win98 or WinNT4 ... */
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETFILEATTRIBUTESEX) (
LPCTSTR lpFileName,
GET_FILEEX_INFO_LEVELS fInfoLevelId,
LPVOID lpFileInformation);
typedef struct
{
HANDLE handle;
int readonly;
} win32file;
Aug 23, 2001
Aug 23, 2001
52
Jun 29, 2002
Jun 29, 2002
53
54
55
56
const char *__PHYSFS_platformDirSeparator = "\\";
static LPFNGETFILEATTRIBUTESEX pGetFileAttributesEx = NULL;
static HANDLE libKernel32 = NULL;
static char *userDir = NULL;
Aug 23, 2001
Aug 23, 2001
57
Jun 29, 2002
Jun 29, 2002
58
59
60
/*
* Users without the platform SDK don't have this defined. The original docs
* for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
Nov 5, 2006
Nov 5, 2006
61
* work as desired.
Jun 29, 2002
Jun 29, 2002
62
*/
Dec 8, 2003
Dec 8, 2003
63
#define PHYSFS_INVALID_SET_FILE_POINTER 0xFFFFFFFF
May 6, 2002
May 6, 2002
64
Jun 29, 2002
Jun 29, 2002
65
/* just in case... */
Dec 8, 2003
Dec 8, 2003
66
67
#define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
Jun 29, 2002
Jun 29, 2002
68
69
Jun 10, 2002
Jun 10, 2002
70
71
72
73
74
75
76
77
78
79
/*
* Figure out what the last failing Win32 API call was, and
* generate a human-readable string for the error message.
*
* The return value is a static buffer that is overwritten with
* each call to this function.
*/
static const char *win32strerror(void)
{
static TCHAR msgbuf[255];
Jun 29, 2002
Jun 29, 2002
80
TCHAR *ptr = msgbuf;
Jun 10, 2002
Jun 10, 2002
81
82
83
84
85
86
87
88
89
90
91
92
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
NULL
);
Jun 29, 2002
Jun 29, 2002
93
94
95
96
97
98
99
100
101
102
/* chop off newlines. */
for (ptr = msgbuf; *ptr; ptr++)
{
if ((*ptr == '\n') || (*ptr == '\r'))
{
*ptr = ' ';
break;
} /* if */
} /* for */
Jun 10, 2002
Jun 10, 2002
103
104
105
106
return((const char *) msgbuf);
} /* win32strerror */
Jun 29, 2002
Jun 29, 2002
107
static char *getExePath(const char *argv0)
Jun 7, 2002
Jun 7, 2002
108
{
Jun 29, 2002
Jun 29, 2002
109
110
111
DWORD buflen;
int success = 0;
char *ptr = NULL;
Mar 14, 2005
Mar 14, 2005
112
char *retval = (char *) allocator.Malloc(sizeof (TCHAR) * (MAX_PATH + 1));
Jun 29, 2002
Jun 29, 2002
113
114
115
116
117
118
119
120
121
122
123
124
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
retval[0] = '\0';
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
if (buflen <= 0)
__PHYSFS_setError(win32strerror());
else
{
retval[buflen] = '\0'; /* does API always null-terminate this? */
/* make sure the string was not truncated. */
Mar 15, 2007
Mar 15, 2007
125
if (__PHYSFS_stricmpASCII(&retval[buflen - 4], ".exe") != 0)
Aug 1, 2002
Aug 1, 2002
126
__PHYSFS_setError(ERR_GETMODFN_TRUNC);
Jun 29, 2002
Jun 29, 2002
127
128
129
130
else
{
ptr = strrchr(retval, '\\');
if (ptr == NULL)
Jul 28, 2002
Jul 28, 2002
131
__PHYSFS_setError(ERR_GETMODFN_NO_DIR);
Jun 29, 2002
Jun 29, 2002
132
133
134
135
136
137
138
139
140
141
142
143
144
else
{
*(ptr + 1) = '\0'; /* chop off filename. */
success = 1;
} /* else */
} /* else */
} /* else */
/* if any part of the previous approach failed, try SearchPath()... */
if (!success)
{
if (argv0 == NULL)
Jul 28, 2002
Jul 28, 2002
145
__PHYSFS_setError(ERR_ARGV0_IS_NULL);
Jun 29, 2002
Jun 29, 2002
146
147
148
149
150
151
else
{
buflen = SearchPath(NULL, argv0, NULL, MAX_PATH+1, retval, &ptr);
if (buflen == 0)
__PHYSFS_setError(win32strerror());
else if (buflen > MAX_PATH)
Jul 28, 2002
Jul 28, 2002
152
__PHYSFS_setError(ERR_SEARCHPATH_TRUNC);
Jun 29, 2002
Jun 29, 2002
153
154
155
156
157
158
159
else
success = 1;
} /* else */
} /* if */
if (!success)
{
Mar 14, 2005
Mar 14, 2005
160
allocator.Free(retval);
Jun 29, 2002
Jun 29, 2002
161
162
163
164
return(NULL); /* physfs error message will be set, above. */
} /* if */
/* free up the bytes we didn't actually use. */
Mar 14, 2005
Mar 14, 2005
165
ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
Jun 29, 2002
Jun 29, 2002
166
167
168
169
170
if (ptr != NULL)
retval = ptr;
return(retval); /* w00t. */
} /* getExePath */
Jun 7, 2002
Jun 7, 2002
171
172
173
/*
Jun 29, 2002
Jun 29, 2002
174
175
176
* Try to make use of GetUserProfileDirectory(), which isn't available on
* some common variants of Win32. If we can't use this, we just punt and
* use the physfs base dir for the user dir, too.
Jun 7, 2002
Jun 7, 2002
177
*
Jun 29, 2002
Jun 29, 2002
178
179
180
181
* On success, module-scope variable (userDir) will have a pointer to
* a malloc()'d string of the user's profile dir, and a non-zero value is
* returned. If we can't determine the profile dir, (userDir) will
* be NULL, and zero is returned.
Jun 7, 2002
Jun 7, 2002
182
*/
Jun 29, 2002
Jun 29, 2002
183
static int determineUserDir(void)
Jun 7, 2002
Jun 7, 2002
184
{
Jun 29, 2002
Jun 29, 2002
185
DWORD psize = 0;
Jun 10, 2002
Jun 10, 2002
186
187
char dummy[1];
BOOL rc = 0;
Jun 29, 2002
Jun 29, 2002
188
189
190
191
HANDLE processHandle; /* Current process handle */
HANDLE accessToken = NULL; /* Security handle to process */
LPFNGETUSERPROFILEDIR GetUserProfileDirectory;
HMODULE lib;
Jun 10, 2002
Jun 10, 2002
192
Jun 29, 2002
Jun 29, 2002
193
assert(userDir == NULL);
Jun 10, 2002
Jun 10, 2002
194
Jun 29, 2002
Jun 29, 2002
195
196
197
198
199
200
/*
* GetUserProfileDirectory() is only available on NT 4.0 and later.
* This means Win95/98/ME (and CE?) users have to do without, so for
* them, we'll default to the base directory when we can't get the
* function pointer.
*/
Jun 10, 2002
Jun 10, 2002
201
Jun 29, 2002
Jun 29, 2002
202
203
204
205
206
lib = LoadLibrary("userenv.dll");
if (lib)
{
/* !!! FIXME: Handle Unicode? */
GetUserProfileDirectory = (LPFNGETUSERPROFILEDIR)
Jun 10, 2002
Jun 10, 2002
207
GetProcAddress(lib, "GetUserProfileDirectoryA");
Jun 29, 2002
Jun 29, 2002
208
209
210
211
212
213
214
215
216
217
218
219
220
221
if (GetUserProfileDirectory)
{
processHandle = GetCurrentProcess();
if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
{
/*
* Should fail. Will write the size of the profile path in
* psize. Also note that the second parameter can't be
* NULL or the function fails.
*/
rc = GetUserProfileDirectory(accessToken, dummy, &psize);
assert(!rc); /* success?! */
/* Allocate memory for the profile directory */
Mar 14, 2005
Mar 14, 2005
222
userDir = (char *) allocator.Malloc(psize);
Jun 29, 2002
Jun 29, 2002
223
224
225
226
if (userDir != NULL)
{
if (!GetUserProfileDirectory(accessToken, userDir, &psize))
{
Mar 14, 2005
Mar 14, 2005
227
allocator.Free(userDir);
Jun 29, 2002
Jun 29, 2002
228
229
230
231
userDir = NULL;
} /* if */
} /* else */
} /* if */
Jun 10, 2002
Jun 10, 2002
232
Jun 29, 2002
Jun 29, 2002
233
234
CloseHandle(accessToken);
} /* if */
Jun 10, 2002
Jun 10, 2002
235
Jun 29, 2002
Jun 29, 2002
236
FreeLibrary(lib);
Jun 10, 2002
Jun 10, 2002
237
238
} /* if */
Jun 29, 2002
Jun 29, 2002
239
if (userDir == NULL) /* couldn't get profile for some reason. */
Jun 7, 2002
Jun 7, 2002
240
{
Jun 29, 2002
Jun 29, 2002
241
242
243
/* Might just be a non-NT system; resort to the basedir. */
userDir = getExePath(NULL);
BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* STILL failed?! */
Jun 10, 2002
Jun 10, 2002
244
} /* if */
Jun 7, 2002
Jun 7, 2002
245
Jun 29, 2002
Jun 29, 2002
246
247
return(1); /* We made it: hit the showers. */
} /* determineUserDir */
Jun 10, 2002
Jun 10, 2002
248
Jun 7, 2002
Jun 7, 2002
249
Jul 14, 2002
Jul 14, 2002
250
static BOOL mediaInDrive(const char *drive)
Jun 7, 2002
Jun 7, 2002
251
{
Jun 29, 2002
Jun 29, 2002
252
UINT oldErrorMode;
Jul 14, 2002
Jul 14, 2002
253
254
DWORD tmp;
BOOL retval;
Jun 7, 2002
Jun 7, 2002
255
Dec 8, 2003
Dec 8, 2003
256
/* Prevent windows warning message appearing when checking media size */
Jun 29, 2002
Jun 29, 2002
257
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
Jun 7, 2002
Jun 7, 2002
258
259
/* If this function succeeds, there's media in the drive */
Jul 14, 2002
Jul 14, 2002
260
retval = GetVolumeInformation(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0);
Jun 7, 2002
Jun 7, 2002
261
262
/* Revert back to old windows error handler */
Jun 29, 2002
Jun 29, 2002
263
264
SetErrorMode(oldErrorMode);
Jul 14, 2002
Jul 14, 2002
265
return(retval);
Jun 29, 2002
Jun 29, 2002
266
} /* mediaInDrive */
Jun 7, 2002
Jun 7, 2002
267
Aug 23, 2001
Aug 23, 2001
268
Sep 29, 2004
Sep 29, 2004
269
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Aug 23, 2001
Aug 23, 2001
270
271
{
char drive_str[4] = "x:\\";
Sep 29, 2004
Sep 29, 2004
272
273
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
Aug 23, 2001
Aug 23, 2001
274
{
Sep 29, 2004
Sep 29, 2004
275
drive_str[0] = ch;
Jun 29, 2002
Jun 29, 2002
276
if (GetDriveType(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
Sep 29, 2004
Sep 29, 2004
277
cb(data, drive_str);
Aug 23, 2001
Aug 23, 2001
278
} /* for */
Sep 29, 2004
Sep 29, 2004
279
} /* __PHYSFS_platformDetectAvailableCDs */
Aug 23, 2001
Aug 23, 2001
280
281
Oct 9, 2001
Oct 9, 2001
282
283
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
May 22, 2003
May 22, 2003
284
285
if ((argv0 != NULL) && (strchr(argv0, '\\') != NULL))
return(NULL); /* default behaviour can handle this. */
Oct 9, 2001
Oct 9, 2001
286
Mar 21, 2002
Mar 21, 2002
287
return(getExePath(argv0));
Aug 23, 2001
Aug 23, 2001
288
289
290
291
292
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
Aug 23, 2001
Aug 23, 2001
293
DWORD bufsize = 0;
Aug 23, 2001
Aug 23, 2001
294
295
296
297
LPTSTR retval = NULL;
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
{
Mar 14, 2005
Mar 14, 2005
298
retval = (LPTSTR) allocator.Malloc(bufsize);
Aug 23, 2001
Aug 23, 2001
299
300
301
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (GetUserName(retval, &bufsize) == 0) /* ?! */
{
Jun 10, 2002
Jun 10, 2002
302
__PHYSFS_setError(win32strerror());
Mar 14, 2005
Mar 14, 2005
303
allocator.Free(retval);
Aug 23, 2001
Aug 23, 2001
304
305
306
307
308
309
310
311
312
313
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
Mar 14, 2005
Mar 14, 2005
314
char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
Jun 10, 2002
Jun 10, 2002
315
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Jun 29, 2002
Jun 29, 2002
316
317
strcpy(retval, userDir); /* calculated at init time. */
return(retval);
Aug 23, 2001
Aug 23, 2001
318
319
320
} /* __PHYSFS_platformGetUserDir */
Apr 3, 2002
Apr 3, 2002
321
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
Aug 23, 2001
Aug 23, 2001
322
{
Jun 29, 2002
Jun 29, 2002
323
return((PHYSFS_uint64) GetCurrentThreadId());
Aug 23, 2001
Aug 23, 2001
324
325
326
327
328
} /* __PHYSFS_platformGetThreadID */
int __PHYSFS_platformExists(const char *fname)
{
Dec 8, 2003
Dec 8, 2003
329
330
331
332
333
BAIL_IF_MACRO
(
GetFileAttributes(fname) == PHYSFS_INVALID_FILE_ATTRIBUTES,
win32strerror(), 0
);
Aug 21, 2002
Aug 21, 2002
334
return(1);
Aug 23, 2001
Aug 23, 2001
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
} /* __PHYSFS_platformExists */
int __PHYSFS_platformIsSymLink(const char *fname)
{
return(0); /* no symlinks on win32. */
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
} /* __PHYSFS_platformIsDirectory */
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
Mar 14, 2005
Mar 14, 2005
357
char *retval = (char *) allocator.Malloc(len);
Aug 23, 2001
Aug 23, 2001
358
char *p;
Aug 23, 2001
Aug 23, 2001
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend)
strcpy(retval, prepend);
else
retval[0] = '\0';
strcat(retval, dirName);
if (append)
strcat(retval, append);
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = '\\';
return(retval);
} /* __PHYSFS_platformCvtToDependent */
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
void __PHYSFS_platformTimeslice(void)
{
Sleep(10);
} /* __PHYSFS_platformTimeslice */
Sep 29, 2004
Sep 29, 2004
386
387
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
Sep 18, 2005
Sep 18, 2005
388
389
PHYSFS_EnumFilesCallback callback,
const char *origdir,
Sep 29, 2004
Sep 29, 2004
390
void *callbackdata)
Aug 23, 2001
Aug 23, 2001
391
392
393
{
HANDLE dir;
WIN32_FIND_DATA ent;
Jun 8, 2002
Jun 8, 2002
394
size_t len = strlen(dirname);
Sep 29, 2004
Sep 29, 2004
395
char *SearchPath;
Jun 7, 2002
Jun 7, 2002
396
Jun 8, 2002
Jun 8, 2002
397
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
Jun 29, 2002
Jun 29, 2002
398
SearchPath = (char *) alloca(len + 3);
Sep 29, 2004
Sep 29, 2004
399
400
if (SearchPath == NULL)
return;
Jun 10, 2002
Jun 10, 2002
401
Jun 7, 2002
Jun 7, 2002
402
403
/* Copy current dirname */
strcpy(SearchPath, dirname);
Jun 8, 2002
Jun 8, 2002
404
405
/* if there's no '\\' at the end of the path, stick one in there. */
Jun 10, 2002
Jun 10, 2002
406
if (SearchPath[len - 1] != '\\')
Jun 8, 2002
Jun 8, 2002
407
{
Jun 10, 2002
Jun 10, 2002
408
409
SearchPath[len++] = '\\';
SearchPath[len] = '\0';
Jun 8, 2002
Jun 8, 2002
410
411
} /* if */
Jun 7, 2002
Jun 7, 2002
412
413
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");
Aug 23, 2001
Aug 23, 2001
414
Jun 7, 2002
Jun 7, 2002
415
dir = FindFirstFile(SearchPath, &ent);
Sep 29, 2004
Sep 29, 2004
416
417
if (dir == INVALID_HANDLE_VALUE)
return;
Aug 23, 2001
Aug 23, 2001
418
Jun 8, 2002
Jun 8, 2002
419
do
Aug 23, 2001
Aug 23, 2001
420
421
422
423
424
425
426
{
if (strcmp(ent.cFileName, ".") == 0)
continue;
if (strcmp(ent.cFileName, "..") == 0)
continue;
Sep 18, 2005
Sep 18, 2005
427
callback(callbackdata, origdir, ent.cFileName);
Jun 8, 2002
Jun 8, 2002
428
} while (FindNextFile(dir, &ent) != 0);
Aug 23, 2001
Aug 23, 2001
429
430
431
432
433
434
435
FindClose(dir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
Aug 23, 2001
Aug 23, 2001
436
LPTSTR retval;
Aug 23, 2001
Aug 23, 2001
437
438
DWORD buflen = 0;
Aug 23, 2001
Aug 23, 2001
439
buflen = GetCurrentDirectory(buflen, NULL);
Mar 14, 2005
Mar 14, 2005
440
retval = (LPTSTR) allocator.Malloc(sizeof (TCHAR) * (buflen + 2));
Sep 1, 2001
Sep 1, 2001
441
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 23, 2001
Aug 23, 2001
442
GetCurrentDirectory(buflen, retval);
Sep 1, 2001
Sep 1, 2001
443
444
445
446
if (retval[buflen - 2] != '\\')
strcat(retval, "\\");
Aug 23, 2001
Aug 23, 2001
447
448
449
450
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
Sep 1, 2001
Sep 1, 2001
451
/* this could probably use a cleanup. */
Aug 23, 2001
Aug 23, 2001
452
453
char *__PHYSFS_platformRealPath(const char *path)
{
Sep 1, 2001
Sep 1, 2001
454
455
456
457
458
459
char *retval = NULL;
char *p = NULL;
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
Mar 14, 2005
Mar 14, 2005
460
retval = (char *) allocator.Malloc(MAX_PATH);
Sep 1, 2001
Sep 1, 2001
461
462
463
464
465
466
467
468
469
470
471
472
473
474
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/*
* If in \\server\path format, it's already an absolute path.
* We'll need to check for "." and ".." dirs, though, just in case.
*/
if ((path[0] == '\\') && (path[1] == '\\'))
strcpy(retval, path);
else
{
char *currentDir = __PHYSFS_platformCurrentDir();
if (currentDir == NULL)
{
Mar 14, 2005
Mar 14, 2005
475
allocator.Free(retval);
Sep 1, 2001
Sep 1, 2001
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
if (path[1] == ':') /* drive letter specified? */
{
/*
* Apparently, "D:mypath" is the same as "D:\\mypath" if
* D: is not the current drive. However, if D: is the
* current drive, then "D:mypath" is a relative path. Ugh.
*/
if (path[2] == '\\') /* maybe an absolute path? */
strcpy(retval, path);
else /* definitely an absolute path. */
{
if (path[0] == currentDir[0]) /* current drive; relative. */
{
strcpy(retval, currentDir);
strcat(retval, path + 2);
} /* if */
else /* not current drive; absolute. */
{
retval[0] = path[0];
retval[1] = ':';
retval[2] = '\\';
strcpy(retval + 3, path + 2);
} /* else */
} /* else */
} /* if */
else /* no drive letter specified. */
{
if (path[0] == '\\') /* absolute path. */
{
retval[0] = currentDir[0];
retval[1] = ':';
strcpy(retval + 2, path);
} /* if */
else
{
strcpy(retval, currentDir);
strcat(retval, path);
} /* else */
} /* else */
Mar 14, 2005
Mar 14, 2005
521
allocator.Free(currentDir);
Sep 1, 2001
Sep 1, 2001
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
} /* else */
/* (whew.) Ok, now take out "." and ".." path entries... */
p = retval;
while ( (p = strstr(p, "\\.")) != NULL)
{
/* it's a "." entry that doesn't end the string. */
if (p[2] == '\\')
memmove(p + 1, p + 3, strlen(p + 3) + 1);
/* it's a "." entry that ends the string. */
else if (p[2] == '\0')
p[0] = '\0';
/* it's a ".." entry. */
else if (p[2] == '.')
{
char *prevEntry = p - 1;
while ((prevEntry != retval) && (*prevEntry != '\\'))
prevEntry--;
if (prevEntry == retval) /* make it look like a "." entry. */
memmove(p + 1, p + 2, strlen(p + 2) + 1);
else
{
if (p[3] != '\0') /* doesn't end string. */
*prevEntry = '\0';
else /* ends string. */
memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
p = prevEntry;
} /* else */
} /* else if */
else
{
p++; /* look past current char. */
} /* else */
} /* while */
/* shrink the retval's memory block if possible... */
Mar 14, 2005
Mar 14, 2005
564
p = (char *) allocator.Realloc(retval, strlen(retval) + 1);
Sep 1, 2001
Sep 1, 2001
565
566
567
568
if (p != NULL)
retval = p;
return(retval);
Aug 23, 2001
Aug 23, 2001
569
570
571
572
573
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
Aug 23, 2001
Aug 23, 2001
574
DWORD rc = CreateDirectory(path, NULL);
Aug 23, 2001
Aug 23, 2001
575
576
577
578
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
Jun 10, 2002
Jun 10, 2002
579
Jun 29, 2002
Jun 29, 2002
580
581
/*
* Get OS info and save the important parts.
Mar 24, 2002
Mar 24, 2002
582
583
584
*
* Returns non-zero if successful, otherwise it returns zero on failure.
*/
Jun 29, 2002
Jun 29, 2002
585
static int getOSInfo(void)
Mar 24, 2002
Mar 24, 2002
586
{
Jun 29, 2002
Jun 29, 2002
587
588
#if 0 /* we don't actually use this at the moment, but may in the future. */
OSVERSIONINFO OSVersionInfo; /* Information about the OS */
Mar 24, 2002
Mar 24, 2002
589
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
Jun 10, 2002
Jun 10, 2002
590
BAIL_IF_MACRO(!GetVersionEx(&OSVersionInfo), win32strerror(), 0);
Mar 24, 2002
Mar 24, 2002
591
592
/* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
Jun 29, 2002
Jun 29, 2002
593
594
595
runningNT = ((OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(OSVersionInfo.dwMajorVersion >= 4));
#endif
Mar 24, 2002
Mar 24, 2002
596
Jun 29, 2002
Jun 29, 2002
597
598
return(1);
} /* getOSInfo */
Mar 24, 2002
Mar 24, 2002
599
Jun 29, 2002
Jun 29, 2002
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
* Some things we want/need are in external DLLs that may or may not be
* available, based on the operating system, etc. This function loads those
* libraries and hunts down the needed pointers.
*
* Libraries that are one-shot deals, or better loaded as needed, are loaded
* elsewhere (see determineUserDir()).
*
* Returns zero if a needed library couldn't load, non-zero if we have enough
* to go on (which means some useful but non-crucial libraries may _NOT_ be
* loaded; check the related module-scope variables).
*/
static int loadLibraries(void)
Mar 24, 2002
Mar 24, 2002
614
{
Aug 28, 2002
Aug 28, 2002
615
616
/* If this get unwieldy, make it table driven. */
Jun 29, 2002
Jun 29, 2002
617
int allNeededLibrariesLoaded = 1; /* flip to zero as needed. */
Mar 24, 2002
Mar 24, 2002
618
Jun 29, 2002
Jun 29, 2002
619
620
libKernel32 = LoadLibrary("kernel32.dll");
if (libKernel32)
Mar 24, 2002
Mar 24, 2002
621
{
Jun 29, 2002
Jun 29, 2002
622
623
pGetFileAttributesEx = (LPFNGETFILEATTRIBUTESEX)
GetProcAddress(libKernel32, "GetFileAttributesExA");
Jun 10, 2002
Jun 10, 2002
624
} /* if */
Jun 29, 2002
Jun 29, 2002
625
626
627
628
629
630
/* add other DLLs here... */
/* see if there's any reason to keep kernel32.dll around... */
if (libKernel32)
Jun 10, 2002
Jun 10, 2002
631
{
Jun 29, 2002
Jun 29, 2002
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
if ((pGetFileAttributesEx == NULL) /* && (somethingElse == NULL) */ )
{
FreeLibrary(libKernel32);
libKernel32 = NULL;
} /* if */
} /* if */
return(allNeededLibrariesLoaded);
} /* loadLibraries */
int __PHYSFS_platformInit(void)
{
BAIL_IF_MACRO(!getOSInfo(), NULL, 0);
BAIL_IF_MACRO(!loadLibraries(), NULL, 0);
BAIL_IF_MACRO(!determineUserDir(), NULL, 0);
return(1); /* It's all good */
} /* __PHYSFS_platformInit */
Mar 24, 2002
Mar 24, 2002
651
652
653
654
int __PHYSFS_platformDeinit(void)
{
Jun 29, 2002
Jun 29, 2002
655
if (userDir != NULL)
Jun 10, 2002
Jun 10, 2002
656
{
Mar 14, 2005
Mar 14, 2005
657
allocator.Free(userDir);
Jun 29, 2002
Jun 29, 2002
658
userDir = NULL;
Jun 10, 2002
Jun 10, 2002
659
} /* if */
Mar 24, 2002
Mar 24, 2002
660
Jun 29, 2002
Jun 29, 2002
661
662
663
664
665
666
if (libKernel32)
{
FreeLibrary(libKernel32);
libKernel32 = NULL;
} /* if */
Jun 29, 2002
Jun 29, 2002
667
668
669
670
671
672
673
674
675
676
677
678
return(1); /* It's all good */
} /* __PHYSFS_platformDeinit */
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
HANDLE fileHandle;
win32file *retval;
fileHandle = CreateFile(fname, mode, FILE_SHARE_READ, NULL,
creation, FILE_ATTRIBUTE_NORMAL, NULL);
Dec 8, 2003
Dec 8, 2003
679
680
BAIL_IF_MACRO
(
Dec 22, 2003
Dec 22, 2003
681
fileHandle == INVALID_HANDLE_VALUE,
Dec 8, 2003
Dec 8, 2003
682
683
win32strerror(), NULL
);
Jun 29, 2002
Jun 29, 2002
684
Mar 14, 2005
Mar 14, 2005
685
retval = (win32file *) allocator.Malloc(sizeof (win32file));
Jun 29, 2002
Jun 29, 2002
686
if (retval == NULL)
Jun 10, 2002
Jun 10, 2002
687
{
Jun 29, 2002
Jun 29, 2002
688
689
CloseHandle(fileHandle);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
Jun 10, 2002
Jun 10, 2002
690
} /* if */
Mar 24, 2002
Mar 24, 2002
691
Jun 29, 2002
Jun 29, 2002
692
693
694
695
696
retval->readonly = rdonly;
retval->handle = fileHandle;
return(retval);
} /* doOpen */
Mar 24, 2002
Mar 24, 2002
697
Apr 3, 2002
Apr 3, 2002
698
699
void *__PHYSFS_platformOpenRead(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
700
701
return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
} /* __PHYSFS_platformOpenRead */
Apr 3, 2002
Apr 3, 2002
702
703
704
705
void *__PHYSFS_platformOpenWrite(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
706
707
return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
} /* __PHYSFS_platformOpenWrite */
Apr 3, 2002
Apr 3, 2002
708
709
710
711
void *__PHYSFS_platformOpenAppend(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
712
713
714
715
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
if (retval != NULL)
{
HANDLE h = ((win32file *) retval)->handle;
Dec 8, 2003
Dec 8, 2003
716
717
DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
Jun 29, 2002
Jun 29, 2002
718
719
720
{
const char *err = win32strerror();
CloseHandle(h);
Mar 14, 2005
Mar 14, 2005
721
allocator.Free(retval);
Jun 29, 2002
Jun 29, 2002
722
723
724
725
726
727
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
return(retval);
} /* __PHYSFS_platformOpenAppend */
Apr 3, 2002
Apr 3, 2002
728
729
730
731
732
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
Sep 26, 2004
Sep 26, 2004
733
HANDLE Handle = ((win32file *) opaque)->handle;
Apr 3, 2002
Apr 3, 2002
734
735
736
737
DWORD CountOfBytesRead;
PHYSFS_sint64 retval;
/* Read data from the file */
Jul 11, 2002
Jul 11, 2002
738
/* !!! FIXME: uint32 might be a greater # than DWORD */
Sep 26, 2004
Sep 26, 2004
739
if(!ReadFile(Handle, buffer, count * size, &CountOfBytesRead, NULL))
Apr 3, 2002
Apr 3, 2002
740
{
Jun 10, 2002
Jun 10, 2002
741
742
BAIL_MACRO(win32strerror(), -1);
} /* if */
Apr 3, 2002
Apr 3, 2002
743
744
745
else
{
/* Return the number of "objects" read. */
Jul 11, 2002
Jul 11, 2002
746
/* !!! FIXME: What if not the right amount of bytes was read to make an object? */
Apr 3, 2002
Apr 3, 2002
747
retval = CountOfBytesRead / size;
Jun 10, 2002
Jun 10, 2002
748
} /* else */
Apr 3, 2002
Apr 3, 2002
749
Jun 29, 2002
Jun 29, 2002
750
751
752
return(retval);
} /* __PHYSFS_platformRead */
Apr 3, 2002
Apr 3, 2002
753
Jun 29, 2002
Jun 29, 2002
754
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
Apr 3, 2002
Apr 3, 2002
755
756
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
Sep 26, 2004
Sep 26, 2004
757
HANDLE Handle = ((win32file *) opaque)->handle;
Apr 3, 2002
Apr 3, 2002
758
759
760
761
DWORD CountOfBytesWritten;
PHYSFS_sint64 retval;
/* Read data from the file */
Jul 11, 2002
Jul 11, 2002
762
/* !!! FIXME: uint32 might be a greater # than DWORD */
Sep 26, 2004
Sep 26, 2004
763
if(!WriteFile(Handle, buffer, count * size, &CountOfBytesWritten, NULL))
Apr 3, 2002
Apr 3, 2002
764
{
Jun 10, 2002
Jun 10, 2002
765
766
BAIL_MACRO(win32strerror(), -1);
} /* if */
Apr 3, 2002
Apr 3, 2002
767
768
769
else
{
/* Return the number of "objects" read. */
Jul 11, 2002
Jul 11, 2002
770
/* !!! FIXME: What if not the right number of bytes was written? */
Apr 3, 2002
Apr 3, 2002
771
retval = CountOfBytesWritten / size;
Jun 10, 2002
Jun 10, 2002
772
} /* else */
Apr 3, 2002
Apr 3, 2002
773
Jun 29, 2002
Jun 29, 2002
774
775
776
return(retval);
} /* __PHYSFS_platformWrite */
Apr 3, 2002
Apr 3, 2002
777
778
779
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
Sep 26, 2004
Sep 26, 2004
780
HANDLE Handle = ((win32file *) opaque)->handle;
Apr 3, 2002
Apr 3, 2002
781
DWORD HighOrderPos;
Dec 8, 2003
Dec 8, 2003
782
DWORD *pHighOrderPos;
Jun 29, 2002
Jun 29, 2002
783
DWORD rc;
Apr 3, 2002
Apr 3, 2002
784
785
786
787
/* Get the high order 32-bits of the position */
HighOrderPos = HIGHORDER_UINT64(pos);
Dec 8, 2003
Dec 8, 2003
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
* MSDN: "If you do not need the high-order 32 bits, this
* pointer must be set to NULL."
*/
pHighOrderPos = (HighOrderPos) ? &HighOrderPos : NULL;
/*
* !!! FIXME: MSDN: "Windows Me/98/95: If the pointer
* !!! FIXME: lpDistanceToMoveHigh is not NULL, then it must
* !!! FIXME: point to either 0, INVALID_SET_FILE_POINTER, or
* !!! FIXME: the sign extension of the value of lDistanceToMove.
* !!! FIXME: Any other value will be rejected."
*/
Apr 3, 2002
Apr 3, 2002
802
/* Move pointer "pos" count from start of file */
Sep 26, 2004
Sep 26, 2004
803
rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
Dec 8, 2003
Dec 8, 2003
804
pHighOrderPos, FILE_BEGIN);
Apr 3, 2002
Apr 3, 2002
805
Dec 8, 2003
Dec 8, 2003
806
807
808
if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
{
Jun 29, 2002
Jun 29, 2002
809
BAIL_MACRO(win32strerror(), 0);
Dec 8, 2003
Dec 8, 2003
810
811
} /* if */
Jun 29, 2002
Jun 29, 2002
812
813
return(1); /* No error occured */
} /* __PHYSFS_platformSeek */
Apr 3, 2002
Apr 3, 2002
814
815
816
817
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
Sep 26, 2004
Sep 26, 2004
818
HANDLE Handle = ((win32file *) opaque)->handle;
Jun 29, 2002
Jun 29, 2002
819
820
DWORD HighPos = 0;
DWORD LowPos;
Apr 3, 2002
Apr 3, 2002
821
822
823
PHYSFS_sint64 retval;
/* Get current position */
Sep 26, 2004
Sep 26, 2004
824
LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
Dec 8, 2003
Dec 8, 2003
825
826
if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
Apr 3, 2002
Apr 3, 2002
827
{
Jun 29, 2002
Jun 29, 2002
828
829
BAIL_MACRO(win32strerror(), 0);
} /* if */
May 8, 2002
May 8, 2002
830
831
832
else
{
/* Combine the high/low order to create the 64-bit position value */
Jun 29, 2002
Jun 29, 2002
833
834
835
836
837
838
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
assert(retval >= 0);
} /* else */
return(retval);
} /* __PHYSFS_platformTell */
Apr 3, 2002
Apr 3, 2002
839
840
Jun 29, 2002
Jun 29, 2002
841
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
Apr 3, 2002
Apr 3, 2002
842
{
Sep 26, 2004
Sep 26, 2004
843
HANDLE Handle = ((win32file *) opaque)->handle;
Jun 29, 2002
Jun 29, 2002
844
845
DWORD SizeHigh;
DWORD SizeLow;
Apr 3, 2002
Apr 3, 2002
846
847
PHYSFS_sint64 retval;
Sep 26, 2004
Sep 26, 2004
848
SizeLow = GetFileSize(Handle, &SizeHigh);
Dec 8, 2003
Dec 8, 2003
849
850
if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
Apr 3, 2002
Apr 3, 2002
851
{
Jun 10, 2002
Jun 10, 2002
852
853
BAIL_MACRO(win32strerror(), -1);
} /* if */
May 8, 2002
May 8, 2002
854
855
856
else
{
/* Combine the high/low order to create the 64-bit position value */
Jun 29, 2002
Jun 29, 2002
857
858
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
assert(retval >= 0);
Jun 10, 2002
Jun 10, 2002
859
} /* else */
Apr 3, 2002
Apr 3, 2002
860
Jun 29, 2002
Jun 29, 2002
861
862
863
return(retval);
} /* __PHYSFS_platformFileLength */
Apr 3, 2002
Apr 3, 2002
864
865
866
867
868
869
870
int __PHYSFS_platformEOF(void *opaque)
{
PHYSFS_sint64 FilePosition;
int retval = 0;
/* Get the current position in the file */
Jun 29, 2002
Jun 29, 2002
871
if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
Apr 3, 2002
Apr 3, 2002
872
{
May 8, 2002
May 8, 2002
873
874
/* Non-zero if EOF is equal to the file length */
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
Jun 29, 2002
Jun 29, 2002
875
876
877
878
} /* if */
return(retval);
} /* __PHYSFS_platformEOF */
Apr 3, 2002
Apr 3, 2002
879
880
881
882
int __PHYSFS_platformFlush(void *opaque)
{
Jun 29, 2002
Jun 29, 2002
883
884
885
win32file *fh = ((win32file *) opaque);
if (!fh->readonly)
BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
Apr 3, 2002
Apr 3, 2002
886
Jun 29, 2002
Jun 29, 2002
887
888
return(1);
} /* __PHYSFS_platformFlush */
Apr 3, 2002
Apr 3, 2002
889
890
891
892
int __PHYSFS_platformClose(void *opaque)
{
Sep 26, 2004
Sep 26, 2004
893
894
HANDLE Handle = ((win32file *) opaque)->handle;
BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
Mar 14, 2005
Mar 14, 2005
895
allocator.Free(opaque);
Jun 29, 2002
Jun 29, 2002
896
897
return(1);
} /* __PHYSFS_platformClose */
Apr 3, 2002
Apr 3, 2002
898
899
900
901
902
int __PHYSFS_platformDelete(const char *path)
{
/* If filename is a folder */
Jun 29, 2002
Jun 29, 2002
903
if (GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
Apr 3, 2002
Apr 3, 2002
904
{
Jun 29, 2002
Jun 29, 2002
905
906
BAIL_IF_MACRO(!RemoveDirectory(path), win32strerror(), 0);
} /* if */
Apr 3, 2002
Apr 3, 2002
907
908
else
{
Jun 29, 2002
Jun 29, 2002
909
910
BAIL_IF_MACRO(!DeleteFile(path), win32strerror(), 0);
} /* else */
Apr 3, 2002
Apr 3, 2002
911
Jun 29, 2002
Jun 29, 2002
912
913
return(1); /* if you got here, it worked. */
} /* __PHYSFS_platformDelete */
Apr 3, 2002
Apr 3, 2002
914
915
916
917
void *__PHYSFS_platformCreateMutex(void)
{
Jun 29, 2002
Jun 29, 2002
918
919
920
return((void *) CreateMutex(NULL, FALSE, NULL));
} /* __PHYSFS_platformCreateMutex */
Apr 3, 2002
Apr 3, 2002
921
922
923
void __PHYSFS_platformDestroyMutex(void *mutex)
{
Jun 29, 2002
Jun 29, 2002
924
925
926
CloseHandle((HANDLE) mutex);
} /* __PHYSFS_platformDestroyMutex */
Apr 3, 2002
Apr 3, 2002
927
928
929
int __PHYSFS_platformGrabMutex(void *mutex)
{
Jun 29, 2002
Jun 29, 2002
930
931
return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
} /* __PHYSFS_platformGrabMutex */
Apr 3, 2002
Apr 3, 2002
932
933
934
935
void __PHYSFS_platformReleaseMutex(void *mutex)
{
Jun 29, 2002
Jun 29, 2002
936
937
938
ReleaseMutex((HANDLE) mutex);
} /* __PHYSFS_platformReleaseMutex */
Apr 3, 2002
Apr 3, 2002
939
Jun 29, 2002
Jun 29, 2002
940
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
Jun 10, 2002
Jun 10, 2002
941
942
943
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
Jun 29, 2002
Jun 29, 2002
944
945
946
TIME_ZONE_INFORMATION tzi;
DWORD tzid;
PHYSFS_sint64 retval;
Jun 10, 2002
Jun 10, 2002
947
948
struct tm tm;
Jun 29, 2002
Jun 29, 2002
949
950
951
BAIL_IF_MACRO(!FileTimeToSystemTime(ft, &st_utc), win32strerror(), -1);
tzid = GetTimeZoneInformation(&tzi);
BAIL_IF_MACRO(tzid == TIME_ZONE_ID_INVALID, win32strerror(), -1);
Jun 10, 2002
Jun 10, 2002
952
Jun 29, 2002
Jun 29, 2002
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* (This API is unsupported and fails on non-NT systems. */
if (!SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz))
{
/* do it by hand. Grumble... */
ULARGE_INTEGER ui64;
FILETIME new_ft;
ui64.LowPart = ft->dwLowDateTime;
ui64.HighPart = ft->dwHighDateTime;
if (tzid == TIME_ZONE_ID_STANDARD)
tzi.Bias += tzi.StandardBias;
else if (tzid == TIME_ZONE_ID_DAYLIGHT)
tzi.Bias += tzi.DaylightBias;
/* convert from minutes to 100-nanosecond increments... */
#if 0 /* For compilers that puke on 64-bit math. */
/* goddamn this is inefficient... */
while (tzi.Bias > 0)
{
DWORD tmp = ui64.LowPart - 60000000;
if ((ui64.LowPart < tmp) && (tmp > 60000000))
ui64.HighPart--;
ui64.LowPart = tmp;
tzi.Bias--;
} /* while */
while (tzi.Bias < 0)
{
DWORD tmp = ui64.LowPart + 60000000;
if ((ui64.LowPart > tmp) && (tmp < 60000000))
ui64.HighPart++;
ui64.LowPart = tmp;
tzi.Bias++;
} /* while */
#else
ui64.QuadPart -= (((LONGLONG) tzi.Bias) * (600000000));
#endif
/* Move it back into a FILETIME structure... */
new_ft.dwLowDateTime = ui64.LowPart;
new_ft.dwHighDateTime = ui64.HighPart;
/* Convert to something human-readable... */
if (!FileTimeToSystemTime(&new_ft, &st_localtz))
BAIL_MACRO(win32strerror(), -1);
} /* if */
/* Convert to a format that mktime() can grok... */