Skip to content

Latest commit

 

History

History
1439 lines (1177 loc) · 42.6 KB

platform_windows.c

File metadata and controls

1439 lines (1177 loc) · 42.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
Mar 26, 2007
Mar 26, 2007
14
15
16
17
18
/* Forcibly disable UNICODE, since we manage this ourselves. */
#ifdef UNICODE
#undef UNICODE
#endif
Aug 23, 2001
Aug 23, 2001
19
20
#include <windows.h>
#include <stdio.h>
Aug 29, 2001
Aug 29, 2001
21
#include <stdlib.h>
Jun 29, 2002
Jun 29, 2002
22
23
#include <string.h>
#include <errno.h>
Aug 29, 2001
Aug 29, 2001
24
#include <ctype.h>
Jun 7, 2002
Jun 7, 2002
25
#include <time.h>
May 6, 2002
May 6, 2002
26
Aug 23, 2001
Aug 23, 2001
27
28
#include "physfs_internal.h"
Oct 1, 2007
Oct 1, 2007
29
30
#define LOWORDER_UINT64(pos) ((PHYSFS_uint32) (pos & 0xFFFFFFFF))
#define HIGHORDER_UINT64(pos) ((PHYSFS_uint32) ((pos >> 32) & 0xFFFFFFFF))
Apr 3, 2002
Apr 3, 2002
31
Mar 25, 2007
Mar 25, 2007
32
33
34
35
36
37
/*
* 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.
*/
#define PHYSFS_INVALID_SET_FILE_POINTER 0xFFFFFFFF
Mar 25, 2007
Mar 25, 2007
38
Mar 25, 2007
Mar 25, 2007
39
40
/* just in case... */
#define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
Jun 29, 2002
Jun 29, 2002
41
Apr 1, 2007
Apr 1, 2007
42
43
44
45
/* Not defined before the Vista SDK. */
#define PHYSFS_IO_REPARSE_TAG_SYMLINK 0xA000000C
Mar 25, 2007
Mar 25, 2007
46
47
48
49
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
if (str == NULL) \
w_assignto = NULL; \
else { \
Jan 22, 2008
Jan 22, 2008
50
const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
Mar 26, 2007
Mar 26, 2007
51
52
53
w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
if (w_assignto != NULL) \
PHYSFS_utf8ToUcs2(str, (PHYSFS_uint16 *) w_assignto, len); \
Mar 25, 2007
Mar 25, 2007
54
55
} \
} \
Jun 29, 2002
Jun 29, 2002
56
Mar 26, 2007
Mar 26, 2007
57
58
59
60
61
static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
{
PHYSFS_uint64 len = 0;
while (*(wstr++))
len++;
Jan 28, 2010
Jan 28, 2010
62
return len;
Mar 26, 2007
Mar 26, 2007
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
} /* wStrLen */
static char *unicodeToUtf8Heap(const WCHAR *w_str)
{
char *retval = NULL;
if (w_str != NULL)
{
void *ptr = NULL;
const PHYSFS_uint64 len = (wStrLen(w_str) * 4) + 1;
retval = allocator.Malloc(len);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
PHYSFS_utf8FromUcs2((const PHYSFS_uint16 *) w_str, retval, len);
ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
if (ptr != NULL)
retval = (char *) ptr;
} /* if */
Jan 28, 2010
Jan 28, 2010
79
return retval;
Mar 26, 2007
Mar 26, 2007
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
} /* unicodeToUtf8Heap */
static char *codepageToUtf8Heap(const char *cpstr)
{
char *retval = NULL;
if (cpstr != NULL)
{
const int len = (int) (strlen(cpstr) + 1);
WCHAR *wbuf = (WCHAR *) __PHYSFS_smallAlloc(len * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, len, wbuf, len);
retval = (char *) allocator.Malloc(len * 4);
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
else
PHYSFS_utf8FromUcs2(wbuf, retval, len * 4);
__PHYSFS_smallFree(wbuf);
} /* if */
Jan 28, 2010
Jan 28, 2010
99
return retval;
Mar 26, 2007
Mar 26, 2007
100
101
102
} /* codepageToUtf8Heap */
Jun 29, 2002
Jun 29, 2002
103
104
105
106
typedef struct
{
HANDLE handle;
int readonly;
Mar 26, 2007
Mar 26, 2007
107
} WinApiFile;
Aug 23, 2001
Aug 23, 2001
108
Mar 26, 2007
Mar 26, 2007
109
Mar 26, 2007
Mar 26, 2007
110
static char *userDir = NULL;
Mar 26, 2007
Mar 26, 2007
111
static int osHasUnicode = 0;
Mar 25, 2007
Mar 25, 2007
112
113
114
/* pointers for APIs that may not exist on some Windows versions... */
Jun 29, 2002
Jun 29, 2002
115
static HANDLE libKernel32 = NULL;
Mar 25, 2007
Mar 25, 2007
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
static HANDLE libUserEnv = NULL;
static HANDLE libAdvApi32 = NULL;
static DWORD (WINAPI *pGetModuleFileNameW)(HMODULE, LPWCH, DWORD);
static BOOL (WINAPI *pGetUserProfileDirectoryW)(HANDLE, LPWSTR, LPDWORD);
static BOOL (WINAPI *pGetUserNameW)(LPWSTR, LPDWORD);
static DWORD (WINAPI *pGetFileAttributesW)(LPCWSTR);
static HANDLE (WINAPI *pFindFirstFileW)(LPCWSTR, LPWIN32_FIND_DATAW);
static BOOL (WINAPI *pFindNextFileW)(HANDLE, LPWIN32_FIND_DATAW);
static DWORD (WINAPI *pGetCurrentDirectoryW)(DWORD, LPWSTR);
static BOOL (WINAPI *pDeleteFileW)(LPCWSTR);
static BOOL (WINAPI *pRemoveDirectoryW)(LPCWSTR);
static BOOL (WINAPI *pCreateDirectoryW)(LPCWSTR, LPSECURITY_ATTRIBUTES);
static BOOL (WINAPI *pGetFileAttributesExA)
(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
static BOOL (WINAPI *pGetFileAttributesExW)
(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
static DWORD (WINAPI *pFormatMessageW)
(DWORD, LPCVOID, DWORD, DWORD, LPWSTR, DWORD, va_list *);
static HANDLE (WINAPI *pCreateFileW)
(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
Mar 26, 2007
Mar 26, 2007
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Fallbacks for missing Unicode functions on Win95/98/ME. These are filled
* into the function pointers if looking up the real Unicode entry points
* in the system DLLs fails, so they're never used on WinNT/XP/Vista/etc.
* They make an earnest effort to convert to/from UTF-8 and UCS-2 to
* the user's current codepage.
*/
static BOOL WINAPI fallbackGetUserNameW(LPWSTR buf, LPDWORD len)
{
const DWORD cplen = *len;
char *cpstr = __PHYSFS_smallAlloc(cplen);
BOOL retval = GetUserNameA(cpstr, len);
if (buf != NULL)
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, cplen, buf, *len);
__PHYSFS_smallFree(cpstr);
Jan 28, 2010
Jan 28, 2010
154
return retval;
Mar 26, 2007
Mar 26, 2007
155
156
157
158
159
160
161
162
163
164
165
166
167
} /* fallbackGetUserNameW */
static DWORD WINAPI fallbackFormatMessageW(DWORD dwFlags, LPCVOID lpSource,
DWORD dwMessageId, DWORD dwLangId,
LPWSTR lpBuf, DWORD nSize,
va_list *Arguments)
{
char *cpbuf = (char *) __PHYSFS_smallAlloc(nSize);
DWORD retval = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLangId,
cpbuf, nSize, Arguments);
if (retval > 0)
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
__PHYSFS_smallFree(cpbuf);
Jan 28, 2010
Jan 28, 2010
168
return retval;
Mar 26, 2007
Mar 26, 2007
169
170
171
172
173
174
175
176
177
178
} /* fallbackFormatMessageW */
static DWORD WINAPI fallbackGetModuleFileNameW(HMODULE hMod, LPWCH lpBuf,
DWORD nSize)
{
char *cpbuf = (char *) __PHYSFS_smallAlloc(nSize);
DWORD retval = GetModuleFileNameA(hMod, cpbuf, nSize);
if (retval > 0)
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
__PHYSFS_smallFree(cpbuf);
Jan 28, 2010
Jan 28, 2010
179
return retval;
Mar 26, 2007
Mar 26, 2007
180
181
182
183
184
185
186
187
188
189
} /* fallbackGetModuleFileNameW */
static DWORD WINAPI fallbackGetFileAttributesW(LPCWSTR fname)
{
DWORD retval = 0;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = GetFileAttributesA(cpstr);
__PHYSFS_smallFree(cpstr);
Jan 28, 2010
Jan 28, 2010
190
return retval;
Mar 26, 2007
Mar 26, 2007
191
192
193
194
195
196
197
198
199
200
201
202
203
204
} /* fallbackGetFileAttributesW */
static DWORD WINAPI fallbackGetCurrentDirectoryW(DWORD buflen, LPWSTR buf)
{
DWORD retval = 0;
char *cpbuf = NULL;
if (buf != NULL)
cpbuf = (char *) __PHYSFS_smallAlloc(buflen);
retval = GetCurrentDirectoryA(buflen, cpbuf);
if (cpbuf != NULL)
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,buf,buflen);
__PHYSFS_smallFree(cpbuf);
} /* if */
Jan 28, 2010
Jan 28, 2010
205
return retval;
Mar 26, 2007
Mar 26, 2007
206
207
208
209
210
211
212
213
214
215
} /* fallbackGetCurrentDirectoryW */
static BOOL WINAPI fallbackRemoveDirectoryW(LPCWSTR dname)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(dname) + 1);
char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
retval = RemoveDirectoryA(cpstr);
__PHYSFS_smallFree(cpstr);
Jan 28, 2010
Jan 28, 2010
216
return retval;
Mar 26, 2007
Mar 26, 2007
217
218
219
220
221
222
223
224
225
226
227
} /* fallbackRemoveDirectoryW */
static BOOL WINAPI fallbackCreateDirectoryW(LPCWSTR dname,
LPSECURITY_ATTRIBUTES attr)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(dname) + 1);
char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
retval = CreateDirectoryA(cpstr, attr);
__PHYSFS_smallFree(cpstr);
Jan 28, 2010
Jan 28, 2010
228
return retval;
Mar 26, 2007
Mar 26, 2007
229
230
231
232
233
234
235
236
237
238
} /* fallbackCreateDirectoryW */
static BOOL WINAPI fallbackDeleteFileW(LPCWSTR fname)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = DeleteFileA(cpstr);
__PHYSFS_smallFree(cpstr);
Jan 28, 2010
Jan 28, 2010
239
return retval;
Mar 26, 2007
Mar 26, 2007
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
} /* fallbackDeleteFileW */
static HANDLE WINAPI fallbackCreateFileW(LPCWSTR fname,
DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttrs,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttrs, HANDLE hTemplFile)
{
HANDLE retval;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = CreateFileA(cpstr, dwDesiredAccess, dwShareMode, lpSecurityAttrs,
dwCreationDisposition, dwFlagsAndAttrs, hTemplFile);
__PHYSFS_smallFree(cpstr);
Jan 28, 2010
Jan 28, 2010
255
return retval;
Mar 26, 2007
Mar 26, 2007
256
} /* fallbackCreateFileW */
Aug 23, 2001
Aug 23, 2001
257
May 6, 2002
May 6, 2002
258
Mar 12, 2008
Mar 12, 2008
259
#if (PHYSFS_MINIMUM_GCC_VERSION(3,3))
Mar 12, 2008
Mar 12, 2008
260
261
262
263
264
265
266
267
typedef FARPROC __attribute__((__may_alias__)) PHYSFS_FARPROC;
#else
typedef FARPROC PHYSFS_FARPROC;
#endif
static void symLookup(HMODULE dll, PHYSFS_FARPROC *addr, const char *sym,
int reallyLook, PHYSFS_FARPROC fallback)
Mar 25, 2007
Mar 25, 2007
268
{
Mar 12, 2008
Mar 12, 2008
269
270
271
272
273
PHYSFS_FARPROC proc;
proc = (PHYSFS_FARPROC) ((reallyLook) ? GetProcAddress(dll, sym) : NULL);
if (proc == NULL)
proc = fallback; /* may also be NULL. */
*addr = proc;
Mar 25, 2007
Mar 25, 2007
274
275
276
277
278
279
280
} /* symLookup */
static int findApiSymbols(void)
{
HMODULE dll = NULL;
Mar 12, 2008
Mar 12, 2008
281
282
283
284
285
286
#define LOOKUP_NOFALLBACK(x, reallyLook) \
symLookup(dll, (PHYSFS_FARPROC *) &p##x, #x, reallyLook, NULL)
#define LOOKUP(x, reallyLook) \
symLookup(dll, (PHYSFS_FARPROC *) &p##x, #x, \
reallyLook, (PHYSFS_FARPROC) fallback##x)
Mar 25, 2007
Mar 25, 2007
287
Mar 26, 2007
Mar 26, 2007
288
289
290
/* Apparently Win9x HAS the Unicode entry points, they just don't WORK. */
/* ...so don't look them up unless we're on NT+. (see osHasUnicode.) */
Mar 26, 2007
Mar 26, 2007
291
dll = libUserEnv = LoadLibraryA("userenv.dll");
Mar 25, 2007
Mar 25, 2007
292
if (dll != NULL)
Mar 26, 2007
Mar 26, 2007
293
LOOKUP_NOFALLBACK(GetUserProfileDirectoryW, osHasUnicode);
Mar 25, 2007
Mar 25, 2007
294
Mar 26, 2007
Mar 26, 2007
295
dll = libAdvApi32 = LoadLibraryA("advapi32.dll");
Mar 25, 2007
Mar 25, 2007
296
if (dll != NULL)
Mar 26, 2007
Mar 26, 2007
297
LOOKUP(GetUserNameW, osHasUnicode);
Mar 25, 2007
Mar 25, 2007
298
Mar 26, 2007
Mar 26, 2007
299
dll = libKernel32 = LoadLibraryA("kernel32.dll");
Mar 25, 2007
Mar 25, 2007
300
301
if (dll != NULL)
{
Mar 26, 2007
Mar 26, 2007
302
303
304
305
306
307
308
309
310
311
312
313
LOOKUP_NOFALLBACK(GetFileAttributesExA, 1);
LOOKUP_NOFALLBACK(GetFileAttributesExW, osHasUnicode);
LOOKUP_NOFALLBACK(FindFirstFileW, osHasUnicode);
LOOKUP_NOFALLBACK(FindNextFileW, osHasUnicode);
LOOKUP(GetModuleFileNameW, osHasUnicode);
LOOKUP(FormatMessageW, osHasUnicode);
LOOKUP(GetFileAttributesW, osHasUnicode);
LOOKUP(GetCurrentDirectoryW, osHasUnicode);
LOOKUP(CreateDirectoryW, osHasUnicode);
LOOKUP(RemoveDirectoryW, osHasUnicode);
LOOKUP(CreateFileW, osHasUnicode);
LOOKUP(DeleteFileW, osHasUnicode);
Mar 25, 2007
Mar 25, 2007
314
} /* if */
Dec 8, 2003
Dec 8, 2003
315
Mar 26, 2007
Mar 26, 2007
316
#undef LOOKUP_NOFALLBACK
Mar 25, 2007
Mar 25, 2007
317
318
#undef LOOKUP
Jan 28, 2010
Jan 28, 2010
319
return 1;
Mar 25, 2007
Mar 25, 2007
320
} /* findApiSymbols */
Jun 29, 2002
Jun 29, 2002
321
322
Mar 26, 2007
Mar 26, 2007
323
324
325
const char *__PHYSFS_platformDirSeparator = "\\";
Jun 10, 2002
Jun 10, 2002
326
/*
Mar 26, 2007
Mar 26, 2007
327
* Figure out what the last failing Windows API call was, and
Jun 10, 2002
Jun 10, 2002
328
329
330
331
332
* 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.
*/
Mar 26, 2007
Mar 26, 2007
333
static const char *winApiStrError(void)
Jun 10, 2002
Jun 10, 2002
334
{
Mar 26, 2007
Mar 26, 2007
335
336
337
338
339
340
341
342
343
344
static char utf8buf[255];
WCHAR msgbuf[255];
WCHAR *ptr;
DWORD rc = pFormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf, __PHYSFS_ARRAYLEN(msgbuf),
NULL);
Jun 10, 2002
Jun 10, 2002
345
Feb 13, 2008
Feb 13, 2008
346
347
348
if (rc == 0)
msgbuf[0] = '\0'; /* oh well. */
Mar 25, 2007
Mar 25, 2007
349
/* chop off newlines. */
Jun 29, 2002
Jun 29, 2002
350
351
352
353
for (ptr = msgbuf; *ptr; ptr++)
{
if ((*ptr == '\n') || (*ptr == '\r'))
{
Mar 26, 2007
Mar 26, 2007
354
*ptr = '\0';
Jun 29, 2002
Jun 29, 2002
355
356
357
358
break;
} /* if */
} /* for */
Mar 26, 2007
Mar 26, 2007
359
360
/* may truncate, but oh well. */
PHYSFS_utf8FromUcs2((PHYSFS_uint16 *) msgbuf, utf8buf, sizeof (utf8buf));
Jan 28, 2010
Jan 28, 2010
361
return ((const char *) utf8buf);
Mar 26, 2007
Mar 26, 2007
362
} /* winApiStrError */
Jun 10, 2002
Jun 10, 2002
363
364
Mar 25, 2007
Mar 25, 2007
365
static char *getExePath(void)
Jun 7, 2002
Jun 7, 2002
366
{
Mar 25, 2007
Mar 25, 2007
367
368
369
DWORD buflen = 64;
LPWSTR modpath = NULL;
char *retval = NULL;
Jun 29, 2002
Jun 29, 2002
370
Mar 25, 2007
Mar 25, 2007
371
while (1)
Jun 29, 2002
Jun 29, 2002
372
{
Mar 25, 2007
Mar 25, 2007
373
374
DWORD rc;
void *ptr;
Jun 29, 2002
Jun 29, 2002
375
Mar 25, 2007
Mar 25, 2007
376
if ( !(ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) )
Jun 29, 2002
Jun 29, 2002
377
{
Mar 25, 2007
Mar 25, 2007
378
379
380
381
allocator.Free(modpath);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
modpath = (LPWSTR) ptr;
Jun 29, 2002
Jun 29, 2002
382
Mar 25, 2007
Mar 25, 2007
383
384
385
386
rc = pGetModuleFileNameW(NULL, modpath, buflen);
if (rc == 0)
{
allocator.Free(modpath);
Mar 26, 2007
Mar 26, 2007
387
BAIL_MACRO(winApiStrError(), NULL);
Mar 25, 2007
Mar 25, 2007
388
} /* if */
Jun 29, 2002
Jun 29, 2002
389
Mar 25, 2007
Mar 25, 2007
390
391
392
393
394
395
396
397
398
399
if (rc < buflen)
{
buflen = rc;
break;
} /* if */
buflen *= 2;
} /* while */
if (buflen > 0) /* just in case... */
Jun 29, 2002
Jun 29, 2002
400
{
Mar 25, 2007
Mar 25, 2007
401
402
403
404
405
406
407
408
409
410
WCHAR *ptr = (modpath + buflen) - 1;
while (ptr != modpath)
{
if (*ptr == '\\')
break;
ptr--;
} /* while */
if ((ptr == modpath) && (*ptr != '\\'))
__PHYSFS_setError(ERR_GETMODFN_NO_DIR);
Jun 29, 2002
Jun 29, 2002
411
412
else
{
Mar 25, 2007
Mar 25, 2007
413
*(ptr + 1) = '\0'; /* chop off filename. */
Mar 26, 2007
Mar 26, 2007
414
retval = unicodeToUtf8Heap(modpath);
Jun 29, 2002
Jun 29, 2002
415
} /* else */
Mar 25, 2007
Mar 25, 2007
416
417
} /* else */
allocator.Free(modpath);
Jun 29, 2002
Jun 29, 2002
418
Jan 28, 2010
Jan 28, 2010
419
return retval; /* w00t. */
Jun 29, 2002
Jun 29, 2002
420
} /* getExePath */
Jun 7, 2002
Jun 7, 2002
421
422
423
/*
Mar 25, 2007
Mar 25, 2007
424
* Try to make use of GetUserProfileDirectoryW(), which isn't available on
Jun 29, 2002
Jun 29, 2002
425
426
* 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
427
*
Jun 29, 2002
Jun 29, 2002
428
429
430
431
* 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
432
*/
Jun 29, 2002
Jun 29, 2002
433
static int determineUserDir(void)
Jun 7, 2002
Jun 7, 2002
434
{
Mar 25, 2007
Mar 25, 2007
435
if (userDir != NULL)
Jan 28, 2010
Jan 28, 2010
436
return 1; /* already good to go. */
Jun 10, 2002
Jun 10, 2002
437
Jun 29, 2002
Jun 29, 2002
438
/*
Mar 25, 2007
Mar 25, 2007
439
* GetUserProfileDirectoryW() is only available on NT 4.0 and later.
Jun 29, 2002
Jun 29, 2002
440
441
* 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
Mar 25, 2007
Mar 25, 2007
442
443
* function pointer. Since this is originally an NT API, we don't
* offer a non-Unicode fallback.
Jun 29, 2002
Jun 29, 2002
444
*/
Mar 25, 2007
Mar 25, 2007
445
if (pGetUserProfileDirectoryW != NULL)
Jun 29, 2002
Jun 29, 2002
446
{
Mar 25, 2007
Mar 25, 2007
447
448
449
HANDLE accessToken = NULL; /* Security handle to process */
HANDLE processHandle = GetCurrentProcess();
if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
Jun 29, 2002
Jun 29, 2002
450
{
Mar 25, 2007
Mar 25, 2007
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
DWORD psize = 0;
WCHAR dummy = 0;
LPWSTR wstr = NULL;
BOOL rc = 0;
/*
* 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 = pGetUserProfileDirectoryW(accessToken, &dummy, &psize);
assert(!rc); /* !!! FIXME: handle this gracefully. */
/* Allocate memory for the profile directory */
wstr = (LPWSTR) __PHYSFS_smallAlloc(psize * sizeof (WCHAR));
if (wstr != NULL)
Jun 29, 2002
Jun 29, 2002
467
{
Mar 25, 2007
Mar 25, 2007
468
if (pGetUserProfileDirectoryW(accessToken, wstr, &psize))
Mar 26, 2007
Mar 26, 2007
469
userDir = unicodeToUtf8Heap(wstr);
Mar 25, 2007
Mar 25, 2007
470
471
__PHYSFS_smallFree(wstr);
} /* else */
Jun 29, 2002
Jun 29, 2002
472
} /* if */
Jun 10, 2002
Jun 10, 2002
473
Mar 25, 2007
Mar 25, 2007
474
CloseHandle(accessToken);
Jun 10, 2002
Jun 10, 2002
475
476
} /* if */
Jun 29, 2002
Jun 29, 2002
477
if (userDir == NULL) /* couldn't get profile for some reason. */
Jun 7, 2002
Jun 7, 2002
478
{
Jun 29, 2002
Jun 29, 2002
479
/* Might just be a non-NT system; resort to the basedir. */
Mar 25, 2007
Mar 25, 2007
480
userDir = getExePath();
Jun 29, 2002
Jun 29, 2002
481
BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* STILL failed?! */
Jun 10, 2002
Jun 10, 2002
482
} /* if */
Jun 7, 2002
Jun 7, 2002
483
Jan 28, 2010
Jan 28, 2010
484
return 1; /* We made it: hit the showers. */
Jun 29, 2002
Jun 29, 2002
485
} /* determineUserDir */
Jun 10, 2002
Jun 10, 2002
486
Jun 7, 2002
Jun 7, 2002
487
Jul 14, 2002
Jul 14, 2002
488
static BOOL mediaInDrive(const char *drive)
Jun 7, 2002
Jun 7, 2002
489
{
Jun 29, 2002
Jun 29, 2002
490
UINT oldErrorMode;
Jul 14, 2002
Jul 14, 2002
491
492
DWORD tmp;
BOOL retval;
Jun 7, 2002
Jun 7, 2002
493
Dec 8, 2003
Dec 8, 2003
494
/* Prevent windows warning message appearing when checking media size */
Jun 29, 2002
Jun 29, 2002
495
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
Jun 7, 2002
Jun 7, 2002
496
497
/* If this function succeeds, there's media in the drive */
Mar 26, 2007
Mar 26, 2007
498
retval = GetVolumeInformationA(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0);
Jun 7, 2002
Jun 7, 2002
499
500
/* Revert back to old windows error handler */
Jun 29, 2002
Jun 29, 2002
501
502
SetErrorMode(oldErrorMode);
Jan 28, 2010
Jan 28, 2010
503
return retval;
Jun 29, 2002
Jun 29, 2002
504
} /* mediaInDrive */
Jun 7, 2002
Jun 7, 2002
505
Aug 23, 2001
Aug 23, 2001
506
Sep 29, 2004
Sep 29, 2004
507
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Aug 23, 2001
Aug 23, 2001
508
{
Mar 25, 2007
Mar 25, 2007
509
510
/* !!! FIXME: Can CD drives be non-drive letter paths? */
/* !!! FIXME: (so can they be Unicode paths?) */
Aug 23, 2001
Aug 23, 2001
511
char drive_str[4] = "x:\\";
Sep 29, 2004
Sep 29, 2004
512
513
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
Aug 23, 2001
Aug 23, 2001
514
{
Sep 29, 2004
Sep 29, 2004
515
drive_str[0] = ch;
Jun 29, 2002
Jun 29, 2002
516
if (GetDriveType(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
Sep 29, 2004
Sep 29, 2004
517
cb(data, drive_str);
Aug 23, 2001
Aug 23, 2001
518
} /* for */
Sep 29, 2004
Sep 29, 2004
519
} /* __PHYSFS_platformDetectAvailableCDs */
Aug 23, 2001
Aug 23, 2001
520
521
Oct 9, 2001
Oct 9, 2001
522
523
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
May 22, 2003
May 22, 2003
524
if ((argv0 != NULL) && (strchr(argv0, '\\') != NULL))
Jan 28, 2010
Jan 28, 2010
525
return NULL; /* default behaviour can handle this. */
Oct 9, 2001
Oct 9, 2001
526
Jan 28, 2010
Jan 28, 2010
527
return getExePath();
Aug 23, 2001
Aug 23, 2001
528
529
530
531
532
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
Aug 23, 2001
Aug 23, 2001
533
DWORD bufsize = 0;
Mar 26, 2007
Mar 26, 2007
534
535
536
char *retval = NULL;
if (pGetUserNameW(NULL, &bufsize) == 0) /* This SHOULD fail. */
Mar 25, 2007
Mar 25, 2007
537
{
Mar 26, 2007
Mar 26, 2007
538
539
540
541
542
543
544
LPWSTR wbuf = (LPWSTR) __PHYSFS_smallAlloc(bufsize * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
if (pGetUserNameW(wbuf, &bufsize) == 0) /* ?! */
__PHYSFS_setError(winApiStrError());
else
retval = unicodeToUtf8Heap(wbuf);
__PHYSFS_smallFree(wbuf);
Mar 25, 2007
Mar 25, 2007
545
546
} /* if */
Jan 28, 2010
Jan 28, 2010
547
return retval;
Aug 23, 2001
Aug 23, 2001
548
549
550
551
552
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
Mar 14, 2005
Mar 14, 2005
553
char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
Jun 10, 2002
Jun 10, 2002
554
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Jun 29, 2002
Jun 29, 2002
555
strcpy(retval, userDir); /* calculated at init time. */
Jan 28, 2010
Jan 28, 2010
556
return retval;
Aug 23, 2001
Aug 23, 2001
557
558
559
} /* __PHYSFS_platformGetUserDir */
Sep 6, 2009
Sep 6, 2009
560
void *__PHYSFS_platformGetThreadID(void)
Aug 23, 2001
Aug 23, 2001
561
{
Jan 28, 2010
Jan 28, 2010
562
return ( (void *) ((size_t) GetCurrentThreadId()) );
Aug 23, 2001
Aug 23, 2001
563
564
565
} /* __PHYSFS_platformGetThreadID */
Mar 26, 2007
Mar 26, 2007
566
static int doPlatformExists(LPWSTR wpath)
Aug 23, 2001
Aug 23, 2001
567
{
Dec 8, 2003
Dec 8, 2003
568
569
BAIL_IF_MACRO
(
Mar 26, 2007
Mar 26, 2007
570
571
pGetFileAttributesW(wpath) == PHYSFS_INVALID_FILE_ATTRIBUTES,
winApiStrError(), 0
Dec 8, 2003
Dec 8, 2003
572
);
Jan 28, 2010
Jan 28, 2010
573
return 1;
Mar 26, 2007
Mar 26, 2007
574
575
576
577
578
579
580
581
582
583
584
} /* doPlatformExists */
int __PHYSFS_platformExists(const char *fname)
{
int retval = 0;
LPWSTR wpath;
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
retval = doPlatformExists(wpath);
__PHYSFS_smallFree(wpath);
Jan 28, 2010
Jan 28, 2010
585
return retval;
Aug 23, 2001
Aug 23, 2001
586
587
588
} /* __PHYSFS_platformExists */
Apr 1, 2007
Apr 1, 2007
589
static int isSymlinkAttrs(const DWORD attr, const DWORD tag)
Mar 31, 2007
Mar 31, 2007
590
{
Apr 1, 2007
Apr 1, 2007
591
592
return ( (attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
(tag == PHYSFS_IO_REPARSE_TAG_SYMLINK) );
Mar 31, 2007
Mar 31, 2007
593
594
595
} /* isSymlinkAttrs */
Aug 23, 2001
Aug 23, 2001
596
597
int __PHYSFS_platformIsSymLink(const char *fname)
{
Mar 31, 2007
Mar 31, 2007
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
/* !!! FIXME:
* Windows Vista can have NTFS symlinks. Can older Windows releases have
* them when talking to a network file server? What happens when you
* mount a NTFS partition on XP that was plugged into a Vista install
* that made a symlink?
*/
int retval = 0;
LPWSTR wpath;
HANDLE dir;
WIN32_FIND_DATAW entw;
/* no unicode entry points? Probably no symlinks. */
BAIL_IF_MACRO(pFindFirstFileW == NULL, NULL, 0);
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
/* !!! FIXME: filter wildcard chars? */
dir = pFindFirstFileW(wpath, &entw);
if (dir != INVALID_HANDLE_VALUE)
{
retval = isSymlinkAttrs(entw.dwFileAttributes, entw.dwReserved0);
FindClose(dir);
} /* if */
__PHYSFS_smallFree(wpath);
Jan 28, 2010
Jan 28, 2010
625
return retval;
Aug 23, 2001
Aug 23, 2001
626
627
628
629
630
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
Mar 26, 2007
Mar 26, 2007
631
632
633
634
635
636
int retval = 0;
LPWSTR wpath;
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
retval = ((pGetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY) != 0);
__PHYSFS_smallFree(wpath);
Jan 28, 2010
Jan 28, 2010
637
return retval;
Aug 23, 2001
Aug 23, 2001
638
639
640
641
642
643
644
645
646
647
} /* __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
648
char *retval = (char *) allocator.Malloc(len);
Aug 23, 2001
Aug 23, 2001
649
char *p;
Aug 23, 2001
Aug 23, 2001
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
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 = '\\';
Jan 28, 2010
Jan 28, 2010
666
return retval;
Aug 23, 2001
Aug 23, 2001
667
668
669
} /* __PHYSFS_platformCvtToDependent */
Sep 29, 2004
Sep 29, 2004
670
671
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
Sep 18, 2005
Sep 18, 2005
672
673
PHYSFS_EnumFilesCallback callback,
const char *origdir,
Sep 29, 2004
Sep 29, 2004
674
void *callbackdata)
Aug 23, 2001
Aug 23, 2001
675
{
Mar 26, 2007
Mar 26, 2007
676
677
const int unicode = (pFindFirstFileW != NULL) && (pFindNextFileW != NULL);
HANDLE dir = INVALID_HANDLE_VALUE;
Aug 23, 2001
Aug 23, 2001
678
WIN32_FIND_DATA ent;
Mar 26, 2007
Mar 26, 2007
679
WIN32_FIND_DATAW entw;
Jun 8, 2002
Jun 8, 2002
680
size_t len = strlen(dirname);
Mar 26, 2007
Mar 26, 2007
681
682
683
char *searchPath = NULL;
WCHAR *wSearchPath = NULL;
char *utf8 = NULL;
Jun 7, 2002
Jun 7, 2002
684
Jun 8, 2002
Jun 8, 2002
685
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
Mar 26, 2007
Mar 26, 2007
686
687
searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
if (searchPath == NULL)
Sep 29, 2004
Sep 29, 2004
688
return;
Jun 10, 2002
Jun 10, 2002
689
Jun 7, 2002
Jun 7, 2002
690
/* Copy current dirname */
Mar 26, 2007
Mar 26, 2007
691
strcpy(searchPath, dirname);
Jun 8, 2002
Jun 8, 2002
692
693
/* if there's no '\\' at the end of the path, stick one in there. */
Mar 26, 2007
Mar 26, 2007
694
if (searchPath[len - 1] != '\\')
Jun 8, 2002
Jun 8, 2002
695
{
Mar 26, 2007
Mar 26, 2007
696
697
searchPath[len++] = '\\';
searchPath[len] = '\0';
Jun 8, 2002
Jun 8, 2002
698
699
} /* if */
Jun 7, 2002
Jun 7, 2002
700
/* Append the "*" to the end of the string */
Mar 26, 2007
Mar 26, 2007
701
strcat(searchPath, "*");
Aug 23, 2001
Aug 23, 2001
702
Mar 26, 2007
Mar 26, 2007
703
704
705
UTF8_TO_UNICODE_STACK_MACRO(wSearchPath, searchPath);
if (wSearchPath == NULL)
return; /* oh well. */
Aug 23, 2001
Aug 23, 2001
706
Mar 26, 2007
Mar 26, 2007
707
708
709
if (unicode)
dir = pFindFirstFileW(wSearchPath, &entw);
else
Aug 23, 2001
Aug 23, 2001
710
{
Mar 26, 2007
Mar 26, 2007
711
712
713
714
715
716
717
718
719
const int len = (int) (wStrLen(wSearchPath) + 1);
char *cp = (char *) __PHYSFS_smallAlloc(len);
if (cp != NULL)
{
WideCharToMultiByte(CP_ACP, 0, wSearchPath, len, cp, len, 0, 0);
dir = FindFirstFileA(cp, &ent);
__PHYSFS_smallFree(cp);
} /* if */
} /* else */
Aug 23, 2001
Aug 23, 2001
720
Mar 26, 2007
Mar 26, 2007
721
722
723
724
__PHYSFS_smallFree(wSearchPath);
__PHYSFS_smallFree(searchPath);
if (dir == INVALID_HANDLE_VALUE)
return;
Aug 23, 2001
Aug 23, 2001
725
Mar 26, 2007
Mar 26, 2007
726
727
728
729
if (unicode)
{
do
{
Apr 1, 2007
Apr 1, 2007
730
const DWORD attr = entw.dwFileAttributes;
Mar 31, 2007
Mar 31, 2007
731
const DWORD tag = entw.dwReserved0;
Mar 26, 2007
Mar 26, 2007
732
733
734
735
736
const WCHAR *fn = entw.cFileName;
if ((fn[0] == '.') && (fn[1] == '\0'))
continue;
if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
continue;
Mar 31, 2007
Mar 31, 2007
737
738
if ((omitSymLinks) && (isSymlinkAttrs(attr, tag)))
continue;
Mar 26, 2007
Mar 26, 2007
739
Mar 31, 2007
Mar 31, 2007
740
utf8 = unicodeToUtf8Heap(fn);
Mar 26, 2007
Mar 26, 2007
741
742
743
744
745
746
747
if (utf8 != NULL)
{
callback(callbackdata, origdir, utf8);
allocator.Free(utf8);
} /* if */
} while (pFindNextFileW(dir, &entw) != 0);
} /* if */
Mar 25, 2007
Mar 25, 2007
748
Mar 26, 2007
Mar 26, 2007
749
750
751
752
else /* ANSI fallback. */
{
do
{
Apr 1, 2007
Apr 1, 2007
753
const DWORD attr = ent.dwFileAttributes;
Mar 31, 2007
Mar 31, 2007
754
const DWORD tag = ent.dwReserved0;
Mar 26, 2007
Mar 26, 2007
755
756
757
758
759
const char *fn = ent.cFileName;
if ((fn[0] == '.') && (fn[1] == '\0'))
continue;
if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
continue;
Mar 31, 2007
Mar 31, 2007
760
761
if ((omitSymLinks) && (isSymlinkAttrs(attr, tag)))
continue;
Mar 26, 2007
Mar 26, 2007
762
Mar 31, 2007
Mar 31, 2007
763
utf8 = codepageToUtf8Heap(fn);
Mar 26, 2007
Mar 26, 2007
764
765
766
767
768
769
770
if (utf8 != NULL)
{
callback(callbackdata, origdir, utf8);
allocator.Free(utf8);
} /* if */
} while (FindNextFileA(dir, &ent) != 0);
} /* else */
Aug 23, 2001
Aug 23, 2001
771
772
773
774
775
776
777
FindClose(dir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
Mar 26, 2007
Mar 26, 2007
778
779
char *retval = NULL;
WCHAR *wbuf = NULL;
Aug 23, 2001
Aug 23, 2001
780
781
DWORD buflen = 0;
Mar 26, 2007
Mar 26, 2007
782
783
784
785
buflen = pGetCurrentDirectoryW(buflen, NULL);
wbuf = (WCHAR *) __PHYSFS_smallAlloc((buflen + 2) * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
pGetCurrentDirectoryW(buflen, wbuf);
Sep 1, 2001
Sep 1, 2001
786
Mar 26, 2007
Mar 26, 2007
787
788
789
790
791
792
793
if (wbuf[buflen - 2] == '\\')
wbuf[buflen-1] = '\0'; /* just in case... */
else
{
wbuf[buflen - 1] = '\\';
wbuf[buflen] = '\0';
} /* else */
Sep 1, 2001
Sep 1, 2001
794
Mar 26, 2007
Mar 26, 2007
795
796
retval = unicodeToUtf8Heap(wbuf);
__PHYSFS_smallFree(wbuf);
Jan 28, 2010
Jan 28, 2010
797
return retval;
Aug 23, 2001
Aug 23, 2001
798
799
800
} /* __PHYSFS_platformCurrentDir */
Sep 1, 2001
Sep 1, 2001
801
/* this could probably use a cleanup. */
Aug 23, 2001
Aug 23, 2001
802
803
char *__PHYSFS_platformRealPath(const char *path)
{
Oct 1, 2007
Oct 1, 2007
804
805
/* !!! FIXME: this should return NULL if (path) doesn't exist? */
/* !!! FIXME: Need to handle symlinks in Vista... */
Mar 26, 2007
Mar 26, 2007
806
/* !!! FIXME: try GetFullPathName() instead? */
Mar 25, 2007
Mar 25, 2007
807
/* this function should be UTF-8 clean. */
Sep 1, 2001
Sep 1, 2001
808
809
810
811
812
813
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
814
retval = (char *) allocator.Malloc(MAX_PATH);
Sep 1, 2001
Sep 1, 2001
815
816
817
818
819
820
821
822
823
824
825
826
827
828
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
829
allocator.Free(retval);
Sep 1, 2001
Sep 1, 2001
830
831
832
833
834
835
836
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
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
875
allocator.Free(currentDir);
Sep 1, 2001
Sep 1, 2001
876
877
878
879
880
881
882
} /* else */
/* (whew.) Ok, now take out "." and ".." path entries... */
p = retval;
while ( (p = strstr(p, "\\.")) != NULL)
{
Mar 25, 2007
Mar 25, 2007
883
/* it's a "." entry that doesn't end the string. */
Sep 1, 2001
Sep 1, 2001
884
885
886
if (p[2] == '\\')
memmove(p + 1, p + 3, strlen(p + 3) + 1);
Mar 25, 2007
Mar 25, 2007
887
/* it's a "." entry that ends the string. */
Sep 1, 2001
Sep 1, 2001
888
889
890
else if (p[2] == '\0')
p[0] = '\0';
Mar 25, 2007
Mar 25, 2007
891
/* it's a ".." entry. */
Sep 1, 2001
Sep 1, 2001
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
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 */
Mar 25, 2007
Mar 25, 2007
917
/* shrink the retval's memory block if possible... */
Mar 14, 2005
Mar 14, 2005
918
p = (char *) allocator.Realloc(retval, strlen(retval) + 1);
Sep 1, 2001
Sep 1, 2001
919
920
921
if (p != NULL)
retval = p;
Jan 28, 2010
Jan 28, 2010
922
return retval;
Aug 23, 2001
Aug 23, 2001
923
924
925
926
927
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
Mar 26, 2007
Mar 26, 2007
928
929
930
931
932
933
WCHAR *wpath;
DWORD rc;
UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
rc = pCreateDirectoryW(wpath, NULL);
__PHYSFS_smallFree(wpath);
BAIL_IF_MACRO(rc == 0, winApiStrError(), 0);
Jan 28, 2010
Jan 28, 2010
934
return 1;
Aug 23, 2001
Aug 23, 2001
935
936
} /* __PHYSFS_platformMkDir */
Jun 10, 2002
Jun 10, 2002
937
Mar 26, 2007
Mar 26, 2007
938
939
940
941
942
/*
* Get OS info and save the important parts.
*
* Returns non-zero if successful, otherwise it returns zero on failure.
*/
Oct 1, 2007
Oct 1, 2007
943
944
945
946
947
948
static int getOSInfo(void)
{
OSVERSIONINFO osVerInfo; /* Information about the OS */
osVerInfo.dwOSVersionInfoSize = sizeof(osVerInfo);
BAIL_IF_MACRO(!GetVersionEx(&osVerInfo), winApiStrError(), 0);
osHasUnicode = (osVerInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS);
Jan 28, 2010
Jan 28, 2010
949
return 1;
Oct 1, 2007
Oct 1, 2007
950
} /* getOSInfo */
Mar 26, 2007
Mar 26, 2007
951
952
Jun 29, 2002
Jun 29, 2002
953
954
int __PHYSFS_platformInit(void)
{
Mar 26, 2007
Mar 26, 2007
955
BAIL_IF_MACRO(!getOSInfo(), NULL, 0);
Mar 25, 2007
Mar 25, 2007
956
BAIL_IF_MACRO(!findApiSymbols(), NULL, 0);
Jun 29, 2002
Jun 29, 2002
957
BAIL_IF_MACRO(!determineUserDir(), NULL, 0);
Jan 28, 2010
Jan 28, 2010
958
return 1; /* It's all good */
Jun 29, 2002
Jun 29, 2002
959
} /* __PHYSFS_platformInit */
Mar 24, 2002
Mar 24, 2002
960
961
962
963
int __PHYSFS_platformDeinit(void)
{
Mar 25, 2007
Mar 25, 2007
964
965
966
967
968
HANDLE *libs[] = { &libKernel32, &libUserEnv, &libAdvApi32, NULL };
int i;
allocator.Free(userDir);
userDir = NULL;
Mar 24, 2002
Mar 24, 2002
969
Mar 25, 2007
Mar 25, 2007
970
for (i = 0; libs[i] != NULL; i++)
Jun 29, 2002
Jun 29, 2002
971
{
Mar 25, 2007
Mar 25, 2007
972
973
974
975
976
const HANDLE lib = *(libs[i]);
if (lib)
FreeLibrary(lib);
*(libs[i]) = NULL;
} /* for */
Jun 29, 2002
Jun 29, 2002
977
Jan 28, 2010
Jan 28, 2010
978
return 1; /* It's all good */
Jun 29, 2002
Jun 29, 2002
979
980
981
982
983
984
} /* __PHYSFS_platformDeinit */
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
HANDLE fileHandle;
Mar 26, 2007
Mar 26, 2007
985
986
WinApiFile *retval;
WCHAR *wfname;
Jun 29, 2002
Jun 29, 2002
987
Mar 26, 2007
Mar 26, 2007
988
989
UTF8_TO_UNICODE_STACK_MACRO(wfname, fname);
BAIL_IF_MACRO(wfname == NULL, ERR_OUT_OF_MEMORY, NULL);
Mar 17, 2010
Mar 17, 2010
990
991
fileHandle = pCreateFileW(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
Mar 26, 2007
Mar 26, 2007
992
__PHYSFS_smallFree(wfname);
Jun 29, 2002
Jun 29, 2002
993
Dec 8, 2003
Dec 8, 2003
994
995
BAIL_IF_MACRO
(
Dec 22, 2003
Dec 22, 2003
996
fileHandle == INVALID_HANDLE_VALUE,
Mar 26, 2007
Mar 26, 2007
997
winApiStrError(), NULL
Dec 8, 2003
Dec 8, 2003
998
);
Jun 29, 2002
Jun 29, 2002
999
Mar 26, 2007
Mar 26, 2007
1000
retval = (WinApiFile *) allocator.Malloc(sizeof (WinApiFile));