Skip to content

Latest commit

 

History

History
704 lines (557 loc) · 21 KB

platform_os2.c

File metadata and controls

704 lines (557 loc) · 21 KB
 
Jul 27, 2002
Jul 27, 2002
1
2
3
/*
* OS/2 support routines for PhysicsFS.
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Jul 27, 2002
Jul 27, 2002
5
6
7
8
*
* This file written by Ryan C. Gordon.
*/
Mar 11, 2007
Mar 11, 2007
9
10
11
12
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_OS2
Jul 27, 2002
Jul 27, 2002
13
14
15
16
17
18
#define INCL_DOSSEMAPHORES
#define INCL_DOSDATETIME
#define INCL_DOSFILEMGR
#define INCL_DOSMODULEMGR
#define INCL_DOSERRORS
Jul 27, 2002
Jul 27, 2002
19
#define INCL_DOSPROCESS
Jul 27, 2002
Jul 27, 2002
20
#define INCL_DOSDEVICES
Jul 27, 2002
Jul 27, 2002
21
22
#define INCL_DOSDEVIOCTL
#define INCL_DOSMISC
Jul 27, 2002
Jul 27, 2002
23
24
25
#include <os2.h>
#include <errno.h>
Jul 27, 2002
Jul 27, 2002
26
27
28
29
30
31
#include <time.h>
#include <ctype.h>
#include "physfs_internal.h"
const char *__PHYSFS_platformDirSeparator = "\\";
Jul 27, 2002
Jul 27, 2002
32
33
Jul 28, 2002
Jul 28, 2002
34
static const char *get_os2_error_string(APIRET rc)
Jul 27, 2002
Jul 27, 2002
35
{
Jul 28, 2002
Jul 28, 2002
36
switch (rc)
Jul 27, 2002
Jul 27, 2002
37
{
Jan 28, 2010
Jan 28, 2010
38
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
case NO_ERROR: return NULL; /* not an error. */
case ERROR_INTERRUPT: return NULL; /* not an error. */
case ERROR_TIMEOUT: return NULL; /* not an error. */
case ERROR_NOT_ENOUGH_MEMORY: return ERR_OUT_OF_MEMORY;
case ERROR_FILE_NOT_FOUND: return ERR_NO_SUCH_FILE;
case ERROR_PATH_NOT_FOUND: return ERR_NO_SUCH_PATH;
case ERROR_ACCESS_DENIED: return ERR_ACCESS_DENIED;
case ERROR_NOT_DOS_DISK: return ERR_NOT_A_DOS_DISK;
case ERROR_SHARING_VIOLATION: return ERR_SHARING_VIOLATION;
case ERROR_CANNOT_MAKE: return ERR_CANNOT_MAKE;
case ERROR_DEVICE_IN_USE: return ERR_DEV_IN_USE;
case ERROR_OPEN_FAILED: return ERR_OPEN_FAILED;
case ERROR_DISK_FULL: return ERR_DISK_FULL;
case ERROR_PIPE_BUSY: return ERR_PIPE_BUSY;
case ERROR_SHARING_BUFFER_EXCEEDED: return ERR_SHARING_BUF_EXCEEDED;
case ERROR_FILENAME_EXCED_RANGE: return ERR_BAD_FILENAME;
case ERROR_META_EXPANSION_TOO_LONG: return ERR_BAD_FILENAME;
case ERROR_TOO_MANY_HANDLES: return ERR_TOO_MANY_HANDLES;
case ERROR_TOO_MANY_OPEN_FILES: return ERR_TOO_MANY_HANDLES;
case ERROR_NO_MORE_SEARCH_HANDLES: return ERR_TOO_MANY_HANDLES;
case ERROR_SEEK_ON_DEVICE: return ERR_SEEK_ERROR;
case ERROR_NEGATIVE_SEEK: return ERR_SEEK_OUT_OF_RANGE;
/*!!! FIXME: Where did this go? case ERROR_DEL_CURRENT_DIRECTORY: return ERR_DEL_CWD;*/
case ERROR_WRITE_PROTECT: return ERR_WRITE_PROTECT_ERROR;
case ERROR_WRITE_FAULT: return ERR_WRITE_FAULT;
case ERROR_LOCK_VIOLATION: return ERR_LOCK_VIOLATION;
case ERROR_GEN_FAILURE: return ERR_GEN_FAILURE;
case ERROR_UNCERTAIN_MEDIA: return ERR_UNCERTAIN_MEDIA;
case ERROR_PROTECTION_VIOLATION: return ERR_PROT_VIOLATION;
case ERROR_BROKEN_PIPE: return ERR_BROKEN_PIPE;
Jul 27, 2002
Jul 27, 2002
68
69
70
71
72
73
74
75
76
77
78
79
80
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:
Jul 28, 2002
Jul 28, 2002
81
case ERROR_NOT_OWNER:
Jan 28, 2010
Jan 28, 2010
82
return ERR_PHYSFS_BAD_OS_CALL;
Jul 27, 2002
Jul 27, 2002
83
Jan 28, 2010
Jan 28, 2010
84
default: return ERR_OS2_GENERIC;
Jul 28, 2002
Jul 28, 2002
85
} /* switch */
Jul 27, 2002
Jul 27, 2002
86
Jan 28, 2010
Jan 28, 2010
87
return NULL;
Jul 28, 2002
Jul 28, 2002
88
} /* get_os2_error_string */
Jul 27, 2002
Jul 27, 2002
89
90
Jul 28, 2002
Jul 28, 2002
91
92
93
94
static APIRET os2err(APIRET retval)
{
char buf[128];
const char *err = get_os2_error_string(retval);
Mar 29, 2009
Mar 29, 2009
95
if (strcmp(err, ERR_OS2_GENERIC) == 0)
Jul 28, 2002
Jul 28, 2002
96
{
Nov 11, 2003
Nov 11, 2003
97
snprintf(buf, sizeof (buf), ERR_OS2_GENERIC, (int) retval);
Jul 28, 2002
Jul 28, 2002
98
99
err = buf;
} /* if */
Jul 27, 2002
Jul 27, 2002
100
101
102
103
if (err != NULL)
__PHYSFS_setError(err);
Jan 28, 2010
Jan 28, 2010
104
return retval;
Jul 27, 2002
Jul 27, 2002
105
106
107
} /* os2err */
Jul 28, 2002
Jul 28, 2002
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
/* (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';
Mar 29, 2009
Mar 29, 2009
139
rc = DosFindFirst((unsigned char *) spec, &hdir, FILE_DIRECTORY,
Jul 28, 2002
Jul 28, 2002
140
141
142
143
144
&fb, sizeof (fb), &count, FIL_STANDARD);
if (rc == NO_ERROR)
{
while (count == 1) /* while still entries to enumerate... */
{
Mar 15, 2007
Mar 15, 2007
145
if (__PHYSFS_stricmpASCII(fb.achName, fname) == 0)
Jul 28, 2002
Jul 28, 2002
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{
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 */
Jul 27, 2002
Jul 27, 2002
167
168
169
170
171
172
static char *baseDir = NULL;
int __PHYSFS_platformInit(void)
{
char buf[CCHMAXPATH];
APIRET rc;
Jul 27, 2002
Jul 27, 2002
173
174
PTIB ptib;
PPIB ppib;
Jul 27, 2002
Jul 27, 2002
175
PHYSFS_sint32 len;
Jul 27, 2002
Jul 27, 2002
176
177
assert(baseDir == NULL);
Jul 27, 2002
Jul 27, 2002
178
179
BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, NULL, 0);
rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
Jul 27, 2002
Jul 27, 2002
180
181
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0);
Jul 27, 2002
Jul 27, 2002
182
183
184
185
186
/* chop off filename, leave path. */
for (len = strlen(buf) - 1; len >= 0; len--)
{
if (buf[len] == '\\')
{
Jul 27, 2002
Jul 27, 2002
187
buf[len] = '\0';
Jul 27, 2002
Jul 27, 2002
188
189
190
191
break;
} /* if */
} /* for */
Jul 27, 2002
Jul 27, 2002
192
assert(len > 0); /* should have been a "x:\\" on the front on string. */
Jul 27, 2002
Jul 27, 2002
193
Jul 28, 2002
Jul 28, 2002
194
195
196
/* The string is capitalized! Figure out the REAL case... */
cvt_path_to_correct_case(buf);
Mar 14, 2005
Mar 14, 2005
197
baseDir = (char *) allocator.Malloc(len + 1);
Jul 27, 2002
Jul 27, 2002
198
BAIL_IF_MACRO(baseDir == NULL, ERR_OUT_OF_MEMORY, 0);
Jul 27, 2002
Jul 27, 2002
199
strcpy(baseDir, buf);
Jan 28, 2010
Jan 28, 2010
200
return 1; /* success. */
Jul 27, 2002
Jul 27, 2002
201
202
203
204
205
206
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
assert(baseDir != NULL);
Mar 14, 2005
Mar 14, 2005
207
allocator.Free(baseDir);
Jul 27, 2002
Jul 27, 2002
208
baseDir = NULL;
Jan 28, 2010
Jan 28, 2010
209
return 1; /* success. */
Jul 27, 2002
Jul 27, 2002
210
211
212
213
214
215
216
217
218
219
} /* __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);
Jan 28, 2010
Jan 28, 2010
220
return (rc == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
221
222
223
} /* is_cdrom_inserted */
Jul 27, 2002
Jul 27, 2002
224
225
226
/* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
#define CD01 0x31304443
Jul 27, 2002
Jul 27, 2002
227
228
static int is_cdrom_drive(ULONG drive)
{
Jul 27, 2002
Jul 27, 2002
229
PHYSFS_uint32 param, data;
Jul 27, 2002
Jul 27, 2002
230
231
ULONG ul1, ul2;
APIRET rc;
Jul 27, 2002
Jul 27, 2002
232
HFILE hfile = NULLHANDLE;
Mar 29, 2009
Mar 29, 2009
233
unsigned char drivename[3] = { 'A' + drive, ':', '\0' };
Jul 27, 2002
Jul 27, 2002
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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);
BAIL_IF_MACRO(rc != NO_ERROR, NULL, 0);
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);
Jan 28, 2010
Jan 28, 2010
248
return ((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
Jul 27, 2002
Jul 27, 2002
249
250
251
} /* is_cdrom_drive */
Sep 29, 2004
Sep 29, 2004
252
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Jul 27, 2002
Jul 27, 2002
253
{
Sep 29, 2004
Sep 29, 2004
254
255
ULONG dummy = 0;
ULONG drivemap = 0;
Jul 27, 2002
Jul 27, 2002
256
ULONG i, bit;
Sep 29, 2004
Sep 29, 2004
257
APIRET rc = DosQueryCurrentDisk(&dummy, &drivemap);
Jul 21, 2005
Jul 21, 2005
258
259
if (os2err(rc) != NO_ERROR)
return;
Jul 27, 2002
Jul 27, 2002
260
Jul 27, 2002
Jul 27, 2002
261
for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
Jul 27, 2002
Jul 27, 2002
262
263
264
265
266
{
if (drivemap & bit) /* this logical drive exists. */
{
if ((is_cdrom_drive(i)) && (disc_is_inserted(i)))
{
Sep 29, 2004
Sep 29, 2004
267
268
269
char drive[4] = "x:\\";
drive[0] = ('A' + i);
cb(data, drive);
Jul 27, 2002
Jul 27, 2002
270
271
272
273
274
275
276
277
} /* if */
} /* if */
} /* for */
} /* __PHYSFS_platformDetectAvailableCDs */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
Mar 14, 2005
Mar 14, 2005
278
char *retval = (char *) allocator.Malloc(strlen(baseDir) + 1);
Jul 27, 2002
Jul 27, 2002
279
280
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, baseDir); /* calculated at init time. */
Jan 28, 2010
Jan 28, 2010
281
return retval;
Jul 27, 2002
Jul 27, 2002
282
283
284
285
286
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
Jan 28, 2010
Jan 28, 2010
287
return NULL; /* (*shrug*) */
Jul 27, 2002
Jul 27, 2002
288
289
290
291
292
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
Jan 28, 2010
Jan 28, 2010
293
return __PHYSFS_platformCalcBaseDir(NULL);
Jul 27, 2002
Jul 27, 2002
294
295
296
} /* __PHYSFS_platformGetUserDir */
Mar 14, 2005
Mar 14, 2005
297
/* !!! FIXME: can we lose the malloc here? */
Jul 27, 2002
Jul 27, 2002
298
299
300
301
302
303
304
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
305
char *retval = allocator.Malloc(len);
Jul 27, 2002
Jul 27, 2002
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
char *p;
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
323
return retval;
Jul 27, 2002
Jul 27, 2002
324
325
326
} /* __PHYSFS_platformCvtToDependent */
Sep 29, 2004
Sep 29, 2004
327
328
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
Sep 18, 2005
Sep 18, 2005
329
330
PHYSFS_EnumFilesCallback callback,
const char *origdir,
Sep 29, 2004
Sep 29, 2004
331
void *callbackdata)
Jul 27, 2002
Jul 27, 2002
332
333
{
char spec[CCHMAXPATH];
Jul 27, 2002
Jul 27, 2002
334
FILEFINDBUF3 fb;
Jul 27, 2002
Jul 27, 2002
335
336
337
338
HDIR hdir = HDIR_CREATE;
ULONG count = 1;
APIRET rc;
Jul 21, 2005
Jul 21, 2005
339
340
341
342
343
344
if (strlen(dirname) > sizeof (spec) - 5)
{
__PHYSFS_setError(ERR_BAD_FILENAME);
return;
} /* if */
Jul 27, 2002
Jul 27, 2002
345
strcpy(spec, dirname);
Jul 27, 2002
Jul 27, 2002
346
strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*");
Jul 27, 2002
Jul 27, 2002
347
Mar 29, 2009
Mar 29, 2009
348
rc = DosFindFirst((unsigned char *) spec, &hdir,
Jul 27, 2002
Jul 27, 2002
349
350
FILE_DIRECTORY | FILE_ARCHIVED |
FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
Jul 27, 2002
Jul 27, 2002
351
&fb, sizeof (fb), &count, FIL_STANDARD);
Jul 21, 2005
Jul 21, 2005
352
353
354
355
if (os2err(rc) != NO_ERROR)
return;
Jul 27, 2002
Jul 27, 2002
356
357
while (count == 1)
{
Jul 27, 2002
Jul 27, 2002
358
if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
Sep 18, 2005
Sep 18, 2005
359
callback(callbackdata, origdir, fb.achName);
Jul 27, 2002
Jul 27, 2002
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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);
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
/* 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. */
Mar 14, 2005
Mar 14, 2005
383
retval = (char *) allocator.Malloc(pathSize + 3); /* plus "x:\\" */
Jul 27, 2002
Jul 27, 2002
384
385
386
387
388
389
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/* Actually get the string this time. */
rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
if (os2err(rc) != NO_ERROR)
{
Mar 14, 2005
Mar 14, 2005
390
allocator.Free(retval);
Jan 28, 2010
Jan 28, 2010
391
return NULL;
Jul 27, 2002
Jul 27, 2002
392
393
394
395
396
} /* if */
retval[0] = ('A' + (currentDisk - 1));
retval[1] = ':';
retval[2] = '\\';
Jan 28, 2010
Jan 28, 2010
397
return retval;
Jul 27, 2002
Jul 27, 2002
398
399
400
} /* __PHYSFS_platformCurrentDir */
Mar 29, 2009
Mar 29, 2009
401
char *__PHYSFS_platformRealPath(const char *_path)
Jul 27, 2002
Jul 27, 2002
402
{
Mar 29, 2009
Mar 29, 2009
403
const unsigned char *path = (const unsigned char *) _path;
Jul 27, 2002
Jul 27, 2002
404
405
char buf[CCHMAXPATH];
char *retval;
Jul 27, 2002
Jul 27, 2002
406
APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
Jul 27, 2002
Jul 27, 2002
407
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
Mar 14, 2005
Mar 14, 2005
408
retval = (char *) allocator.Malloc(strlen(buf) + 1);
Jul 27, 2002
Jul 27, 2002
409
410
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, buf);
Jan 28, 2010
Jan 28, 2010
411
return retval;
Jul 27, 2002
Jul 27, 2002
412
413
414
} /* __PHYSFS_platformRealPath */
Mar 29, 2009
Mar 29, 2009
415
int __PHYSFS_platformMkDir(const char *_filename)
Jul 27, 2002
Jul 27, 2002
416
{
Mar 29, 2009
Mar 29, 2009
417
const unsigned char *filename = (const unsigned char *) _filename;
Jan 28, 2010
Jan 28, 2010
418
return (os2err(DosCreateDir(filename, NULL)) == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
419
420
421
} /* __PHYSFS_platformMkDir */
Mar 29, 2009
Mar 29, 2009
422
void *__PHYSFS_platformOpenRead(const char *_filename)
Jul 27, 2002
Jul 27, 2002
423
{
Mar 29, 2009
Mar 29, 2009
424
const unsigned char *filename = (const unsigned char *) _filename;
Jul 27, 2002
Jul 27, 2002
425
426
427
428
429
430
431
432
433
434
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.
*/
os2err(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 |
Jul 27, 2002
Jul 27, 2002
435
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
Jul 27, 2002
Jul 27, 2002
436
437
OPEN_ACCESS_READONLY, NULL));
Jan 28, 2010
Jan 28, 2010
438
return ((void *) hfile);
Jul 27, 2002
Jul 27, 2002
439
440
441
} /* __PHYSFS_platformOpenRead */
Mar 29, 2009
Mar 29, 2009
442
void *__PHYSFS_platformOpenWrite(const char *_filename)
Jul 27, 2002
Jul 27, 2002
443
{
Mar 29, 2009
Mar 29, 2009
444
const unsigned char *filename = (const unsigned char *) _filename;
Jul 27, 2002
Jul 27, 2002
445
446
447
448
449
450
451
452
453
454
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.
*/
os2err(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 |
Jul 27, 2002
Jul 27, 2002
455
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
Jul 27, 2002
Jul 27, 2002
456
457
OPEN_ACCESS_READWRITE, NULL));
Jan 28, 2010
Jan 28, 2010
458
return ((void *) hfile);
Jul 27, 2002
Jul 27, 2002
459
460
461
} /* __PHYSFS_platformOpenWrite */
Mar 29, 2009
Mar 29, 2009
462
void *__PHYSFS_platformOpenAppend(const char *_filename)
Jul 27, 2002
Jul 27, 2002
463
{
Mar 29, 2009
Mar 29, 2009
464
const unsigned char *filename = (const unsigned char *) _filename;
Jul 27, 2002
Jul 27, 2002
465
466
ULONG dummy = 0;
HFILE hfile = NULLHANDLE;
Jul 27, 2002
Jul 27, 2002
467
APIRET rc;
Jul 27, 2002
Jul 27, 2002
468
469
470
471
472
473
474
475
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
rc = os2err(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 |
Jul 27, 2002
Jul 27, 2002
476
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
Jul 27, 2002
Jul 27, 2002
477
478
479
480
481
482
483
484
485
486
487
OPEN_ACCESS_READWRITE, NULL));
if (rc == NO_ERROR)
{
if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR)
{
DosClose(hfile);
hfile = NULLHANDLE;
} /* if */
} /* if */
Jan 28, 2010
Jan 28, 2010
488
return ((void *) hfile);
Jul 27, 2002
Jul 27, 2002
489
490
491
} /* __PHYSFS_platformOpenAppend */
Aug 21, 2010
Aug 21, 2010
492
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
Jul 27, 2002
Jul 27, 2002
493
{
Aug 21, 2010
Aug 21, 2010
494
495
496
497
498
ULONG br = 0;
BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
if (os2err(DosRead((HFILE) opaque, buf, (ULONG) len, &br)) != NO_ERROR)
return (br > 0) ? ((PHYSFS_sint64) br) : -1;
return (PHYSFS_sint64) br;
Jul 27, 2002
Jul 27, 2002
499
500
501
} /* __PHYSFS_platformRead */
Aug 21, 2010
Aug 21, 2010
502
503
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buf,
PHYSFS_uint64 len)
Jul 27, 2002
Jul 27, 2002
504
{
Aug 21, 2010
Aug 21, 2010
505
506
507
508
509
ULONG bw = 0;
BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
if (os2err(DosWrite((HFILE) opaque, buf, (ULONG) len, &bw)) != NO_ERROR)
return (bw > 0) ? ((PHYSFS_sint64) bw) : -1;
return (PHYSFS_sint64) bw;
Jul 27, 2002
Jul 27, 2002
510
511
512
513
514
515
516
517
518
519
520
521
} /* __PHYSFS_platformWrite */
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
ULONG dummy;
HFILE hfile = (HFILE) opaque;
LONG dist = (LONG) pos;
/* hooray for 32-bit filesystem limits! :) */
BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0);
Jan 28, 2010
Jan 28, 2010
522
return (os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
523
524
525
526
527
528
529
530
531
} /* __PHYSFS_platformSeek */
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
ULONG pos;
HFILE hfile = (HFILE) opaque;
APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos));
BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1);
Jan 28, 2010
Jan 28, 2010
532
return ((PHYSFS_sint64) pos);
Jul 27, 2002
Jul 27, 2002
533
534
535
536
537
538
539
540
541
} /* __PHYSFS_platformTell */
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
{
FILESTATUS3 fs;
HFILE hfile = (HFILE) opaque;
APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
Jan 28, 2010
Jan 28, 2010
542
return ((PHYSFS_sint64) fs.cbFile);
Jul 27, 2002
Jul 27, 2002
543
544
545
546
547
548
549
550
551
552
553
554
} /* __PHYSFS_platformFileLength */
int __PHYSFS_platformEOF(void *opaque)
{
PHYSFS_sint64 len, pos;
len = __PHYSFS_platformFileLength(opaque);
BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */
pos = __PHYSFS_platformTell(opaque);
BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */
Jan 28, 2010
Jan 28, 2010
555
return (pos >= len);
Jul 27, 2002
Jul 27, 2002
556
557
558
559
560
} /* __PHYSFS_platformEOF */
int __PHYSFS_platformFlush(void *opaque)
{
Jan 28, 2010
Jan 28, 2010
561
return (os2err(DosResetBuffer((HFILE) opaque)) == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
562
563
564
} /* __PHYSFS_platformFlush */
Aug 30, 2010
Aug 30, 2010
565
void __PHYSFS_platformClose(void *opaque)
Jul 27, 2002
Jul 27, 2002
566
{
Aug 30, 2010
Aug 30, 2010
567
DosClose((HFILE) opaque); /* ignore errors. You should have flushed! */
Jul 27, 2002
Jul 27, 2002
568
569
570
} /* __PHYSFS_platformClose */
Mar 29, 2009
Mar 29, 2009
571
int __PHYSFS_platformDelete(const char *_path)
Jul 27, 2002
Jul 27, 2002
572
{
Sep 5, 2010
Sep 5, 2010
573
FILESTATUS3 fs;
Mar 29, 2009
Mar 29, 2009
574
const unsigned char *path = (const unsigned char *) _path;
Sep 5, 2010
Sep 5, 2010
575
576
577
578
579
APIRET rc = os2err(DosQueryPathInfo(path, FIL_STANDARD, &fs, sizeof (fs)));
BAIL_IF_MACRO(rc != NO_ERROR, NULL, 0);
if (fs.attrFile & FILE_DIRECTORY)
Jan 28, 2010
Jan 28, 2010
580
return (os2err(DosDeleteDir(path)) == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
581
Jan 28, 2010
Jan 28, 2010
582
return (os2err(DosDelete(path)) == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
583
584
585
} /* __PHYSFS_platformDelete */
Feb 15, 2010
Feb 15, 2010
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* Convert to a format PhysicsFS can grok... */
PHYSFS_sint64 os2TimeToUnixTime(const FDATE *date, const FTIME *time)
{
struct tm tm;
tm.tm_sec = ((PHYSFS_uint32) time->.twosecs) * 2;
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 */
static int __PHYSFS_platformStat(const char *_fname, int *exists,
PHYSFS_Stat *stat)
{
struct tm tm;
FILESTATUS3 fs;
const unsigned char *fname = (const unsigned char *) _fname;
const APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
if (rc != NO_ERROR)
{
if (rc == ERROR_PATH_NOT_FOUND)
{
*exists = 0;
return 0;
} /* if */
Aug 21, 2010
Aug 21, 2010
620
BAIL_MACRO(get_os2_error_string(rc), 0);
Feb 15, 2010
Feb 15, 2010
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
} /* if */
*exists = 1;
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);
Aug 21, 2010
Aug 21, 2010
650
return 1;
Feb 15, 2010
Feb 15, 2010
651
652
653
} /* __PHYSFS_platformStat */
Sep 6, 2009
Sep 6, 2009
654
void *__PHYSFS_platformGetThreadID(void)
Jul 27, 2002
Jul 27, 2002
655
{
Jul 27, 2002
Jul 27, 2002
656
657
PTIB ptib;
PPIB ppib;
Jul 27, 2002
Jul 27, 2002
658
659
660
661
662
/*
* Allegedly, this API never fails, but we'll punt and return a
* default value (zero might as well do) if it does.
*/
Jul 27, 2002
Jul 27, 2002
663
BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, 0, 0);
Jan 28, 2010
Jan 28, 2010
664
return ((void *) ptib->tib_ordinal);
Jul 27, 2002
Jul 27, 2002
665
666
667
668
669
670
671
} /* __PHYSFS_platformGetThreadID */
void *__PHYSFS_platformCreateMutex(void)
{
HMTX hmtx = NULLHANDLE;
os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0));
Jan 28, 2010
Jan 28, 2010
672
return ((void *) hmtx);
Jul 27, 2002
Jul 27, 2002
673
674
675
676
677
678
679
680
681
682
683
684
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
DosCloseMutexSem((HMTX) mutex);
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
/* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */
Jan 28, 2010
Jan 28, 2010
685
return (DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
Jul 27, 2002
Jul 27, 2002
686
687
688
689
690
691
692
693
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
DosReleaseMutexSem((HMTX) mutex);
} /* __PHYSFS_platformReleaseMutex */
Jul 23, 2005
Jul 23, 2005
694
Mar 20, 2007
Mar 20, 2007
695
696
/* !!! FIXME: Don't use C runtime for allocators? */
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
Jul 23, 2005
Jul 23, 2005
697
{
Jan 28, 2010
Jan 28, 2010
698
return 0; /* just use malloc() and friends. */
Mar 20, 2007
Mar 20, 2007
699
} /* __PHYSFS_platformSetDefaultAllocator */
Jul 23, 2005
Jul 23, 2005
700
Mar 11, 2007
Mar 11, 2007
701
#endif /* PHYSFS_PLATFORM_OS2 */
Jul 27, 2002
Jul 27, 2002
702
703
/* end of os2.c ... */