Skip to content

Latest commit

 

History

History
784 lines (644 loc) · 24.1 KB

physfs_platform_os2.c

File metadata and controls

784 lines (644 loc) · 24.1 KB
 
Jul 6, 2017
Jul 6, 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* OS/2 support routines for PhysicsFS.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_OS2
Jul 7, 2017
Jul 7, 2017
14
#define INCL_DOSMODULEMGR
Jul 6, 2017
Jul 6, 2017
15
16
17
18
19
20
21
22
23
24
#define INCL_DOSSEMAPHORES
#define INCL_DOSDATETIME
#define INCL_DOSFILEMGR
#define INCL_DOSMODULEMGR
#define INCL_DOSERRORS
#define INCL_DOSPROCESS
#define INCL_DOSDEVICES
#define INCL_DOSDEVIOCTL
#define INCL_DOSMISC
#include <os2.h>
Jul 7, 2017
Jul 7, 2017
25
#include <uconv.h>
Jul 6, 2017
Jul 6, 2017
26
27
28
29
30
31
32
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include "physfs_internal.h"
Jul 7, 2017
Jul 7, 2017
33
34
35
36
37
38
static HMODULE uconvdll = 0;
static UconvObject uconv = 0;
static int (_System *pUniCreateUconvObject)(UniChar *, UconvObject *) = NULL;
static int (_System *pUniFreeUconvObject)(UconvObject *) = NULL;
static int (_System *pUniUconvToUcs)(UconvObject,void **,size_t *, UniChar**, size_t *, size_t *) = NULL;
static int (_System *pUniUconvFromUcs)(UconvObject,UniChar **,size_t *,void **,size_t *,size_t *) = NULL;
Jul 7, 2017
Jul 7, 2017
39
Jul 6, 2017
Jul 6, 2017
40
41
42
43
44
45
46
static PHYSFS_ErrorCode errcodeFromAPIRET(const APIRET rc)
{
switch (rc)
{
case NO_ERROR: return PHYSFS_ERR_OK; /* not an error. */
case ERROR_INTERRUPT: return PHYSFS_ERR_OK; /* not an error. */
case ERROR_TIMEOUT: return PHYSFS_ERR_OK; /* not an error. */
Jul 6, 2017
Jul 6, 2017
47
case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
Jul 6, 2017
Jul 6, 2017
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
case ERROR_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
case ERROR_NOT_DOS_DISK: return PHYSFS_ERR_NOT_FOUND;
case ERROR_SHARING_VIOLATION: return PHYSFS_ERR_PERMISSION;
case ERROR_CANNOT_MAKE: return PHYSFS_ERR_IO; /* maybe this is wrong? */
case ERROR_DEVICE_IN_USE: return PHYSFS_ERR_BUSY;
case ERROR_OPEN_FAILED: return PHYSFS_ERR_IO; /* maybe this is wrong? */
case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
case ERROR_PIPE_BUSY: return PHYSFS_ERR_BUSY;
case ERROR_SHARING_BUFFER_EXCEEDED: return PHYSFS_ERR_IO;
case ERROR_FILENAME_EXCED_RANGE: return PHYSFS_ERR_BAD_FILENAME;
case ERROR_META_EXPANSION_TOO_LONG: return PHYSFS_ERR_BAD_FILENAME;
case ERROR_TOO_MANY_HANDLES: return PHYSFS_ERR_IO;
case ERROR_TOO_MANY_OPEN_FILES: return PHYSFS_ERR_IO;
case ERROR_NO_MORE_SEARCH_HANDLES: return PHYSFS_ERR_IO;
case ERROR_SEEK_ON_DEVICE: return PHYSFS_ERR_IO;
case ERROR_NEGATIVE_SEEK: return PHYSFS_ERR_INVALID_ARGUMENT;
case ERROR_WRITE_PROTECT: return PHYSFS_ERR_PERMISSION;
case ERROR_WRITE_FAULT: return PHYSFS_ERR_IO;
case ERROR_UNCERTAIN_MEDIA: return PHYSFS_ERR_IO;
case ERROR_PROTECTION_VIOLATION: return PHYSFS_ERR_IO;
case ERROR_BROKEN_PIPE: return PHYSFS_ERR_IO;
/* !!! FIXME: some of these might be PHYSFS_ERR_BAD_FILENAME, etc */
case ERROR_LOCK_VIOLATION:
case ERROR_GEN_FAILURE:
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_NAME:
case ERROR_INVALID_DRIVE:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_FUNCTION:
case ERROR_INVALID_LEVEL:
case ERROR_INVALID_CATEGORY:
case ERROR_DUPLICATE_NAME:
case ERROR_BUFFER_OVERFLOW:
case ERROR_BAD_LENGTH:
case ERROR_BAD_DRIVER_LEVEL:
case ERROR_DIRECT_ACCESS_HANDLE:
case ERROR_NOT_OWNER:
return PHYSFS_ERR_OS_ERROR;
default: break;
} /* switch */
return PHYSFS_ERR_OTHER_ERROR;
} /* errcodeFromAPIRET */
Jul 7, 2017
Jul 7, 2017
96
static char *cvtUtf8ToCodepage(const char *utf8str)
Jul 7, 2017
Jul 7, 2017
97
98
99
100
{
if (uconvdll)
{
int rc;
Jul 7, 2017
Jul 7, 2017
101
size_t len = strlen(utf8str) + 1;
Jul 7, 2017
Jul 7, 2017
102
const size_t uc2buflen = len * sizeof (UniChar);
Jul 7, 2017
Jul 7, 2017
103
UniChar *uc2ptr = (UniChar *) __PHYSFS_smallAlloc(uc2buflen);
Jul 7, 2017
Jul 7, 2017
104
105
106
107
108
109
110
111
UniChar *uc2str = uc2ptr;
char *cpptr = NULL;
char *cpstr = NULL;
size_t subs = 0;
size_t unilen;
size_t cplen;
GOTO_IF(!uc2str, PHYSFS_ERR_OUT_OF_MEMORY, failed);
Jul 7, 2017
Jul 7, 2017
112
PHYSFS_utf8ToUcs2(utf8str, (PHYSFS_uint16 *) uc2str, uc2buflen);
Jul 7, 2017
Jul 7, 2017
113
114
115
116
117
118
119
120
for (unilen = 0; uc2str[unilen]; unilen++) { /* spin */ }
unilen++; /* null terminator. */
cplen = unilen * 4; /* overallocate, just in case. */
cpptr = (char *) allocator.Malloc(cplen);
GOTO_IF(!cpptr, PHYSFS_ERR_OUT_OF_MEMORY, failed);
cpstr = cpptr;
Jul 7, 2017
Jul 7, 2017
121
rc = pUniUconvFromUcs(uconv, &uc2str, &unilen, (void **) &cpstr, &cplen, &subs);
Jul 7, 2017
Jul 7, 2017
122
123
124
125
126
127
128
129
130
131
132
133
134
135
GOTO_IF(rc != ULS_SUCCESS, PHYSFS_ERR_BAD_FILENAME, failed);
GOTO_IF(subs > 0, PHYSFS_ERR_BAD_FILENAME, failed);
assert(unilen == 0);
return cpptr;
failed:
__PHYSFS_smallFree(uc2ptr);
allocator.Free(cpptr);
} /* if */
return NULL;
} /* cvtUtf8ToCodepage */
Jul 7, 2017
Jul 7, 2017
136
137
138
139
140
141
142
143
144
145
static char *cvtCodepageToUtf8(const char *cpstr)
{
char *retval = NULL;
if (uconvdll)
{
int rc;
size_t len = strlen(cpstr) + 1;
size_t cplen = len;
size_t unilen = len;
size_t subs = 0;
Jul 7, 2017
Jul 7, 2017
146
147
148
149
150
UniChar *uc2ptr = __PHYSFS_smallAlloc(len * sizeof (UniChar));
UniChar *uc2str = uc2ptr;
BAIL_IF(!uc2ptr, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
rc = pUniUconvToUcs(uconv, (void **) &cpstr, &cplen, &uc2str, &unilen, &subs);
Jul 7, 2017
Jul 7, 2017
151
152
GOTO_IF(rc != ULS_SUCCESS, PHYSFS_ERR_BAD_FILENAME, done);
GOTO_IF(subs > 0, PHYSFS_ERR_BAD_FILENAME, done);
Jul 7, 2017
Jul 7, 2017
153
assert(cplen == 0);
Jul 7, 2017
Jul 7, 2017
154
155
retval = (char *) allocator.Malloc(len * 4);
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, done);
Jul 7, 2017
Jul 7, 2017
156
PHYSFS_utf8FromUcs2((const PHYSFS_uint16 *) uc2ptr, retval, len * 4);
Jul 7, 2017
Jul 7, 2017
157
done:
Jul 7, 2017
Jul 7, 2017
158
159
__PHYSFS_smallFree(uc2ptr);
} /* if */
Jul 7, 2017
Jul 7, 2017
160
161
162
163
return retval;
} /* cvtCodepageToUtf8 */
Jul 6, 2017
Jul 6, 2017
164
165
/* (be gentle, this function isn't very robust.) */
Jul 7, 2017
Jul 7, 2017
166
static char *cvtPathToCorrectCase(char *buf)
Jul 6, 2017
Jul 6, 2017
167
{
Jul 7, 2017
Jul 7, 2017
168
char *retval = buf;
Jul 6, 2017
Jul 6, 2017
169
170
171
172
173
174
175
176
177
178
179
180
char *fname = buf + 3; /* point to first element. */
char *ptr = strchr(fname, '\\'); /* find end of first element. */
buf[0] = toupper(buf[0]); /* capitalize drive letter. */
/*
* Go through each path element, and enumerate its parent dir until
* a case-insensitive match is found. If one is (and it SHOULD be)
* then overwrite the original element with the correct case.
* If there's an error, or the path has vanished for some reason, it
* won't hurt to have the original case, so we just keep going.
*/
Jul 7, 2017
Jul 7, 2017
181
while ((fname != NULL) && (*fname != '\0'))
Jul 6, 2017
Jul 6, 2017
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
char spec[CCHMAXPATH];
FILEFINDBUF3 fb;
HDIR hdir = HDIR_CREATE;
ULONG count = 1;
APIRET rc;
*(fname - 1) = '\0'; /* isolate parent dir string. */
strcpy(spec, buf); /* copy isolated parent dir... */
strcat(spec, "\\*.*"); /* ...and add wildcard search spec. */
if (ptr != NULL) /* isolate element to find (fname is the start). */
*ptr = '\0';
rc = DosFindFirst((unsigned char *) spec, &hdir, FILE_DIRECTORY,
&fb, sizeof (fb), &count, FIL_STANDARD);
if (rc == NO_ERROR)
{
while (count == 1) /* while still entries to enumerate... */
{
Jul 7, 2017
Jul 7, 2017
203
204
int cmp;
char *utf8 = cvtCodepageToUtf8(fb.achName);
Aug 11, 2017
Aug 11, 2017
205
206
if (!utf8) /* ugh, maybe we'll get lucky with the C runtime. */
cmp = stricmp(utf8, fname);
Jul 7, 2017
Jul 7, 2017
207
208
else
{
Aug 11, 2017
Aug 11, 2017
209
cmp = PHYSFS_utf8stricmp(utf8, fname);
Jul 7, 2017
Jul 7, 2017
210
211
212
213
allocator.Free(utf8);
} /* else */
if (cmp == 0)
Jul 6, 2017
Jul 6, 2017
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
{
strcpy(fname, fb.achName);
break; /* there it is. Overwrite and stop searching. */
} /* if */
DosFindNext(hdir, &fb, sizeof (fb), &count);
} /* while */
DosFindClose(hdir);
} /* if */
*(fname - 1) = '\\'; /* unisolate parent dir. */
fname = ptr; /* point to next element. */
if (ptr != NULL)
{
*ptr = '\\'; /* unisolate element. */
ptr = strchr(++fname, '\\'); /* find next element. */
} /* if */
} /* while */
Jul 7, 2017
Jul 7, 2017
232
233
234
235
236
237
238
239
return retval;
} /* cvtPathToCorrectCase */
static void prepUnicodeSupport(void)
{
/* really old OS/2 might not have Unicode support _at all_, so load
the system library and do without if it doesn't exist. */
Jul 7, 2017
Jul 7, 2017
240
int ok = 0;
Jul 7, 2017
Jul 7, 2017
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
char buf[CCHMAXPATH];
UniChar defstr[] = { 0 };
if (DosLoadModule(buf, sizeof (buf) - 1, "uconv", &uconvdll) == NO_ERROR)
{
#define LOAD(x) (DosQueryProcAddr(uconvdll,0,#x,(PFN*)&p##x)==NO_ERROR)
ok = LOAD(UniCreateUconvObject) &&
LOAD(UniFreeUconvObject) &&
LOAD(UniUconvToUcs) &&
LOAD(UniUconvFromUcs);
#undef LOAD
} /* else */
if (!ok || (pUniCreateUconvObject(defstr, &uconv) != ULS_SUCCESS))
{
/* oh well, live without it. */
if (uconvdll)
{
if (uconv)
pUniFreeUconvObject(uconv);
DosFreeModule(uconvdll);
uconvdll = 0;
} /* if */
} /* if */
} /* prepUnicodeSupport */
Jul 6, 2017
Jul 6, 2017
265
266
267
268
int __PHYSFS_platformInit(void)
{
Jul 7, 2017
Jul 7, 2017
269
270
prepUnicodeSupport();
return 1; /* ready to go! */
Jul 6, 2017
Jul 6, 2017
271
272
273
} /* __PHYSFS_platformInit */
Aug 6, 2017
Aug 6, 2017
274
void __PHYSFS_platformDeinit(void)
Jul 6, 2017
Jul 6, 2017
275
{
Jul 7, 2017
Jul 7, 2017
276
277
278
279
280
281
282
if (uconvdll)
{
pUniFreeUconvObject(uconv);
uconv = 0;
DosFreeModule(uconvdll);
uconvdll = 0;
} /* if */
Jul 6, 2017
Jul 6, 2017
283
284
285
} /* __PHYSFS_platformDeinit */
Jul 7, 2017
Jul 7, 2017
286
static int discIsInserted(ULONG drive)
Jul 6, 2017
Jul 6, 2017
287
288
289
290
291
292
293
294
295
296
297
298
299
{
int rc;
char buf[20];
DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
rc = DosQueryFSInfo(drive + 1, FSIL_VOLSER, buf, sizeof (buf));
DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
return (rc == NO_ERROR);
} /* is_cdrom_inserted */
/* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
#define CD01 0x31304443
Jul 7, 2017
Jul 7, 2017
300
static int isCdRomDrive(ULONG drive)
Jul 6, 2017
Jul 6, 2017
301
302
303
304
305
{
PHYSFS_uint32 param, data;
ULONG ul1, ul2;
APIRET rc;
HFILE hfile = NULLHANDLE;
Jul 6, 2017
Jul 6, 2017
306
unsigned char drivename[3] = { 0, ':', '\0' };
Jul 6, 2017
Jul 6, 2017
307
308
309
drivename[0] = 'A' + drive;
Jul 6, 2017
Jul 6, 2017
310
311
312
313
rc = DosOpen(drivename, &hfile, &ul1, 0, 0,
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, NULL);
Jul 6, 2017
Jul 6, 2017
314
315
if (rc != NO_ERROR)
return 0;
Jul 6, 2017
Jul 6, 2017
316
317
318
319
320
321
322
323
324
data = 0;
param = PHYSFS_swapULE32(CD01);
ul1 = ul2 = sizeof (PHYSFS_uint32);
rc = DosDevIOCtl(hfile, IOCTL_CDROMDISK, CDROMDISK_GETDRIVER,
&param, sizeof (param), &ul1, &data, sizeof (data), &ul2);
DosClose(hfile);
return ((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
Jul 7, 2017
Jul 7, 2017
325
} /* isCdRomDrive */
Jul 6, 2017
Jul 6, 2017
326
327
328
329
330
331
332
333
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
{
ULONG dummy = 0;
ULONG drivemap = 0;
ULONG i, bit;
const APIRET rc = DosQueryCurrentDisk(&dummy, &drivemap);
Jul 6, 2017
Jul 6, 2017
334
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc),);
Jul 6, 2017
Jul 6, 2017
335
336
337
338
339
for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
{
if (drivemap & bit) /* this logical drive exists. */
{
Jul 7, 2017
Jul 7, 2017
340
if ((isCdRomDrive(i)) && (discIsInserted(i)))
Jul 6, 2017
Jul 6, 2017
341
342
343
344
345
346
347
348
349
350
351
{
char drive[4] = "x:\\";
drive[0] = ('A' + i);
cb(data, drive);
} /* if */
} /* if */
} /* for */
} /* __PHYSFS_platformDetectAvailableCDs */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
Jul 6, 2017
Jul 6, 2017
352
353
354
355
356
357
358
{
char *retval = NULL;
char buf[CCHMAXPATH];
APIRET rc;
PTIB ptib;
PPIB ppib;
PHYSFS_sint32 len;
Jul 6, 2017
Jul 6, 2017
359
Jul 6, 2017
Jul 6, 2017
360
rc = DosGetInfoBlocks(&ptib, &ppib);
Jul 6, 2017
Jul 6, 2017
361
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
362
rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
Jul 6, 2017
Jul 6, 2017
363
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 7, 2017
Jul 7, 2017
364
365
retval = cvtCodepageToUtf8(buf);
BAIL_IF_ERRPASS(!retval, NULL);
Jul 6, 2017
Jul 6, 2017
366
Jul 6, 2017
Jul 6, 2017
367
/* chop off filename, leave path. */
Jul 7, 2017
Jul 7, 2017
368
for (len = strlen(retval) - 1; len >= 0; len--)
Jul 6, 2017
Jul 6, 2017
369
{
Jul 7, 2017
Jul 7, 2017
370
if (retval[len] == '\\')
Jul 6, 2017
Jul 6, 2017
371
{
Jul 7, 2017
Jul 7, 2017
372
retval[len + 1] = '\0';
Jul 6, 2017
Jul 6, 2017
373
374
375
break;
} /* if */
} /* for */
Jul 6, 2017
Jul 6, 2017
376
Jul 6, 2017
Jul 6, 2017
377
assert(len > 0); /* should have been a "x:\\" on the front on string. */
Jul 6, 2017
Jul 6, 2017
378
Jul 6, 2017
Jul 6, 2017
379
/* The string is capitalized! Figure out the REAL case... */
Jul 7, 2017
Jul 7, 2017
380
return cvtPathToCorrectCase(retval);
Jul 6, 2017
Jul 6, 2017
381
382
} /* __PHYSFS_platformCalcBaseDir */
Jul 6, 2017
Jul 6, 2017
383
char *__PHYSFS_platformCalcUserDir(void)
Jul 6, 2017
Jul 6, 2017
384
{
Aug 14, 2017
Aug 14, 2017
385
return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME: ? */
Jul 6, 2017
Jul 6, 2017
386
} /* __PHYSFS_platformCalcUserDir */
Jul 6, 2017
Jul 6, 2017
387
Jul 6, 2017
Jul 6, 2017
388
389
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
Aug 14, 2017
Aug 14, 2017
390
return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME: ? */
Aug 6, 2017
Aug 6, 2017
391
} /* __PHYSFS_platformCalcPrefDir */
Jul 6, 2017
Jul 6, 2017
392
Aug 19, 2017
Aug 19, 2017
393
PHYSFS_EnumerateCallbackResult __PHYSFS_platformEnumerate(const char *dirname,
Aug 12, 2017
Aug 12, 2017
394
395
PHYSFS_EnumerateCallback callback,
const char *origdir, void *callbackdata)
Jul 6, 2017
Jul 6, 2017
396
{
Aug 19, 2017
Aug 19, 2017
397
PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
Jul 7, 2017
Jul 7, 2017
398
size_t utf8len = strlen(dirname);
Jul 7, 2017
Jul 7, 2017
399
400
char *utf8 = (char *) __PHYSFS_smallAlloc(utf8len + 5);
char *cpspec = NULL;
Jul 6, 2017
Jul 6, 2017
401
402
403
404
405
FILEFINDBUF3 fb;
HDIR hdir = HDIR_CREATE;
ULONG count = 1;
APIRET rc;
Aug 19, 2017
Aug 19, 2017
406
BAIL_IF(!utf8, PHYSFS_ERR_OUT_OF_MEMORY, PHYSFS_ENUM_ERROR);
Jul 7, 2017
Jul 7, 2017
407
408
409
410
411
412
strcpy(utf8, dirname);
if (utf8[utf8len - 1] != '\\')
strcpy(utf8 + utf8len, "\\*.*");
else
strcpy(utf8 + utf8len, "*.*");
Jul 6, 2017
Jul 6, 2017
413
Jul 7, 2017
Jul 7, 2017
414
415
cpspec = cvtUtf8ToCodepage(utf8);
__PHYSFS_smallFree(utf8);
Aug 19, 2017
Aug 19, 2017
416
BAIL_IF_ERRPASS(!cpspec, PHYSFS_ENUM_ERROR);
Jul 6, 2017
Jul 6, 2017
417
Jul 7, 2017
Jul 7, 2017
418
rc = DosFindFirst((unsigned char *) cpspec, &hdir,
Jul 6, 2017
Jul 6, 2017
419
420
421
FILE_DIRECTORY | FILE_ARCHIVED |
FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
&fb, sizeof (fb), &count, FIL_STANDARD);
Jul 7, 2017
Jul 7, 2017
422
allocator.Free(cpspec);
Jul 6, 2017
Jul 6, 2017
423
Aug 19, 2017
Aug 19, 2017
424
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), PHYSFS_ENUM_ERROR);
Jul 6, 2017
Jul 6, 2017
425
426
427
428
while (count == 1)
{
if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
Jul 7, 2017
Jul 7, 2017
429
430
{
utf8 = cvtCodepageToUtf8(fb.achName);
Aug 12, 2017
Aug 12, 2017
431
if (!utf8)
Aug 19, 2017
Aug 19, 2017
432
retval = PHYSFS_ENUM_ERROR;
Aug 12, 2017
Aug 12, 2017
433
else
Jul 7, 2017
Jul 7, 2017
434
{
Aug 12, 2017
Aug 12, 2017
435
retval = callback(callbackdata, origdir, utf8);
Jul 7, 2017
Jul 7, 2017
436
allocator.Free(utf8);
Aug 19, 2017
Aug 19, 2017
437
if (retval == PHYSFS_ENUM_ERROR)
Aug 12, 2017
Aug 12, 2017
438
PHYSFS_setErrorCode(PHYSFS_ERR_APP_CALLBACK);
Aug 12, 2017
Aug 12, 2017
439
} /* else */
Jul 7, 2017
Jul 7, 2017
440
} /* if */
Aug 12, 2017
Aug 12, 2017
441
Aug 19, 2017
Aug 19, 2017
442
if (retval != PHYSFS_ENUM_OK)
Aug 12, 2017
Aug 12, 2017
443
444
break;
Jul 6, 2017
Jul 6, 2017
445
446
447
448
DosFindNext(hdir, &fb, sizeof (fb), &count);
} /* while */
DosFindClose(hdir);
Aug 12, 2017
Aug 12, 2017
449
450
451
return retval;
} /* __PHYSFS_platformEnumerate */
Jul 6, 2017
Jul 6, 2017
452
453
454
455
456
char *__PHYSFS_platformCurrentDir(void)
{
char *retval;
Jul 7, 2017
Jul 7, 2017
457
458
char *cpstr;
char *utf8;
Jul 6, 2017
Jul 6, 2017
459
460
461
462
463
464
465
ULONG currentDisk;
ULONG dummy;
ULONG pathSize = 0;
APIRET rc;
BYTE byte;
rc = DosQueryCurrentDisk(&currentDisk, &dummy);
Jul 6, 2017
Jul 6, 2017
466
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
467
468
469
470
/* The first call just tells us how much space we need for the string. */
rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
pathSize++; /* Add space for null terminator. */
Jul 7, 2017
Jul 7, 2017
471
472
cpstr = (char *) __PHYSFS_smallAlloc(pathSize);
BAIL_IF(cpstr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 6, 2017
Jul 6, 2017
473
474
/* Actually get the string this time. */
Jul 7, 2017
Jul 7, 2017
475
rc = DosQueryCurrentDir(currentDisk, (PBYTE) cpstr, &pathSize);
Jul 6, 2017
Jul 6, 2017
476
477
if (rc != NO_ERROR)
{
Jul 7, 2017
Jul 7, 2017
478
__PHYSFS_smallFree(cpstr);
Jul 6, 2017
Jul 6, 2017
479
BAIL(errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
480
481
} /* if */
Jul 7, 2017
Jul 7, 2017
482
483
484
485
486
487
488
489
490
491
492
493
utf8 = cvtCodepageToUtf8(cpstr);
__PHYSFS_smallFree(cpstr);
BAIL_IF_ERRPASS(utf8 == NULL, NULL);
/* +4 for "x:\\" drive selector and null terminator. */
retval = (char *) allocator.Malloc(strlen(utf8) + 4);
if (retval == NULL)
{
allocator.Free(utf8);
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
} /* if */
Jul 6, 2017
Jul 6, 2017
494
495
496
retval[0] = ('A' + (currentDisk - 1));
retval[1] = ':';
retval[2] = '\\';
Jul 7, 2017
Jul 7, 2017
497
strcpy(retval + 3, utf8);
Jul 6, 2017
Jul 6, 2017
498
Jul 7, 2017
Jul 7, 2017
499
allocator.Free(utf8);
Jul 6, 2017
Jul 6, 2017
500
501
return retval;
Jul 7, 2017
Jul 7, 2017
502
} /* __PHYSFS_platformCurrentDir */
Jul 6, 2017
Jul 6, 2017
503
504
Jul 7, 2017
Jul 7, 2017
505
int __PHYSFS_platformMkDir(const char *filename)
Jul 6, 2017
Jul 6, 2017
506
{
Jul 7, 2017
Jul 7, 2017
507
508
509
510
511
APIRET rc;
char *cpstr = cvtUtf8ToCodepage(filename);
BAIL_IF_ERRPASS(!cpstr, 0);
rc = DosCreateDir((unsigned char *) cpstr, NULL);
allocator.Free(cpstr);
Jul 6, 2017
Jul 6, 2017
512
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
513
514
515
516
return 1;
} /* __PHYSFS_platformMkDir */
Jul 7, 2017
Jul 7, 2017
517
static HFILE openFile(const char *filename, const ULONG flags, const ULONG mode)
Jul 6, 2017
Jul 6, 2017
518
{
Jul 7, 2017
Jul 7, 2017
519
520
char *cpfname = cvtUtf8ToCodepage(filename);
ULONG action = 0;
Jul 6, 2017
Jul 6, 2017
521
HFILE hfile = NULLHANDLE;
Jul 7, 2017
Jul 7, 2017
522
APIRET rc;
Jul 6, 2017
Jul 6, 2017
523
Jul 7, 2017
Jul 7, 2017
524
BAIL_IF_ERRPASS(!cpfname, 0);
Jul 6, 2017
Jul 6, 2017
525
Jul 7, 2017
Jul 7, 2017
526
527
528
rc = DosOpen(cpfname, &hfile, &action, 0, FILE_NORMAL, flags, mode, NULL);
allocator.Free(cpfname);
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
529
Jul 7, 2017
Jul 7, 2017
530
531
return hfile;
} /* openFile */
Jul 6, 2017
Jul 6, 2017
532
Jul 7, 2017
Jul 7, 2017
533
void *__PHYSFS_platformOpenRead(const char *filename)
Jul 6, 2017
Jul 6, 2017
534
535
{
/*
Jul 7, 2017
Jul 7, 2017
536
* File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
Jul 6, 2017
Jul 6, 2017
537
538
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
Jul 7, 2017
Jul 7, 2017
539
540
541
542
543
544
return (void *) openFile(filename,
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
OPEN_ACCESS_READONLY);
} /* __PHYSFS_platformOpenRead */
Jul 6, 2017
Jul 6, 2017
545
Jul 7, 2017
Jul 7, 2017
546
547
548
549
550
551
552
553
void *__PHYSFS_platformOpenWrite(const char *filename)
{
return (void *) openFile(filename,
OPEN_ACTION_REPLACE_IF_EXISTS |
OPEN_ACTION_CREATE_IF_NEW,
OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE);
Jul 6, 2017
Jul 6, 2017
554
555
556
} /* __PHYSFS_platformOpenWrite */
Jul 7, 2017
Jul 7, 2017
557
void *__PHYSFS_platformOpenAppend(const char *filename)
Jul 6, 2017
Jul 6, 2017
558
559
{
APIRET rc;
Jul 7, 2017
Jul 7, 2017
560
561
ULONG dummy = 0;
HFILE hfile;
Jul 6, 2017
Jul 6, 2017
562
563
564
565
566
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
Jul 7, 2017
Jul 7, 2017
567
568
569
570
571
572
hfile = openFile(filename,
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
OPEN_ACCESS_READWRITE);
BAIL_IF_ERRPASS(!hfile, NULL);
Jul 6, 2017
Jul 6, 2017
573
574
575
576
577
rc = DosSetFilePtr(hfile, 0, FILE_END, &dummy);
if (rc != NO_ERROR)
{
DosClose(hfile);
Jul 6, 2017
Jul 6, 2017
578
BAIL(errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
579
580
581
582
583
584
585
586
587
588
} /* if */
return ((void *) hfile);
} /* __PHYSFS_platformOpenAppend */
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
{
ULONG br = 0;
APIRET rc;
Jul 6, 2017
Jul 6, 2017
589
BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
Jul 6, 2017
Jul 6, 2017
590
rc = DosRead((HFILE) opaque, buf, (ULONG) len, &br);
Jul 6, 2017
Jul 6, 2017
591
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (br > 0) ? ((PHYSFS_sint64) br) : -1);
Jul 6, 2017
Jul 6, 2017
592
593
594
595
596
597
598
599
600
return (PHYSFS_sint64) br;
} /* __PHYSFS_platformRead */
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buf,
PHYSFS_uint64 len)
{
ULONG bw = 0;
APIRET rc;
Jul 6, 2017
Jul 6, 2017
601
BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
Jul 6, 2017
Jul 6, 2017
602
rc = DosWrite((HFILE) opaque, (void *) buf, (ULONG) len, &bw);
Jul 6, 2017
Jul 6, 2017
603
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (bw > 0) ? ((PHYSFS_sint64) bw) : -1);
Jul 6, 2017
Jul 6, 2017
604
605
606
607
608
609
610
611
612
613
614
615
return (PHYSFS_sint64) bw;
} /* __PHYSFS_platformWrite */
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
ULONG dummy;
HFILE hfile = (HFILE) opaque;
LONG dist = (LONG) pos;
APIRET rc;
/* hooray for 32-bit filesystem limits! :) */
Jul 6, 2017
Jul 6, 2017
616
BAIL_IF((PHYSFS_uint64) dist != pos, PHYSFS_ERR_INVALID_ARGUMENT, 0);
Jul 6, 2017
Jul 6, 2017
617
rc = DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy);
Jul 6, 2017
Jul 6, 2017
618
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
619
620
621
622
623
624
625
626
627
return 1;
} /* __PHYSFS_platformSeek */
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
ULONG pos;
HFILE hfile = (HFILE) opaque;
const APIRET rc = DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos);
Jul 6, 2017
Jul 6, 2017
628
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
Jul 6, 2017
Jul 6, 2017
629
630
631
632
633
634
635
636
637
return ((PHYSFS_sint64) pos);
} /* __PHYSFS_platformTell */
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
{
FILESTATUS3 fs;
HFILE hfile = (HFILE) opaque;
const APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
Jul 6, 2017
Jul 6, 2017
638
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
Jul 6, 2017
Jul 6, 2017
639
640
641
642
643
644
645
return ((PHYSFS_sint64) fs.cbFile);
} /* __PHYSFS_platformFileLength */
int __PHYSFS_platformFlush(void *opaque)
{
const APIRET rc = DosResetBuffer((HFILE) opaque);
Jul 6, 2017
Jul 6, 2017
646
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
647
648
649
650
651
652
653
654
655
656
return 1;
} /* __PHYSFS_platformFlush */
void __PHYSFS_platformClose(void *opaque)
{
DosClose((HFILE) opaque); /* ignore errors. You should have flushed! */
} /* __PHYSFS_platformClose */
Jul 7, 2017
Jul 7, 2017
657
int __PHYSFS_platformDelete(const char *path)
Jul 6, 2017
Jul 6, 2017
658
{
Jul 7, 2017
Jul 7, 2017
659
char *cppath = cvtUtf8ToCodepage(path);
Jul 6, 2017
Jul 6, 2017
660
FILESTATUS3 fs;
Jul 7, 2017
Jul 7, 2017
661
662
663
664
665
666
APIRET rc;
int retval = 0;
BAIL_IF_ERRPASS(!cppath, 0);
rc = DosQueryPathInfo(cppath, FIL_STANDARD, &fs, sizeof (fs));
GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
Jul 6, 2017
Jul 6, 2017
667
rc = (fs.attrFile & FILE_DIRECTORY) ? DosDeleteDir(path) : DosDelete(path);
Jul 7, 2017
Jul 7, 2017
668
669
670
671
672
673
GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
retval = 1; /* success */
done:
allocator.Free(cppath);
return retval;
Jul 6, 2017
Jul 6, 2017
674
675
676
677
678
679
680
681
} /* __PHYSFS_platformDelete */
/* Convert to a format PhysicsFS can grok... */
PHYSFS_sint64 os2TimeToUnixTime(const FDATE *date, const FTIME *time)
{
struct tm tm;
Jul 6, 2017
Jul 6, 2017
682
tm.tm_sec = ((PHYSFS_uint32) time->twosecs) * 2;
Jul 6, 2017
Jul 6, 2017
683
684
685
686
687
688
689
690
691
692
693
694
695
tm.tm_min = time->minutes;
tm.tm_hour = time->hours;
tm.tm_mday = date->day;
tm.tm_mon = date->month;
tm.tm_year = ((PHYSFS_uint32) date->year) + 80;
tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
tm.tm_yday = -1;
tm.tm_isdst = -1;
return (PHYSFS_sint64) mktime(&tm);
} /* os2TimeToUnixTime */
Jul 6, 2017
Jul 6, 2017
696
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *stat)
Jul 6, 2017
Jul 6, 2017
697
{
Jul 7, 2017
Jul 7, 2017
698
char *cpfname = cvtUtf8ToCodepage(filename);
Jul 6, 2017
Jul 6, 2017
699
FILESTATUS3 fs;
Jul 7, 2017
Jul 7, 2017
700
701
702
int retval = 0;
APIRET rc;
Jul 7, 2017
Jul 7, 2017
703
BAIL_IF_ERRPASS(!cpfname, 0);
Jul 7, 2017
Jul 7, 2017
704
705
706
rc = DosQueryPathInfo(cpfname, FIL_STANDARD, &fs, sizeof (fs));
GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
Jul 6, 2017
Jul 6, 2017
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
if (fs.attrFile & FILE_DIRECTORY)
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
else
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = fs.cbFile;
} /* else */
stat->modtime = os2TimeToUnixTime(&fs.fdateLastWrite, &fs.ftimeLastWrite);
if (stat->modtime < 0)
stat->modtime = 0;
stat->accesstime = os2TimeToUnixTime(&fs.fdateLastAccess, &fs.ftimeLastAccess);
if (stat->accesstime < 0)
stat->accesstime = 0;
stat->createtime = os2TimeToUnixTime(&fs.fdateCreation, &fs.ftimeCreation);
if (stat->createtime < 0)
stat->createtime = 0;
stat->readonly = ((fs.attrFile & FILE_READONLY) == FILE_READONLY);
Jul 7, 2017
Jul 7, 2017
732
return 1; /* success */
Jul 6, 2017
Jul 6, 2017
733
Jul 7, 2017
Jul 7, 2017
734
done:
Jul 7, 2017
Jul 7, 2017
735
allocator.Free(cpfname);
Jul 7, 2017
Jul 7, 2017
736
return retval;
Jul 6, 2017
Jul 6, 2017
737
738
739
740
741
742
743
744
745
746
747
748
749
} /* __PHYSFS_platformStat */
void *__PHYSFS_platformGetThreadID(void)
{
PTIB ptib;
PPIB ppib;
/*
* Allegedly, this API never fails, but we'll punt and return a
* default value (zero might as well do) if it does.
*/
const APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
Jul 6, 2017
Jul 6, 2017
750
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
751
752
753
754
755
756
757
758
return ((void *) ptib->tib_ordinal);
} /* __PHYSFS_platformGetThreadID */
void *__PHYSFS_platformCreateMutex(void)
{
HMTX hmtx = NULLHANDLE;
const APIRET rc = DosCreateMutexSem(NULL, &hmtx, 0, 0);
Jul 6, 2017
Jul 6, 2017
759
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
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 ((void *) hmtx);
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
DosCloseMutexSem((HMTX) mutex);
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
/* Do _NOT_ set the physfs error message in here! */
return (DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
DosReleaseMutexSem((HMTX) mutex);
} /* __PHYSFS_platformReleaseMutex */
#endif /* PHYSFS_PLATFORM_OS2 */
Jul 22, 2017
Jul 22, 2017
784
/* end of physfs_platform_os2.c ... */