Skip to content

Latest commit

 

History

History
976 lines (785 loc) · 25.9 KB

win32.c

File metadata and controls

976 lines (785 loc) · 25.9 KB
 
Aug 23, 2001
Aug 23, 2001
1
/*
Aug 23, 2001
Aug 23, 2001
2
* Win32 support routines for PhysicsFS.
Aug 23, 2001
Aug 23, 2001
3
4
5
*
* Please see the file LICENSE in the source's root directory.
*
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
*/
May 10, 2002
May 10, 2002
9
10
11
12
#if HAVE_CONFIG_H
# include <config.h>
#endif
Aug 23, 2001
Aug 23, 2001
13
14
#include <windows.h>
#include <stdio.h>
Aug 29, 2001
Aug 29, 2001
15
16
#include <stdlib.h>
#include <ctype.h>
Jun 7, 2002
Jun 7, 2002
17
#include <time.h>
Jun 10, 2002
Jun 10, 2002
18
#include <assert.h>
May 6, 2002
May 6, 2002
19
Aug 23, 2001
Aug 23, 2001
20
21
22
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Jun 29, 2002
Jun 29, 2002
23
24
25
26
#ifndef _MSC_VER /* for Cygwin, etc. */
#define _alloca alloca
#endif
Apr 3, 2002
Apr 3, 2002
27
28
29
#define LOWORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0x00000000FFFFFFFF)
#define HIGHORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0xFFFFFFFF00000000)
Aug 23, 2001
Aug 23, 2001
30
31
const char *__PHYSFS_platformDirSeparator = "\\";
Jun 10, 2002
Jun 10, 2002
32
static int runningNT = 0; /* TRUE if NT derived OS */
Mar 24, 2002
Mar 24, 2002
33
34
static OSVERSIONINFO OSVersionInfo; /* Information about the OS */
static char *ProfileDirectory = NULL; /* User profile folder */
Aug 23, 2001
Aug 23, 2001
35
May 6, 2002
May 6, 2002
36
37
38
39
40
41
42
/* Users without the platform SDK don't have this defined. The original docs
for SetFilePointer() just said to compare with 0xFFFFFFF, so this should
work as desired */
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
Jun 10, 2002
Jun 10, 2002
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* 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];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
NULL
);
return((const char *) msgbuf);
} /* win32strerror */
Jun 7, 2002
Jun 7, 2002
69
70
71
72
73
74
75
/*
* Uninitialize any NT specific stuff done in doNTInit().
*
* Return zero if there was a catastrophic failure and non-zero otherwise.
*/
static int doNTDeinit(void)
{
Jun 10, 2002
Jun 10, 2002
76
77
78
/* nothing NT-specific to deinit at this point. */
return 1; /* It's all good */
} /* doNTDeinit */
Jun 7, 2002
Jun 7, 2002
79
80
Jun 10, 2002
Jun 10, 2002
81
82
83
84
85
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
HANDLE hToken,
LPTSTR lpProfileDir,
LPDWORD lpcchSize);
Jun 7, 2002
Jun 7, 2002
86
87
88
89
90
91
92
93
/*
* Initialize any NT specific stuff. This includes any OS based on NT.
*
* Return zero if there was a catastrophic failure and non-zero otherwise.
*/
static int doNTInit(void)
{
DWORD pathsize = 0;
Jun 10, 2002
Jun 10, 2002
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
char dummy[1];
BOOL rc = 0;
HANDLE ProcessHandle = NULL; /* Current process handle */
HANDLE AccessTokenHandle = NULL; /* Security handle to process */
LPFNGETUSERPROFILEDIR GetUserProfileDirectory = NULL;
HMODULE lib = NULL;
const char *err = NULL;
/* Hooray for spaghetti code! */
lib = LoadLibrary("userenv.dll");
if (!lib)
goto ntinit_failed;
/* !!! FIXME: Handle Unicode? */
GetUserProfileDirectory = (LPFNGETUSERPROFILEDIR)
GetProcAddress(lib, "GetUserProfileDirectoryA");
if (!GetUserProfileDirectory)
goto ntinit_failed;
/* Create a process handle associated with the current process ID */
ProcessHandle = GetCurrentProcess();
Jun 7, 2002
Jun 7, 2002
116
117
118
/* Create a process access token handle */
if(!OpenProcessToken(ProcessHandle, TOKEN_QUERY, &AccessTokenHandle))
Jun 10, 2002
Jun 10, 2002
119
120
121
122
123
124
125
126
127
128
goto ntinit_failed; /* we need that token to get the profile dir. */
/* Should fail. Will write the size of the profile path in pathsize */
/* Second parameter can't be NULL or the function fails. */
rc = GetUserProfileDirectory(AccessTokenHandle, dummy, &pathsize);
assert(!rc); /* success?! */
/* Allocate memory for the profile directory */
ProfileDirectory = (char *) malloc(pathsize);
if (ProfileDirectory == NULL)
Jun 7, 2002
Jun 7, 2002
129
{
Jun 10, 2002
Jun 10, 2002
130
131
132
133
134
135
136
137
138
err = ERR_OUT_OF_MEMORY;
goto ntinit_failed;
} /* if */
/* Try to get the profile directory */
if(!GetUserProfileDirectory(AccessTokenHandle, ProfileDirectory, &pathsize))
goto ntinit_failed;
goto ntinit_succeeded; /* We made it: hit the showers. */
Jun 7, 2002
Jun 7, 2002
139
Jun 10, 2002
Jun 10, 2002
140
141
142
143
144
ntinit_failed:
if (err == NULL) /* set an error string if we haven't yet. */
__PHYSFS_setError(win32strerror());
if (ProfileDirectory != NULL)
Jun 7, 2002
Jun 7, 2002
145
{
Jun 10, 2002
Jun 10, 2002
146
147
148
free(ProfileDirectory);
ProfileDirectory = NULL;
} /* if */
Jun 7, 2002
Jun 7, 2002
149
Jun 10, 2002
Jun 10, 2002
150
151
152
153
154
155
156
157
158
159
160
/* drop through and clean up the rest of the stuff... */
ntinit_succeeded:
if (lib != NULL)
FreeLibrary(lib);
if (AccessTokenHandle != NULL)
CloseHandle(AccessTokenHandle);
return ((err == NULL) ? 1 : 0);
} /* doNTInit */
Jun 7, 2002
Jun 7, 2002
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
static BOOL MediaInDrive(const char *DriveLetter)
{
UINT OldErrorMode;
DWORD DummyValue;
BOOL ReturnValue;
/* Prevent windows warning message to appear when checking media size */
OldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
/* If this function succeeds, there's media in the drive */
ReturnValue = GetDiskFreeSpace(DriveLetter, &DummyValue, &DummyValue, &DummyValue, &DummyValue);
/* Revert back to old windows error handler */
SetErrorMode(OldErrorMode);
return ReturnValue;
Jun 10, 2002
Jun 10, 2002
178
} /* MediaInDrive */
Aug 23, 2001
Aug 23, 2001
179
180
181
182
183
184
185
186
187
char **__PHYSFS_platformDetectAvailableCDs(void)
{
char **retval = (char **) malloc(sizeof (char *));
int cd_count = 1; /* We count the NULL entry. */
char drive_str[4] = "x:\\";
for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
{
Jun 7, 2002
Jun 7, 2002
188
if (GetDriveType(drive_str) == DRIVE_CDROM && MediaInDrive(drive_str))
Aug 23, 2001
Aug 23, 2001
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{
char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
if (tmp)
{
retval = tmp;
retval[cd_count-1] = (char *) malloc(4);
if (retval[cd_count-1])
{
strcpy(retval[cd_count-1], drive_str);
cd_count++;
} /* if */
} /* if */
} /* if */
} /* for */
retval[cd_count - 1] = NULL;
return(retval);
} /* __PHYSFS_detectAvailableCDs */
Mar 21, 2002
Mar 21, 2002
209
static char *getExePath(const char *argv0)
Aug 23, 2001
Aug 23, 2001
210
{
Mar 24, 2002
Mar 24, 2002
211
char *filepart = NULL;
Jun 10, 2002
Jun 10, 2002
212
213
214
215
216
217
218
219
220
221
222
223
224
char *retval;
DWORD buflen;
retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
if (buflen == 0)
{
const char *err = win32strerror();
free(retval);
BAIL_MACRO(err, NULL);
} /* if */
Aug 29, 2001
Aug 29, 2001
225
226
227
228
229
230
231
232
233
234
235
retval[buflen] = '\0'; /* does API always null-terminate the string? */
/* make sure the string was not truncated. */
if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") == 0)
{
char *ptr = strrchr(retval, '\\');
if (ptr != NULL)
{
*(ptr + 1) = '\0'; /* chop off filename. */
/* free up the bytes we didn't actually use. */
Jun 10, 2002
Jun 10, 2002
236
237
238
239
240
ptr = (char *) realloc(retval, strlen(retval) + 1);
if (ptr != NULL)
retval = ptr;
return(retval);
Aug 29, 2001
Aug 29, 2001
241
242
243
244
} /* if */
} /* if */
/* if any part of the previous approach failed, try SearchPath()... */
Aug 23, 2001
Aug 23, 2001
245
buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
Aug 29, 2001
Aug 29, 2001
246
retval = (char *) realloc(retval, buflen);
Aug 23, 2001
Aug 23, 2001
247
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
Aug 23, 2001
Aug 23, 2001
248
SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
Aug 29, 2001
Aug 29, 2001
249
Aug 23, 2001
Aug 23, 2001
250
return(retval);
Oct 9, 2001
Oct 9, 2001
251
252
253
254
255
256
257
258
} /* getExePath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
return(NULL);
Mar 21, 2002
Mar 21, 2002
259
return(getExePath(argv0));
Aug 23, 2001
Aug 23, 2001
260
261
262
263
264
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
Aug 23, 2001
Aug 23, 2001
265
DWORD bufsize = 0;
Aug 23, 2001
Aug 23, 2001
266
267
268
269
270
271
272
273
LPTSTR retval = NULL;
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
{
retval = (LPTSTR) malloc(bufsize);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (GetUserName(retval, &bufsize) == 0) /* ?! */
{
Jun 10, 2002
Jun 10, 2002
274
__PHYSFS_setError(win32strerror());
Aug 23, 2001
Aug 23, 2001
275
276
277
278
279
280
281
282
283
284
285
free(retval);
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
Jun 10, 2002
Jun 10, 2002
286
287
288
289
char *retval = (char *) malloc(strlen(ProfileDirectory) + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, ProfileDirectory); /* calculated at init time. */
return retval;
Aug 23, 2001
Aug 23, 2001
290
291
292
} /* __PHYSFS_platformGetUserDir */
Apr 3, 2002
Apr 3, 2002
293
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
Aug 23, 2001
Aug 23, 2001
294
{
Apr 3, 2002
Apr 3, 2002
295
return((PHYSFS_uint64)GetCurrentThreadId());
Aug 23, 2001
Aug 23, 2001
296
297
298
} /* __PHYSFS_platformGetThreadID */
Aug 23, 2001
Aug 23, 2001
299
/* ...make this Cygwin AND Visual C friendly... */
Aug 23, 2001
Aug 23, 2001
300
301
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
Apr 12, 2002
Apr 12, 2002
302
303
304
#if (defined _MSC_VER)
return(stricmp(x, y));
#else
Aug 23, 2001
Aug 23, 2001
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
int ux, uy;
do
{
ux = toupper((int) *x);
uy = toupper((int) *y);
if (ux > uy)
return(1);
else if (ux < uy)
return(-1);
x++;
y++;
} while ((ux) && (uy));
return(0);
Apr 12, 2002
Apr 12, 2002
320
#endif
Aug 23, 2001
Aug 23, 2001
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
} /* __PHYSFS_platformStricmp */
int __PHYSFS_platformExists(const char *fname)
{
return(GetFileAttributes(fname) != 0xffffffff);
} /* __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;
char *retval = malloc(len);
Aug 23, 2001
Aug 23, 2001
350
char *p;
Aug 23, 2001
Aug 23, 2001
351
352
353
354
355
356
357
358
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 */
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks)
{
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
HANDLE dir;
WIN32_FIND_DATA ent;
Jun 7, 2002
Jun 7, 2002
386
char *SearchPath;
Jun 8, 2002
Jun 8, 2002
387
size_t len = strlen(dirname);
Jun 7, 2002
Jun 7, 2002
388
Jun 8, 2002
Jun 8, 2002
389
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
Jun 10, 2002
Jun 10, 2002
390
391
392
SearchPath = (char *) _alloca(len + 3);
BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
Jun 7, 2002
Jun 7, 2002
393
394
/* Copy current dirname */
strcpy(SearchPath, dirname);
Jun 8, 2002
Jun 8, 2002
395
396
/* if there's no '\\' at the end of the path, stick one in there. */
Jun 10, 2002
Jun 10, 2002
397
if (SearchPath[len - 1] != '\\')
Jun 8, 2002
Jun 8, 2002
398
{
Jun 10, 2002
Jun 10, 2002
399
400
SearchPath[len++] = '\\';
SearchPath[len] = '\0';
Jun 8, 2002
Jun 8, 2002
401
402
} /* if */
Jun 7, 2002
Jun 7, 2002
403
404
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");
Aug 23, 2001
Aug 23, 2001
405
Jun 7, 2002
Jun 7, 2002
406
dir = FindFirstFile(SearchPath, &ent);
Aug 23, 2001
Aug 23, 2001
407
408
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
Jun 8, 2002
Jun 8, 2002
409
do
Aug 23, 2001
Aug 23, 2001
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{
if (strcmp(ent.cFileName, ".") == 0)
continue;
if (strcmp(ent.cFileName, "..") == 0)
continue;
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
if (l == NULL)
break;
l->str = (char *) malloc(strlen(ent.cFileName) + 1);
if (l->str == NULL)
{
free(l);
break;
} /* if */
strcpy(l->str, ent.cFileName);
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
Jun 8, 2002
Jun 8, 2002
437
} while (FindNextFile(dir, &ent) != 0);
Aug 23, 2001
Aug 23, 2001
438
439
440
441
442
443
444
445
FindClose(dir);
return(retval);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
Aug 23, 2001
Aug 23, 2001
446
LPTSTR retval;
Aug 23, 2001
Aug 23, 2001
447
448
DWORD buflen = 0;
Aug 23, 2001
Aug 23, 2001
449
buflen = GetCurrentDirectory(buflen, NULL);
Sep 1, 2001
Sep 1, 2001
450
451
retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 23, 2001
Aug 23, 2001
452
GetCurrentDirectory(buflen, retval);
Sep 1, 2001
Sep 1, 2001
453
454
455
456
if (retval[buflen - 2] != '\\')
strcat(retval, "\\");
Aug 23, 2001
Aug 23, 2001
457
458
459
460
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
Sep 1, 2001
Sep 1, 2001
461
/* this could probably use a cleanup. */
Aug 23, 2001
Aug 23, 2001
462
463
char *__PHYSFS_platformRealPath(const char *path)
{
Sep 1, 2001
Sep 1, 2001
464
465
466
467
468
469
470
471
472
473
474
475
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
521
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
char *retval = NULL;
char *p = NULL;
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
retval = (char *) malloc(MAX_PATH);
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)
{
free(retval);
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 */
free(currentDir);
} /* 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... */
p = (char *) realloc(retval, strlen(retval) + 1);
if (p != NULL)
retval = p;
return(retval);
Aug 23, 2001
Aug 23, 2001
579
580
581
582
583
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
Aug 23, 2001
Aug 23, 2001
584
DWORD rc = CreateDirectory(path, NULL);
Aug 23, 2001
Aug 23, 2001
585
586
587
588
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
Jun 10, 2002
Jun 10, 2002
589
Mar 24, 2002
Mar 24, 2002
590
591
592
593
594
595
596
597
598
/*
* Get OS info and save it.
*
* Returns non-zero if successful, otherwise it returns zero on failure.
*/
int getOSInfo(void)
{
/* Get OS info */
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
Jun 10, 2002
Jun 10, 2002
599
BAIL_IF_MACRO(!GetVersionEx(&OSVersionInfo), win32strerror(), 0);
Mar 24, 2002
Mar 24, 2002
600
601
602
603
604
605
606
607
608
609
/* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
runningNT = (OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(OSVersionInfo.dwMajorVersion > 3);
return 1;
}
int __PHYSFS_platformInit(void)
{
Jun 10, 2002
Jun 10, 2002
610
BAIL_IF_MACRO(!getOSInfo(), NULL, 0);
Mar 24, 2002
Mar 24, 2002
611
612
613
614
/* If running an NT system (NT/Win2k/XP, etc...) */
if(runningNT)
{
Jun 10, 2002
Jun 10, 2002
615
616
617
618
619
620
621
622
BAIL_IF_MACRO(!doNTInit(), NULL, 0);
} /* if */
else
{
/* Profile directory is the exe path on 95/98/ME systems. */
ProfileDirectory = getExePath(NULL);
BAIL_IF_MACRO(ProfileDirectory == NULL, win32strerror(), 0);
} /* else */
Mar 24, 2002
Mar 24, 2002
623
Jun 10, 2002
Jun 10, 2002
624
return 1; /* It's all good */
Mar 24, 2002
Mar 24, 2002
625
626
627
628
}
int __PHYSFS_platformDeinit(void)
{
Jun 10, 2002
Jun 10, 2002
629
630
631
632
if (runningNT)
{
BAIL_IF_MACRO(!doNTDeinit(), NULL, 0);
} /* if */
Mar 24, 2002
Mar 24, 2002
633
Jun 10, 2002
Jun 10, 2002
634
635
636
637
638
if (ProfileDirectory != NULL)
{
free(ProfileDirectory);
ProfileDirectory = NULL;
} /* if */
Mar 24, 2002
Mar 24, 2002
639
Jun 10, 2002
Jun 10, 2002
640
return 1; /* It's all good */
Mar 24, 2002
Mar 24, 2002
641
642
}
Apr 3, 2002
Apr 3, 2002
643
644
645
646
647
648
649
650
651
void *__PHYSFS_platformOpenRead(const char *filename)
{
HANDLE FileHandle;
/* Open an existing file for read only. File can be opened by others
who request read access on the file only. */
FileHandle = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Jun 10, 2002
Jun 10, 2002
652
BAIL_IF_MACRO(FileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
Apr 3, 2002
Apr 3, 2002
653
654
655
656
657
658
659
660
661
662
663
664
return (void *)FileHandle;
}
void *__PHYSFS_platformOpenWrite(const char *filename)
{
HANDLE FileHandle;
/* Open an existing file for write only. File can be opened by others
who request read access to the file only */
FileHandle = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Jun 10, 2002
Jun 10, 2002
665
BAIL_IF_MACRO(FileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
Apr 3, 2002
Apr 3, 2002
666
667
668
669
670
671
672
673
674
675
return (void *)FileHandle;
}
void *__PHYSFS_platformOpenAppend(const char *filename)
{
HANDLE FileHandle;
/* Open an existing file for appending only. File can be opened by others
who request read access to the file only. */
FileHandle = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
Jun 10, 2002
Jun 10, 2002
676
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Apr 3, 2002
Apr 3, 2002
677
Jun 10, 2002
Jun 10, 2002
678
BAIL_IF_MACRO(FileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
Apr 3, 2002
Apr 3, 2002
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
return (void *)FileHandle;
}
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
HANDLE FileHandle;
DWORD CountOfBytesRead;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Read data from the file */
/*!!! - uint32 might be a greater # than DWORD */
if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL))
{
Jun 10, 2002
Jun 10, 2002
696
697
BAIL_MACRO(win32strerror(), -1);
} /* if */
Apr 3, 2002
Apr 3, 2002
698
699
700
701
702
else
{
/* Return the number of "objects" read. */
/* !!! - What if not the right amount of bytes was read to make an object? */
retval = CountOfBytesRead / size;
Jun 10, 2002
Jun 10, 2002
703
} /* else */
Apr 3, 2002
Apr 3, 2002
704
705
706
707
return retval;
}
Jun 29, 2002
Jun 29, 2002
708
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
Apr 3, 2002
Apr 3, 2002
709
710
711
712
713
714
715
716
717
718
719
720
721
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
HANDLE FileHandle;
DWORD CountOfBytesWritten;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Read data from the file */
/*!!! - uint32 might be a greater # than DWORD */
if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL))
{
Jun 10, 2002
Jun 10, 2002
722
723
BAIL_MACRO(win32strerror(), -1);
} /* if */
Apr 3, 2002
Apr 3, 2002
724
725
726
727
728
else
{
/* Return the number of "objects" read. */
/*!!! - What if not the right number of bytes was written? */
retval = CountOfBytesWritten / size;
Jun 10, 2002
Jun 10, 2002
729
} /* else */
Apr 3, 2002
Apr 3, 2002
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
return retval;
}
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
HANDLE FileHandle;
int retval;
DWORD HighOrderPos;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Get the high order 32-bits of the position */
HighOrderPos = HIGHORDER_UINT64(pos);
/*!!! SetFilePointer needs a signed 64-bit value. */
/* Move pointer "pos" count from start of file */
if((SetFilePointer(FileHandle, LOWORDER_UINT64(pos), &HighOrderPos, FILE_BEGIN)
== INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
/* An error occured. Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
retval = 0;
}
else
{
/* No error occured */
retval = 1;
}
return retval;
}
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
HANDLE FileHandle;
DWORD HighOrderPos = 0;
DWORD LowOrderPos;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Get current position */
if(((LowOrderPos = SetFilePointer(FileHandle, 0, &HighOrderPos, FILE_CURRENT))
== INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
/* We errored out */
retval = 0;
}
May 8, 2002
May 8, 2002
784
785
786
787
788
789
790
else
{
/* Combine the high/low order to create the 64-bit position value */
retval = HighOrderPos;
retval = retval << 32;
retval |= LowOrderPos;
}
Apr 3, 2002
Apr 3, 2002
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/*!!! Can't find a file pointer routine?!?!?!!?!?*/
return retval;
}
PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle)
{
HANDLE FileHandle;
DWORD FileSizeHigh;
DWORD FileSizeLow;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)handle;
May 8, 2002
May 8, 2002
805
806
/* Get the file size. Condition evaluates to TRUE if an error occured */
Apr 3, 2002
Apr 3, 2002
807
808
809
if(((FileSizeLow = GetFileSize(FileHandle, &FileSizeHigh))
== INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
Jun 10, 2002
Jun 10, 2002
810
811
BAIL_MACRO(win32strerror(), -1);
} /* if */
May 8, 2002
May 8, 2002
812
813
814
815
816
817
else
{
/* Combine the high/low order to create the 64-bit position value */
retval = FileSizeHigh;
retval = retval << 32;
retval |= FileSizeLow;
Jun 10, 2002
Jun 10, 2002
818
} /* else */
Apr 3, 2002
Apr 3, 2002
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
return retval;
}
int __PHYSFS_platformEOF(void *opaque)
{
HANDLE FileHandle;
PHYSFS_sint64 FilePosition;
int retval = 0;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Get the current position in the file */
if((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
{
May 8, 2002
May 8, 2002
835
836
/* Non-zero if EOF is equal to the file length */
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
Apr 3, 2002
Apr 3, 2002
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
}
return retval;
}
int __PHYSFS_platformFlush(void *opaque)
{
HANDLE FileHandle;
int retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Close the file */
if(!(retval = FlushFileBuffers(FileHandle)))
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
}
return retval;
}
int __PHYSFS_platformClose(void *opaque)
{
HANDLE FileHandle;
int retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Close the file */
if(!(retval = CloseHandle(FileHandle)))
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
}
return retval;
}
int __PHYSFS_platformDelete(const char *path)
{
int retval;
/* If filename is a folder */
if(GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
{
retval = RemoveDirectory(path);
}
else
{
retval = DeleteFile(path);
}
if(!retval)
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
}
return retval;
}
void *__PHYSFS_platformCreateMutex(void)
{
return (void *)CreateMutex(NULL, FALSE, NULL);
}
void __PHYSFS_platformDestroyMutex(void *mutex)
{
CloseHandle((HANDLE)mutex);
}
int __PHYSFS_platformGrabMutex(void *mutex)
{
int retval;
if(WaitForSingleObject((HANDLE)mutex, INFINITE) == WAIT_FAILED)
{
/* Our wait failed for some unknown reason */
Jun 10, 2002
Jun 10, 2002
918
retval = 0;
Apr 3, 2002
Apr 3, 2002
919
920
921
922
}
else
{
/* Good to go */
Jun 10, 2002
Jun 10, 2002
923
retval = 1;
Apr 3, 2002
Apr 3, 2002
924
925
926
927
928
929
930
931
932
933
}
return retval;
}
void __PHYSFS_platformReleaseMutex(void *mutex)
{
ReleaseMutex((HANDLE)mutex);
}
Jun 10, 2002
Jun 10, 2002
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
static time_t FileTimeToTimeT(FILETIME *ft)
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
TIME_ZONE_INFORMATION TimeZoneInfo;
struct tm tm;
FileTimeToSystemTime(ft, &st_utc);
GetTimeZoneInformation(&TimeZoneInfo);
SystemTimeToTzSpecificLocalTime(&TimeZoneInfo, &st_utc, &st_localtz);
tm.tm_sec = st_localtz.wSecond;
tm.tm_min = st_localtz.wMinute;
tm.tm_hour = st_localtz.wHour;
tm.tm_mday = st_localtz.wDay;
tm.tm_mon = st_localtz.wMonth - 1;
tm.tm_year = st_localtz.wYear - 1900;
tm.tm_wday = st_localtz.wDayOfWeek;
tm.tm_yday = -1;
tm.tm_isdst = -1;
return mktime(&tm);
} /* FileTimeToTimeT */
Jun 7, 2002
Jun 7, 2002
957
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
May 6, 2002
May 6, 2002
958
{
Jun 7, 2002
Jun 7, 2002
959
WIN32_FILE_ATTRIBUTE_DATA AttributeData;
May 6, 2002
May 6, 2002
960
Jun 7, 2002
Jun 7, 2002
961
962
963
964
GetFileAttributesEx(fname, GetFileExInfoStandard, &AttributeData);
/* 0 return value indicates an error or not supported */
if(AttributeData.ftLastWriteTime.dwHighDateTime == 0 &&
AttributeData.ftLastWriteTime.dwLowDateTime == 0)
May 6, 2002
May 6, 2002
965
{
Jun 7, 2002
Jun 7, 2002
966
/* Return error */
Jun 10, 2002
Jun 10, 2002
967
BAIL_MACRO(win32strerror(), -1);
May 6, 2002
May 6, 2002
968
}
Jun 10, 2002
Jun 10, 2002
969
970
971
972
/* Return UNIX time_t version of last write time */
return (PHYSFS_sint64)FileTimeToTimeT(&AttributeData.ftLastWriteTime);
/*return (PHYSFS_sint64)FileTimeToTimeT(&AttributeData.ftCreationTime);*/
May 25, 2002
May 25, 2002
973
974
} /* __PHYSFS_platformGetLastModTime */
Aug 23, 2001
Aug 23, 2001
975
/* end of win32.c ... */