rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
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
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
//
// GENERATED FILE
//

use super::*;
use f2rust_std::*;

const FTSIZE: i32 = 5000;
const RSVUNT: i32 = 2;
const SCRUNT: i32 = 1;
const UTSIZE: i32 = ((20 + SCRUNT) + RSVUNT);
const READ: i32 = 1;
const WRITE: i32 = 2;
const SCRTCH: i32 = 3;
const NEW: i32 = 4;
const NUMAMH: i32 = 4;
const BIGI3E: i32 = 1;
const LTLI3E: i32 = 2;
const VAXGFL: i32 = 3;
const VAXDFL: i32 = 4;
const NUMBFF: i32 = 4;
const STRSIZ: i32 = 8;
const STRLEN: i32 = ((STRSIZ + 1) * NUMBFF);
const DAF: i32 = 1;
const DAS: i32 = 2;
const NUMARC: i32 = 2;
const RECL: i32 = 1024;
const FILEN: i32 = 255;
const CBFSIZ: i32 = 1024;

struct SaveVars {
    FIRST: bool,
    OPNFST: bool,
    STRAMH: ActualCharArray,
    STRARC: ActualCharArray,
    STRBFF: ActualCharArray,
    NFT: i32,
    FTABS: ActualArray<i32>,
    FTAMH: ActualArray<i32>,
    FTARC: ActualArray<i32>,
    FTBFF: ActualArray<i32>,
    FTHAN: ActualArray<i32>,
    FTNAM: ActualCharArray,
    FTRTM: ActualArray<i32>,
    FTMNM: ActualArray<f64>,
    NEXT: i32,
    NUT: i32,
    UTCST: StackArray<i32, 23>,
    UTHAN: StackArray<i32, 23>,
    UTLCK: StackArray<bool, 23>,
    UTLUN: StackArray<i32, 23>,
    NATBFF: i32,
    SUPBFF: StackArray<i32, 4>,
    NUMSUP: i32,
    REQCNT: i32,
}

impl SaveInit for SaveVars {
    fn new() -> Self {
        let mut FIRST: bool = false;
        let mut OPNFST: bool = false;
        let mut STRAMH = ActualCharArray::new(STRSIZ, 1..=NUMAMH);
        let mut STRARC = ActualCharArray::new(STRSIZ, 1..=NUMARC);
        let mut STRBFF = ActualCharArray::new(STRSIZ, 1..=NUMBFF);
        let mut NFT: i32 = 0;
        let mut FTABS = ActualArray::<i32>::new(1..=FTSIZE);
        let mut FTAMH = ActualArray::<i32>::new(1..=FTSIZE);
        let mut FTARC = ActualArray::<i32>::new(1..=FTSIZE);
        let mut FTBFF = ActualArray::<i32>::new(1..=FTSIZE);
        let mut FTHAN = ActualArray::<i32>::new(1..=FTSIZE);
        let mut FTNAM = ActualCharArray::new(FILEN, 1..=FTSIZE);
        let mut FTRTM = ActualArray::<i32>::new(1..=FTSIZE);
        let mut FTMNM = ActualArray::<f64>::new(1..=FTSIZE);
        let mut NEXT: i32 = 0;
        let mut NUT: i32 = 0;
        let mut UTCST = StackArray::<i32, 23>::new(1..=UTSIZE);
        let mut UTHAN = StackArray::<i32, 23>::new(1..=UTSIZE);
        let mut UTLCK = StackArray::<bool, 23>::new(1..=UTSIZE);
        let mut UTLUN = StackArray::<i32, 23>::new(1..=UTSIZE);
        let mut NATBFF: i32 = 0;
        let mut SUPBFF = StackArray::<i32, 4>::new(1..=NUMBFF);
        let mut NUMSUP: i32 = 0;
        let mut REQCNT: i32 = 0;

        FIRST = true;
        OPNFST = true;
        NFT = 0;
        NEXT = 0;
        NUT = 0;
        REQCNT = 0;

        Self {
            FIRST,
            OPNFST,
            STRAMH,
            STRARC,
            STRBFF,
            NFT,
            FTABS,
            FTAMH,
            FTARC,
            FTBFF,
            FTHAN,
            FTNAM,
            FTRTM,
            FTMNM,
            NEXT,
            NUT,
            UTCST,
            UTHAN,
            UTLCK,
            UTLUN,
            NATBFF,
            SUPBFF,
            NUMSUP,
            REQCNT,
        }
    }
}

//$Procedure ZZDDHMAN ( Private --- DAF/DAS Handle Manager )
pub fn ZZDDHMAN(
    LOCK: bool,
    ARCH: &[u8],
    FNAME: &[u8],
    METHOD: &[u8],
    HANDLE: i32,
    UNIT: i32,
    INTAMH: i32,
    INTARC: i32,
    INTBFF: i32,
    NATIVE: bool,
    FOUND: bool,
    KILL: bool,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    //
    // SPICELIB Functions
    //

    //
    // Local Variables
    //

    //
    // This logical allows initialization code to execute.
    //

    //
    // These strings store the labels for the parameters defined
    // in the include file and retrieved by ZZDDHINI.
    //

    //
    // The file table consists of a set of arrays which serve as
    // 'columns' of the table.  The sets of elements having the same
    // index in the arrays form the 'rows' of the table.  Each column
    // contains a particular type of information; each row contains
    // all of the information pertaining to a particular file.
    //
    // All column names in the file table begin with 'FT'.  The columns
    // are:
    //
    //    ABS      Absolute value of HAN
    //    AMH      File access method
    //    ARC      File architecture
    //    BFF      Binary file format
    //    HAN      Handle
    //    NAM      Filename
    //    RTM      RTRIM (right trimmed value for NAM)
    //    MNM      Unique DP number (the Magic NuMber ;)
    //
    // New 'rows' are added to the end of the list; the list is repacked
    // whenever a file is removed from the list.
    //
    // NFT is the number of files currently loaded; this may not be
    // greater than FTSIZE.  FINDEX refers to a file of interest within
    // the table.  Since handles are always assigned in an increasing
    // fashion, FTABS is guaranteed to be a sorted list.  We will use
    // this fact to improve handle lookups in the file table.
    //

    //
    // NEXT stores the next handle to be used for file access.  This
    // could be either for read or write based operations. NEXT is
    // incremented just before entries in the file table are made.
    // It begins as zero valued.
    //

    //
    // The unit table consists of a set of arrays which serve as
    // 'columns' of the table.  The sets of elements having the same
    // index in the arrays form the 'rows' of the table.  Each column
    // contains a particular type of information; each row contains
    // all of the information pertaining to a particular logical unit.
    //
    // All column names in the unit table begin with 'UT'.  The columns
    // are:
    //
    //    CST      Cost to remove the file from the unit table
    //    HAN      Handle
    //    LCK      Is this logical unit locked to this handle?
    //    LUN      Logical unit
    //
    // New 'rows' are added to the end of the list; the list is repacked
    // whenever a logical unit is no longer needed.
    //
    // NUT is the number of units currently stored in the table; this
    // may not exceed UTSIZE.  UINDEX refers to a unit of interest
    // within the table.
    //

    //
    // The following stores the native binary file format, a list of
    // codes for supported binary formats, and the number of entries
    // in SUPBFF.
    //

    //
    // Request counter used to determine cost.
    //

    //
    // Saved Variables
    //

    //
    // Data Statements
    //

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    } else {
        CHKIN(b"ZZDDHMAN", ctx)?;
        SIGERR(b"SPICE(BOGUSENTRY)", ctx)?;
        CHKOUT(b"ZZDDHMAN", ctx)?;
    }

    Ok(())
}

//$Procedure ZZDDHOPN ( Private --- Load file )
pub fn ZZDDHOPN(
    FNAME: &[u8],
    METHOD: &[u8],
    ARCH: &[u8],
    HANDLE: &mut i32,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut LOCFNM = [b' '; FILEN as usize];
    let mut TMPSTR = [b' '; STRSIZ as usize];
    let mut MNM: f64 = 0.0;
    let mut ACCMET: i32 = 0;
    let mut BFF: i32 = 0;
    let mut FILARC: i32 = 0;
    let mut INQHAN: i32 = 0;
    let mut IOSTAT: i32 = 0;
    let mut LCHAR: i32 = 0;
    let mut LOCKED: i32 = 0;
    let mut LOCLUN: i32 = 0;
    let mut SUPIDX: i32 = 0;
    let mut ERROR: bool = false;
    let mut INQEXT: bool = false;
    let mut INQOPN: bool = false;
    let mut LOCFND: bool = false;
    let PLATOK: bool = false;
    let mut FINDEX: i32 = 0;
    let mut UINDEX: i32 = 0;

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    } else {
        CHKIN(b"ZZDDHOPN", ctx)?;
    }

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED() to handle the unlikely event that
        // ZZDDHINI signaled SPICE(BUG).
        //
        if FAILED(ctx) {
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // On first pass, perform any runtime environment checks.
    //
    if save.OPNFST {
        ZZPLTCHK(PLATOK, ctx)?;

        if FAILED(ctx) {
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }

        //
        // Clear OPNFST, since we've performed the diagnostics.
        //
        save.OPNFST = false;
    }

    //
    // Initialize the value of HANDLE to 0.  In the event an error
    // is signaled this invalid value will be returned to the caller
    // for safety.
    //
    *HANDLE = 0;

    //
    // Left justify FNAME to compress off any leading spaces.
    //
    LJUST(FNAME, &mut LOCFNM);

    //
    // Translate the value of the requested access method to the
    // corresponding integer code.
    //
    fstr::assign(&mut TMPSTR, METHOD);
    UCASE(&TMPSTR.clone(), &mut TMPSTR, ctx);
    ACCMET = ISRCHC(&TMPSTR, NUMAMH, save.STRAMH.as_arg());

    //
    // Check if the code was located.
    //
    if (ACCMET == 0) {
        //
        // Recall HANDLE was initialized to 0, and this invalid
        // value is returned to the caller.
        //
        SETMSG(b"The attempt to load file, \'#\', with access method, \'#\', failed because this access method is unsupported.", ctx);
        ERRCH(b"#", &LOCFNM, ctx);
        ERRCH(b"#", METHOD, ctx);
        SIGERR(b"SPICE(UNSUPPORTEDMETHOD)", ctx)?;
        CHKOUT(b"ZZDDHOPN", ctx)?;
        return Ok(());
    }

    //
    // Translate the value of the requested file architecture to
    // the appropriate integer code.
    //
    fstr::assign(&mut TMPSTR, ARCH);
    UCASE(&TMPSTR.clone(), &mut TMPSTR, ctx);
    FILARC = ISRCHC(&TMPSTR, NUMARC, save.STRARC.as_arg());

    //
    // Check if the code was located.
    //
    if (FILARC == 0) {
        //
        // Recall HANDLE was initialized to 0, and this invalid
        // value is returned to the caller.
        //
        SETMSG(b"The attempt to load file, \'#\', with architecture, \'#\', failed because this file architecture is unsupported.", ctx);
        ERRCH(b"#", &LOCFNM, ctx);
        ERRCH(b"#", ARCH, ctx);
        SIGERR(b"SPICE(UNSUPPORTEDARCH)", ctx)?;
        CHKOUT(b"ZZDDHOPN", ctx)?;
        return Ok(());
    }

    //
    // Perform any preliminary checks that must be done before
    // fetching a logical unit from the unit table.  This requires
    // branching based on ACCMET's value.
    //
    if (ACCMET == SCRTCH) {
        //
        // Check to see if there are enough units available for locking
        // in the unit table.  If not, signal an error as all files
        // open with SCRTCH access must be locked to their units.
        //
        LOCKED = ZZDDHCLU(save.UTLCK.as_slice(), save.NUT);

        if (LOCKED >= (UTSIZE - RSVUNT)) {
            //
            // Recall HANDLE was initialized to 0, and this invalid
            // value is returned to the caller.
            //
            SETMSG(b"The maximum number of units are locked to handles.  As such, there is no room to open the requested scratch file.", ctx);
            SIGERR(b"SPICE(UTFULL)", ctx)?;
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }

    //
    // The NEW, READ, and WRITE access methods perform the same
    // checks on LOCFNM.
    //
    } else if (((ACCMET == NEW) || (ACCMET == READ)) || (ACCMET == WRITE)) {
        //
        // Check for a non-blank file name.
        //
        if fstr::eq(&LOCFNM, b" ") {
            //
            // Recall HANDLE was initialized to 0, and this invalid
            // value is returned to the caller.
            //
            SETMSG(
                b"The attempt to load the file has failed, because the filename is blank.",
                ctx,
            );
            SIGERR(b"SPICE(BLANKFILENAME)", ctx)?;
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }
    }

    MNM = 0.0;

    //
    // In the READ or WRITE cases verify that LOCFNM is not already
    // in the file table.
    //
    if ((ACCMET == READ) || (ACCMET == WRITE)) {
        //
        // Check to see if the file associated with LOCFNM is already in
        // the file table.
        //
        ZZDDHF2H(
            &LOCFNM,
            save.FTABS.as_slice(),
            save.FTAMH.as_slice(),
            save.FTARC.as_slice(),
            save.FTBFF.as_slice(),
            save.FTHAN.as_slice(),
            save.FTNAM.as_arg(),
            save.FTRTM.as_slice(),
            save.FTMNM.as_slice(),
            save.NFT,
            save.UTCST.as_slice_mut(),
            save.UTHAN.as_slice_mut(),
            save.UTLCK.as_slice_mut(),
            save.UTLUN.as_slice_mut(),
            &mut save.NUT,
            &mut INQEXT,
            &mut INQOPN,
            &mut INQHAN,
            &mut LOCFND,
            &mut MNM,
            ctx,
        )?;

        //
        // First, check FAILED(), and return if anything has gone awry.
        // Recall HANDLE was initialized to 0, and this invalid
        // value is returned to the caller.
        //
        if FAILED(ctx) {
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }

        //
        // Now perform some simple sanity checks before preparing to
        // load the file.  First check to see if the file exists, it must
        // if we are going to open it with ACCMET set to READ or WRITE.
        //
        if !INQEXT {
            //
            // Recall HANDLE was initialized to 0, and this invalid
            // value is returned to the caller.
            //
            SETMSG(b"The file \'#\' does not exist.", ctx);
            ERRCH(b"#", &LOCFNM, ctx);
            SIGERR(b"SPICE(FILENOTFOUND)", ctx)?;
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }

        //
        // Now if the file was not found in the file table, and it is
        // attached to a unit, this presents a problem.
        //
        if (!LOCFND && INQOPN) {
            //
            // Get the unit to include in the error message.
            //
            {
                use f2rust_std::io;

                let specs = io::InquireSpecs {
                    file: Some(&LOCFNM),
                    number: Some(&mut LOCLUN),
                    ..Default::default()
                };
                IOSTAT = io::capture_iostat(|| ctx.inquire(specs))?;
            }

            //
            // Since we performed a very similar INQUIRE statement in
            // ZZDDHF2H, a non-zero IOSTAT value indicates a severe error.
            //
            if (IOSTAT != 0) {
                //
                // Recall HANDLE was initialized to 0, and this invalid
                // value is returned to the caller.
                //
                SETMSG(b"INQUIRE failed.", ctx);
                SIGERR(b"SPICE(BUG)", ctx)?;
                CHKOUT(b"ZZDDHOPN", ctx)?;
                return Ok(());
            }

            //
            // Signal the error. Recall HANDLE was initialized to 0, and
            // this invalid value is returned to the caller.
            //
            SETMSG(b"The file \'#\' is already connected to unit #.", ctx);
            ERRCH(b"#", &LOCFNM, ctx);
            ERRINT(b"#", LOCLUN, ctx);
            SIGERR(b"SPICE(IMPROPEROPEN)", ctx)?;
            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }

        //
        // Lastly check to see if the file in the file table, and
        // perform the appropriate sanity checks.
        //
        if LOCFND {
            FINDEX = BSRCHI(i32::abs(INQHAN), save.NFT, save.FTABS.as_slice());

            //
            // Check to see if the requested architecture does not match
            // that of the entry in the file table.
            //
            if (FILARC != save.FTARC[FINDEX]) {
                //
                // Recall HANDLE was initialized to 0, and this invalid
                // value is returned to the caller.
                //
                SETMSG(b"The attempt to load file \'#\' as a # has failed because it is already loaded as a #.", ctx);
                ERRCH(b"#", &LOCFNM, ctx);
                ERRCH(b"#", &save.STRARC[FILARC], ctx);
                ERRCH(b"#", &save.STRARC[save.FTARC[FINDEX]], ctx);
                SIGERR(b"SPICE(FILARCMISMATCH)", ctx)?;
                CHKOUT(b"ZZDDHOPN", ctx)?;
                return Ok(());
            }

            //
            // Check to see if the access method is anything other
            // than READ.  If so, signal the appropriate error.
            // Note: this is only for READ.
            //
            if (ACCMET != READ) {
                //
                // Recall HANDLE was initialized to 0, and this invalid
                // value is returned to the caller.
                //
                SETMSG(b"File \'#\' already loaded.", ctx);
                ERRCH(b"#", &LOCFNM, ctx);
                SIGERR(b"SPICE(FILEOPENCONFLICT)", ctx)?;
                CHKOUT(b"ZZDDHOPN", ctx)?;
                return Ok(());
            }

            //
            // If we reach here, then we have a file that exists
            // in the table, and the caller is attempting to load it
            // for READ access.  Check to make certain it is not
            // already loaded with another method.
            //
            if (ACCMET != save.FTAMH[FINDEX]) {
                //
                // Recall HANDLE was initialized to 0, and this invalid
                // value is returned to the caller.
                //
                SETMSG(b"Unable to load file \'#\' for # access.  It is already loaded with the conflicting access #.", ctx);
                ERRCH(b"#", &LOCFNM, ctx);
                ERRCH(b"#", &save.STRAMH[ACCMET], ctx);
                ERRCH(b"#", &save.STRAMH[save.FTAMH[FINDEX]], ctx);
                SIGERR(b"SPICE(RWCONFLICT)", ctx)?;
                CHKOUT(b"ZZDDHOPN", ctx)?;
                return Ok(());
            }

            //
            // If we make it this far, the file is in the file table
            // and all the sanity checks have passed. Return to the
            // caller as this is effectively a no-op.
            //
            *HANDLE = save.FTHAN[FINDEX];

            CHKOUT(b"ZZDDHOPN", ctx)?;
            return Ok(());
        }
    }

    //
    // Now check to see if there is room in the file table for this
    // new file.
    //
    if (save.NFT == FTSIZE) {
        //
        // Recall HANDLE was initialized to 0, and this invalid
        // value is returned to the caller.
        //
        SETMSG(b"The file table is full, with # entries. As a result, the file \'#\' could not be loaded.", ctx);
        ERRINT(b"#", save.NFT, ctx);
        ERRCH(b"#", &LOCFNM, ctx);
        SIGERR(b"SPICE(FTFULL)", ctx)?;
        CHKOUT(b"ZZDDHOPN", ctx)?;
        return Ok(());
    }

    //
    // We are about to attempt a HANDLE to LUN connection, increment
    // the request counter.
    //
    ZZDDHRCM(save.NUT, save.UTCST.as_slice_mut(), &mut save.REQCNT);

    //
    // Free up a logical unit in the UNIT table for our usage.
    //
    ZZDDHGTU(
        save.UTCST.as_slice_mut(),
        save.UTHAN.as_slice_mut(),
        save.UTLCK.as_slice_mut(),
        save.UTLUN.as_slice_mut(),
        &mut save.NUT,
        &mut UINDEX,
        ctx,
    )?;

    //
    // Check FAILED() since ZZDDHGTU may have invoked GETLUN.
    // Recall HANDLE was initialized to 0, and this invalid
    // value is returned to the caller.
    //
    if FAILED(ctx) {
        CHKOUT(b"ZZDDHOPN", ctx)?;
        return Ok(());
    }

    //
    // Trim up the filename.
    //
    if (ACCMET != SCRTCH) {
        LCHAR = RTRIM(&LOCFNM);
    }

    //
    // If we have made it this far, then we're ready to perform the
    // appropriate open.  First get the handle ready.
    //
    save.NEXT = (save.NEXT + 1);

    //
    // Determine the sign of the new handle based on the requested
    // METHOD.
    //
    if (ACCMET == READ) {
        save.UTHAN[UINDEX] = save.NEXT;
    } else {
        save.UTHAN[UINDEX] = -save.NEXT;
    }

    //
    // The code that follows is structured a little strangely.  This
    // discussion is an attempt to clarify what the code does and
    // the motivation that led to its peculiar construction.
    //
    // First, the file, scratch or otherwise, is opened with the
    // appropriate OPEN statement.  Then, the logical ERROR is set
    // to TRUE or FALSE depending on whether and IOSTAT error has
    // occurred as a result of the OPEN.  At this point, the code
    // enters into a IF block structured in the following manner:
    //
    //    IF ( ERROR ) THEN
    //
    //       Signal the IOSTAT related error from the OPEN statement.
    //
    //    ELSE IF ( ACCMET .EQ SCRTCH ) THEN
    //
    //       Attempt to INQUIRE on the UNIT assigned to the scratch
    //       file to determine its name.  Store a default value,
    //       in the event one is not returned.
    //
    //    ELSE IF ( ACCMET .EQ. READ ) .OR. ( ACCMET .EQ. WRITE ) THEN
    //
    //       Examine the preexisting file to determine if its FTP
    //       detection string, file architecture, and binary
    //       file format are acceptable.  If not, then signal the
    //       error, set ERROR to TRUE, and do not check out or
    //       return.
    //
    //    END IF
    //
    //    IF ( ERROR ) THEN
    //
    //       Remove the UNIT from the unit table. Decrement NEXT,
    //       since the current value is not to be assigned as
    //       a handle for this file. Check out and return.
    //
    //    END IF
    //
    // The reason the code is structured in this unusual fashion
    // is to allow for a single treatment of the clean up on error
    // code to exist.
    //

    //
    // Perform the OPEN.  Branch on the appropriate access method.
    //
    if (ACCMET == SCRTCH) {
        {
            use f2rust_std::io;

            let specs = io::OpenSpecs {
                unit: Some(save.UTLUN[UINDEX]),
                access: Some(b"DIRECT"),
                recl: Some(RECL),
                status: Some(b"SCRATCH"),
                ..Default::default()
            };
            IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
        }

        BFF = save.NATBFF;
    } else if (ACCMET == NEW) {
        {
            use f2rust_std::io;

            let specs = io::OpenSpecs {
                unit: Some(save.UTLUN[UINDEX]),
                file: Some(fstr::substr(&LOCFNM, 1..=LCHAR)),
                access: Some(b"DIRECT"),
                recl: Some(RECL),
                status: Some(b"NEW"),
                ..Default::default()
            };
            IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
        }

        BFF = save.NATBFF;
    } else if (ACCMET == READ) {
        {
            use f2rust_std::io;

            let specs = io::OpenSpecs {
                unit: Some(save.UTLUN[UINDEX]),
                file: Some(fstr::substr(&LOCFNM, 1..=LCHAR)),
                access: Some(b"DIRECT"),
                recl: Some(RECL),
                status: Some(b"OLD"),
                ..Default::default()
            };
            IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
        }
    } else if (ACCMET == WRITE) {
        {
            use f2rust_std::io;

            let specs = io::OpenSpecs {
                unit: Some(save.UTLUN[UINDEX]),
                file: Some(fstr::substr(&LOCFNM, 1..=LCHAR)),
                access: Some(b"DIRECT"),
                recl: Some(RECL),
                status: Some(b"OLD"),
                ..Default::default()
            };
            IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
        }
    }

    //
    // Verify that IOSTAT is non-zero.
    //
    ERROR = (IOSTAT != 0);

    //
    // Partially process the error.
    //
    if ERROR {
        //
        // Now signal the error, but delay cleaning up and checking
        // out until leaving this IF block.
        //
        if (ACCMET == SCRTCH) {
            SETMSG(b"Attempt to open scratch file failed. IOSTAT was #.", ctx);
        } else if (ACCMET == NEW) {
            SETMSG(
                b"Attempt to create new file, \'$\' failed. IOSTAT was #.",
                ctx,
            );
        } else {
            SETMSG(
                b"Attempt to open file, \'$\' for % access failed. IOSTAT was #.",
                ctx,
            );
        }

        ERRCH(b"$", &LOCFNM, ctx);
        ERRCH(b"%", &save.STRAMH[ACCMET], ctx);
        ERRINT(b"#", IOSTAT, ctx);
        SIGERR(b"SPICE(FILEOPENFAIL)", ctx)?;

    //
    // If no IOSTAT based error has occurred as a result of the OPEN
    // statement, then perform any remaining checks or I/O operations
    // that are necessary to support loading the file.
    //
    } else if (ACCMET == SCRTCH) {
        //
        // Inquire on the logical unit to produce the file name for
        // the scratch file.  Set the initial value of LOCFNM, in case
        // the INQUIRE does not replace it.
        //
        fstr::assign(&mut LOCFNM, b"# SCRATCH FILE");
        REPMC(&LOCFNM.clone(), b"#", &save.STRARC[FILARC], &mut LOCFNM);

        {
            use f2rust_std::io;

            let specs = io::InquireSpecs {
                unit: Some(save.UTLUN[UINDEX]),
                name: Some(&mut LOCFNM),
                ..Default::default()
            };
            IOSTAT = io::capture_iostat(|| ctx.inquire(specs))?;
        }

        //
        // In the event that this INQUIRE failed, replace the value
        // stored in LOCFNM with the initial value.
        //
        if (IOSTAT != 0) {
            fstr::assign(&mut LOCFNM, b"# SCRATCH FILE");
            REPMC(&LOCFNM.clone(), b"#", &save.STRARC[FILARC], &mut LOCFNM);
        }

        //
        // Store the RTRIM value of this filename in LCHAR.
        //
        LCHAR = RTRIM(&LOCFNM);
    } else if ((ACCMET == READ) || (ACCMET == WRITE)) {
        //
        // Check for FTP errors, verify that FILARC is appropriate,
        // and determine the binary file format of the preexisting
        // file LOCFNM.
        //
        ZZDDHPPF(save.UTLUN[UINDEX], FILARC, &mut BFF, ctx)?;

        //
        // Set ERROR.
        //
        ERROR = FAILED(ctx);

        //
        // If no error has occurred, verify that BFF is among the
        // list of supported format ID codes for the requested access
        // method.
        //
        if !ERROR {
            //
            // This platform supports reading from files whose
            // format codes are listed in SUPBFF.
            //
            if (ACCMET == READ) {
                SUPIDX = ISRCHI(BFF, save.NUMSUP, save.SUPBFF.as_slice());

                if (SUPIDX == 0) {
                    //
                    // Delay clean up and check out.
                    //
                    ERROR = true;

                    if (BFF == 0) {
                        SETMSG(b"Attempt to open file, \'#\', for read access has failed.  This file utilizes an unknown binary file format.  This error may result from attempting to open a corrupt file or one of an unknown type.", ctx);
                        ERRCH(b"#", &LOCFNM, ctx);
                        SIGERR(b"SPICE(UNSUPPORTEDBFF)", ctx)?;
                    } else {
                        SETMSG(b"Attempt to open file, \'#\', for read access has failed.  The non-native binary file format \'#\' is not currently supported on this platform.  Obtain a transfer format version, and convert it to the native format. See the Convert User\'s Guide for details.", ctx);
                        ERRCH(b"#", &LOCFNM, ctx);
                        ERRCH(b"#", &save.STRBFF[BFF], ctx);
                        SIGERR(b"SPICE(UNSUPPORTEDBFF)", ctx)?;
                    }
                }

            //
            // This platform only supports writing to files whose
            // binary formats are native.
            //
            } else {
                //
                // Delay clean up and check out.
                //
                if (BFF == 0) {
                    ERROR = true;

                    SETMSG(b"Attempt to open file, \'#\', for write access has failed.  This file utilizes an unknown binary file format.  This error may result from attempting to open a corrupt file or one of an unknown type.", ctx);
                    ERRCH(b"#", &LOCFNM, ctx);
                    SIGERR(b"SPICE(UNSUPPORTEDBFF)", ctx)?;
                } else if (BFF != save.NATBFF) {
                    ERROR = true;

                    SETMSG(b"Attempt to open file, \'#\', for write access has failed.  This file utilizes the non-native binary file format \'#\'.  At this time only files of the native format, \'#\', are supported for write access.  See the Convert User\'s Guide for details.", ctx);
                    ERRCH(b"#", &LOCFNM, ctx);
                    ERRCH(b"#", &save.STRBFF[BFF], ctx);
                    ERRCH(b"#", &save.STRBFF[save.NATBFF], ctx);
                    SIGERR(b"SPICE(UNSUPPORTEDBFF)", ctx)?;
                }
            }
        }
    }

    //
    // If an error has occurred as a result of opening the file or
    // examining its contents, clean up and check out.
    //
    if ERROR {
        //
        // Close the unit we were using.  Remember to delete the file
        // if it was a 'new' one.
        //
        if (ACCMET == NEW) {
            {
                use f2rust_std::io;

                let specs = io::CloseSpecs {
                    unit: Some(save.UTLUN[UINDEX]),
                    status: Some(b"DELETE"),
                    ..Default::default()
                };
                ctx.close(specs)?;
            }
        } else {
            {
                use f2rust_std::io;

                let specs = io::CloseSpecs {
                    unit: Some(save.UTLUN[UINDEX]),
                    ..Default::default()
                };
                ctx.close(specs)?;
            }
        }

        //
        // Remove the unit from the unit table, since this UNIT
        // is no longer in use.
        //
        ZZDDHRMU(
            UINDEX,
            save.NFT,
            save.UTCST.as_slice_mut(),
            save.UTHAN.as_slice_mut(),
            save.UTLCK.as_slice_mut(),
            save.UTLUN.as_slice_mut(),
            &mut save.NUT,
            ctx,
        )?;

        //
        // Decrement NEXT since this handle was never assigned to
        // a file.
        //
        save.NEXT = (save.NEXT - 1);

        //
        // Recall HANDLE was initialized to 0, and this invalid
        // value is returned to the caller.
        //
        CHKOUT(b"ZZDDHOPN", ctx)?;
        return Ok(());
    }

    //
    // Finish filling out the unit table.
    //
    save.UTCST[UINDEX] = save.REQCNT;

    //
    // Only scratch files get the units locked to handles, this is
    // because they only exist as long as they have a unit.
    //
    save.UTLCK[UINDEX] = (ACCMET == SCRTCH);

    //
    // Now fill out the file table.
    //
    save.NFT = (save.NFT + 1);

    //
    // Use the absolute value of the handle used to index the file
    // table.
    //
    save.FTABS[save.NFT] = i32::abs(save.UTHAN[UINDEX]);

    //
    // Assign access method, file architecture, and native binary file
    // format to the appropriate columns.
    //
    save.FTAMH[save.NFT] = ACCMET;
    save.FTARC[save.NFT] = FILARC;
    save.FTBFF[save.NFT] = BFF;

    //
    // Assign the handle, filename, RTRIM ( FTNAM(NFT) ) as FTRTM, and
    // unique DP number as FTMNM.
    //
    save.FTHAN[save.NFT] = save.UTHAN[UINDEX];
    fstr::assign(
        save.FTNAM.get_mut(save.NFT),
        fstr::substr(&LOCFNM, 1..=LCHAR),
    );
    save.FTRTM[save.NFT] = LCHAR;
    save.FTMNM[save.NFT] = MNM;

    //
    // Assign HANDLE the value of the new handle.
    //
    *HANDLE = save.FTHAN[save.NFT];

    CHKOUT(b"ZZDDHOPN", ctx)?;
    Ok(())
}

//$Procedure ZZDDHCLS ( Private --- Close file )
pub fn ZZDDHCLS(HANDLE: i32, ARCH: &[u8], KILL: bool, ctx: &mut Context) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut TMPSTR = [b' '; STRSIZ as usize];
    let mut ACCMET: i32 = 0;
    let mut FILARC: i32 = 0;
    let mut FINDEX: i32 = 0;
    let mut UINDEX: i32 = 0;

    //

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    } else {
        CHKIN(b"ZZDDHCLS", ctx)?;
    }

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED() only to trap the possibility of ZZDDHINI
        // signaling SPICE(BUG).
        //
        if FAILED(ctx) {
            CHKOUT(b"ZZDDHCLS", ctx)?;
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // Find the file in the handle table.
    //
    FINDEX = BSRCHI(i32::abs(HANDLE), save.NFT, save.FTABS.as_slice());

    //
    // Check to see whether we found the handle or not.
    //
    if (FINDEX == 0) {
        CHKOUT(b"ZZDDHCLS", ctx)?;
        return Ok(());
    } else if (save.FTHAN[FINDEX] != HANDLE) {
        CHKOUT(b"ZZDDHCLS", ctx)?;
        return Ok(());
    }

    //
    // Before actually closing the file, check the input architecture
    // matches that listed in the file table for this handle.  This is
    // to prevent one architecture's code from stepping on another's.
    //
    fstr::assign(&mut TMPSTR, ARCH);
    UCASE(&TMPSTR.clone(), &mut TMPSTR, ctx);
    FILARC = ISRCHC(&TMPSTR, NUMARC, save.STRARC.as_arg());

    //
    // Check to see if FILARC matches the code stored in the FTARC
    // column of the file table for this handle.  If it doesn't,
    // signal an error.
    //
    if (FILARC != save.FTARC[FINDEX]) {
        SETMSG(b"Logical unit associated with # file $, is trying to be closed by routines in in the % system.", ctx);
        ERRCH(b"#", &save.STRARC[save.FTARC[FINDEX]], ctx);
        ERRCH(b"%", &TMPSTR, ctx);
        ERRCH(b"$", &save.FTNAM[FINDEX], ctx);
        SIGERR(b"SPICE(FILARCMISMATCH)", ctx)?;
        CHKOUT(b"ZZDDHCLS", ctx)?;
        return Ok(());
    }

    //
    // Now check that if KILL is set, the file is accessible for
    // WRITE.
    //
    if (KILL && (save.FTAMH[FINDEX] == READ)) {
        SETMSG(
            b"# file $ is open for READ access.  Attempt to close and delete file has failed. ",
            ctx,
        );
        ERRCH(b"#", &save.STRARC[save.FTARC[FINDEX]], ctx);
        ERRCH(b"#", &save.FTNAM[FINDEX], ctx);
        SIGERR(b"SPICE(INVALIDACCESS)", ctx)?;
        CHKOUT(b"ZZDDHCLS", ctx)?;
        return Ok(());
    }

    //
    // Buffer the access method for HANDLE, since we may need it
    // when deciding which close to perform.
    //
    ACCMET = save.FTAMH[FINDEX];

    //
    // If we reach here, we need to remove the row FINDEX from
    // the file table.
    //
    for I in (FINDEX + 1)..=save.NFT {
        save.FTABS[(I - 1)] = save.FTABS[I];
        save.FTAMH[(I - 1)] = save.FTAMH[I];
        save.FTARC[(I - 1)] = save.FTARC[I];
        save.FTBFF[(I - 1)] = save.FTBFF[I];
        save.FTHAN[(I - 1)] = save.FTHAN[I];
        let val = save.FTNAM.get(I).to_vec();
        fstr::assign(save.FTNAM.get_mut((I - 1)), &val);
        save.FTRTM[(I - 1)] = save.FTRTM[I];
        save.FTMNM[(I - 1)] = save.FTMNM[I];
    }

    save.NFT = (save.NFT - 1);

    //
    // Locate HANDLE in the unit table.
    //
    UINDEX = ISRCHI(HANDLE, save.NUT, save.UTHAN.as_slice());

    if (UINDEX != 0) {
        //
        // Close the unit.
        //
        if (KILL && (ACCMET != SCRTCH)) {
            {
                use f2rust_std::io;

                let specs = io::CloseSpecs {
                    unit: Some(save.UTLUN[UINDEX]),
                    status: Some(b"DELETE"),
                    ..Default::default()
                };
                ctx.close(specs)?;
            }
        } else {
            {
                use f2rust_std::io;

                let specs = io::CloseSpecs {
                    unit: Some(save.UTLUN[UINDEX]),
                    ..Default::default()
                };
                ctx.close(specs)?;
            }
        }

        //
        // Remove its entry from the unit table.
        //
        ZZDDHRMU(
            UINDEX,
            save.NFT,
            save.UTCST.as_slice_mut(),
            save.UTHAN.as_slice_mut(),
            save.UTLCK.as_slice_mut(),
            save.UTLUN.as_slice_mut(),
            &mut save.NUT,
            ctx,
        )?;
    } else {
        //
        // First, check to see if KILL is set, if it is signal an error
        // since we are unable to delete the file.
        //
        if (KILL && (ACCMET != SCRTCH)) {
            SETMSG(b"File successfully closed.  Unable to delete file as requested.  File not currently present in the UNIT table. ", ctx);
            SIGERR(b"SPICE(FILENOTCONNECTED)", ctx)?;
            CHKOUT(b"ZZDDHCLS", ctx)?;
            return Ok(());
        }

        //
        // If we were unable to find the HANDLE in the unit table,
        // check to see if we have to clean up the UNIT table.
        //
        if (save.NFT < save.NUT) {
            UINDEX = ISRCHI(0, save.NUT, save.UTHAN.as_slice());

            //
            // Now check to see if we located a zero valued handle.
            // If we did not manage to, then this is an error condition,
            // since we have more LUNs listed in the unit table than
            // files in the file table.
            //
            if (UINDEX == 0) {
                SETMSG(b"There are less files in the file table than units in the unit table, and no row with a zero-valued handle can be found.  This should never occur.", ctx);
                SIGERR(b"SPICE(BUG)", ctx)?;
                CHKOUT(b"ZZDDHCLS", ctx)?;
                return Ok(());
            }

            //
            // Free the unit.
            //
            FRELUN(save.UTLUN[UINDEX], ctx);

            //
            // Compress the table.
            //
            for I in (UINDEX + 1)..=save.NUT {
                save.UTCST[(I - 1)] = save.UTCST[I];
                save.UTHAN[(I - 1)] = save.UTHAN[I];
                save.UTLCK[(I - 1)] = save.UTLCK[I];
                save.UTLUN[(I - 1)] = save.UTLUN[I];
            }

            //
            // Decrement NUT.
            //
            save.NUT = (save.NUT - 1);
        }
    }

    CHKOUT(b"ZZDDHCLS", ctx)?;
    Ok(())
}

//$Procedure ZZDDHHLU ( Private --- Handle to Logical Unit )
pub fn ZZDDHHLU(
    HANDLE: i32,
    ARCH: &[u8],
    LOCK: bool,
    UNIT: &mut i32,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut TMPSTR = [b' '; STRSIZ as usize];
    let mut FILARC: i32 = 0;
    let mut IOSTAT: i32 = 0;
    let mut LOCKED: i32 = 0;
    let mut ERROR: bool = false;
    let mut FINDEX: i32 = 0;
    let mut UINDEX: i32 = 0;

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    } else {
        CHKIN(b"ZZDDHHLU", ctx)?;
    }

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED() only to trap the possibility of ZZDDHINI
        // signaling SPICE(BUG).
        //
        if FAILED(ctx) {
            *UNIT = 0;
            CHKOUT(b"ZZDDHHLU", ctx)?;
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // Locate HANDLE in the file table.
    //
    FINDEX = BSRCHI(i32::abs(HANDLE), save.NFT, save.FTABS.as_slice());

    if (FINDEX == 0) {
        ERROR = true;
    } else if (save.FTHAN[FINDEX] != HANDLE) {
        ERROR = true;
    } else {
        ERROR = false;
    }

    if ERROR {
        *UNIT = 0;

        SETMSG(b"There is no file loaded with handle = #", ctx);
        ERRINT(b"#", HANDLE, ctx);
        SIGERR(b"SPICE(NOSUCHHANDLE)", ctx)?;
        CHKOUT(b"ZZDDHHLU", ctx)?;
        return Ok(());
    }

    //
    // Before actually fetching the unit, check the input architecture
    // matches that listed in the file table for this handle.  This is
    // to prevent one architectures code from stepping on another's.
    //
    fstr::assign(&mut TMPSTR, ARCH);
    UCASE(&TMPSTR.clone(), &mut TMPSTR, ctx);
    FILARC = ISRCHC(&TMPSTR, NUMARC, save.STRARC.as_arg());

    //
    // Check to see if FILARC matches the code stored in the FTARC
    // column of the file table for this handle.  If it doesn't,
    // signal an error.
    //
    if (FILARC != save.FTARC[FINDEX]) {
        *UNIT = 0;

        SETMSG(b"Logical unit associated with # file $, is trying to be unlocked by routines in in the % system.", ctx);
        ERRCH(b"#", &save.STRARC[save.FTARC[FINDEX]], ctx);
        ERRCH(b"%", &TMPSTR, ctx);
        ERRCH(b"$", &save.FTNAM[FINDEX], ctx);
        SIGERR(b"SPICE(FILARCMISMATCH)", ctx)?;
        CHKOUT(b"ZZDDHHLU", ctx)?;
        return Ok(());
    }

    //
    // If we make it this far, then we will be processing a handle
    // to logical unit request.  Increment REQCNT.
    //
    ZZDDHRCM(save.NUT, save.UTCST.as_slice_mut(), &mut save.REQCNT);

    //
    // Now check to see if the handle is already present in the
    // unit table.
    //
    UINDEX = ISRCHI(HANDLE, save.NUT, save.UTHAN.as_slice());

    //
    // Check to see if we didn't locate the HANDLE in the table.
    // If we didn't, open the file associated with HANDLE again,
    // and get it into the unit table.
    //
    if (UINDEX == 0) {
        //
        // We need a unit from the unit table, get one.
        //
        ZZDDHGTU(
            save.UTCST.as_slice_mut(),
            save.UTHAN.as_slice_mut(),
            save.UTLCK.as_slice_mut(),
            save.UTLUN.as_slice_mut(),
            &mut save.NUT,
            &mut UINDEX,
            ctx,
        )?;

        //
        // Check FAILED, since ZZDDHGTU may have invoked GETLUN.
        //
        if FAILED(ctx) {
            *UNIT = 0;

            CHKOUT(b"ZZDDHHLU", ctx)?;
            return Ok(());
        }

        //
        // Re-attach the file to a logical unit.  Branch based on the
        // access method stored in the file table.
        //
        if ((save.FTAMH[FINDEX] == NEW) || (save.FTAMH[FINDEX] == WRITE)) {
            {
                use f2rust_std::io;

                let specs = io::OpenSpecs {
                    unit: Some(save.UTLUN[UINDEX]),
                    file: Some(fstr::substr(save.FTNAM.get(FINDEX), 1..=save.FTRTM[FINDEX])),
                    access: Some(b"DIRECT"),
                    recl: Some(RECL),
                    status: Some(b"OLD"),
                    ..Default::default()
                };
                IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
            }
        } else if (save.FTAMH[FINDEX] == READ) {
            {
                use f2rust_std::io;

                let specs = io::OpenSpecs {
                    unit: Some(save.UTLUN[UINDEX]),
                    file: Some(fstr::substr(save.FTNAM.get(FINDEX), 1..=save.FTRTM[FINDEX])),
                    access: Some(b"DIRECT"),
                    recl: Some(RECL),
                    status: Some(b"OLD"),
                    ..Default::default()
                };
                IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
            }
        } else {
            *UNIT = 0;

            SETMSG(
                b"Invalid access method. This error should never be signaled.",
                ctx,
            );
            SIGERR(b"SPICE(BUG)", ctx)?;
            CHKOUT(b"ZZDDHHLU", ctx)?;
            return Ok(());
        }

        //
        // Check IOSTAT for troubles.
        //
        if (IOSTAT != 0) {
            //
            // The re-open was unsuccessful, leave the entry in the file
            // table and clean up the row in the unit table before
            // returning.  Normally when we call ZZDDHRMU it is to
            // remove a unit from the unit table.  In this case we
            // know the unit will remain since we have not decreased
            // the entries in the file table.
            //
            ZZDDHRMU(
                UINDEX,
                save.NFT,
                save.UTCST.as_slice_mut(),
                save.UTHAN.as_slice_mut(),
                save.UTLCK.as_slice_mut(),
                save.UTLUN.as_slice_mut(),
                &mut save.NUT,
                ctx,
            )?;

            //
            // Now signal the error.
            //
            *UNIT = 0;

            SETMSG(
                b"Attempt to reconnect logical unit to file \'#\' failed. IOSTAT was #.",
                ctx,
            );
            ERRCH(b"#", &save.FTNAM[FINDEX], ctx);
            ERRINT(b"#", IOSTAT, ctx);
            SIGERR(b"SPICE(FILEOPENFAIL)", ctx)?;
            CHKOUT(b"ZZDDHHLU", ctx)?;
            return Ok(());
        }

        //
        // Lastly populate the unit table values.
        //
        save.UTHAN[UINDEX] = save.FTHAN[FINDEX];
        save.UTLCK[UINDEX] = false;
    }

    //
    // At this point UINDEX points to the row in the unit table that
    // contains the connection information.  We need to update the cost
    // row with the new value of REQCNT, and then set the lock row to
    // TRUE if a lock request was made.
    //
    save.UTCST[UINDEX] = save.REQCNT;

    if (LOCK && !save.UTLCK[UINDEX]) {
        //
        // First check to see if we have enough lockable units
        // left in the unit table.
        //
        LOCKED = ZZDDHCLU(save.UTLCK.as_slice(), save.NUT);

        if (LOCKED >= ((UTSIZE - RSVUNT) - SCRUNT)) {
            *UNIT = 0;

            SETMSG(b"Unable to lock handle for file \'#\' to a logical unit.  There are no rows available for locking in the unit table.", ctx);
            ERRCH(b"#", &save.FTNAM[FINDEX], ctx);
            SIGERR(b"SPICE(HLULOCKFAILED)", ctx)?;
            CHKOUT(b"ZZDDHHLU", ctx)?;
            return Ok(());
        }

        save.UTLCK[UINDEX] = true;
    }

    //
    // Set the value of UNIT and return.
    //
    *UNIT = save.UTLUN[UINDEX];

    CHKOUT(b"ZZDDHHLU", ctx)?;
    Ok(())
}

//$Procedure ZZDDHUNL ( Private --- Unlock Logical Unit from Handle )
pub fn ZZDDHUNL(HANDLE: i32, ARCH: &[u8], ctx: &mut Context) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut TMPSTR = [b' '; STRSIZ as usize];
    let mut FILARC: i32 = 0;
    let mut FINDEX: i32 = 0;
    let mut UINDEX: i32 = 0;

    //
    // Standard SPICE discovery error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    }

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED() only to trap the possibility of ZZDDHINI
        // signaling SPICE(BUG).  No check out is performed, see the
        // $Restrictions section of the entry point header for details.
        //
        if FAILED(ctx) {
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }
    //
    // Prevent the user from locating zero handle rows.  This is not
    // really necessary since zero handle rows in the unit table are
    // empty and awaiting connections.  The state of the UTLCK is
    // not significant.
    //
    if (HANDLE == 0) {
        return Ok(());
    }

    //
    // Look up the handle in the unit table.
    //
    UINDEX = ISRCHI(HANDLE, save.NUT, save.UTHAN.as_slice());

    //
    // Now check the results of the lookup.  If HANDLE was not found
    // in the unit table or the unit was not locked, just return as
    // there is nothing to do.
    //
    if (UINDEX == 0) {
        return Ok(());
    } else if !save.UTLCK[UINDEX] {
        return Ok(());
    }

    //
    // Now look up the handle in the table. Remember FTABS is a sorted
    // list in increasing order.
    //
    FINDEX = BSRCHI(i32::abs(HANDLE), save.NFT, save.FTABS.as_slice());

    //
    // Check to see if HANDLE is in the file table.  We know it has
    // to be since it is in the unit table if we make it this far.
    // These checks are just for safety's sake.
    //
    if (FINDEX == 0) {
        CHKIN(b"ZZDDHUNL", ctx)?;
        SETMSG(b"HANDLE # was not found in the file table but was located in the unit table.  This error should never occur.", ctx);
        ERRINT(b"#", HANDLE, ctx);
        SIGERR(b"SPICE(BUG)", ctx)?;
        CHKOUT(b"ZZDDHUNL", ctx)?;
        return Ok(());
    } else if (save.FTHAN[FINDEX] != HANDLE) {
        CHKIN(b"ZZDDHUNL", ctx)?;
        SETMSG(b"HANDLE # was not found in the file table but was located in the unit table.  This error should never occur.", ctx);
        ERRINT(b"#", HANDLE, ctx);
        SIGERR(b"SPICE(BUG)", ctx)?;
        CHKOUT(b"ZZDDHUNL", ctx)?;
        return Ok(());
    }

    //
    // Before actually unlocking the unit, check the input architecture
    // matches that listed in the file table for this handle.  This is
    // to prevent one architectures code from stepping on another's.
    //
    fstr::assign(&mut TMPSTR, ARCH);
    UCASE(&TMPSTR.clone(), &mut TMPSTR, ctx);
    FILARC = ISRCHC(&TMPSTR, NUMARC, save.STRARC.as_arg());

    //
    // Check to see if FILARC matches the code stored in the FTARC
    // column of the file table for this handle.  If it doesn't,
    // signal an error.
    //
    if (FILARC != save.FTARC[FINDEX]) {
        CHKIN(b"ZZDDHUNL", ctx)?;
        SETMSG(b"Logical unit associated with # file $, is trying to be unlocked by routines in in the % system.", ctx);
        ERRCH(b"#", &save.STRARC[save.FTARC[FINDEX]], ctx);
        ERRCH(b"%", &TMPSTR, ctx);
        ERRCH(b"$", &save.FTNAM[FINDEX], ctx);
        SIGERR(b"SPICE(FILARCMISMATCH)", ctx)?;
        CHKOUT(b"ZZDDHUNL", ctx)?;
        return Ok(());
    }

    //
    // Lastly, check to see if the access method for HANDLE indicates
    // scratch access.  If it is, just return, since scratch files
    // can not have their units unlocked.
    //
    if (save.FTAMH[FINDEX] == SCRTCH) {
        return Ok(());
    }

    save.UTLCK[UINDEX] = false;

    Ok(())
}

//$Procedure ZZDDHNFO ( Private --- Get information about a Handle )
pub fn ZZDDHNFO(
    HANDLE: i32,
    FNAME: &mut [u8],
    INTARC: &mut i32,
    INTBFF: &mut i32,
    INTAMH: &mut i32,
    FOUND: &mut bool,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut FINDEX: i32 = 0;

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED(), and return on failure.  We are not checking
        // out or in since this routine would be error free if not for
        // the possibility of ZZDDHINI signaling SPICE(BUG).  See
        // $Restrictions for details.
        //
        if FAILED(ctx) {
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // Look up the handle in the table.  Remember FTABS is sorted
    // listed in increasing order.
    //
    FINDEX = BSRCHI(i32::abs(HANDLE), save.NFT, save.FTABS.as_slice());

    //
    // Check to see if HANDLE is in the handle table.  Remember that
    // we are indexing the table using the absolute value of handle.
    // So include a check to see that HANDLE is FTHAN(FINDEX).
    //
    if (FINDEX == 0) {
        fstr::assign(FNAME, b" ");
        *INTARC = 0;
        *INTBFF = 0;
        *INTAMH = 0;
        *FOUND = false;
        return Ok(());
    } else if (save.FTHAN[FINDEX] != HANDLE) {
        fstr::assign(FNAME, b" ");
        *INTARC = 0;
        *INTBFF = 0;
        *INTAMH = 0;
        *FOUND = false;
        return Ok(());
    }

    //
    // If we make it this far, then we have a handle that is in
    // the handle table at row FINDEX.
    //
    *FOUND = true;
    fstr::assign(
        FNAME,
        fstr::substr(save.FTNAM.get(FINDEX), 1..=save.FTRTM[FINDEX]),
    );
    *INTARC = save.FTARC[FINDEX];
    *INTBFF = save.FTBFF[FINDEX];
    *INTAMH = save.FTAMH[FINDEX];

    Ok(())
}

//$Procedure ZZDDHISN ( Private --- Is Handle Native? )
pub fn ZZDDHISN(
    HANDLE: i32,
    NATIVE: &mut bool,
    FOUND: &mut bool,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut FINDEX: i32 = 0;

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED(), and return on failure.  We are not checking
        // out or in since this routine would be error free if not for
        // the possibility of ZZDDHINI signaling SPICE(BUG).  See
        // $Restrictions for details.
        //
        if FAILED(ctx) {
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // Look up the handle in the table. Remember FTABS is sorted
    // listed in increasing order.
    //
    FINDEX = BSRCHI(i32::abs(HANDLE), save.NFT, save.FTABS.as_slice());

    //
    // Check to see if HANDLE is in the handle table.  Remember
    // that we are indexing the table using the absolute value of
    // handle.  So include a check to see that HANDLE is FTHAN(FINDEX).
    //
    if (FINDEX == 0) {
        *FOUND = false;
        return Ok(());
    } else if (save.FTHAN[FINDEX] != HANDLE) {
        *FOUND = false;
        return Ok(());
    }

    //
    // If we make it this far, then we have found HANDLE in the file
    // table.  Set NATIVE appropriately and FOUND to TRUE.
    //
    *NATIVE = (save.NATBFF == save.FTBFF[FINDEX]);
    *FOUND = true;

    Ok(())
}

//$Procedure ZZDDHFNH ( Private --- Filename to Handle )
pub fn ZZDDHFNH(
    FNAME: &[u8],
    HANDLE: &mut i32,
    FOUND: &mut bool,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut LOCFNM = [b' '; FILEN as usize];
    let mut MNM: f64 = 0.0;
    let mut INQHAN: i32 = 0;
    let mut INQEXT: bool = false;
    let mut INQOPN: bool = false;

    if RETURN(ctx) {
        return Ok(());
    } else {
        CHKIN(b"ZZDDHFNH", ctx)?;
    }

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED() only to trap the possibility of ZZDDHINI
        // signaling SPICE(BUG).
        //
        if FAILED(ctx) {
            *HANDLE = 0;
            CHKOUT(b"ZZDDHFNH", ctx)?;
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // Left justify FNAME to trim any leading white space.
    //
    LJUST(FNAME, &mut LOCFNM);

    //
    // Look up FNAME in the handle table.
    //
    ZZDDHF2H(
        &LOCFNM,
        save.FTABS.as_slice(),
        save.FTAMH.as_slice(),
        save.FTARC.as_slice(),
        save.FTBFF.as_slice(),
        save.FTHAN.as_slice(),
        save.FTNAM.as_arg(),
        save.FTRTM.as_slice(),
        save.FTMNM.as_slice(),
        save.NFT,
        save.UTCST.as_slice_mut(),
        save.UTHAN.as_slice_mut(),
        save.UTLCK.as_slice_mut(),
        save.UTLUN.as_slice_mut(),
        &mut save.NUT,
        &mut INQEXT,
        &mut INQOPN,
        &mut INQHAN,
        FOUND,
        &mut MNM,
        ctx,
    )?;

    //
    // Check found and set HANDLE if we have got one.  No need to
    // check FAILED() since ZZDDHF2H returns FOUND set to FALSE on
    // error.
    //
    if *FOUND {
        *HANDLE = INQHAN;
    } else {
        *HANDLE = 0;
    }

    CHKOUT(b"ZZDDHFNH", ctx)?;
    Ok(())
}

//$Procedure ZZDDHLUH ( Private --- Logical Unit to Handle )
pub fn ZZDDHLUH(
    UNIT: i32,
    HANDLE: &mut i32,
    FOUND: &mut bool,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut UINDEX: i32 = 0;

    //
    // Do the initialization tasks.
    //
    if save.FIRST {
        ZZDDHINI(
            &mut save.NATBFF,
            save.SUPBFF.as_slice_mut(),
            &mut save.NUMSUP,
            save.STRAMH.as_arg_mut(),
            save.STRARC.as_arg_mut(),
            save.STRBFF.as_arg_mut(),
            ctx,
        )?;

        //
        // Check FAILED(), and return on failure.  We are not checking
        // out or in since this routine would be error free if not for
        // the possibility of ZZDDHINI signaling SPICE(BUG).  See
        // $Restrictions for details.
        //
        if FAILED(ctx) {
            *HANDLE = 0;
            return Ok(());
        }

        //
        // Clear FIRST since we've done the initialization.
        //
        save.FIRST = false;
    }

    //
    // Look up the unit in the table.
    //
    UINDEX = ISRCHI(UNIT, save.NUT, save.UTLUN.as_slice());

    if (UINDEX == 0) {
        *HANDLE = 0;
        *FOUND = false;
        return Ok(());
    } else if (save.UTHAN[UINDEX] == 0) {
        *HANDLE = 0;
        *FOUND = false;
        return Ok(());
    }

    //
    // We've got a handle, store the value and return.
    //
    *HANDLE = save.UTHAN[UINDEX];
    *FOUND = true;

    Ok(())
}