Skip to content

Latest commit

 

History

History
660 lines (529 loc) · 20.5 KB

platform_os2.c

File metadata and controls

660 lines (529 loc) · 20.5 KB
 
Jul 6, 2017
Jul 6, 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* 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
#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>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include "physfs_internal.h"
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
38
case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
Jul 6, 2017
Jul 6, 2017
39
40
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
173
174
175
176
177
178
179
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;
/*!!! FIXME: Where did this go? case ERROR_DEL_CURRENT_DIRECTORY: return ERR_DEL_CWD;*/
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 */
/* (be gentle, this function isn't very robust.) */
static void cvt_path_to_correct_case(char *buf)
{
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.
*/
while (fname != NULL)
{
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... */
{
if (__PHYSFS_stricmpASCII(fb.achName, fname) == 0)
{
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 */
} /* cvt_file_to_correct_case */
int __PHYSFS_platformInit(void)
{
return 1; /* it's all good. */
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
return 1; /* success. */
} /* __PHYSFS_platformDeinit */
static int disc_is_inserted(ULONG drive)
{
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
static int is_cdrom_drive(ULONG drive)
{
PHYSFS_uint32 param, data;
ULONG ul1, ul2;
APIRET rc;
HFILE hfile = NULLHANDLE;
Jul 6, 2017
Jul 6, 2017
180
unsigned char drivename[3] = { 0, ':', '\0' };
Jul 6, 2017
Jul 6, 2017
181
182
183
drivename[0] = 'A' + drive;
Jul 6, 2017
Jul 6, 2017
184
185
186
187
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
188
189
if (rc != NO_ERROR)
return 0;
Jul 6, 2017
Jul 6, 2017
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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));
} /* is_cdrom_drive */
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
208
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc),);
Jul 6, 2017
Jul 6, 2017
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
{
if (drivemap & bit) /* this logical drive exists. */
{
if ((is_cdrom_drive(i)) && (disc_is_inserted(i)))
{
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
226
227
228
229
230
231
232
{
char *retval = NULL;
char buf[CCHMAXPATH];
APIRET rc;
PTIB ptib;
PPIB ppib;
PHYSFS_sint32 len;
Jul 6, 2017
Jul 6, 2017
233
Jul 6, 2017
Jul 6, 2017
234
rc = DosGetInfoBlocks(&ptib, &ppib);
Jul 6, 2017
Jul 6, 2017
235
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
236
rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
Jul 6, 2017
Jul 6, 2017
237
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
238
Jul 6, 2017
Jul 6, 2017
239
240
241
242
/* chop off filename, leave path. */
for (len = strlen(buf) - 1; len >= 0; len--)
{
if (buf[len] == '\\')
Jul 6, 2017
Jul 6, 2017
243
{
Jul 6, 2017
Jul 6, 2017
244
buf[len + 1] = '\0';
Jul 6, 2017
Jul 6, 2017
245
246
247
break;
} /* if */
} /* for */
Jul 6, 2017
Jul 6, 2017
248
Jul 6, 2017
Jul 6, 2017
249
assert(len > 0); /* should have been a "x:\\" on the front on string. */
Jul 6, 2017
Jul 6, 2017
250
Jul 6, 2017
Jul 6, 2017
251
252
/* The string is capitalized! Figure out the REAL case... */
cvt_path_to_correct_case(buf);
Jul 6, 2017
Jul 6, 2017
253
Jul 6, 2017
Jul 6, 2017
254
retval = (char *) allocator.Malloc(len + 1);
Jul 6, 2017
Jul 6, 2017
255
BAIL_IF(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 6, 2017
Jul 6, 2017
256
strcpy(retval, buf);
Jul 6, 2017
Jul 6, 2017
257
258
259
260
261
262
263
264
265
266
return retval;
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
return NULL; /* (*shrug*) */
} /* __PHYSFS_platformGetUserName */
Jul 6, 2017
Jul 6, 2017
267
char *__PHYSFS_platformCalcUserDir(void)
Jul 6, 2017
Jul 6, 2017
268
{
Jul 6, 2017
Jul 6, 2017
269
270
return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME: ? */
} /* __PHYSFS_platformCalcUserDir */
Jul 6, 2017
Jul 6, 2017
271
Jul 6, 2017
Jul 6, 2017
272
273
274
275
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME: ? */
}
Jul 6, 2017
Jul 6, 2017
276
277
278
279
280
281
282
283
284
285
286
287
/* !!! FIXME: can we lose the malloc here? */
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 = allocator.Malloc(len);
char *p;
Jul 6, 2017
Jul 6, 2017
288
BAIL_IF(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 6, 2017
Jul 6, 2017
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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 */
void __PHYSFS_platformEnumerateFiles(const char *dirname,
PHYSFS_EnumFilesCallback callback,
const char *origdir,
void *callbackdata)
Jul 6, 2017
Jul 6, 2017
311
{
Jul 6, 2017
Jul 6, 2017
312
313
314
315
316
317
char spec[CCHMAXPATH];
FILEFINDBUF3 fb;
HDIR hdir = HDIR_CREATE;
ULONG count = 1;
APIRET rc;
Jul 6, 2017
Jul 6, 2017
318
BAIL_IF(strlen(dirname) > sizeof (spec) - 5, PHYSFS_ERR_BAD_FILENAME,);
Jul 6, 2017
Jul 6, 2017
319
320
321
322
323
324
325
326
327
strcpy(spec, dirname);
strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*");
rc = DosFindFirst((unsigned char *) spec, &hdir,
FILE_DIRECTORY | FILE_ARCHIVED |
FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
&fb, sizeof (fb), &count, FIL_STANDARD);
Jul 6, 2017
Jul 6, 2017
328
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc),);
Jul 6, 2017
Jul 6, 2017
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
while (count == 1)
{
if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
callback(callbackdata, origdir, fb.achName);
DosFindNext(hdir, &fb, sizeof (fb), &count);
} /* while */
DosFindClose(hdir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
char *retval;
ULONG currentDisk;
ULONG dummy;
ULONG pathSize = 0;
APIRET rc;
BYTE byte;
rc = DosQueryCurrentDisk(&currentDisk, &dummy);
Jul 6, 2017
Jul 6, 2017
352
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
353
354
355
356
357
/* 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. */
retval = (char *) allocator.Malloc(pathSize + 3); /* plus "x:\\" */
Jul 6, 2017
Jul 6, 2017
358
BAIL_IF(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 6, 2017
Jul 6, 2017
359
360
361
362
363
364
/* Actually get the string this time. */
rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
if (rc != NO_ERROR)
{
allocator.Free(retval);
Jul 6, 2017
Jul 6, 2017
365
BAIL(errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
} /* if */
retval[0] = ('A' + (currentDisk - 1));
retval[1] = ':';
retval[2] = '\\';
return retval;
} /* __PHYSFS_platformCurrentDir */
char *__PHYSFS_platformRealPath(const char *_path)
{
const unsigned char *path = (const unsigned char *) _path;
char buf[CCHMAXPATH];
char *retval;
APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
Jul 6, 2017
Jul 6, 2017
381
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
382
retval = (char *) allocator.Malloc(strlen(buf) + 1);
Jul 6, 2017
Jul 6, 2017
383
BAIL_IF(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 6, 2017
Jul 6, 2017
384
385
386
387
388
389
390
391
392
strcpy(retval, buf);
return retval;
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *_filename)
{
const unsigned char *filename = (const unsigned char *) _filename;
const APIRET rc = DosCreateDir(filename, NULL);
Jul 6, 2017
Jul 6, 2017
393
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
return 1;
} /* __PHYSFS_platformMkDir */
void *__PHYSFS_platformOpenRead(const char *_filename)
{
const unsigned char *filename = (const unsigned char *) _filename;
ULONG actionTaken = 0;
HFILE hfile = NULLHANDLE;
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
const APIRET rc = DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
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, NULL);
Jul 6, 2017
Jul 6, 2017
413
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
return ((void *) hfile);
} /* __PHYSFS_platformOpenRead */
void *__PHYSFS_platformOpenWrite(const char *_filename)
{
const unsigned char *filename = (const unsigned char *) _filename;
ULONG actionTaken = 0;
HFILE hfile = NULLHANDLE;
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
const APIRET rc = DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
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 |
OPEN_ACCESS_READWRITE, NULL);
Jul 6, 2017
Jul 6, 2017
434
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
return ((void *) hfile);
} /* __PHYSFS_platformOpenWrite */
void *__PHYSFS_platformOpenAppend(const char *_filename)
{
const unsigned char *filename = (const unsigned char *) _filename;
ULONG dummy = 0;
HFILE hfile = NULLHANDLE;
APIRET rc;
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
rc = DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL,
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, NULL);
Jul 6, 2017
Jul 6, 2017
457
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
458
459
460
461
462
rc = DosSetFilePtr(hfile, 0, FILE_END, &dummy);
if (rc != NO_ERROR)
{
DosClose(hfile);
Jul 6, 2017
Jul 6, 2017
463
BAIL(errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
464
465
466
467
468
469
470
471
472
473
} /* 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
474
BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
Jul 6, 2017
Jul 6, 2017
475
rc = DosRead((HFILE) opaque, buf, (ULONG) len, &br);
Jul 6, 2017
Jul 6, 2017
476
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (br > 0) ? ((PHYSFS_sint64) br) : -1);
Jul 6, 2017
Jul 6, 2017
477
478
479
480
481
482
483
484
485
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
486
BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
Jul 6, 2017
Jul 6, 2017
487
rc = DosWrite((HFILE) opaque, (void *) buf, (ULONG) len, &bw);
Jul 6, 2017
Jul 6, 2017
488
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (bw > 0) ? ((PHYSFS_sint64) bw) : -1);
Jul 6, 2017
Jul 6, 2017
489
490
491
492
493
494
495
496
497
498
499
500
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
501
BAIL_IF((PHYSFS_uint64) dist != pos, PHYSFS_ERR_INVALID_ARGUMENT, 0);
Jul 6, 2017
Jul 6, 2017
502
rc = DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy);
Jul 6, 2017
Jul 6, 2017
503
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
504
505
506
507
508
509
510
511
512
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
513
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
Jul 6, 2017
Jul 6, 2017
514
515
516
517
518
519
520
521
522
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
523
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
Jul 6, 2017
Jul 6, 2017
524
525
526
527
528
529
530
return ((PHYSFS_sint64) fs.cbFile);
} /* __PHYSFS_platformFileLength */
int __PHYSFS_platformFlush(void *opaque)
{
const APIRET rc = DosResetBuffer((HFILE) opaque);
Jul 6, 2017
Jul 6, 2017
531
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
return 1;
} /* __PHYSFS_platformFlush */
void __PHYSFS_platformClose(void *opaque)
{
DosClose((HFILE) opaque); /* ignore errors. You should have flushed! */
} /* __PHYSFS_platformClose */
int __PHYSFS_platformDelete(const char *_path)
{
FILESTATUS3 fs;
const unsigned char *path = (const unsigned char *) _path;
APIRET rc = DosQueryPathInfo(path, FIL_STANDARD, &fs, sizeof (fs));
Jul 6, 2017
Jul 6, 2017
547
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
548
rc = (fs.attrFile & FILE_DIRECTORY) ? DosDeleteDir(path) : DosDelete(path);
Jul 6, 2017
Jul 6, 2017
549
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
550
551
552
553
554
555
556
557
558
return 1;
} /* __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
559
tm.tm_sec = ((PHYSFS_uint32) time->twosecs) * 2;
Jul 6, 2017
Jul 6, 2017
560
561
562
563
564
565
566
567
568
569
570
571
572
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
573
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *stat)
Jul 6, 2017
Jul 6, 2017
574
575
{
FILESTATUS3 fs;
Jul 6, 2017
Jul 6, 2017
576
const unsigned char *fname = (const unsigned char *) filename;
Jul 6, 2017
Jul 6, 2017
577
const APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
Jul 6, 2017
Jul 6, 2017
578
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
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
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);
return 1;
} /* __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
619
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
Jul 6, 2017
Jul 6, 2017
620
621
622
623
624
625
626
627
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
628
BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
Jul 6, 2017
Jul 6, 2017
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
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 */
/* !!! FIXME: Don't use C runtime for allocators? */
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
return 0; /* just use malloc() and friends. */
} /* __PHYSFS_platformSetDefaultAllocator */
#endif /* PHYSFS_PLATFORM_OS2 */
/* end of os2.c ... */