Skip to content

Latest commit

 

History

History
815 lines (661 loc) · 24 KB

macclassic.c

File metadata and controls

815 lines (661 loc) · 24 KB
 
1
2
3
4
5
6
7
8
/*
* MacOS Classic support routines for PhysicsFS.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
May 10, 2002
May 10, 2002
9
10
11
12
#if HAVE_CONFIG_H
# include <config.h>
#endif
13
14
#include <stdlib.h>
#include <string.h>
Apr 6, 2002
Apr 6, 2002
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <alloca.h>
/*
* Most of the API calls in here are, according to ADC, available since
* MacOS 8.1. I don't think I used any MacOS 9 or CarbonLib-specific
* functions. There might be one or two 8.5 calls, and perhaps when the
* ADC docs say "Available in MacOS 8.1" they really mean "this works
* with System 6, but we don't want to hear about it at this point."
*
* IsAliasFile() showed up in MacOS 8.5. You can duplicate this code with
* PBGetCatInfoSync(), which is an older API, if you hope the bits in the
* catalog info never change (which they won't for, say, MacOS 8.1).
* See Apple Technote FL-30:
* http://developer.apple.com/technotes/fl/fl_30.html
*
* If you want to use weak pointers and Gestalt, and choose the correct
* code to use during __PHYSFS_platformInit(), I'll accept a patch, but
* chances are, it wasn't worth the time it took to write this, let alone
* implement that.
*/
36
37
38
39
/*
* Please note that I haven't tried this code with CarbonLib or under
* MacOS X at all. The code in unix.c is known to work with Darwin,
Apr 6, 2002
Apr 6, 2002
40
41
* and you may or may not be better off using that, especially since
* mutexes are no-ops in this file. Patches welcome.
Apr 6, 2002
Apr 6, 2002
43
#ifdef __PHYSFS_CARBONIZED__ /* this is currently not defined anywhere. */
44
45
46
47
48
#include <Carbon.h>
#else
#include <OSUtils.h>
#include <Processes.h>
#include <Files.h>
Apr 2, 2002
Apr 2, 2002
49
50
51
#include <TextUtils.h>
#include <Resources.h>
#include <MacMemory.h>
Apr 3, 2002
Apr 3, 2002
52
#include <Events.h>
Apr 5, 2002
Apr 5, 2002
53
#include <DriverGestalt.h>
Apr 6, 2002
Apr 6, 2002
54
#include <Aliases.h>
55
56
57
58
59
60
61
62
#endif
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
const char *__PHYSFS_platformDirSeparator = ":";
Apr 5, 2002
Apr 5, 2002
63
64
static struct ProcessInfoRec procInfo;
static FSSpec procfsspec;
65
66
67
int __PHYSFS_platformInit(void)
{
Apr 5, 2002
Apr 5, 2002
68
69
70
71
72
73
74
75
76
77
OSErr err;
ProcessSerialNumber psn;
BAIL_IF_MACRO(GetCurrentProcess(&psn) != noErr, ERR_OS_ERROR, 0);
memset(&procInfo, '\0', sizeof (ProcessInfoRec));
memset(&procfsspec, '\0', sizeof (FSSpec));
procInfo.processInfoLength = sizeof (ProcessInfoRec);
procInfo.processAppSpec = &procfsspec;
err = GetProcessInformation(&psn, &procInfo);
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
return(1); /* we're golden. */
78
79
80
81
82
83
84
85
86
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
return(1); /* always succeed. */
} /* __PHYSFS_platformDeinit */
Apr 5, 2002
Apr 5, 2002
87
88
89
90
/*
* CD detection code is borrowed from Apple Technical Q&A DV18.
* http://developer.apple.com/qa/dv/dv18.html
*/
91
92
char **__PHYSFS_platformDetectAvailableCDs(void)
{
Apr 5, 2002
Apr 5, 2002
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
DriverGestaltParam pb;
DrvQEl *dqp;
OSErr status;
char **retval = (char **) malloc(sizeof (char *));
int cd_count = 1;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
*retval = NULL;
pb.csCode = kDriverGestaltCode;
pb.driverGestaltSelector = kdgDeviceType;
dqp = (DrvQEl *) GetDrvQHdr()->qHead;
while (dqp != NULL)
{
pb.ioCRefNum = dqp->dQRefNum;
pb.ioVRefNum = dqp->dQDrive;
status = PBStatusSync((ParmBlkPtr) &pb);
if ((status == noErr) && (pb.driverGestaltResponse == kdgCDType))
{
Str63 volName;
HParamBlockRec hpbr;
memset(&hpbr, '\0', sizeof (HParamBlockRec));
hpbr.volumeParam.ioNamePtr = volName;
hpbr.volumeParam.ioVRefNum = dqp->dQDrive;
hpbr.volumeParam.ioVolIndex = 0;
if (PBHGetVInfoSync(&hpbr) == noErr)
{
char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
if (tmp)
{
char *str = (char *) malloc(volName[0] + 1);
retval = tmp;
if (str != NULL)
{
memcpy(str, &volName[1], volName[0]);
str[volName[0]] = '\0';
retval[cd_count-1] = str;
cd_count++;
} /* if */
} /* if */
} /* if */
} /* if */
dqp = (DrvQEl *) dqp->qLink;
} /* while */
retval[cd_count - 1] = NULL;
return(retval);
143
144
145
} /* __PHYSFS_platformDetectAvailableCDs */
Apr 6, 2002
Apr 6, 2002
146
static char *convFSSpecToPath(FSSpec *spec, int includeFile)
147
148
149
150
151
152
{
char *ptr;
char *retval = NULL;
UInt32 retLength = 0;
CInfoPBRec infoPB;
Str255 str255;
Apr 6, 2002
Apr 6, 2002
153
154
155
156
str255[0] = spec->name[0];
memcpy(&str255[1], &spec->name[1], str255[0]);
157
memset(&infoPB, '\0', sizeof (CInfoPBRec));
Apr 5, 2002
Apr 5, 2002
158
infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
Apr 6, 2002
Apr 6, 2002
159
160
161
infoPB.dirInfo.ioVRefNum = spec->vRefNum; /* ID of bin's volume. */
infoPB.dirInfo.ioDrParID = spec->parID; /* ID of bin's dir. */
infoPB.dirInfo.ioFDirIndex = (includeFile) ? 0 : -1;
162
163
164
165
166
167
/* walk the tree back to the root dir (volume), building path string... */
do
{
/* check parent dir of what we last looked at... */
infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
Apr 4, 2002
Apr 4, 2002
168
if (PBGetCatInfoSync(&infoPB) != noErr)
169
170
171
172
173
{
if (retval != NULL)
free(retval);
BAIL_MACRO(ERR_OS_ERROR, NULL);
} /* if */
Apr 6, 2002
Apr 6, 2002
174
175
176
infoPB.dirInfo.ioFDirIndex = -1; /* look at parent dir next time. */
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* allocate more space for the retval... */
retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
ptr = (char *) malloc(retLength);
if (ptr == NULL)
{
if (retval != NULL)
free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
/* prepend new dir to retval and cleanup... */
memcpy(ptr, &str255[1], str255[0]);
ptr[str255[0]] = '\0'; /* null terminate it. */
if (retval != NULL)
{
strcat(ptr, ":");
strcat(ptr, retval);
free(retval);
} /* if */
retval = ptr;
} while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
return(retval);
Apr 6, 2002
Apr 6, 2002
200
201
202
203
204
205
206
207
208
209
} /* convFSSpecToPath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
FSSpec spec;
/* Get the name of the binary's parent directory. */
FSMakeFSSpec(procfsspec.vRefNum, procfsspec.parID, procfsspec.name, &spec);
return(convFSSpecToPath(&spec, 0));
210
211
212
213
214
215
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
char *retval = NULL;
Apr 2, 2002
Apr 2, 2002
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
StringHandle strHandle;
short origResourceFile = CurResFile();
/* use the System resource file. */
UseResFile(0);
/* apparently, -16096 specifies the username. */
strHandle = GetString(-16096);
UseResFile(origResourceFile);
BAIL_IF_MACRO(strHandle == NULL, ERR_OS_ERROR, NULL);
HLock((Handle) strHandle);
retval = (char *) malloc((*strHandle)[0] + 1);
if (retval == NULL)
{
HUnlock((Handle) strHandle);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
HUnlock((Handle) strHandle);
237
238
239
240
241
242
return(retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
Apr 6, 2002
Apr 6, 2002
243
#if 0
244
return(NULL); /* bah...use default behaviour, I guess. */
Apr 6, 2002
Apr 6, 2002
245
246
247
248
#else
/* (Hmm. Default behaviour is broken in the base library. :) ) */
return(__PHYSFS_platformCalcBaseDir(NULL));
#endif
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
} /* __PHYSFS_platformGetUserDir */
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
{
return(1); /* single threaded. */
} /* __PHYSFS_platformGetThreadID */
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
extern int _stricmp(const char *, const char *);
return(_stricmp(x, y)); /* (*shrug*) */
} /* __PHYSFS_platformStricmp */
Apr 6, 2002
Apr 6, 2002
265
static OSErr fnameToFSSpecNoAlias(const char *fname, FSSpec *spec)
Apr 4, 2002
Apr 4, 2002
266
{
Apr 4, 2002
Apr 4, 2002
267
OSErr err;
Apr 4, 2002
Apr 4, 2002
268
Str255 str255;
Apr 5, 2002
Apr 5, 2002
269
270
int needColon = (strchr(fname, ':') == NULL);
int len = strlen(fname) + ((needColon) ? 1 : 0);
Apr 4, 2002
Apr 4, 2002
271
272
273
274
275
if (len > 255)
return(bdNamErr);
/* !!! FIXME: What happens with relative pathnames? */
Apr 5, 2002
Apr 5, 2002
276
277
278
279
280
281
282
str255[0] = len;
memcpy(&str255[1], fname, len);
/* probably just a volume name, which seems to need a ':' at the end. */
if (needColon)
str255[len] = ':';
Apr 4, 2002
Apr 4, 2002
283
284
err = FSMakeFSSpec(0, 0, str255, spec);
return(err);
Apr 6, 2002
Apr 6, 2002
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
} /* fnameToFSSpecNoAlias */
/* !!! FIXME: This code is pretty heinous. */
static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
{
Boolean alias = 0;
Boolean folder = 0;
OSErr err = fnameToFSSpecNoAlias(fname, spec);
if (err == dirNFErr) /* might be an alias in the middle of the path. */
{
/*
* Has to be at least two ':' chars, or we wouldn't get a
* dir-not-found condition. (no ':' means it was just a volume,
* just one ':' means we would have gotten a fnfErr, if anything.
*/
char *ptr;
char *start;
char *path = alloca(strlen(fname) + 1);
strcpy(path, fname);
ptr = strchr(path, ':');
BAIL_IF_MACRO(!ptr, ERR_FILE_NOT_FOUND, err); /* just in case */
ptr = strchr(ptr + 1, ':');
BAIL_IF_MACRO(!ptr, ERR_FILE_NOT_FOUND, err); /* just in case */
*ptr = '\0';
err = fnameToFSSpecNoAlias(path, spec); /* get first dir. */
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
start = ptr;
ptr = strchr(start + 1, ':');
do
{
CInfoPBRec infoPB;
memset(&infoPB, '\0', sizeof (CInfoPBRec));
infoPB.dirInfo.ioNamePtr = spec->name;
infoPB.dirInfo.ioVRefNum = spec->vRefNum;
infoPB.dirInfo.ioDrDirID = spec->parID;
infoPB.dirInfo.ioFDirIndex = 0;
err = PBGetCatInfoSync(&infoPB);
if (err != noErr) /* not an alias, really a bogus path. */
return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0)
spec->parID = infoPB.dirInfo.ioDrDirID;
if (ptr != NULL)
*ptr = '\0';
*start = strlen(start + 1); /* make it a pstring. */
err = FSMakeFSSpec(spec->vRefNum, spec->parID,
(const unsigned char *) start, spec);
if (err != noErr) /* not an alias, really a bogus path. */
return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
if (err != noErr) /* not an alias, really a bogus path. */
return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
start = ptr;
if (ptr != NULL)
ptr = strchr(start + 1, ':');
} while (start != NULL);
} /* if */
else /* there's something there; make sure final file is not an alias. */
{
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
} /* else */
return(noErr); /* w00t. */
Apr 4, 2002
Apr 4, 2002
356
357
358
} /* fnameToFSSpec */
359
360
int __PHYSFS_platformExists(const char *fname)
{
Apr 4, 2002
Apr 4, 2002
361
362
FSSpec spec;
return(fnameToFSSpec(fname, &spec) == noErr);
363
364
365
366
367
} /* __PHYSFS_platformExists */
int __PHYSFS_platformIsSymLink(const char *fname)
{
Apr 6, 2002
Apr 6, 2002
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
OSErr err;
FSSpec spec;
Boolean a = 0;
Boolean f = 0;
CInfoPBRec infoPB;
char *ptr;
char *dir = alloca(strlen(fname) + 1);
BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
strcpy(dir, fname);
ptr = strrchr(dir, ':');
if (ptr == NULL) /* just a volume name? Can't be a symlink. */
return(0);
/* resolve aliases up to the actual file... */
*ptr = '\0';
BAIL_IF_MACRO(fnameToFSSpec(dir, &spec) != noErr, ERR_OS_ERROR, 0);
*ptr = strlen(ptr + 1); /* ptr is now a pascal string. Yikes! */
memset(&infoPB, '\0', sizeof (CInfoPBRec));
infoPB.dirInfo.ioNamePtr = spec.name;
infoPB.dirInfo.ioVRefNum = spec.vRefNum;
infoPB.dirInfo.ioDrDirID = spec.parID;
infoPB.dirInfo.ioFDirIndex = 0;
BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, 0);
err = FSMakeFSSpec(spec.vRefNum, infoPB.dirInfo.ioDrDirID,
(const unsigned char *) ptr, &spec);
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
BAIL_IF_MACRO(IsAliasFile(&spec, &a, &f) != noErr, ERR_OS_ERROR, 0);
return(a);
398
399
400
401
402
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
Apr 4, 2002
Apr 4, 2002
403
404
FSSpec spec;
CInfoPBRec infoPB;
Apr 4, 2002
Apr 4, 2002
405
OSErr err;
Apr 4, 2002
Apr 4, 2002
406
407
408
409
410
BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, 0);
memset(&infoPB, '\0', sizeof (CInfoPBRec));
infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
Apr 4, 2002
Apr 4, 2002
411
infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
Apr 4, 2002
Apr 4, 2002
412
infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
Apr 4, 2002
Apr 4, 2002
413
err = PBGetCatInfoSync(&infoPB);
Apr 5, 2002
Apr 5, 2002
414
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
Apr 4, 2002
Apr 4, 2002
415
return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
416
417
418
419
420
421
422
} /* __PHYSFS_platformIsDirectory */
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
Apr 3, 2002
Apr 3, 2002
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
const char *src;
char *dst;
char *retval = malloc(len);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend != NULL)
{
strcpy(retval, prepend);
dst = retval + strlen(retval);
} /* if */
else
{
*retval = '\0';
dst = retval;
} /* else */
for (src = dirName; *src; src++, dst++)
*dst = ((*src == '/') ? ':' : *src);
*dst = '\0';
return(retval);
447
448
449
450
451
} /* __PHYSFS_platformCvtToDependent */
void __PHYSFS_platformTimeslice(void)
{
Apr 3, 2002
Apr 3, 2002
452
SystemTask();
453
454
455
456
457
458
} /* __PHYSFS_platformTimeslice */
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks)
{
Apr 4, 2002
Apr 4, 2002
459
460
461
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
Apr 5, 2002
Apr 5, 2002
462
463
UInt16 i;
UInt16 max;
Apr 4, 2002
Apr 4, 2002
464
465
466
FSSpec spec;
CInfoPBRec infoPB;
Str255 str255;
Apr 5, 2002
Apr 5, 2002
467
long dirID;
Apr 4, 2002
Apr 4, 2002
468
469
470
BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, ERR_OS_ERROR, 0);
Apr 5, 2002
Apr 5, 2002
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* get the dir ID of what we want to enumerate... */
memset(&infoPB, '\0', sizeof (CInfoPBRec));
infoPB.dirInfo.ioNamePtr = spec.name; /* name of dir to enum. */
infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of dir. */
infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, NULL);
if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) == 0)
BAIL_MACRO(ERR_NOT_A_DIR, NULL);
dirID = infoPB.dirInfo.ioDrDirID;
max = infoPB.dirInfo.ioDrNmFls;
for (i = 1; i <= max; i++)
Apr 4, 2002
Apr 4, 2002
486
{
Apr 6, 2002
Apr 6, 2002
487
488
489
490
FSSpec aliasspec;
Boolean alias = 0;
Boolean folder = 0;
Apr 4, 2002
Apr 4, 2002
491
492
memset(&infoPB, '\0', sizeof (CInfoPBRec));
str255[0] = 0;
Apr 5, 2002
Apr 5, 2002
493
494
495
496
infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of dir's volume. */
infoPB.dirInfo.ioDrDirID = dirID; /* ID of dir. */
infoPB.dirInfo.ioFDirIndex = i; /* next file's info. */
Apr 4, 2002
Apr 4, 2002
497
if (PBGetCatInfoSync(&infoPB) != noErr)
Apr 5, 2002
Apr 5, 2002
498
continue; /* skip this file. Oh well. */
Apr 4, 2002
Apr 4, 2002
499
Apr 6, 2002
Apr 6, 2002
500
501
502
503
504
505
506
507
508
509
510
if (FSMakeFSSpec(spec.vRefNum, dirID, str255, &aliasspec) != noErr)
continue; /* skip it. */
if (IsAliasFile(&aliasspec, &alias, &folder) != noErr)
continue; /* skip it. */
if ((alias) && (omitSymLinks))
continue;
/* still here? Add it to the list. */
Apr 4, 2002
Apr 4, 2002
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
if (l == NULL)
break;
l->str = (char *) malloc(str255[0] + 1);
if (l->str == NULL)
{
free(l);
break;
} /* if */
memcpy(l->str, &str255[1], str255[0]);
l->str[str255[0]] = '\0';
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
Apr 5, 2002
Apr 5, 2002
532
} /* for */
Apr 4, 2002
Apr 4, 2002
533
534
return(retval);
535
536
537
538
539
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
Apr 3, 2002
Apr 3, 2002
540
541
542
543
544
545
546
/*
* I don't think MacOS has a concept of "current directory", beyond
* what is grafted on by a given standard C library implementation,
* so just return the base dir.
* We don't use this for anything crucial at the moment anyhow.
*/
return(__PHYSFS_platformCalcBaseDir(NULL));
547
548
549
550
551
} /* __PHYSFS_platformCurrentDir */
char *__PHYSFS_platformRealPath(const char *path)
{
Apr 6, 2002
Apr 6, 2002
552
553
554
555
556
557
558
559
560
561
/*
* fnameToFSSpec() will resolve any symlinks to get to the real
* file's FSSpec, which, when converted, will contain the real
* direct path to a given file. convFSSpecToPath() mallocs a
* return value buffer.
*/
FSSpec spec;
BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, NULL);
return(convFSSpecToPath(&spec, 1));
562
563
564
565
566
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
Apr 5, 2002
Apr 5, 2002
567
568
569
570
571
572
573
574
575
576
SInt32 val = 0;
FSSpec spec;
OSErr err = fnameToFSSpec(path, &spec);
BAIL_IF_MACRO(err == noErr, ERR_FILE_EXISTS, 0);
BAIL_IF_MACRO(err != fnfErr, ERR_OS_ERROR, 0);
err = DirCreate(spec.vRefNum, spec.parID, spec.name, &val);
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
return(1);
577
578
579
} /* __PHYSFS_platformMkDir */
Apr 5, 2002
Apr 5, 2002
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
static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
{
int created = 0;
SInt16 *retval = NULL;
FSSpec spec;
OSErr err = fnameToFSSpec(fname, &spec);
BAIL_IF_MACRO((err != noErr) && (err != fnfErr), ERR_OS_ERROR, NULL);
if (err == fnfErr)
{
BAIL_IF_MACRO(!createIfMissing, ERR_FILE_NOT_FOUND, NULL);
err = HCreate(spec.vRefNum, spec.parID, spec.name,
procInfo.processSignature, 'BINA');
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, NULL);
created = 1;
} /* if */
retval = (SInt16 *) malloc(sizeof (SInt16));
if (retval == NULL)
{
if (created)
HDelete(spec.vRefNum, spec.parID, spec.name);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
if (HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval) != noErr)
{
free(retval);
if (created)
HDelete(spec.vRefNum, spec.parID, spec.name);
BAIL_MACRO(ERR_OS_ERROR, NULL);
} /* if */
return(retval);
} /* macDoOpen */
616
617
void *__PHYSFS_platformOpenRead(const char *filename)
{
Apr 5, 2002
Apr 5, 2002
618
619
620
621
622
623
624
625
626
627
628
SInt16 *retval = macDoOpen(filename, fsRdPerm, 0);
if (retval != NULL) /* got a file; seek to start. */
{
if (SetFPos(*retval, fsFromStart, 0) != noErr)
{
FSClose(*retval);
BAIL_MACRO(ERR_OS_ERROR, NULL);
} /* if */
} /* if */
return((void *) retval);
629
630
631
632
633
} /* __PHYSFS_platformOpenRead */
void *__PHYSFS_platformOpenWrite(const char *filename)
{
Apr 5, 2002
Apr 5, 2002
634
635
636
637
638
639
640
641
642
643
644
645
SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
if (retval != NULL) /* got a file; truncate it. */
{
if ((SetEOF(*retval, 0) != noErr) ||
(SetFPos(*retval, fsFromStart, 0) != noErr))
{
FSClose(*retval);
BAIL_MACRO(ERR_OS_ERROR, NULL);
} /* if */
} /* if */
return((void *) retval);
646
647
648
649
650
} /* __PHYSFS_platformOpenWrite */
void *__PHYSFS_platformOpenAppend(const char *filename)
{
Apr 5, 2002
Apr 5, 2002
651
652
653
654
655
656
657
658
659
660
661
SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
if (retval != NULL) /* got a file; seek to end. */
{
if (SetFPos(*retval, fsFromLEOF, 0) != noErr)
{
FSClose(*retval);
BAIL_MACRO(ERR_OS_ERROR, NULL);
} /* if */
} /* if */
return(retval);
662
663
664
665
666
667
} /* __PHYSFS_platformOpenAppend */
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
Apr 5, 2002
Apr 5, 2002
668
669
670
671
672
673
674
675
676
677
678
679
680
SInt16 ref = *((SInt16 *) opaque);
SInt32 br;
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
{
br = size;
BAIL_IF_MACRO(FSRead(ref, &br, buffer) != noErr, ERR_OS_ERROR, i);
BAIL_IF_MACRO(br != size, ERR_OS_ERROR, i);
buffer = ((PHYSFS_uint8 *) buffer) + size;
} /* for */
return(count);
681
682
683
684
685
686
} /* __PHYSFS_platformRead */
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
Apr 5, 2002
Apr 5, 2002
687
688
689
690
691
692
693
694
695
696
697
698
699
SInt16 ref = *((SInt16 *) opaque);
SInt32 bw;
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
{
bw = size;
BAIL_IF_MACRO(FSWrite(ref, &bw, buffer) != noErr, ERR_OS_ERROR, i);
BAIL_IF_MACRO(bw != size, ERR_OS_ERROR, i);
buffer = ((PHYSFS_uint8 *) buffer) + size;
} /* for */
return(count);
700
701
702
703
704
} /* __PHYSFS_platformWrite */
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
Apr 5, 2002
Apr 5, 2002
705
706
707
708
SInt16 ref = *((SInt16 *) opaque);
OSErr err = SetFPos(ref, fsFromStart, (SInt32) pos);
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
return(1);
709
710
711
712
713
} /* __PHYSFS_platformSeek */
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
Apr 5, 2002
Apr 5, 2002
714
715
716
717
SInt16 ref = *((SInt16 *) opaque);
SInt32 curPos;
BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, -1);
return((PHYSFS_sint64) curPos);
718
719
720
721
722
} /* __PHYSFS_platformTell */
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
{
Apr 5, 2002
Apr 5, 2002
723
724
725
726
SInt16 ref = *((SInt16 *) opaque);
SInt32 eofPos;
BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, -1);
return((PHYSFS_sint64) eofPos);
727
728
729
730
731
} /* __PHYSFS_platformFileLength */
int __PHYSFS_platformEOF(void *opaque)
{
Apr 5, 2002
Apr 5, 2002
732
733
734
735
736
SInt16 ref = *((SInt16 *) opaque);
SInt32 eofPos, curPos;
BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, 1);
BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, 1);
return(curPos >= eofPos);
737
738
739
740
741
} /* __PHYSFS_platformEOF */
int __PHYSFS_platformFlush(void *opaque)
{
Apr 5, 2002
Apr 5, 2002
742
743
744
745
746
747
SInt16 ref = *((SInt16 *) opaque);
ParamBlockRec pb;
memset(&pb, '\0', sizeof (ParamBlockRec));
pb.ioParam.ioRefNum = ref;
BAIL_IF_MACRO(PBFlushFileSync(&pb) != noErr, ERR_OS_ERROR, 0);
return(1);
748
749
750
751
752
} /* __PHYSFS_platformFlush */
int __PHYSFS_platformClose(void *opaque)
{
Apr 5, 2002
Apr 5, 2002
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
SInt16 ref = *((SInt16 *) opaque);
SInt16 vRefNum;
HParamBlockRec hpbr;
Str63 volName;
BAIL_IF_MACRO(GetVRefNum (ref, &vRefNum) != noErr, ERR_OS_ERROR, 0);
memset(&hpbr, '\0', sizeof (HParamBlockRec));
hpbr.volumeParam.ioNamePtr = volName;
hpbr.volumeParam.ioVRefNum = vRefNum;
hpbr.volumeParam.ioVolIndex = 0;
BAIL_IF_MACRO(PBHGetVInfoSync(&hpbr) != noErr, ERR_OS_ERROR, 0);
BAIL_IF_MACRO(FSClose(ref) != noErr, ERR_OS_ERROR, 0);
free(opaque);
FlushVol(volName, vRefNum);
return(1);
771
772
773
774
775
} /* __PHYSFS_platformClose */
int __PHYSFS_platformDelete(const char *path)
{
Apr 5, 2002
Apr 5, 2002
776
777
778
779
780
781
FSSpec spec;
OSErr err;
BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, 0);
err = HDelete(spec.vRefNum, spec.parID, spec.name);
BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
return(1);
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
} /* __PHYSFS_platformDelete */
void *__PHYSFS_platformCreateMutex(void)
{
return((void *) 0x0001); /* no mutexes on MacOS Classic. */
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
/* no mutexes on MacOS Classic. */
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
return(1); /* no mutexes on MacOS Classic. */
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
/* no mutexes on MacOS Classic. */
} /* __PHYSFS_platformReleaseMutex */
May 25, 2002
May 25, 2002
808
809
810
811
812
813
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
{
BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1); /* !!! FIXME! */
} /* __PHYSFS_platformGetLastModTime */
Apr 5, 2002
Apr 5, 2002
814
/* end of macclassic.c ... */