Skip to content

Latest commit

 

History

History
1091 lines (880 loc) · 30.2 KB

win32.c

File metadata and controls

1091 lines (880 loc) · 30.2 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
Jul 10, 2002
Jul 10, 2002
13
14
#ifdef WIN32
Aug 23, 2001
Aug 23, 2001
15
16
#include <windows.h>
#include <stdio.h>
Aug 29, 2001
Aug 29, 2001
17
#include <stdlib.h>
Jun 29, 2002
Jun 29, 2002
18
19
#include <string.h>
#include <errno.h>
Aug 29, 2001
Aug 29, 2001
20
#include <ctype.h>
Jun 7, 2002
Jun 7, 2002
21
#include <time.h>
Jun 10, 2002
Jun 10, 2002
22
#include <assert.h>
May 6, 2002
May 6, 2002
23
Aug 23, 2001
Aug 23, 2001
24
25
26
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Jun 29, 2002
Jun 29, 2002
27
28
#ifdef _MSC_VER /* for Cygwin, etc. */
#define alloca _alloca
Jun 29, 2002
Jun 29, 2002
29
30
#endif
Apr 3, 2002
Apr 3, 2002
31
32
33
#define LOWORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0x00000000FFFFFFFF)
#define HIGHORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0xFFFFFFFF00000000)
Jun 29, 2002
Jun 29, 2002
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* 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
51
Jun 29, 2002
Jun 29, 2002
52
53
54
55
const char *__PHYSFS_platformDirSeparator = "\\";
static LPFNGETFILEATTRIBUTESEX pGetFileAttributesEx = NULL;
static HANDLE libKernel32 = NULL;
static char *userDir = NULL;
Aug 23, 2001
Aug 23, 2001
56
Jun 29, 2002
Jun 29, 2002
57
58
59
60
61
/*
* Users without the platform SDK don't have this defined. The original docs
* for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
* work as desired
*/
May 6, 2002
May 6, 2002
62
#ifndef INVALID_SET_FILE_POINTER
Jun 29, 2002
Jun 29, 2002
63
# define INVALID_SET_FILE_POINTER 0xFFFFFFFF
May 6, 2002
May 6, 2002
64
65
#endif
Jun 29, 2002
Jun 29, 2002
66
67
68
69
70
71
/* just in case... */
#ifndef INVALID_FILE_ATTRIBUTES
# define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
#endif
Jun 10, 2002
Jun 10, 2002
72
73
74
75
76
77
78
79
80
81
/*
* 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
82
TCHAR *ptr = msgbuf;
Jun 10, 2002
Jun 10, 2002
83
84
85
86
87
88
89
90
91
92
93
94
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
95
96
97
98
99
100
101
102
103
104
/* chop off newlines. */
for (ptr = msgbuf; *ptr; ptr++)
{
if ((*ptr == '\n') || (*ptr == '\r'))
{
*ptr = ' ';
break;
} /* if */
} /* for */
Jun 10, 2002
Jun 10, 2002
105
106
107
108
return((const char *) msgbuf);
} /* win32strerror */
Jun 29, 2002
Jun 29, 2002
109
static char *getExePath(const char *argv0)
Jun 7, 2002
Jun 7, 2002
110
{
Jun 29, 2002
Jun 29, 2002
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
DWORD buflen;
int success = 0;
char *ptr = NULL;
char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
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. */
if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") != 0)
__PHYSFS_setError("WIN32: GetModuleFileName() got truncated.");
else
{
ptr = strrchr(retval, '\\');
if (ptr == NULL)
__PHYSFS_setError("WIN32: GetModuleFileName() had no dir.");
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)
__PHYSFS_setError("WIN32: argv0 is NULL.");
else
{
buflen = SearchPath(NULL, argv0, NULL, MAX_PATH+1, retval, &ptr);
if (buflen == 0)
__PHYSFS_setError(win32strerror());
else if (buflen > MAX_PATH)
__PHYSFS_setError("Win32: SearchPath() got truncated.");
else
success = 1;
} /* else */
} /* if */
if (!success)
{
free(retval);
return(NULL); /* physfs error message will be set, above. */
} /* if */
/* free up the bytes we didn't actually use. */
ptr = (char *) realloc(retval, strlen(retval) + 1);
if (ptr != NULL)
retval = ptr;
return(retval); /* w00t. */
} /* getExePath */
Jun 7, 2002
Jun 7, 2002
173
174
175
/*
Jun 29, 2002
Jun 29, 2002
176
177
178
* 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
179
*
Jun 29, 2002
Jun 29, 2002
180
181
182
183
* 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
184
*/
Jun 29, 2002
Jun 29, 2002
185
static int determineUserDir(void)
Jun 7, 2002
Jun 7, 2002
186
{
Jun 29, 2002
Jun 29, 2002
187
DWORD psize = 0;
Jun 10, 2002
Jun 10, 2002
188
189
char dummy[1];
BOOL rc = 0;
Jun 29, 2002
Jun 29, 2002
190
191
192
193
HANDLE processHandle; /* Current process handle */
HANDLE accessToken = NULL; /* Security handle to process */
LPFNGETUSERPROFILEDIR GetUserProfileDirectory;
HMODULE lib;
Jun 10, 2002
Jun 10, 2002
194
Jun 29, 2002
Jun 29, 2002
195
assert(userDir == NULL);
Jun 10, 2002
Jun 10, 2002
196
Jun 29, 2002
Jun 29, 2002
197
198
199
200
201
202
/*
* 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
203
Jun 29, 2002
Jun 29, 2002
204
205
206
207
208
lib = LoadLibrary("userenv.dll");
if (lib)
{
/* !!! FIXME: Handle Unicode? */
GetUserProfileDirectory = (LPFNGETUSERPROFILEDIR)
Jun 10, 2002
Jun 10, 2002
209
GetProcAddress(lib, "GetUserProfileDirectoryA");
Jun 29, 2002
Jun 29, 2002
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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 */
userDir = (char *) malloc(psize);
if (userDir != NULL)
{
if (!GetUserProfileDirectory(accessToken, userDir, &psize))
{
free(userDir);
userDir = NULL;
} /* if */
} /* else */
} /* if */
Jun 10, 2002
Jun 10, 2002
234
Jun 29, 2002
Jun 29, 2002
235
236
CloseHandle(accessToken);
} /* if */
Jun 10, 2002
Jun 10, 2002
237
Jun 29, 2002
Jun 29, 2002
238
FreeLibrary(lib);
Jun 10, 2002
Jun 10, 2002
239
240
} /* if */
Jun 29, 2002
Jun 29, 2002
241
if (userDir == NULL) /* couldn't get profile for some reason. */
Jun 7, 2002
Jun 7, 2002
242
{
Jun 29, 2002
Jun 29, 2002
243
244
245
/* 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
246
} /* if */
Jun 7, 2002
Jun 7, 2002
247
Jun 29, 2002
Jun 29, 2002
248
249
return(1); /* We made it: hit the showers. */
} /* determineUserDir */
Jun 10, 2002
Jun 10, 2002
250
Jun 7, 2002
Jun 7, 2002
251
Jun 29, 2002
Jun 29, 2002
252
static BOOL mediaInDrive(const char *driveLetter)
Jun 7, 2002
Jun 7, 2002
253
{
Jun 29, 2002
Jun 29, 2002
254
255
256
UINT oldErrorMode;
DWORD dummyValue;
BOOL returnValue;
Jun 7, 2002
Jun 7, 2002
257
258
/* Prevent windows warning message to appear when checking media size */
Jun 29, 2002
Jun 29, 2002
259
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
Jun 7, 2002
Jun 7, 2002
260
261
/* If this function succeeds, there's media in the drive */
Jun 29, 2002
Jun 29, 2002
262
returnValue = GetDiskFreeSpace(driveLetter, &dummyValue, &dummyValue, &dummyValue, &dummyValue);
Jun 7, 2002
Jun 7, 2002
263
264
/* Revert back to old windows error handler */
Jun 29, 2002
Jun 29, 2002
265
266
267
268
SetErrorMode(oldErrorMode);
return(returnValue);
} /* mediaInDrive */
Jun 7, 2002
Jun 7, 2002
269
Aug 23, 2001
Aug 23, 2001
270
271
272
273
274
275
276
277
278
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 29, 2002
Jun 29, 2002
279
if (GetDriveType(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
Aug 23, 2001
Aug 23, 2001
280
{
Jun 29, 2002
Jun 29, 2002
281
char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
Aug 23, 2001
Aug 23, 2001
282
283
284
if (tmp)
{
retval = tmp;
Jun 29, 2002
Jun 29, 2002
285
286
retval[cd_count - 1] = (char *) malloc(4);
if (retval[cd_count - 1])
Aug 23, 2001
Aug 23, 2001
287
{
Jun 29, 2002
Jun 29, 2002
288
strcpy(retval[cd_count - 1], drive_str);
Aug 23, 2001
Aug 23, 2001
289
290
291
292
293
294
295
296
297
298
299
cd_count++;
} /* if */
} /* if */
} /* if */
} /* for */
retval[cd_count - 1] = NULL;
return(retval);
} /* __PHYSFS_detectAvailableCDs */
Oct 9, 2001
Oct 9, 2001
300
301
302
303
304
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
return(NULL);
Mar 21, 2002
Mar 21, 2002
305
return(getExePath(argv0));
Aug 23, 2001
Aug 23, 2001
306
307
308
309
310
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
Aug 23, 2001
Aug 23, 2001
311
DWORD bufsize = 0;
Aug 23, 2001
Aug 23, 2001
312
313
314
315
316
317
318
319
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
320
__PHYSFS_setError(win32strerror());
Aug 23, 2001
Aug 23, 2001
321
322
323
324
325
326
327
328
329
330
331
free(retval);
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
Jun 29, 2002
Jun 29, 2002
332
char *retval = (char *) malloc(strlen(userDir) + 1);
Jun 10, 2002
Jun 10, 2002
333
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Jun 29, 2002
Jun 29, 2002
334
335
strcpy(retval, userDir); /* calculated at init time. */
return(retval);
Aug 23, 2001
Aug 23, 2001
336
337
338
} /* __PHYSFS_platformGetUserDir */
Apr 3, 2002
Apr 3, 2002
339
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
Aug 23, 2001
Aug 23, 2001
340
{
Jun 29, 2002
Jun 29, 2002
341
return((PHYSFS_uint64) GetCurrentThreadId());
Aug 23, 2001
Aug 23, 2001
342
343
344
} /* __PHYSFS_platformGetThreadID */
Aug 23, 2001
Aug 23, 2001
345
/* ...make this Cygwin AND Visual C friendly... */
Aug 23, 2001
Aug 23, 2001
346
347
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
Apr 12, 2002
Apr 12, 2002
348
349
350
#if (defined _MSC_VER)
return(stricmp(x, y));
#else
Aug 23, 2001
Aug 23, 2001
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
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
366
#endif
Aug 23, 2001
Aug 23, 2001
367
368
369
370
371
} /* __PHYSFS_platformStricmp */
int __PHYSFS_platformExists(const char *fname)
{
Jun 29, 2002
Jun 29, 2002
372
return(GetFileAttributes(fname) != INVALID_FILE_ATTRIBUTES);
Aug 23, 2001
Aug 23, 2001
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
} /* __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
396
char *p;
Aug 23, 2001
Aug 23, 2001
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
432
char *SearchPath;
Jun 8, 2002
Jun 8, 2002
433
size_t len = strlen(dirname);
Jun 7, 2002
Jun 7, 2002
434
Jun 8, 2002
Jun 8, 2002
435
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
Jun 29, 2002
Jun 29, 2002
436
SearchPath = (char *) alloca(len + 3);
Jun 10, 2002
Jun 10, 2002
437
438
BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
Jun 7, 2002
Jun 7, 2002
439
440
/* Copy current dirname */
strcpy(SearchPath, dirname);
Jun 8, 2002
Jun 8, 2002
441
442
/* if there's no '\\' at the end of the path, stick one in there. */
Jun 10, 2002
Jun 10, 2002
443
if (SearchPath[len - 1] != '\\')
Jun 8, 2002
Jun 8, 2002
444
{
Jun 10, 2002
Jun 10, 2002
445
446
SearchPath[len++] = '\\';
SearchPath[len] = '\0';
Jun 8, 2002
Jun 8, 2002
447
448
} /* if */
Jun 7, 2002
Jun 7, 2002
449
450
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");
Aug 23, 2001
Aug 23, 2001
451
Jun 7, 2002
Jun 7, 2002
452
dir = FindFirstFile(SearchPath, &ent);
Aug 23, 2001
Aug 23, 2001
453
454
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
Jun 8, 2002
Jun 8, 2002
455
do
Aug 23, 2001
Aug 23, 2001
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
{
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
483
} while (FindNextFile(dir, &ent) != 0);
Aug 23, 2001
Aug 23, 2001
484
485
486
487
488
489
490
491
FindClose(dir);
return(retval);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
Aug 23, 2001
Aug 23, 2001
492
LPTSTR retval;
Aug 23, 2001
Aug 23, 2001
493
494
DWORD buflen = 0;
Aug 23, 2001
Aug 23, 2001
495
buflen = GetCurrentDirectory(buflen, NULL);
Sep 1, 2001
Sep 1, 2001
496
497
retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 23, 2001
Aug 23, 2001
498
GetCurrentDirectory(buflen, retval);
Sep 1, 2001
Sep 1, 2001
499
500
501
502
if (retval[buflen - 2] != '\\')
strcat(retval, "\\");
Aug 23, 2001
Aug 23, 2001
503
504
505
506
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
Sep 1, 2001
Sep 1, 2001
507
/* this could probably use a cleanup. */
Aug 23, 2001
Aug 23, 2001
508
509
char *__PHYSFS_platformRealPath(const char *path)
{
Sep 1, 2001
Sep 1, 2001
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
625
626
627
628
629
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
Aug 23, 2001
Aug 23, 2001
630
DWORD rc = CreateDirectory(path, NULL);
Aug 23, 2001
Aug 23, 2001
631
632
633
634
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
Jun 10, 2002
Jun 10, 2002
635
Jun 29, 2002
Jun 29, 2002
636
637
/*
* Get OS info and save the important parts.
Mar 24, 2002
Mar 24, 2002
638
639
640
*
* Returns non-zero if successful, otherwise it returns zero on failure.
*/
Jun 29, 2002
Jun 29, 2002
641
static int getOSInfo(void)
Mar 24, 2002
Mar 24, 2002
642
{
Jun 29, 2002
Jun 29, 2002
643
644
#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
645
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
Jun 10, 2002
Jun 10, 2002
646
BAIL_IF_MACRO(!GetVersionEx(&OSVersionInfo), win32strerror(), 0);
Mar 24, 2002
Mar 24, 2002
647
648
/* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
Jun 29, 2002
Jun 29, 2002
649
650
651
runningNT = ((OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(OSVersionInfo.dwMajorVersion >= 4));
#endif
Mar 24, 2002
Mar 24, 2002
652
Jun 29, 2002
Jun 29, 2002
653
654
return(1);
} /* getOSInfo */
Mar 24, 2002
Mar 24, 2002
655
Jun 29, 2002
Jun 29, 2002
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* 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
670
{
Jun 29, 2002
Jun 29, 2002
671
672
/* !!! FIXME: Make this table driven? */
int allNeededLibrariesLoaded = 1; /* flip to zero as needed. */
Mar 24, 2002
Mar 24, 2002
673
Jun 29, 2002
Jun 29, 2002
674
675
libKernel32 = LoadLibrary("kernel32.dll");
if (libKernel32)
Mar 24, 2002
Mar 24, 2002
676
{
Jun 29, 2002
Jun 29, 2002
677
678
pGetFileAttributesEx = (LPFNGETFILEATTRIBUTESEX)
GetProcAddress(libKernel32, "GetFileAttributesExA");
Jun 10, 2002
Jun 10, 2002
679
} /* if */
Jun 29, 2002
Jun 29, 2002
680
681
682
683
684
685
/* add other DLLs here... */
/* see if there's any reason to keep kernel32.dll around... */
if (libKernel32)
Jun 10, 2002
Jun 10, 2002
686
{
Jun 29, 2002
Jun 29, 2002
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
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
706
707
708
709
int __PHYSFS_platformDeinit(void)
{
Jun 29, 2002
Jun 29, 2002
710
if (userDir != NULL)
Jun 10, 2002
Jun 10, 2002
711
{
Jun 29, 2002
Jun 29, 2002
712
713
free(userDir);
userDir = NULL;
Jun 10, 2002
Jun 10, 2002
714
} /* if */
Mar 24, 2002
Mar 24, 2002
715
Jun 29, 2002
Jun 29, 2002
716
717
718
719
720
721
if (libKernel32)
{
FreeLibrary(libKernel32);
libKernel32 = NULL;
} /* if */
Jun 29, 2002
Jun 29, 2002
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
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);
BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
retval = malloc(sizeof (win32file));
if (retval == NULL)
Jun 10, 2002
Jun 10, 2002
738
{
Jun 29, 2002
Jun 29, 2002
739
740
CloseHandle(fileHandle);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
Jun 10, 2002
Jun 10, 2002
741
} /* if */
Mar 24, 2002
Mar 24, 2002
742
Jun 29, 2002
Jun 29, 2002
743
744
745
746
747
retval->readonly = rdonly;
retval->handle = fileHandle;
return(retval);
} /* doOpen */
Mar 24, 2002
Mar 24, 2002
748
Apr 3, 2002
Apr 3, 2002
749
750
void *__PHYSFS_platformOpenRead(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
751
752
return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
} /* __PHYSFS_platformOpenRead */
Apr 3, 2002
Apr 3, 2002
753
754
755
756
void *__PHYSFS_platformOpenWrite(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
757
758
return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
} /* __PHYSFS_platformOpenWrite */
Apr 3, 2002
Apr 3, 2002
759
760
761
762
void *__PHYSFS_platformOpenAppend(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
if (retval != NULL)
{
HANDLE h = ((win32file *) retval)->handle;
if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
{
const char *err = win32strerror();
CloseHandle(h);
free(retval);
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
return(retval);
} /* __PHYSFS_platformOpenAppend */
Apr 3, 2002
Apr 3, 2002
778
779
780
781
782
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
Jun 29, 2002
Jun 29, 2002
783
HANDLE FileHandle = ((win32file *) opaque)->handle;
Apr 3, 2002
Apr 3, 2002
784
785
786
787
788
789
790
DWORD CountOfBytesRead;
PHYSFS_sint64 retval;
/* 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
791
792
BAIL_MACRO(win32strerror(), -1);
} /* if */
Apr 3, 2002
Apr 3, 2002
793
794
795
796
797
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
798
} /* else */
Apr 3, 2002
Apr 3, 2002
799
Jun 29, 2002
Jun 29, 2002
800
801
802
return(retval);
} /* __PHYSFS_platformRead */
Apr 3, 2002
Apr 3, 2002
803
Jun 29, 2002
Jun 29, 2002
804
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
Apr 3, 2002
Apr 3, 2002
805
806
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
Jun 29, 2002
Jun 29, 2002
807
HANDLE FileHandle = ((win32file *) opaque)->handle;
Apr 3, 2002
Apr 3, 2002
808
809
810
811
812
813
814
DWORD CountOfBytesWritten;
PHYSFS_sint64 retval;
/* 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
815
816
BAIL_MACRO(win32strerror(), -1);
} /* if */
Apr 3, 2002
Apr 3, 2002
817
818
819
820
821
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
822
} /* else */
Apr 3, 2002
Apr 3, 2002
823
Jun 29, 2002
Jun 29, 2002
824
825
826
return(retval);
} /* __PHYSFS_platformWrite */
Apr 3, 2002
Apr 3, 2002
827
828
829
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
Jun 29, 2002
Jun 29, 2002
830
HANDLE FileHandle = ((win32file *) opaque)->handle;
Apr 3, 2002
Apr 3, 2002
831
DWORD HighOrderPos;
Jun 29, 2002
Jun 29, 2002
832
DWORD rc;
Apr 3, 2002
Apr 3, 2002
833
834
835
836
837
838
/* 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 */
Jun 29, 2002
Jun 29, 2002
839
840
rc = SetFilePointer(FileHandle, LOWORDER_UINT64(pos),
&HighOrderPos, FILE_BEGIN);
Apr 3, 2002
Apr 3, 2002
841
Jun 29, 2002
Jun 29, 2002
842
843
844
845
846
if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
BAIL_MACRO(win32strerror(), 0);
return(1); /* No error occured */
} /* __PHYSFS_platformSeek */
Apr 3, 2002
Apr 3, 2002
847
848
849
850
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
Jun 29, 2002
Jun 29, 2002
851
852
853
HANDLE FileHandle = ((win32file *) opaque)->handle;
DWORD HighPos = 0;
DWORD LowPos;
Apr 3, 2002
Apr 3, 2002
854
855
856
PHYSFS_sint64 retval;
/* Get current position */
Jun 29, 2002
Jun 29, 2002
857
858
LowPos = SetFilePointer(FileHandle, 0, &HighPos, FILE_CURRENT);
if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
Apr 3, 2002
Apr 3, 2002
859
{
Jun 29, 2002
Jun 29, 2002
860
861
BAIL_MACRO(win32strerror(), 0);
} /* if */
May 8, 2002
May 8, 2002
862
863
864
else
{
/* Combine the high/low order to create the 64-bit position value */
Jun 29, 2002
Jun 29, 2002
865
866
867
868
869
870
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
assert(retval >= 0);
} /* else */
return(retval);
} /* __PHYSFS_platformTell */
Apr 3, 2002
Apr 3, 2002
871
872
Jun 29, 2002
Jun 29, 2002
873
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
Apr 3, 2002
Apr 3, 2002
874
{
Jun 29, 2002
Jun 29, 2002
875
876
877
HANDLE FileHandle = ((win32file *) opaque)->handle;
DWORD SizeHigh;
DWORD SizeLow;
Apr 3, 2002
Apr 3, 2002
878
879
PHYSFS_sint64 retval;
Jun 29, 2002
Jun 29, 2002
880
881
SizeLow = GetFileSize(FileHandle, &SizeHigh);
if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
Apr 3, 2002
Apr 3, 2002
882
{
Jun 10, 2002
Jun 10, 2002
883
884
BAIL_MACRO(win32strerror(), -1);
} /* if */
May 8, 2002
May 8, 2002
885
886
887
else
{
/* Combine the high/low order to create the 64-bit position value */
Jun 29, 2002
Jun 29, 2002
888
889
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
assert(retval >= 0);
Jun 10, 2002
Jun 10, 2002
890
} /* else */
Apr 3, 2002
Apr 3, 2002
891
Jun 29, 2002
Jun 29, 2002
892
893
894
return(retval);
} /* __PHYSFS_platformFileLength */
Apr 3, 2002
Apr 3, 2002
895
896
897
898
899
900
901
int __PHYSFS_platformEOF(void *opaque)
{
PHYSFS_sint64 FilePosition;
int retval = 0;
/* Get the current position in the file */
Jun 29, 2002
Jun 29, 2002
902
if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
Apr 3, 2002
Apr 3, 2002
903
{
May 8, 2002
May 8, 2002
904
905
/* Non-zero if EOF is equal to the file length */
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
Jun 29, 2002
Jun 29, 2002
906
907
908
909
} /* if */
return(retval);
} /* __PHYSFS_platformEOF */
Apr 3, 2002
Apr 3, 2002
910
911
912
913
int __PHYSFS_platformFlush(void *opaque)
{
Jun 29, 2002
Jun 29, 2002
914
915
916
win32file *fh = ((win32file *) opaque);
if (!fh->readonly)
BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
Apr 3, 2002
Apr 3, 2002
917
Jun 29, 2002
Jun 29, 2002
918
919
return(1);
} /* __PHYSFS_platformFlush */
Apr 3, 2002
Apr 3, 2002
920
921
922
923
int __PHYSFS_platformClose(void *opaque)
{
Jun 29, 2002
Jun 29, 2002
924
925
926
927
928
HANDLE FileHandle = ((win32file *) opaque)->handle;
BAIL_IF_MACRO(!CloseHandle(FileHandle), win32strerror(), 0);
free(opaque);
return(1);
} /* __PHYSFS_platformClose */
Apr 3, 2002
Apr 3, 2002
929
930
931
932
933
int __PHYSFS_platformDelete(const char *path)
{
/* If filename is a folder */
Jun 29, 2002
Jun 29, 2002
934
if (GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
Apr 3, 2002
Apr 3, 2002
935
{
Jun 29, 2002
Jun 29, 2002
936
937
BAIL_IF_MACRO(!RemoveDirectory(path), win32strerror(), 0);
} /* if */
Apr 3, 2002
Apr 3, 2002
938
939
else
{
Jun 29, 2002
Jun 29, 2002
940
941
BAIL_IF_MACRO(!DeleteFile(path), win32strerror(), 0);
} /* else */
Apr 3, 2002
Apr 3, 2002
942
Jun 29, 2002
Jun 29, 2002
943
944
return(1); /* if you got here, it worked. */
} /* __PHYSFS_platformDelete */
Apr 3, 2002
Apr 3, 2002
945
946
947
948
void *__PHYSFS_platformCreateMutex(void)
{
Jun 29, 2002
Jun 29, 2002
949
950
951
return((void *) CreateMutex(NULL, FALSE, NULL));
} /* __PHYSFS_platformCreateMutex */
Apr 3, 2002
Apr 3, 2002
952
953
954
void __PHYSFS_platformDestroyMutex(void *mutex)
{
Jun 29, 2002
Jun 29, 2002
955
956
957
CloseHandle((HANDLE) mutex);
} /* __PHYSFS_platformDestroyMutex */
Apr 3, 2002
Apr 3, 2002
958
959
960
int __PHYSFS_platformGrabMutex(void *mutex)
{
Jun 29, 2002
Jun 29, 2002
961
962
return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
} /* __PHYSFS_platformGrabMutex */
Apr 3, 2002
Apr 3, 2002
963
964
965
966
void __PHYSFS_platformReleaseMutex(void *mutex)
{
Jun 29, 2002
Jun 29, 2002
967
968
969
ReleaseMutex((HANDLE) mutex);
} /* __PHYSFS_platformReleaseMutex */
Apr 3, 2002
Apr 3, 2002
970
Jun 29, 2002
Jun 29, 2002
971
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
Jun 10, 2002
Jun 10, 2002
972
973
974
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
Jun 29, 2002
Jun 29, 2002
975
976
977
TIME_ZONE_INFORMATION tzi;
DWORD tzid;
PHYSFS_sint64 retval;
Jun 10, 2002
Jun 10, 2002
978
979
struct tm tm;
Jun 29, 2002
Jun 29, 2002
980
981
982
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
983
Jun 29, 2002
Jun 29, 2002
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... */