Skip to content

Latest commit

 

History

History
466 lines (367 loc) · 11.9 KB

win32.c

File metadata and controls

466 lines (367 loc) · 11.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
6
7
8
9
10
11
12
13
14
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if (defined __STRICT_ANSI__)
#define __PHYSFS_DOING_STRICT_ANSI__
#endif
#include <windows.h>
#include <stdio.h>
Aug 29, 2001
Aug 29, 2001
15
16
#include <stdlib.h>
#include <ctype.h>
Aug 23, 2001
Aug 23, 2001
17
18
19
20
21
22
23
24
25
26
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
const char *__PHYSFS_platformDirSeparator = "\\";
static const char *win32strerror(void)
{
Aug 23, 2001
Aug 23, 2001
27
static TCHAR msgbuf[255];
Aug 23, 2001
Aug 23, 2001
28
29
30
31
32
33
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
Aug 23, 2001
Aug 23, 2001
34
35
36
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
Aug 23, 2001
Aug 23, 2001
37
38
39
NULL
);
Aug 23, 2001
Aug 23, 2001
40
return((const char *) msgbuf);
Aug 23, 2001
Aug 23, 2001
41
42
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
69
70
71
72
} /* win32strerror */
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]++)
{
if (GetDriveType(drive_str) == DRIVE_CDROM)
{
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
73
static char *getExePath(const char *argv0)
Aug 23, 2001
Aug 23, 2001
74
{
Mar 21, 2002
Mar 21, 2002
75
char *filepart = NULL;
Oct 9, 2001
Oct 9, 2001
76
char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
Mar 21, 2002
Mar 21, 2002
77
DWORD buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
Aug 29, 2001
Aug 29, 2001
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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. */
retval = (char *) realloc(retval, strlen(retval) + 1);
if (retval != NULL)
return(retval);
} /* if */
} /* if */
/* if any part of the previous approach failed, try SearchPath()... */
Aug 23, 2001
Aug 23, 2001
96
buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
Aug 29, 2001
Aug 29, 2001
97
retval = (char *) realloc(retval, buflen);
Aug 23, 2001
Aug 23, 2001
98
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
Aug 23, 2001
Aug 23, 2001
99
SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
Aug 29, 2001
Aug 29, 2001
100
Aug 23, 2001
Aug 23, 2001
101
return(retval);
Oct 9, 2001
Oct 9, 2001
102
103
104
105
106
107
108
109
} /* getExePath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
return(NULL);
Mar 21, 2002
Mar 21, 2002
110
return(getExePath(argv0));
Aug 23, 2001
Aug 23, 2001
111
112
113
114
115
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
Aug 23, 2001
Aug 23, 2001
116
DWORD bufsize = 0;
Aug 23, 2001
Aug 23, 2001
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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) /* ?! */
{
free(retval);
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
Aug 29, 2001
Aug 29, 2001
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
static char *copyEnvironmentVariable(const char *varname)
{
const char *envr = getenv(varname);
char *retval = NULL;
if (envr != NULL)
{
retval = malloc(strlen(envr) + 1);
if (retval != NULL)
strcpy(retval, envr);
} /* if */
return(retval);
} /* copyEnvironmentVariable */
Aug 23, 2001
Aug 23, 2001
150
151
char *__PHYSFS_platformGetUserDir(void)
{
Aug 29, 2001
Aug 29, 2001
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
char *home = copyEnvironmentVariable("HOME");
const char *homedrive = getenv("HOMEDRIVE");
const char *homepath = getenv("HOMEPATH");
if (home != NULL)
return(home);
if ((homedrive != NULL) && (homepath != NULL))
{
char *retval = (char *) malloc(strlen(homedrive)+strlen(homepath)+2);
if (retval != NULL)
{
strcpy(retval, homedrive);
if ((homepath[0] != '\\') &&
(homedrive[strlen(homedrive)-1] != '\\'))
{
strcat(retval, "\\");
} /* if */
strcat(retval, homepath);
return(retval);
} /* if */
} /* if */
Mar 21, 2002
Mar 21, 2002
175
return(NULL);
Aug 23, 2001
Aug 23, 2001
176
177
178
179
180
181
182
183
184
} /* __PHYSFS_platformGetUserDir */
int __PHYSFS_platformGetThreadID(void)
{
return((int) GetCurrentThreadId());
} /* __PHYSFS_platformGetThreadID */
Aug 23, 2001
Aug 23, 2001
185
/* ...make this Cygwin AND Visual C friendly... */
Aug 23, 2001
Aug 23, 2001
186
187
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
Aug 23, 2001
Aug 23, 2001
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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);
Aug 23, 2001
Aug 23, 2001
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
} /* __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
232
char *p;
Aug 23, 2001
Aug 23, 2001
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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;
dir = FindFirstFile(dirname, &ent);
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
Mar 5, 2002
Mar 5, 2002
272
while (FindNextFile(dir, &ent) != 0)
Aug 23, 2001
Aug 23, 2001
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{
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;
} /* while */
FindClose(dir);
return(retval);
} /* __PHYSFS_platformEnumerateFiles */
int __PHYSFS_platformFileLength(FILE *handle)
{
fpos_t curpos;
int retval;
fgetpos(handle, &curpos);
fseek(handle, 0, SEEK_END);
retval = ftell(handle);
fsetpos(handle, &curpos);
return(retval);
} /* __PHYSFS_platformFileLength */
char *__PHYSFS_platformCurrentDir(void)
{
Aug 23, 2001
Aug 23, 2001
322
LPTSTR retval;
Aug 23, 2001
Aug 23, 2001
323
324
DWORD buflen = 0;
Aug 23, 2001
Aug 23, 2001
325
buflen = GetCurrentDirectory(buflen, NULL);
Sep 1, 2001
Sep 1, 2001
326
327
retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 23, 2001
Aug 23, 2001
328
GetCurrentDirectory(buflen, retval);
Sep 1, 2001
Sep 1, 2001
329
330
331
332
if (retval[buflen - 2] != '\\')
strcat(retval, "\\");
Aug 23, 2001
Aug 23, 2001
333
334
335
336
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
Sep 1, 2001
Sep 1, 2001
337
/* this could probably use a cleanup. */
Aug 23, 2001
Aug 23, 2001
338
339
char *__PHYSFS_platformRealPath(const char *path)
{
Sep 1, 2001
Sep 1, 2001
340
341
342
343
344
345
346
347
348
349
350
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
386
387
388
389
390
391
392
393
394
395
396
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
455
456
457
458
459
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
Aug 23, 2001
Aug 23, 2001
460
DWORD rc = CreateDirectory(path, NULL);
Aug 23, 2001
Aug 23, 2001
461
462
463
464
465
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
/* end of win32.c ... */