summaryrefslogtreecommitdiff
path: root/macos/source/macstuff.c
blob: 03236073c0c38b3025a3725a28d3f75a2b8e8958 (plain)
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
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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
619
620
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
/*
These Functions were originally part of More Files version 1.4.8

More Files fixes many of the broken or underfunctional
parts of the file system.

More Files

A collection of File Manager and related routines

by Jim Luther (Apple Macintosh Developer Technical Support Emeritus)
with significant code contributions by Nitin Ganatra
(Apple Macintosh Developer Technical Support Emeritus)
Copyright  1992-1998 Apple Computer, Inc.
Portions copyright  1995 Jim Luther
All rights reserved.

The Package "More Files" is distributed under the following
license terms:

         "You may incorporate this sample code into your
          applications without restriction, though the
          sample code has been provided "AS IS" and the
          responsibility for its operation is 100% yours.
          However, what you are not permitted to do is to
          redistribute the source as "DSC Sample Code" after
          having made changes. If you're going to
          redistribute the source, we require that you make
          it clear in the source that the code was descended
          from Apple Sample Code, but that you've made
          changes."


The following changes are made by Info-ZIP:

- The only changes are made by pasting the functions
  (mostly found in MoreFilesExtras.c / MoreFiles.c)
  directly into macstuff.c / macstuff.h and slightly
  reformatting the text (replacement of TABs by spaces,
  removal/replacement of non-ASCII characters).
  The code itself is NOT changed.

This file has been modified by Info-ZIP for use in MacZip.
This file is NOT part of the original package More Files.

More Files can be found on the MetroWerks CD and Developer CD from
Apple. You can also download the latest version from:

    http://members.aol.com/JumpLong/#MoreFiles

Jim Luther's Home-page:
    http://members.aol.com/JumpLong/


*/

#include <string.h>


#include "macstuff.h"



extern int errno;

static  OSErr   GetCommentFromDesktopFile(short vRefNum,
                                          long dirID,
                                          ConstStr255Param name,
                                          Str255 comment);

static  OSErr   GetCommentID(short vRefNum,
                             long dirID,
                             ConstStr255Param name,
                             short *commentID);

static  OSErr   GetDesktopFileName(short vRefNum,
                                   Str255 desktopName);


enum
{
    kBNDLResType    = 'BNDL',
    kFREFResType    = 'FREF',
    kIconFamResType = 'ICN#',
    kFCMTResType    = 'FCMT',
    kAPPLResType    = 'APPL'
};


/*****************************************************************************/

/*
**  File Manager FSp calls
*/

/*****************************************************************************/

pascal  OSErr   FSMakeFSSpecCompat(short vRefNum,
                                   long dirID,
                                   ConstStr255Param fileName,
                                   FSSpec *spec)
{
    OSErr   result;

#if !__MACOSSEVENORLATER
    if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
    {
        Boolean isDirectory;

        result = GetObjectLocation(vRefNum, dirID, fileName,
                                    &(spec->vRefNum), &(spec->parID), spec->name,
                                    &isDirectory);
    }
    else
#endif  /* !__MACOSSEVENORLATER */
    {
     /* Let the file system create the FSSpec if it can since it does the job */
     /* much more efficiently than I can. */
        result = FSMakeFSSpec(vRefNum, dirID, fileName, spec);

        /* Fix a bug in Macintosh PC Exchange's MakeFSSpec code where 0 is */
        /* returned in the parID field when making an FSSpec to the volume's */
        /* root directory by passing a full pathname in MakeFSSpec's */
        /* fileName parameter. Fixed in Mac OS 8.1 */
        if ( (result == noErr) && (spec->parID == 0) )
            spec->parID = fsRtParID;
    }
    return ( result );
}


/*****************************************************************************/
/* FSHasFSSpecCalls returns true if the file system provides FSSpec calls. */

#if !__MACOSSEVENORLATER
static  Boolean FSHasFSSpecCalls(void)
{
    long            response;
#if !GENERATENODATA
    static Boolean  tested = false;
    static Boolean  result = false;
#else
    Boolean result = false;
#endif

#if !GENERATENODATA
    if ( !tested )
    {
        tested = true;
#endif
        if ( Gestalt(gestaltFSAttr, &response) == noErr )
        {
            result = ((response & (1L << gestaltHasFSSpecCalls)) != 0);
        }
#if !GENERATENODATA
    }
#endif
    return ( result );
}
#endif  /* !__MACOSSEVENORLATER */



/*****************************************************************************/
/* QTHasFSSpecCalls returns true if QuickTime provides FSSpec calls */
/* except for FSpExchangeFiles. */

#if !__MACOSSEVENORLATER
static  Boolean QTHasFSSpecCalls(void)
{
    long            response;
#if !GENERATENODATA
    static Boolean  tested = false;
    static Boolean  result = false;
#else
    Boolean result = false;
#endif

#if !GENERATENODATA
    if ( !tested )
    {
        tested = true;
#endif
        result = (Gestalt(gestaltQuickTimeVersion, &response) == noErr);
#if !GENERATENODATA
    }
#endif
    return ( result );
}
#endif  /* !__MACOSSEVENORLATER */




/*
 *----------------------------------------------------------------------
 *
 * FSpGetDefaultDir --
 *
 *  This function gets the current default directory.
 *
 * Results:
 *  The provided FSSpec is changed to point to the "default"
 *  directory.  The function returns what ever errors
 *  FSMakeFSSpecCompat may encounter.
 *
 * Side effects:
 *  None.
 *
 *----------------------------------------------------------------------
 */

int FSpGetDefaultDir(FSSpecPtr dirSpec) /* On return the default directory. */
{
    OSErr err;
    short vRefNum = 0;
    long int dirID = 0;

    err = HGetVol(NULL, &vRefNum, &dirID);

    if (err == noErr) {
    err = FSMakeFSSpecCompat(vRefNum, dirID, (ConstStr255Param) NULL,
        dirSpec);
    }

    return err;
}

/*
 *----------------------------------------------------------------------
 *
 * FSpSetDefaultDir --
 *
 *  This function sets the default directory to the directory
 *  pointed to by the provided FSSpec.
 *
 * Results:
 *  The function returns what ever errors HSetVol may encounter.
 *
 * Side effects:
 *  None.
 *
 *----------------------------------------------------------------------
 */

int FSpSetDefaultDir(FSSpecPtr dirSpec) /* The new default directory. */
{
    OSErr err;

    /*
     * The following special case is needed to work around a bug
     * in the Macintosh OS.  (Acutally PC Exchange.)
     */

    if (dirSpec->parID == fsRtParID) {
    err = HSetVol(NULL, dirSpec->vRefNum, fsRtDirID);
    } else {
    err = HSetVol(dirSpec->name, dirSpec->vRefNum, dirSpec->parID);
    }

    return err;
}

/*
 *----------------------------------------------------------------------
 *
 * FSpFindFolder --
 *
 *  This function is a version of the FindFolder function that
 *  returns the result as a FSSpec rather than a vRefNum and dirID.
 *
 * Results:
 *  Results will be simaler to that of the FindFolder function.
 *
 * Side effects:
 *  None.
 *
 *----------------------------------------------------------------------
 */

OSErr
FSpFindFolder(
    short vRefNum,      /* Volume reference number. */
    OSType folderType,      /* Folder type taken by FindFolder. */
    Boolean createFolder,   /* Should we create it if non-existant. */
    FSSpec *spec)       /* Pointer to resulting directory. */
{
    short foundVRefNum;
    long foundDirID;
    OSErr err;

    err = FindFolder(vRefNum, folderType, createFolder,
        &foundVRefNum, &foundDirID);
    if (err != noErr) {
    return err;
    }

    err = FSMakeFSSpecCompat(foundVRefNum, foundDirID, "\p", spec);
    return err;
}



/*
 *----------------------------------------------------------------------
 *
 * FSpPathFromLocation --
 *
 *  This function obtains a full path name for a given macintosh
 *  FSSpec.  Unlike the More Files function FSpGetFullPath, this
 *  function will return a C string in the Handle.  It also will
 *  create paths for FSSpec that do not yet exist.
 *
 * Results:
 *  OSErr code.
 *
 * Side effects:
 *  None.
 *
 *----------------------------------------------------------------------
 */

OSErr
FSpPathFromLocation(
    FSSpec *spec,       /* The location we want a path for. */
    int *length,        /* Length of the resulting path. */
    Handle *fullPath)       /* Handle to path. */
{
    OSErr err;
    FSSpec tempSpec;
    CInfoPBRec pb;

    *fullPath = NULL;

    /*
     * Make a copy of the input FSSpec that can be modified.
     */
    BlockMoveData(spec, &tempSpec, sizeof(FSSpec));

    if (tempSpec.parID == fsRtParID) {
    /*
     * The object is a volume.  Add a colon to make it a full
     * pathname.  Allocate a handle for it and we are done.
     */
    tempSpec.name[0] += 2;
    tempSpec.name[tempSpec.name[0] - 1] = ':';
    tempSpec.name[tempSpec.name[0]] = '\0';

    err = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
    } else {
    /*
     * The object isn't a volume.  Is the object a file or a directory?
     */
    pb.dirInfo.ioNamePtr = tempSpec.name;
    pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
    pb.dirInfo.ioDrDirID = tempSpec.parID;
    pb.dirInfo.ioFDirIndex = 0;
    err = PBGetCatInfoSync(&pb);

    if ((err == noErr) || (err == fnfErr)) {
        /*
         * If the file doesn't currently exist we start over.  If the
         * directory exists everything will work just fine.  Otherwise we
         * will just fail later.  If the object is a directory, append a
         * colon so full pathname ends with colon.
         */
        if (err == fnfErr) {
        BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
        } else if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 ) {
        tempSpec.name[0] += 1;
        tempSpec.name[tempSpec.name[0]] = ':';
        }

        /*
         * Create a new Handle for the object - make it a C string.
         */
        tempSpec.name[0] += 1;
        tempSpec.name[tempSpec.name[0]] = '\0';
        err = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
        if (err == noErr) {
        /*
         * Get the ancestor directory names - loop until we have an
         * error or find the root directory.
         */
        pb.dirInfo.ioNamePtr = tempSpec.name;
        pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
        pb.dirInfo.ioDrParID = tempSpec.parID;
        do {
            pb.dirInfo.ioFDirIndex = -1;
            pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
            err = PBGetCatInfoSync(&pb);
            if (err == noErr) {
            /*
             * Append colon to directory name and add
             * directory name to beginning of fullPath.
             */
            ++tempSpec.name[0];
            tempSpec.name[tempSpec.name[0]] = ':';

            (void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1],
                tempSpec.name[0]);
            err = MemError();
            }
        } while ( (err == noErr) &&
            (pb.dirInfo.ioDrDirID != fsRtDirID) );
        }
    }
    }

    /*
     * On error Dispose the handle, set it to NULL & return the err.
     * Otherwise, set the length & return.
     */
    if (err == noErr) {
    *length = GetHandleSize(*fullPath) - 1;
    } else {
    if ( *fullPath != NULL ) {
        DisposeHandle(*fullPath);
    }
    *fullPath = NULL;
    *length = 0;
    }

    return err;
}



/*****************************************************************************/

pascal  OSErr   FSpGetDirectoryID(const FSSpec *spec,
                                  long *theDirID,
                                  Boolean *isDirectory)
{
    return ( GetDirectoryID(spec->vRefNum, spec->parID, spec->name,
             theDirID, isDirectory) );
}


/*****************************************************************************/

pascal  OSErr   GetDirectoryID(short vRefNum,
                               long dirID,
                               ConstStr255Param name,
                               long *theDirID,
                               Boolean *isDirectory)
{
    CInfoPBRec pb;
    OSErr error;

    error = GetCatInfoNoName(vRefNum, dirID, name, &pb);
    if ( error == noErr )
    {
        *isDirectory = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
        if ( *isDirectory )
        {
            *theDirID = pb.dirInfo.ioDrDirID;
        }
        else
        {
            *theDirID = pb.hFileInfo.ioFlParID;
        }
    }

    return ( error );
}


/*****************************************************************************/

pascal  OSErr GetCatInfoNoName(short vRefNum,
                               long dirID,
                               ConstStr255Param name,
                               CInfoPBPtr pb)
{
    Str31 tempName;
    OSErr error;

    /* Protection against File Sharing problem */
    if ( (name == NULL) || (name[0] == 0) )
    {
        tempName[0] = 0;
        pb->dirInfo.ioNamePtr = tempName;
        pb->dirInfo.ioFDirIndex = -1;   /* use ioDirID */
    }
    else
    {
        pb->dirInfo.ioNamePtr = (StringPtr)name;
        pb->dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
    }
    pb->dirInfo.ioVRefNum = vRefNum;
    pb->dirInfo.ioDrDirID = dirID;
    error = PBGetCatInfoSync(pb);
    pb->dirInfo.ioNamePtr = NULL;
    return ( error );
}



/*****************************************************************************/

pascal  OSErr   GetObjectLocation(short vRefNum,
                                  long dirID,
                                  ConstStr255Param pathname,
                                  short *realVRefNum,
                                  long *realParID,
                                  Str255 realName,
                                  Boolean *isDirectory)
{
    OSErr error;
    CInfoPBRec pb;
    Str255 tempPathname;

    /* clear results */
    *realVRefNum = 0;
    *realParID = 0;
    realName[0] = 0;

    /*
    **  Get the real vRefNum
    */
    error = DetermineVRefNum(pathname, vRefNum, realVRefNum);
    if ( error == noErr )
    {
        /*
        **  Determine if the object already exists and if so,
        **  get the real parent directory ID if it's a file
        */

        /* Protection against File Sharing problem */
        if ( (pathname == NULL) || (pathname[0] == 0) )
        {
            tempPathname[0] = 0;
            pb.hFileInfo.ioNamePtr = tempPathname;
            pb.hFileInfo.ioFDirIndex = -1;  /* use ioDirID */
        }
        else
        {
            pb.hFileInfo.ioNamePtr = (StringPtr)pathname;
            pb.hFileInfo.ioFDirIndex = 0;   /* use ioNamePtr and ioDirID */
        }
        pb.hFileInfo.ioVRefNum = vRefNum;
        pb.hFileInfo.ioDirID = dirID;
        error = PBGetCatInfoSync(&pb);
        if ( error == noErr )
        {
            /*
            **  The file system object is present and we have the file's
            **  real parID
            */

            /*  Is it a directory or a file? */
            *isDirectory = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
            if ( *isDirectory )
            {
                /*
                **  It's a directory, get its name and parent dirID, and then
                **  we're done
                */

                pb.dirInfo.ioNamePtr = realName;
                pb.dirInfo.ioVRefNum = *realVRefNum;
                /* pb.dirInfo.ioDrDirID already contains the dirID of the
                   directory object */
                pb.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
                error = PBGetCatInfoSync(&pb);

                /* get the parent ID here, because the file system can return the */
                /* wrong parent ID from the last call. */
                *realParID = pb.dirInfo.ioDrParID;
            }
            else
            {
                /*
                **  It's a file - use the parent directory ID from the last call
                **  to GetCatInfoparse, get the file name, and then we're done
                */
                *realParID = pb.hFileInfo.ioFlParID;
                error = GetFilenameFromPathname(pathname, realName);
            }
        }
        else if ( error == fnfErr )
        {
            /*
            **  The file system object is not present - see if its parent is present
            */

            /*
            **  Parse to get the object name from end of pathname
            */
            error = GetFilenameFromPathname(pathname, realName);

            /* if we can't get the object name from the end, we can't continue */
            if ( error == noErr )
            {
                /*
                **  What we want now is the pathname minus the object name
                **  for example:
                **  if pathname is 'vol:dir:file' tempPathname becomes 'vol:dir:'
                **  if pathname is 'vol:dir:file:' tempPathname becomes 'vol:dir:'
                **  if pathname is ':dir:file' tempPathname becomes ':dir:'
                **  if pathname is ':dir:file:' tempPathname becomes ':dir:'
                **  if pathname is ':file' tempPathname becomes ':'
                **  if pathname is 'file or file:' tempPathname becomes ''
                */

                /* get a copy of the pathname */
                BlockMoveData(pathname, tempPathname, pathname[0] + 1);

                /* remove the object name */
                tempPathname[0] -= realName[0];
                /* and the trailing colon (if any) */
                if ( pathname[pathname[0]] == ':' )
                {
                    --tempPathname[0];
                }

                /* OK, now get the parent's directory ID */

                /* Protection against File Sharing problem */
                pb.hFileInfo.ioNamePtr = (StringPtr)tempPathname;
                if ( tempPathname[0] != 0 )
                {
                    pb.hFileInfo.ioFDirIndex = 0;   /* use ioNamePtr and ioDirID */
                }
                else
                {
                    pb.hFileInfo.ioFDirIndex = -1;  /* use ioDirID */
                }
                pb.hFileInfo.ioVRefNum = vRefNum;
                pb.hFileInfo.ioDirID = dirID;
                error = PBGetCatInfoSync(&pb);
                *realParID = pb.dirInfo.ioDrDirID;

                *isDirectory = false;   /* we don't know what the object is
                                           really going to be */
            }

            if ( error != noErr )
            {
                error = dirNFErr;   /* couldn't find parent directory */
            }
            else
            {
                error = fnfErr; /* we found the parent, but not the file */
            }
        }
    }

    return ( error );
}



/*****************************************************************************/

pascal  OSErr   DetermineVRefNum(ConstStr255Param pathname,
                                 short vRefNum,
                                 short *realVRefNum)
{
    HParamBlockRec pb;
    OSErr error;

    error = GetVolumeInfoNoName(pathname,vRefNum, &pb);
    if ( error == noErr )
    {
        *realVRefNum = pb.volumeParam.ioVRefNum;
    }
    return ( error );
}


/*****************************************************************************/

pascal  OSErr   GetFilenameFromPathname(ConstStr255Param pathname,
                                        Str255 filename)
{
    short   index;
    short   nameEnd;
    OSErr   error;

    /* default to no filename */
    filename[0] = 0;

    /* check for no pathname */
    if ( pathname != NULL )
    {
        /* get string length */
        index = pathname[0];

        /* check for empty string */
        if ( index != 0 )
        {
            /* skip over last trailing colon (if any) */
            if ( pathname[index] == ':' )
            {
                --index;
            }

            /* save the end of the string */
            nameEnd = index;

            /* if pathname ends with multiple colons, then this pathname refers */
            /* to a directory, not a file */
            if ( pathname[index] != ':' )
            {
                /* parse backwards until we find a colon or hit the beginning
                   of the pathname */
                while ( (index != 0) && (pathname[index] != ':') )
                {
                    --index;
                }

                /* if we parsed to the beginning of the pathname and the
                   pathname ended */
                /* with a colon, then pathname is a full pathname to a volume,
                   not a file */
                if ( (index != 0) || (pathname[pathname[0]] != ':') )
                {
                    /* get the filename and return noErr */
                    filename[0] = (char)(nameEnd - index);
                    BlockMoveData(&pathname[index+1], &filename[1], nameEnd - index);
                    error = noErr;
                }
                else
                {
                    /* pathname to a volume, not a file */
                    error = notAFileErr;
                }
            }
            else
            {
                /* directory, not a file */
                error = notAFileErr;
            }
        }
        else
        {
            /* empty string isn't a file */
            error = notAFileErr;
        }
    }
    else
    {
        /* NULL pathname isn't a file */
        error = notAFileErr;
    }

    return ( error );
}



/*****************************************************************************/

/*
**  GetVolumeInfoNoName uses pathname and vRefNum to call PBHGetVInfoSync
**  in cases where the returned volume name is not needed by the caller.
**  The pathname and vRefNum parameters are not touched, and the pb
**  parameter is initialized by PBHGetVInfoSync except that ioNamePtr in
**  the parameter block is always returned as NULL (since it might point
**  to the local tempPathname).
**
**  I noticed using this code in several places, so here it is once.
**  This reduces the code size of MoreFiles.
*/
pascal  OSErr   GetVolumeInfoNoName(ConstStr255Param pathname,
                                    short vRefNum,
                                    HParmBlkPtr pb)
{
    Str255 tempPathname;
    OSErr error;

    /* Make sure pb parameter is not NULL */
    if ( pb != NULL )
    {
        pb->volumeParam.ioVRefNum = vRefNum;
        if ( pathname == NULL )
        {
            pb->volumeParam.ioNamePtr = NULL;
            pb->volumeParam.ioVolIndex = 0;     /* use ioVRefNum only */
        }
        else
        {                                   /* make a copy of the string and */
            BlockMoveData(pathname, tempPathname, pathname[0] + 1);
                                    /* use the copy so original isn't trashed */
            pb->volumeParam.ioNamePtr = (StringPtr)tempPathname;
                                       /* use ioNamePtr/ioVRefNum combination */
            pb->volumeParam.ioVolIndex = -1;
        }
        error = PBHGetVInfoSync(pb);
        pb->volumeParam.ioNamePtr = NULL;   /* ioNamePtr may point to local
                                            tempPathname, so don't return it */
    }
    else
    {
        error = paramErr;
    }
    return ( error );
}




/*****************************************************************************/

pascal  OSErr   FSpGetFullPath(const FSSpec *spec,
                               short *fullPathLength,
                               Handle *fullPath)
{
    OSErr       result;
    OSErr       realResult;
    FSSpec      tempSpec;
    CInfoPBRec  pb;

    *fullPathLength = 0;
    *fullPath = NULL;

    /* Default to noErr */
    realResult = noErr;

    /* Make a copy of the input FSSpec that can be modified */
    BlockMoveData(spec, &tempSpec, sizeof(FSSpec));

    if ( tempSpec.parID == fsRtParID )
    {
        /* The object is a volume */

        /* Add a colon to make it a full pathname */
        ++tempSpec.name[0];
        tempSpec.name[tempSpec.name[0]] = ':';

        /* We're done */
        result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
    }
    else
    {
        /* The object isn't a volume */

        /* Is the object a file or a directory? */
        pb.dirInfo.ioNamePtr = tempSpec.name;
        pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
        pb.dirInfo.ioDrDirID = tempSpec.parID;
        pb.dirInfo.ioFDirIndex = 0;
        result = PBGetCatInfoSync(&pb);
        /* Allow file/directory name at end of path to not exist. */
        realResult = result;
        if ( (result == noErr) || (result == fnfErr) )
        {
            /* if the object is a directory, append a colon so full pathname
               ends with colon */
            if ( (result == noErr) && (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
            {
                ++tempSpec.name[0];
                tempSpec.name[tempSpec.name[0]] = ':';
            }

            /* Put the object name in first */
            result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
            if ( result == noErr )
            {
                /* Get the ancestor directory names */
                pb.dirInfo.ioNamePtr = tempSpec.name;
                pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
                pb.dirInfo.ioDrParID = tempSpec.parID;
                do  /* loop until we have an error or find the root directory */
                {
                    pb.dirInfo.ioFDirIndex = -1;
                    pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
                    result = PBGetCatInfoSync(&pb);
                    if ( result == noErr )
                    {
                        /* Append colon to directory name */
                        ++tempSpec.name[0];
                        tempSpec.name[tempSpec.name[0]] = ':';

                        /* Add directory name to beginning of fullPath */
                        (void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1],
                                      tempSpec.name[0]);
                        result = MemError();
                    }
                } while ( (result == noErr) && (pb.dirInfo.ioDrDirID != fsRtDirID) );
            }
        }
    }
    if ( result == noErr )
    {
        /* Return the length */
        *fullPathLength = InlineGetHandleSize(*fullPath);
        result = realResult;    /* return realResult in case it was fnfErr */
    }
    else
    {
        /* Dispose of the handle and return NULL and zero length */
        if ( *fullPath != NULL )
        {
            DisposeHandle(*fullPath);
        }
        *fullPath = NULL;
        *fullPathLength = 0;
    }

    return ( result );
}



/*****************************************************************************/

pascal OSErr FSpLocationFromFullPath(short fullPathLength,
                                     const void *fullPath,
                                     FSSpec *spec)
{
    AliasHandle alias;
    OSErr       result;
    Boolean     wasChanged;
    Str32       nullString;

    /* Create a minimal alias from the full pathname */
    nullString[0] = 0;  /* null string to indicate no zone or server name */
    result = NewAliasMinimalFromFullPath(fullPathLength, fullPath, nullString,
                                         nullString, &alias);

    if ( result == noErr )
    {
        /* Let the Alias Manager resolve the alias. */
        result = ResolveAlias(NULL, alias, spec, &wasChanged);

        DisposeHandle((Handle)alias);   /* Free up memory used */
    }

    return ( result );
}



/*****************************************************************************/

pascal  OSErr   GetFullPath(short vRefNum,
                            long dirID,
                            ConstStr255Param name,
                            short *fullPathLength,
                            Handle *fullPath)
{
    OSErr       result;
    FSSpec      spec;

    *fullPathLength = 0;
    *fullPath = NULL;

    result = FSMakeFSSpecCompat(vRefNum, dirID, name, &spec);
    if ( (result == noErr) || (result == fnfErr) )
    {
        result = FSpGetFullPath(&spec, fullPathLength, fullPath);
    }

    return ( result );
}



/*****************************************************************************/

pascal  OSErr   ChangeCreatorType(short vRefNum,
                                  long dirID,
                                  ConstStr255Param name,
                                  OSType creator,
                                  OSType fileType)
{
    CInfoPBRec pb;
    OSErr error;
    short realVRefNum;
    long parID;

    pb.hFileInfo.ioNamePtr = (StringPtr)name;
    pb.hFileInfo.ioVRefNum = vRefNum;
    pb.hFileInfo.ioDirID = dirID;
    pb.hFileInfo.ioFDirIndex = 0;   /* use ioNamePtr and ioDirID */
    error = PBGetCatInfoSync(&pb);
    if ( error == noErr )
    {
        if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) == 0 )   /* if file */
        {                            /* save parent dirID for BumpDate call */
            parID = pb.hFileInfo.ioFlParID;

            /* If creator not 0x00000000, change creator */
            if ( creator != (OSType)0x00000000 )
            {
                pb.hFileInfo.ioFlFndrInfo.fdCreator = creator;
            }

            /* If fileType not 0x00000000, change fileType */
            if ( fileType != (OSType)0x00000000 )
            {
                pb.hFileInfo.ioFlFndrInfo.fdType = fileType;
            }

            pb.hFileInfo.ioDirID = dirID;
            error = PBSetCatInfoSync(&pb);  /* now, save the new information
                                               back to disk */

            if ( (error == noErr) && (parID != fsRtParID) ) /* can't
                                                            bump fsRtParID */
            {
                /* get the real vRefNum in case a full pathname was passed */
                error = DetermineVRefNum(name, vRefNum, &realVRefNum);
                if ( error == noErr )
                {
                    error = BumpDate(realVRefNum, parID, NULL);
                        /* and bump the parent directory's mod date to wake
                           up the Finder */
                        /* to the change we just made */
                }
            }
        }
        else
        {
            /* it was a directory, not a file */
            error = notAFileErr;
        }
    }

    return ( error );
}

/*****************************************************************************/

pascal  OSErr   FSpChangeCreatorType(const FSSpec *spec,
                                     OSType creator,
                                     OSType fileType)
{
    return ( ChangeCreatorType(spec->vRefNum, spec->parID, spec->name,
             creator, fileType) );
}

/*****************************************************************************/

pascal  OSErr   BumpDate(short vRefNum,
                         long dirID,
                         ConstStr255Param name)
/* Given a file or directory, change its modification date to the
   current date/time. */
{
    CInfoPBRec pb;
    Str31 tempName;
    OSErr error;
    unsigned long secs;

    /* Protection against File Sharing problem */
    if ( (name == NULL) || (name[0] == 0) )
    {
        tempName[0] = 0;
        pb.hFileInfo.ioNamePtr = tempName;
        pb.hFileInfo.ioFDirIndex = -1;  /* use ioDirID */
    }
    else
    {
        pb.hFileInfo.ioNamePtr = (StringPtr)name;
        pb.hFileInfo.ioFDirIndex = 0;   /* use ioNamePtr and ioDirID */
    }
    pb.hFileInfo.ioVRefNum = vRefNum;
    pb.hFileInfo.ioDirID = dirID;
    error = PBGetCatInfoSync(&pb);
    if ( error == noErr )
    {
        GetDateTime(&secs);
        /* set mod date to current date, or one second into the future
            if mod date = current date */
        pb.hFileInfo.ioFlMdDat =
                          (secs == pb.hFileInfo.ioFlMdDat) ? (++secs) : (secs);
        if ( pb.dirInfo.ioNamePtr == tempName )
        {
            pb.hFileInfo.ioDirID = pb.hFileInfo.ioFlParID;
        }
        else
        {
            pb.hFileInfo.ioDirID = dirID;
        }
        error = PBSetCatInfoSync(&pb);
    }

    return ( error );
}

/*****************************************************************************/

pascal  OSErr   FSpBumpDate(const FSSpec *spec)
{
    return ( BumpDate(spec->vRefNum, spec->parID, spec->name) );
}


/*****************************************************************************/

pascal  OSErr   OnLine(FSSpecPtr volumes,
                       short reqVolCount,
                       short *actVolCount,
                       short *volIndex)
{
    HParamBlockRec pb;
    OSErr error = noErr;
    FSSpec *endVolArray;

    if ( *volIndex > 0 )
    {
        *actVolCount = 0;
        for ( endVolArray = volumes + reqVolCount;
              (volumes < endVolArray) && (error == noErr); ++volumes )
        {
            pb.volumeParam.ioNamePtr = (StringPtr) & volumes->name;
            pb.volumeParam.ioVolIndex = *volIndex;
            error = PBHGetVInfoSync(&pb);
            if ( error == noErr )
            {
                volumes->parID = fsRtParID;     /* the root directory's
                                                   parent is 1 */
                volumes->vRefNum = pb.volumeParam.ioVRefNum;
                ++*volIndex;
                ++*actVolCount;
            }
        }
    }
    else
    {
        error = paramErr;
    }

    return ( error );
}


/*****************************************************************************/

pascal  OSErr   DTGetComment(short vRefNum,
                             long dirID,
                             ConstStr255Param name,
                             Str255 comment)
{
    DTPBRec pb;
    OSErr error;
    short dtRefNum;
    Boolean newDTDatabase;

    if (comment != NULL)
    {
        comment[0] = 0; /* return nothing by default */

        /* attempt to open the desktop database */
        error = DTOpen(name, vRefNum, &dtRefNum, &newDTDatabase);
        if ( error == noErr )
        {
            /* There was a desktop database and it's now open */

            if ( !newDTDatabase )
            {
                pb.ioDTRefNum = dtRefNum;
                pb.ioNamePtr = (StringPtr)name;
                pb.ioDirID = dirID;
                pb.ioDTBuffer = (Ptr)&comment[1];
                /*
                **  IMPORTANT NOTE #1: Inside Macintosh says that comments
                **  are up to 200 characters. While that may be correct for
                **  the HFS file system's Desktop Manager, other file
                **  systems (such as Apple Photo Access) return up to
                **  255 characters. Make sure the comment buffer is a Str255
                **  or you'll regret it.
                **
                **  IMPORTANT NOTE #2: Although Inside Macintosh doesn't
                **  mention it, ioDTReqCount is a input field to
                **  PBDTGetCommentSync. Some file systems (like HFS) ignore
                **  ioDTReqCount and always return the full comment --
                **  others (like AppleShare) respect ioDTReqCount and only
                **  return up to ioDTReqCount characters of the comment.
                */
                pb.ioDTReqCount = sizeof(Str255) - 1;
                error = PBDTGetCommentSync(&pb);
                if (error == noErr)
                {
                    comment[0] = (unsigned char)pb.ioDTActCount;
                }
            }
        }
        else
        {
            /* There is no desktop database - try the Desktop file */
            error = GetCommentFromDesktopFile(vRefNum, dirID, name, comment);
            if ( error != noErr )
            {
                error = afpItemNotFound;    /* return an expected error */
            }
        }
    }
    else
    {
        error = paramErr;
    }

    return (error);
}

/*****************************************************************************/

pascal  OSErr   FSpDTGetComment(const FSSpec *spec,
                              Str255 comment)
{
    return (DTGetComment(spec->vRefNum, spec->parID, spec->name, comment));
}


/*****************************************************************************/

pascal  OSErr   DTSetComment(short vRefNum,
                             long dirID,
                             ConstStr255Param name,
                             ConstStr255Param comment)
{
    DTPBRec pb;
    OSErr error;
    short dtRefNum;
    Boolean newDTDatabase;

    error = DTOpen(name, vRefNum, &dtRefNum, &newDTDatabase);
    if ( error == noErr )
    {
        pb.ioDTRefNum = dtRefNum;
        pb.ioNamePtr = (StringPtr)name;
        pb.ioDirID = dirID;
        pb.ioDTBuffer = (Ptr)&comment[1];
        /* Truncate the comment to 200 characters just in case */
        /* some file system doesn't range check */
        if ( comment[0] <= 200 )
        {
            pb.ioDTReqCount = comment[0];
        }
        else
        {
            pb.ioDTReqCount = 200;
        }
        error = PBDTSetCommentSync(&pb);
    }
    return (error);
}

/*****************************************************************************/

pascal  OSErr   FSpDTSetComment(const FSSpec *spec,
                              ConstStr255Param comment)
{
    return (DTSetComment(spec->vRefNum, spec->parID, spec->name, comment));
}


/*****************************************************************************/

pascal  OSErr   DTOpen(ConstStr255Param volName,
                       short vRefNum,
                       short *dtRefNum,
                       Boolean *newDTDatabase)
{
    OSErr error;
    GetVolParmsInfoBuffer volParmsInfo;
    long infoSize;
    DTPBRec pb;

    /* Check for volume Desktop Manager support before calling */
    infoSize = sizeof(GetVolParmsInfoBuffer);
    error = HGetVolParms(volName, vRefNum, &volParmsInfo, &infoSize);
    if ( error == noErr )
    {
        if ( hasDesktopMgr(volParmsInfo) )
        {
            pb.ioNamePtr = (StringPtr)volName;
            pb.ioVRefNum = vRefNum;
            error = PBDTOpenInform(&pb);
            /* PBDTOpenInform informs us if the desktop was just created */
            /* by leaving the low bit of ioTagInfo clear (0) */
            *newDTDatabase = ((pb.ioTagInfo & 1L) == 0);
            if ( error == paramErr )
            {
                error = PBDTGetPath(&pb);
                /* PBDTGetPath doesn't tell us if the database is new */
                /* so assume it is not new */
                *newDTDatabase = false;
            }
            *dtRefNum = pb.ioDTRefNum;
        }
        else
        {
            error = paramErr;
        }
    }
    return ( error );
}

/*****************************************************************************/

/*
**  GetCommentFromDesktopFile
**
**  Get a file or directory's Finder comment field (if any) from the
**  Desktop file's 'FCMT' resources.
*/
static  OSErr   GetCommentFromDesktopFile(short vRefNum,
                                          long dirID,
                                          ConstStr255Param name,
                                          Str255 comment)
{
    OSErr error;
    short commentID;
    short realVRefNum;
    Str255 desktopName;
    short savedResFile;
    short dfRefNum;
    StringHandle commentHandle;

    /* Get the comment ID number */
    error = GetCommentID(vRefNum, dirID, name, &commentID);
    if ( error == noErr )
    {
        if ( commentID != 0 )   /* commentID == 0 means there's no comment */
        {
            error = DetermineVRefNum(name, vRefNum, &realVRefNum);
            if ( error == noErr )
            {
                error = GetDesktopFileName(realVRefNum, desktopName);
                if ( error == noErr )
                {
                    savedResFile = CurResFile();
                    /*
                    **  Open the 'Desktop' file in the root directory. (because
                    **  opening the resource file could preload unwanted resources,
                    **  bracket the call with SetResLoad(s))
                    */
                    SetResLoad(false);
                    dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName,
                                            fsRdPerm);
                    SetResLoad(true);

                    if ( dfRefNum != -1)
                    {
                        /* Get the comment resource */
                        commentHandle = (StringHandle)Get1Resource(kFCMTResType,
                                                                   commentID);
                        if ( commentHandle != NULL )
                        {
                            if ( InlineGetHandleSize((Handle)commentHandle) > 0 )
                            {
                                BlockMoveData(*commentHandle, comment,
                                              *commentHandle[0] + 1);
                            }
                            else
                            {                       /* no comment available */
                                error = afpItemNotFound;
                            }
                        }
                        else
                        {                           /* no comment available */
                            error = afpItemNotFound;
                        }

                        /* restore the resource chain and close
                           the Desktop file */
                        UseResFile(savedResFile);
                        CloseResFile(dfRefNum);
                    }
                    else
                    {
                        error = afpItemNotFound;
                    }
                }
                else
                {
                    error = afpItemNotFound;
                }
            }
        }
        else
        {
            error = afpItemNotFound;    /* no comment available */
        }
    }

    return ( error );
}

/*****************************************************************************/

pascal  OSErr   HGetVolParms(ConstStr255Param volName,
                             short vRefNum,
                             GetVolParmsInfoBuffer *volParmsInfo,
                             long *infoSize)
{
    HParamBlockRec pb;
    OSErr error;

    pb.ioParam.ioNamePtr = (StringPtr)volName;
    pb.ioParam.ioVRefNum = vRefNum;
    pb.ioParam.ioBuffer = (Ptr)volParmsInfo;
    pb.ioParam.ioReqCount = *infoSize;
    error = PBHGetVolParmsSync(&pb);
    if ( error == noErr )
    {
        *infoSize = pb.ioParam.ioActCount;
    }
    return ( error );
}

/*****************************************************************************/
/*
**  GetCommentID
**
**  Get the comment ID number for the Desktop file's 'FCMT' resource ID from
**  the file or folders fdComment (frComment) field.
*/
static  OSErr   GetCommentID(short vRefNum,
                             long dirID,
                             ConstStr255Param name,
                             short *commentID)
{
    CInfoPBRec pb;
    OSErr error;

    error = GetCatInfoNoName(vRefNum, dirID, name, &pb);
    *commentID = pb.hFileInfo.ioFlXFndrInfo.fdComment;
    return ( error );
}

/*****************************************************************************/

/*
**  GetDesktopFileName
**
**  Get the name of the Desktop file.
*/
static  OSErr   GetDesktopFileName(short vRefNum,
                                   Str255 desktopName)
{
    OSErr           error;
    HParamBlockRec  pb;
    short           index;
    Boolean         found;

    pb.fileParam.ioNamePtr = desktopName;
    pb.fileParam.ioVRefNum = vRefNum;
    pb.fileParam.ioFVersNum = 0;
    index = 1;
    found = false;
    do
    {
        pb.fileParam.ioDirID = fsRtDirID;
        pb.fileParam.ioFDirIndex = index;
        error = PBHGetFInfoSync(&pb);
        if ( error == noErr )
        {
            if ( (pb.fileParam.ioFlFndrInfo.fdType == 'FNDR') &&
                 (pb.fileParam.ioFlFndrInfo.fdCreator == 'ERIK') )
            {
                found = true;
            }
        }
        ++index;
    } while ( (error == noErr) && !found );

    return ( error );
}


/*****************************************************************************/

pascal  OSErr   XGetVInfo(short volReference,
                          StringPtr volName,
                          short *vRefNum,
                          UnsignedWide *freeBytes,
                          UnsignedWide *totalBytes)
{
    OSErr           result;
    long            response;
    XVolumeParam    pb;

    /* See if large volume support is available */
    if ( ( Gestalt(gestaltFSAttr, &response) == noErr ) && ((response & (1L << gestaltFSSupports2TBVols)) != 0) )
    {
        /* Large volume support is available */
        pb.ioVRefNum = volReference;
        pb.ioNamePtr = volName;
        pb.ioXVersion = 0;  /* this XVolumeParam version (0) */
        pb.ioVolIndex = 0;  /* use ioVRefNum only, return volume name */
        result = PBXGetVolInfoSync(&pb);
        if ( result == noErr )
        {
            /* The volume name was returned in volName (if not NULL) and */
            /* we have the volume's vRefNum and allocation block size */
            *vRefNum = pb.ioVRefNum;

            /* return the freeBytes and totalBytes */
            *totalBytes = pb.ioVTotalBytes;
            *freeBytes = pb.ioVFreeBytes;
        }
    }
    else
    {
        /* No large volume support */

        /* Use HGetVInfo to get the results */
        result = HGetVInfo(volReference, volName, vRefNum, &freeBytes->lo, &totalBytes->lo);
        if ( result == noErr )
        {
            /* zero the high longs of totalBytes and freeBytes */
            totalBytes->hi = 0;
            freeBytes->hi = 0;
        }
    }
    return ( result );
}



/*****************************************************************************/

pascal  OSErr   HGetVInfo(short volReference,
                          StringPtr volName,
                          short *vRefNum,
                          unsigned long *freeBytes,
                          unsigned long *totalBytes)
{
    HParamBlockRec  pb;
    unsigned long   allocationBlockSize;
    unsigned short  numAllocationBlocks;
    unsigned short  numFreeBlocks;
    VCB             *theVCB;
    Boolean         vcbFound;
    OSErr           result;

    /* Use the File Manager to get the real vRefNum */
    pb.volumeParam.ioVRefNum = volReference;
    pb.volumeParam.ioNamePtr = volName;
    pb.volumeParam.ioVolIndex = 0;  /* use ioVRefNum only, return volume name */
    result = PBHGetVInfoSync(&pb);

    if ( result == noErr )
    {
        /* The volume name was returned in volName (if not NULL) and */
        /* we have the volume's vRefNum and allocation block size */
        *vRefNum = pb.volumeParam.ioVRefNum;
        allocationBlockSize = (unsigned long)pb.volumeParam.ioVAlBlkSiz;

        /* System 7.5 (and beyond) pins the number of allocation blocks and */
        /* the number of free allocation blocks returned by PBHGetVInfo to */
        /* a value so that when multiplied by the allocation block size, */
        /* the volume will look like it has $7fffffff bytes or less. This */
        /* was done so older applications that use signed math or that use */
        /* the GetVInfo function (which uses signed math) will continue to work. */
        /* However, the unpinned numbers (which we want) are always available */
        /* in the volume's VCB so we'll get those values from the VCB if possible. */

        /* Find the volume's VCB */
        vcbFound = false;
        theVCB = (VCB *)(GetVCBQHdr()->qHead);
        while ( (theVCB != NULL) && !vcbFound )
        {
            /* Check VCB signature before using VCB. Don't have to check for */
            /* MFS (0xd2d7) because they can't get big enough to be pinned */
            if ( theVCB->vcbSigWord == 0x4244 )
            {
                if ( theVCB->vcbVRefNum == *vRefNum )
                {
                    vcbFound = true;
                }
            }

            if ( !vcbFound )
            {
                theVCB = (VCB *)(theVCB->qLink);
            }
        }

        if ( theVCB != NULL )
        {
            /* Found a VCB we can use. Get the un-pinned number of allocation blocks */
            /* and the number of free blocks from the VCB. */
            numAllocationBlocks = (unsigned short)theVCB->vcbNmAlBlks;
            numFreeBlocks = (unsigned short)theVCB->vcbFreeBks;
        }
        else
        {
            /* Didn't find a VCB we can use. Return the number of allocation blocks */
            /* and the number of free blocks returned by PBHGetVInfoSync. */
            numAllocationBlocks = (unsigned short)pb.volumeParam.ioVNmAlBlks;
            numFreeBlocks = (unsigned short)pb.volumeParam.ioVFrBlk;
        }

        /* Now, calculate freeBytes and totalBytes using unsigned values */
        *freeBytes = numFreeBlocks * allocationBlockSize;
        *totalBytes = numAllocationBlocks * allocationBlockSize;
    }

    return ( result );
}


/*
**  PBXGetVolInfoSync is the glue code needed to make PBXGetVolInfoSync
**  File Manager requests from CFM-based programs. At some point, Apple
**  will get around to adding this to the standard libraries you link with
**  and you'll get a duplicate symbol link error. At that time, just delete
**  this code (or comment it out).
**
**  Non-CFM 68K programs don't needs this glue (and won't get it) because
**  they instead use the inline assembly glue found in the Files.h interface
**  file.
*/

#if __WANTPASCALELIMINATION
#undef  pascal
#endif

#if GENERATINGCFM
pascal OSErr PBXGetVolInfoSync(XVolumeParamPtr paramBlock)
{
    enum
    {
        kXGetVolInfoSelector = 0x0012,  /* Selector for XGetVolInfo */

        uppFSDispatchProcInfo = kRegisterBased
             | REGISTER_RESULT_LOCATION(kRegisterD0)
             | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
             | REGISTER_ROUTINE_PARAMETER(1, kRegisterD1, SIZE_CODE(sizeof(long)))  /* trap word */
             | REGISTER_ROUTINE_PARAMETER(2, kRegisterD0, SIZE_CODE(sizeof(long)))  /* selector */
             | REGISTER_ROUTINE_PARAMETER(3, kRegisterA0, SIZE_CODE(sizeof(XVolumeParamPtr)))
    };

    return ( CallOSTrapUniversalProc(NGetTrapAddress(_FSDispatch, OSTrap),
                                        uppFSDispatchProcInfo,
                                        _FSDispatch,
                                        kXGetVolInfoSelector,
                                        paramBlock) );
}
#endif

#if __WANTPASCALELIMINATION
#define pascal
#endif

/*****************************************************************************/

pascal  OSErr   GetDirName(short vRefNum,
                           long dirID,
                           Str31 name)
{
    CInfoPBRec pb;
    OSErr error;

    if ( name != NULL )
    {
        pb.dirInfo.ioNamePtr = name;
        pb.dirInfo.ioVRefNum = vRefNum;
        pb.dirInfo.ioDrDirID = dirID;
        pb.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
        error = PBGetCatInfoSync(&pb);
    }
    else
    {
        error = paramErr;
    }

    return ( error );
}


/*****************************************************************************/

pascal  OSErr   GetVolFileSystemID(ConstStr255Param pathname,
                                   short vRefNum,
                                   short *fileSystemID)
{
    HParamBlockRec pb;
    OSErr error;

    error = GetVolumeInfoNoName(pathname,vRefNum, &pb);
    if ( error == noErr )
    {
        *fileSystemID = pb.volumeParam.ioVFSID;
    }

    return ( error );
}

/*****************************************************************************/

pascal  OSErr GetDInfo(short vRefNum,
                       long dirID,
                       ConstStr255Param name,
                       DInfo *fndrInfo)
{
    CInfoPBRec pb;
    OSErr error;

    error = GetCatInfoNoName(vRefNum, dirID, name, &pb);
    if ( error == noErr )
    {
        if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
        {
            /* it's a directory, return the DInfo */
            *fndrInfo = pb.dirInfo.ioDrUsrWds;
        }
        else
        {
            /* oops, a file was passed */
            error = dirNFErr;
        }
    }

    return ( error );
}

/*****************************************************************************/

pascal  OSErr FSpGetDInfo(const FSSpec *spec,
                          DInfo *fndrInfo)
{
    return ( GetDInfo(spec->vRefNum, spec->parID, spec->name, fndrInfo) );
}