summaryrefslogtreecommitdiff
path: root/genisoimage/write.c
blob: a423ab1c16240e30443e37404d51f96fad030256 (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
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
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
/*
 * This file has been modified for the cdrkit suite.
 *
 * The behaviour and appearence of the program code below can differ to a major
 * extent from the version distributed by the original author(s).
 *
 * For details, see Changelog file distributed with the cdrkit package. If you
 * received this file from another source then ask the distributing person for
 * a log of modifications.
 *
 */

/* @(#)write.c	1.88 06/02/01 joerg */
/* Parts from @(#)write.c	1.106 07/02/17 joerg */
/*
 * Program write.c - dump memory  structures to  file for iso9660 filesystem.
 *
 * Written by Eric Youngdale (1993).
 *
 * Copyright 1993 Yggdrasil Computing, Incorporated
 * Copyright (c) 1999-2003 J. Schilling
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* APPLE_HYB James Pearson j.pearson@ge.ucl.ac.uk 23/2/2000 */

#include <mconfig.h>
#include "genisoimage.h"
#include <timedefs.h>
#include <fctldefs.h>
#ifdef SORTING
#include "match.h"
#endif /* SORTING */
#include <errno.h>
#include <schily.h>
#ifdef DVD_VIDEO
#include "dvd_reader.h"
#include "dvd_file.h"
#include "ifo_read.h"
#endif
#ifdef APPLE_HYB
#include <ctype.h>
#endif

#ifdef	VMS
#include "vms.h"
#endif

/* Max number of sectors we will write at  one time */
#define	NSECT 16

/* Counters for statistics */

static int	table_size = 0;
static int	total_dir_size = 0;
static int	rockridge_size = 0;
static struct directory **pathlist;
static int	next_path_index = 1;
static int	sort_goof;

static int	is_rr_dir = 0;

struct output_fragment *out_tail;
struct output_fragment *out_list;

struct iso_primary_descriptor vol_desc;

void	set_721(char *pnt, unsigned int i);
void	set_722(char *pnt, unsigned int i);
void	set_723(char *pnt, unsigned int i);
void	set_731(char *pnt, unsigned int i);
void	set_732(char *pnt, unsigned int i);
void	set_733(char *pnt, unsigned int i);
int	get_731(char *p);
int	get_732(char *p);
int	get_733(char *p);
static	int	xawrite(void *buffer, int size, int count, FILE *file, 
							  int submode, BOOL islast);
void	xfwrite(void *buffer, int size, int count, FILE *file, int submode, 
				  BOOL islast);
static 	int	assign_directory_addresses(struct directory *node);
#ifdef APPLE_HYB
static 	void	write_one_file(char *filename, off_t size, FILE *outfile, 
										off_t off);
#else
static 	void	write_one_file(char *filename, off_t size, FILE *outfile);
#endif
static 	void	write_files(FILE *outfile);
#if 0
static 	void	dump_filelist	__PR((void));
#endif
static 	int	compare_dirs(const void *rr, const void *ll);
int	sort_directory(struct directory_entry **sort_dir, int rr);
static 	int	root_gen(void);
static 	BOOL	assign_file_addresses(struct directory *dpnt, BOOL isnest);
static 	void	free_one_directory(struct directory *dpnt);
static 	void	free_directories(struct directory *dpnt);
void	generate_one_directory(struct directory *dpnt, FILE *outfile);
static 	void	build_pathlist(struct directory *node);
static 	int	compare_paths(void const *r, void const *l);
static 	int	generate_path_tables(void);
void	memcpy_max(char *to, char *from, int max);
void	outputlist_insert(struct output_fragment *frag);
static 	int	file_write(FILE *outfile);
static 	int	pvd_write(FILE *outfile);
static 	int	xpvd_write(FILE *outfile);
static 	int	evd_write(FILE *outfile);
static 	int	vers_write(FILE *outfile);
static 	int	graftcp(char *to, char *from, char *ep);
static 	int	pathcp(char *to, char *from, char *ep);
static 	int	pathtab_write(FILE *outfile);
static 	int	exten_write(FILE *outfile);
int	oneblock_size(int starting_extent);
static 	int	pathtab_size(int starting_extent);
static 	int	startpad_size(int starting_extent);
static 	int	interpad_size(int starting_extent);
static 	int	endpad_size(int starting_extent);
static 	int	file_gen(void);
static 	int	dirtree_dump(void);
static 	int	dirtree_fixup(int starting_extent);
static 	int	dirtree_size(int starting_extent);
static 	int	ext_size(int starting_extent);
static 	int	dirtree_write(FILE *outfile);
static 	int	dirtree_cleanup(FILE *outfile);
static 	int	startpad_write(FILE *outfile);
static 	int	interpad_write(FILE *outfile);
static 	int	endpad_write(FILE *outfile);
#ifdef APPLE_HYB
static 	int	hfs_pad;
static 	int	hfs_get_parms(char *key);
static 	void	hfs_file_gen(int start_extent);
static 	void	gen_prepboot(void);
Ulong	get_adj_size(int Csize);
int	adj_size(int Csize, int start_extent, int extra);
void	adj_size_other(struct directory *dpnt);
static 	int	hfs_hce_write(FILE * outfile);
int	insert_padding_file(int size);
#endif	/* APPLE_HYB */

#ifdef SORTING
static 	int	compare_sort(const void * rr, const void * ll);
static 	void	reassign_link_addresses(struct directory * dpnt);
static 	int	sort_file_addresses(void);
#endif /* SORTING */

/*
 * Routines to actually write the disc.  We write sequentially so that
 * we could write a tape, or write the disc directly
 */
#define	FILL_SPACE(X)	memset(vol_desc.X, ' ', sizeof (vol_desc.X))

void
set_721(char *pnt, unsigned int i)
{
	pnt[0] = i & 0xff;
	pnt[1] = (i >> 8) & 0xff;
}

void
set_722(char *pnt, unsigned int i)
{
	pnt[0] = (i >> 8) & 0xff;
	pnt[1] = i & 0xff;
}

void
set_723(char *pnt, unsigned int i)
{
	pnt[3] = pnt[0] = i & 0xff;
	pnt[2] = pnt[1] = (i >> 8) & 0xff;
}

void
set_731(char *pnt, unsigned int i)
{
	pnt[0] = i & 0xff;
	pnt[1] = (i >> 8) & 0xff;
	pnt[2] = (i >> 16) & 0xff;
	pnt[3] = (i >> 24) & 0xff;
}

void
set_732(char *pnt, unsigned int i)
{
	pnt[3] = i & 0xff;
	pnt[2] = (i >> 8) & 0xff;
	pnt[1] = (i >> 16) & 0xff;
	pnt[0] = (i >> 24) & 0xff;
}

void
set_733(char *pnt, unsigned int i)
{
	pnt[7] = pnt[0] = i & 0xff;
	pnt[6] = pnt[1] = (i >> 8) & 0xff;
	pnt[5] = pnt[2] = (i >> 16) & 0xff;
	pnt[4] = pnt[3] = (i >> 24) & 0xff;
}

int
get_731(char *p)
{
	return ((p[0] & 0xff)
		| ((p[1] & 0xff) << 8)
		| ((p[2] & 0xff) << 16)
		| ((p[3] & 0xff) << 24));
}

int
get_732(char *p)
{
	return ((p[3] & 0xff)
		| ((p[2] & 0xff) << 8)
		| ((p[1] & 0xff) << 16)
		| ((p[0] & 0xff) << 24));
}

int
get_733(char *p)
{
	return ((p[0] & 0xff)
		| ((p[1] & 0xff) << 8)
		| ((p[2] & 0xff) << 16)
		| ((p[3] & 0xff) << 24));
}

void
xfwrite(void *buffer, int size, int count, FILE *file, int submode, BOOL islast)
{
	/*
	 * This is a hack that could be made better.
	 * XXXIs this the only place?
	 * It is definitely needed on Operating Systems that do not allow to
	 * write files that are > 2GB. If the system is fast enough to be able
	 * to feed 1400 KB/s writing speed of a DVD-R drive, use stdout.
	 * If the system cannot do this reliable, you need to use this hacky
	 * option.
	 */
	static int	idx = 0;

#ifdef	XFWRITE_DEBUG
	if (count != 1 || (size % 2048) != 0)
		fprintf(stderr, "Count: %d, size: %d\n", count, size);
#endif

	if (split_output != 0 &&
		(idx == 0 || ftell(file) >= ((off_t)1024 * 1024 * 1024))) {
		char		nbuf[512];
		extern char	*outfile;

		if (idx == 0)
			unlink(outfile);
		sprintf(nbuf, "%s_%02d", outfile, idx++);
		file = freopen(nbuf, "wb", file);
		if (file == NULL) {
#ifdef	USE_LIBSCHILY
			comerr("Cannot open '%s'.\n", nbuf);
#else
			fprintf(stderr, "Cannot open '%s'.\n", nbuf);
			exit(1);
#endif
		}
	}
	while (count) {
		int	got;

		seterrno(0);
		if (osecsize != 0)
			got = xawrite(buffer, size, count, file, submode, islast);
		else
			got = fwrite(buffer, size, count, file);

		if (got <= 0) {
#ifdef	USE_LIBSCHILY
			comerr("cannot fwrite %d*%d\n", size, count);
#else
			fprintf(stderr, "cannot fwrite %d*%d\n", size, count);
			exit(1);
#endif
		}
		/*
		 * This comment is in hope to prevent silly people from
		 * e.g. SuSE (who did not yet learn C but believe that
		 * they need to patch other peoples code) from changing the
		 * next cast into an illegal lhs cast expression.
		 * The cast below is the correct way to handle the problem.
		 * The (void *) cast is to avoid a GCC warning like:
		 * "warning: dereferencing type-punned pointer will break \
		 * strict-aliasing rules"
		 * which is wrong this code. (void *) introduces a compatible
		 * intermediate type in the cast list.
		 */
		count -= got;
		buffer = (void *)(((char *)buffer) + size * got);
	}
}

static int
xawrite(void *buffer, int size, int count, FILE *file, int submode, BOOL islast)
{
	register char	*p = buffer;
	register int	amt = size * count;
	register int	n;
	struct xa_subhdr subhdr[2];

	if (osecsize == 2048)
		return (fwrite(buffer, size, count, file));

	if (amt % 2048)
		comerrno(EX_BAD,
			"Trying to write %d bytes (not a multiple of 2048).\n",
			amt);

	subhdr[0].file_number		= subhdr[1].file_number		= 0;
	subhdr[0].channel_number	= subhdr[1].channel_number	= 0;
	subhdr[0].coding		= subhdr[1].coding		= 0;

	while (amt > 0) {
#ifdef	LATER
		if (submode < 0)
			subhdr[0].sub_mode = subhdr[1].sub_mode = XA_SUBH_DATA;
		else
			subhdr[0].sub_mode = subhdr[1].sub_mode = submode;
#else
		subhdr[0].sub_mode = subhdr[1].sub_mode = XA_SUBH_DATA;
#endif

		if ((amt <= 2048) && islast) {
			subhdr[0].sub_mode = subhdr[1].sub_mode
						|= (XA_SUBH_EOR|XA_SUBH_EOF);
		}
		n = fwrite(&subhdr, sizeof (subhdr), 1, file);
		if (n <= 0)
			return (n);

		n = fwrite(p, 2048, 1, file);
		if (n <= 0)
			return (n);

		p += 2048;
		amt -= 2048;
	}
	return (1);
}

#ifdef APPLE_HYB
/*
 * use the deferred_write struct to store info about the hfs_boot_file
 */
static struct deferred_write mac_boot;

#endif	/* APPLE_HYB */
static struct deferred_write	*dw_head = NULL,
				*dw_tail = NULL;

unsigned int	last_extent_written = 0;
static	Uint	path_table_index;
time_t	begun;

/*
 * We recursively walk through all of the directories and assign extent
 * numbers to them.  We have already assigned extent numbers to everything that
 * goes in front of them
 */
static int
assign_directory_addresses(struct directory *node)
{
	int		dir_size;
	struct directory *dpnt;

	dpnt = node;

	while (dpnt) {
		/* skip if it's hidden */
		if (dpnt->dir_flags & INHIBIT_ISO9660_ENTRY) {
			dpnt = dpnt->next;
			continue;
		}
		/*
		 * If we already have an extent for this (i.e. it came from a
		 * multisession disc), then don't reassign a new extent.
		 */
		dpnt->path_index = next_path_index++;
		if (dpnt->extent == 0) {
			dpnt->extent = last_extent;
			dir_size = ISO_BLOCKS(dpnt->size);

			last_extent += dir_size;

			/*
			 * Leave room for the CE entries for this directory.
			 * Keep them close to the reference directory so that
			 * access will be quick.
			 */
			if (dpnt->ce_bytes) {
				last_extent += ISO_BLOCKS(dpnt->ce_bytes);
			}
		}
		if (dpnt->subdir) {
			assign_directory_addresses(dpnt->subdir);
		}
		dpnt = dpnt->next;
	}
	return (0);
}

#ifdef APPLE_HYB
static void
write_one_file(char *filename, off_t size, FILE *outfile, off_t off)
#else
static void
write_one_file(char *filename, off_t size, FILE *outfile)
#endif	/* APPLE_HYB */
{
	/*
	 * It seems that there are still stone age C-compilers
	 * around.
	 * The Metrowerks C found on BeOS/PPC does not allow
	 * more than 32kB of local vars.
	 * As we do not need to call write_one_file() recursively
	 * we make buffer static.
	 */
static	char		buffer[SECTOR_SIZE * NSECT];
	FILE		*infile;
	off_t		remain;
	int	use;

	char *mirror_name;
	unsigned char md5[16];
	int include_in_jigdo = list_file_in_jigdo(filename, size, &mirror_name, md5);

	if ((infile = fopen(filename, "rb")) == NULL) {
#ifdef	USE_LIBSCHILY
		comerr("cannot open '%s'\n", filename);
#else
#ifndef	HAVE_STRERROR
		fprintf(stderr, "cannot open '%s': (%d)\n",
				filename, errno);
#else
		fprintf(stderr, "cannot open '%s': %s\n",
				filename, strerror(errno));
#endif
		exit(1);
#endif
	}
#ifdef APPLE_HYB
	fseek(infile, off, SEEK_SET);
#endif	/* APPLE_HYB */
	remain = size;

	if (include_in_jigdo)
		write_jt_match_record(filename, mirror_name, SECTOR_SIZE, size, md5);

	while (remain > 0) {
		int	amt;

		use = (remain > SECTOR_SIZE * NSECT - 1 ?
				NSECT * SECTOR_SIZE : remain);
		use = ISO_ROUND_UP(use);	/* Round up to nearest sector */
						/* boundary */
		memset(buffer, 0, use);
		seterrno(0);
		amt = fread(buffer, 1, use, infile);
		if (amt < use && amt < remain) {
			/*
			 * Note that genisoimage is not star and no 100% archiver.
			 * We only detect file growth if the new size does not
			 * match 'use' at the last read.
			 */
			if (geterrno() == 0) {
#ifdef	USE_LIBSCHILY
         comerrno(EX_BAD,
               "File '%s' did shrink.\n"
               "Files must not be changed while genisoimage runs!\n",
               filename);
#else
         fprintf(stderr,
               "File '%s' did shrink.\n"
               "Files must not be changed while genisoimage runs!\n",
               filename);
				exit(EX_BAD);
#endif
			}
#ifdef	USE_LIBSCHILY
			comerr("Cannot read from '%s'\n", filename);
#else
			fprintf(stderr, "Cannot read from '%s'\n", filename);
			exit(1);
#endif
		}
		if (!include_in_jigdo)
			jtwrite(buffer, use, 1,
			        XA_SUBH_DATA, remain <= (SECTOR_SIZE * NSECT));
		xfwrite(buffer, use, 1, outfile,
				XA_SUBH_DATA, remain <= (SECTOR_SIZE * NSECT));
		last_extent_written += use / SECTOR_SIZE;
#if 0
		if ((last_extent_written % 1000) < use / SECTOR_SIZE) {
			fprintf(stderr, "%d..", last_extent_written);
		}
#else
		if (verbose > 0 &&
		    (int)(last_extent_written % (gui ? 500 : 5000)) <
							use / SECTOR_SIZE) {
			time_t	now;
			time_t	the_end;
			double	frac;

			time(&now);
			frac = last_extent_written / (1.0 * last_extent);
			the_end = begun + (now - begun) / frac;
#ifndef NO_FLOATINGPOINT
			fprintf(stderr, "%6.2f%% done, estimate finish %s",
				frac * 100., ctime(&the_end));
#else
			fprintf(stderr, "%3d.%-02d%% done, estimate finish %s",
				(int)(frac * 100.),
				(int)((frac+.00005) * 10000.)%100,
				ctime(&the_end));
#endif
			fflush(stderr);
		}
#endif
		remain -= use;
	}
	fclose(infile);
}/* write_one_file(... */

static void
write_files(FILE *outfile)
{
	struct deferred_write	*dwpnt,
				*dwnext;

	dwpnt = dw_head;
	while (dwpnt) {
/*#define DEBUG*/
#ifdef DEBUG
		fprintf(stderr,
		"The file name is %s and pad is %d, size is %lld and extent is %d\n",
				dwpnt->name, dwpnt->pad,
				(Llong)dwpnt->size, dwpnt->extent);
#endif
		if (dwpnt->table) {
			jtwrite(dwpnt->table, ISO_ROUND_UP(dwpnt->size), 1, XA_SUBH_DATA, TRUE);
			xfwrite(dwpnt->table, ISO_ROUND_UP(dwpnt->size), 1,
							outfile,
							XA_SUBH_DATA, TRUE);
			last_extent_written += ISO_BLOCKS(dwpnt->size);
			table_size += dwpnt->size;
/*			fprintf(stderr, "Size %lld ", (Llong)dwpnt->size); */
			free(dwpnt->table);
			dwpnt->table = NULL;
		} else {

#ifdef VMS
			vms_write_one_file(dwpnt->name, dwpnt->size, outfile);
#else
#ifdef APPLE_HYB
			write_one_file(dwpnt->name, dwpnt->size, outfile,
								dwpnt->off);
#else
			write_one_file(dwpnt->name, dwpnt->size, outfile);
#endif	/* APPLE_HYB */
#endif
			free(dwpnt->name);
			dwpnt->name = NULL;
		}


#ifndef DVD_VIDEO
#define	dvd_video	0
#endif

#ifndef APPLE_HYB
#define	apple_hyb	0
#endif

#if	defined(APPLE_HYB) || defined(DVD_VIDEO)

		if (apple_hyb || dvd_video) {
			/*
			 * we may have to pad out ISO files to work with HFS
			 * clump sizes
			 */
			char	blk[SECTOR_SIZE];
			Uint	i;

			for (i = 0; i < dwpnt->pad; i++) {
				jtwrite(blk, SECTOR_SIZE, 1, 0, FALSE);
				xfwrite(blk, SECTOR_SIZE, 1, outfile, 0, FALSE);
				last_extent_written++;
			}
		}
#endif	/* APPLE_HYB || DVD_VIDEO */


		dwnext = dwpnt;
		dwpnt = dwpnt->next;
		free(dwnext);
		dwnext = NULL;
	}
}/* write_files(... */

#if 0
static void
dump_filelist()
{
	struct deferred_write *dwpnt;

	dwpnt = dw_head;
	while (dwpnt) {
		fprintf(stderr, "File %s\n", dwpnt->name);
		dwpnt = dwpnt->next;
	}
	fprintf(stderr, "\n");
}

#endif

static int
compare_dirs(const void *rr, const void *ll)
{
	char		*rpnt,
			*lpnt;
	struct directory_entry **r,
			**l;

	r = (struct directory_entry **) rr;
	l = (struct directory_entry **) ll;
	rpnt = (*r)->isorec.name;
	lpnt = (*l)->isorec.name;

#ifdef APPLE_HYB
	/*
	 * resource fork MUST (not sure if this is true for HFS volumes) be
	 * before the data fork - so force it here
	 */
	if ((*r)->assoc && (*r)->assoc == (*l))
		return (1);
	if ((*l)->assoc && (*l)->assoc == (*r))
		return (-1);
#endif	/* APPLE_HYB */

	/* If the entries are the same, this is an error. */
	if (strcmp(rpnt, lpnt) == 0) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD,
			"Error: '%s' and '%s' have the same ISO9660 name '%s'.\n",
			(*r)->whole_name, (*l)->whole_name,
			rpnt);
#else
		fprintf(stderr,
			"Error: '%s' and '%s' have the same ISO9660 name '%s'.\n",
			(*r)->whole_name, (*l)->whole_name,
			rpnt);
#endif
		sort_goof++;
	}
	/* Check we don't have the same RR name */
	if (use_RockRidge && !is_rr_dir) {
		/*
		 * entries *can* have the same RR name in the "rr_moved"
		 * directory so skip checks if we're in reloc_dir
		 */
		if (!(strcmp((*r)->name, (*l)->name))) {
#ifdef	USE_LIBSCHILY
			errmsgno(EX_BAD,
			"Error: '%s' and '%s' have the same Rock Ridge name '%s'.\n",
				(*r)->whole_name, (*l)->whole_name,
				(*r)->name);
#else
			fprintf(stderr,
			"Error: '%s' and '%s' have the same Rock Ridge name '%s'.\n",
				(*r)->whole_name, (*l)->whole_name,
				(*r)->name);
#endif
			sort_goof++;
		}
	}
	/*
	 * Put the '.' and '..' entries on the head of the sorted list. For
	 * normal ASCII, this always happens to be the case, but out of band
	 * characters cause this not to be the case sometimes.
	 * FIXME(eric) - these tests seem redundant, in that the name is never
	 * assigned these values.  It will instead be \000 or \001, and thus
	 * should always be sorted correctly.   I need to figure out why I
	 * thought I needed this in the first place.
	 */
#if 0
	if (strcmp(rpnt, ".") == 0)
		return (-1);
	if (strcmp(lpnt, ".") == 0)
		return (1);

	if (strcmp(rpnt, "..") == 0)
		return (-1);
	if (strcmp(lpnt, "..") == 0)
		return (1);
#else
	/*
	 * The code above is wrong (as explained in Eric's comment), leading to
	 * incorrect sort order iff the -L option ("allow leading dots") is in
	 * effect and a directory contains entries that start with a dot.
	 *  (TF, Tue Dec 29 13:49:24 CET 1998)
	 */
	if ((*r)->isorec.name_len[0] == 1 && *rpnt == 0)
		return (-1);	/* '.' */
	if ((*l)->isorec.name_len[0] == 1 && *lpnt == 0)
		return (1);

	if ((*r)->isorec.name_len[0] == 1 && *rpnt == 1)
		return (-1);	/* '..' */
	if ((*l)->isorec.name_len[0] == 1 && *lpnt == 1)
		return (1);
#endif

	while (*rpnt && *lpnt) {
		if (*rpnt == ';' && *lpnt != ';')
			return (-1);
		if (*rpnt != ';' && *lpnt == ';')
			return (1);

		if (*rpnt == ';' && *lpnt == ';')
			return (0);

		if (*rpnt == '.' && *lpnt != '.')
			return (-1);
		if (*rpnt != '.' && *lpnt == '.')
			return (1);

		if ((unsigned char) *rpnt < (unsigned char) *lpnt)
			return (-1);
		if ((unsigned char) *rpnt > (unsigned char) *lpnt)
			return (1);
		rpnt++;
		lpnt++;
	}
	if (*rpnt)
		return (1);
	if (*lpnt)
		return (-1);
	return (0);
}

/*
 * Function:		sort_directory
 *
 * Purpose:		Sort the directory in the appropriate ISO9660
 *			order.
 *
 * Notes:		Returns 0 if OK, returns > 0 if an error occurred.
 */
int
sort_directory(struct directory_entry **sort_dir, int rr)
{
	int		dcount = 0;
	int		xcount = 0;
	int		j;
	int		i,
			len;
	struct directory_entry *s_entry;
	struct directory_entry **sortlist;

	/* need to keep a count of how many entries are hidden */
	s_entry = *sort_dir;
	while (s_entry) {
		if (s_entry->de_flags & INHIBIT_ISO9660_ENTRY)
			xcount++;
		dcount++;
		s_entry = s_entry->next;
	}

	if (dcount == 0) {
		return (0);
	}
	/* OK, now we know how many there are.  Build a vector for sorting. */
	sortlist = (struct directory_entry **)
		e_malloc(sizeof (struct directory_entry *) * dcount);

	j = dcount - 1;
	dcount = 0;
	s_entry = *sort_dir;
	while (s_entry) {
		if (s_entry->de_flags & INHIBIT_ISO9660_ENTRY) {
			/* put any hidden entries at the end of the vector */
			sortlist[j--] = s_entry;
		} else {
			sortlist[dcount] = s_entry;
			dcount++;
		}
		len = s_entry->isorec.name_len[0];
		s_entry->isorec.name[len] = 0;
		s_entry = s_entry->next;
	}

	/* Each directory is required to contain at least . and .. */
	if (dcount < 2) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD,
			"Directory size too small (. or .. missing ??%s)\n",
			"?");	/* Try to avoid a GCC trigraph warning */
#else
		fprintf(stderr,
			"Directory size too small (. or .. missing ??%s)\n",
			"?");	/* Try to avoid a GCC trigraph warning */
#endif
		sort_goof = 1;

	} else {
		/* only sort the non-hidden entries */
		sort_goof = 0;
		is_rr_dir = rr;
#ifdef PROTOTYPES
		qsort(sortlist, dcount, sizeof (struct directory_entry *),
			(int (*) (const void *, const void *)) compare_dirs);
#else
		qsort(sortlist, dcount, sizeof (struct directory_entry *),
			compare_dirs);
#endif

		/*
		 * Now reassemble the linked list in the proper sorted order
		 * We still need the hidden entries, as they may be used in
		 * the Joliet tree.
		 */
		for (i = 0; i < dcount + xcount - 1; i++) {
			sortlist[i]->next = sortlist[i + 1];
		}

		sortlist[dcount + xcount - 1]->next = NULL;
		*sort_dir = sortlist[0];
	}

	free(sortlist);
	sortlist = NULL;
	return (sort_goof);
}

static int
root_gen()
{
	init_fstatbuf();

	root_record.length[0] = 1 +
			offsetof(struct iso_directory_record, name[0]);
	root_record.ext_attr_length[0] = 0;
	set_733((char *) root_record.extent, root->extent);
	set_733((char *) root_record.size, ISO_ROUND_UP(root->size));
	iso9660_date(root_record.date, root_statbuf.st_mtime);
	root_record.flags[0] = ISO_DIRECTORY;
	root_record.file_unit_size[0] = 0;
	root_record.interleave[0] = 0;
	set_723(root_record.volume_sequence_number, volume_sequence_number);
	root_record.name_len[0] = 1;
	return (0);
}

#ifdef SORTING
/*
 *	sorts deferred_write entries based on the sort weight
 */
static int
compare_sort(const void *rr, const void *ll)
{
	struct deferred_write	**r;
	struct deferred_write	**l;
	int			r_sort;
	int			l_sort;

	r = (struct deferred_write **) rr;
	l = (struct deferred_write **) ll;
	r_sort = (*r)->s_entry->sort;
	l_sort = (*l)->s_entry->sort;

	if (r_sort != l_sort)
		return (r_sort < l_sort ? 1 : -1);
	else
		return ((*r)->extent - (*l)->extent);
}

/*
 *	reassign start extents to files that are "hard links" to
 *	files that may have been sorted
 */
static void
reassign_link_addresses(struct directory *dpnt)
{
	struct directory_entry	*s_entry;
	struct file_hash	*s_hash;

	while (dpnt) {
		s_entry = dpnt->contents;
		for (s_entry = dpnt->contents; s_entry; s_entry = s_entry->next) {
			/* link files have already been given the weight NOT_SORTED */
			if (s_entry->sort == NOT_SORTED)
			{
				/* update the start extent */
				s_hash = find_hash(s_entry->dev, s_entry->inode);
				if (s_hash) {
					set_733((char *) s_entry->isorec.extent,
							s_hash->starting_block);
					s_entry->starting_block = s_hash->starting_block;
				}
			}

			if (verbose > 2 && s_entry->size != 0) {
				fprintf(stderr, "%8u %8u ",
					s_entry->starting_block,
					(unsigned int)(s_entry->starting_block + ISO_BLOCKS(s_entry->size) - 1));

				if (s_entry->inode != TABLE_INODE) {
					fprintf(stderr, "%s\n", s_entry->whole_name);
				} else {
					fprintf(stderr, "%s%s%s\n",
						s_entry->filedir->whole_name,
						SPATH_SEPARATOR, trans_tbl);
				}
			}
		}
		if (dpnt->subdir) {
			reassign_link_addresses(dpnt->subdir);
		}

		dpnt = dpnt->next;
	}
}

/*
 *	sort files in order of the given sort weight
 */
static int
sort_file_addresses()
{
	struct deferred_write	*dwpnt;
	struct deferred_write	**sortlist;
	struct directory_entry	*s_entry;
	int			start_extent;
	int			num = 0;
	int			i;

	/* need to store start extents for linked files */
	flush_hash();

	/* find out how many files we have */
	dwpnt = dw_head;
	while (dwpnt) {
		num++;
		dwpnt = dwpnt->next;
	}

	/* return if we have none */
	if (num == 0) {
		return (1);
	}

	/* save the start extent of the first file */
	start_extent = dw_head->extent;

	/* set up vector to store entries */
	sortlist = (struct deferred_write **)
		e_malloc(sizeof (struct deferred_write *) * num);

	for (i = 0, dwpnt = dw_head; i < num; i++, dwpnt = dwpnt->next)
		sortlist[i] = dwpnt;

	/* sort the list */
#ifdef PROTOTYPES
	qsort(sortlist, num, sizeof (struct deferred_write *),
		(int (*)(const void *, const void *))compare_sort);
#else
	qsort(sortlist, num, sizeof (struct deferred_write *), compare_sort);
#endif

	/* reconstruct the linked list */
	for (i = 0; i < num-1; i++) {
		sortlist[i]->next = sortlist[i+1];
	}

	sortlist[num-1]->next = NULL;
	dw_head = sortlist[0];

	free(sortlist);

	/* set the new start extents for the sorted list */
	for (i = 0, dwpnt = dw_head; i < num; i++, dwpnt = dwpnt->next) {
		s_entry = dwpnt->s_entry;
		dwpnt->extent = s_entry->starting_block = start_extent;
		set_733((char *) s_entry->isorec.extent, start_extent);

		start_extent += ISO_BLOCKS(s_entry->size);
#ifdef DVD_VIDEO
		/*
		 * Shouldn't this be done for every type of sort? Otherwise
		 * we will loose every pad info we add if we sort the files
		 */
		if (dvd_video) {
			start_extent += dwpnt->pad;
		}
#endif /* DVD_VIDEO */

		/* cache start extents for any linked files */
		add_hash(s_entry);
	}

	return (0);
}
#endif /* SORTING */



static BOOL
assign_file_addresses(struct directory *dpnt, BOOL isnest)
{
	struct directory *finddir;
	struct directory_entry *s_entry;
	struct file_hash *s_hash;
	struct deferred_write *dwpnt;
	char		whole_path[PATH_MAX];
#ifdef DVD_VIDEO
	char		dvd_path[PATH_MAX];
	title_set_info_t * title_set_info = NULL;
	char	*p;
#endif
	BOOL	ret = FALSE;

	while (dpnt) {
#ifdef DVD_VIDEO
		if (dvd_video && root == dpnt->parent &&
		    ((p = strstr(dpnt->whole_name, "VIDEO_TS")) != 0)&&
		    strcmp(p, "VIDEO_TS") == 0) {

			int     maxlen = strlen(dpnt->whole_name)-8;
			if (maxlen > (sizeof (dvd_path)-1))
				maxlen = sizeof (dvd_path)-1;
			strncpy(dvd_path, dpnt->whole_name, maxlen);
			dvd_path[maxlen] = '\0';

#ifdef DEBUG
			fprintf(stderr, "Found 'VIDEO_TS', the path is %s \n", dvd_path);
#endif
			title_set_info = DVDGetFileSet(dvd_path);
			if (title_set_info == 0) {
				/*
				 * Do not switch off -dvd-video but let is fail later.
				 */
/*				dvd_video = 0;*/
				errmsgno(EX_BAD, "Unable to parse DVD-Video structures.\n");
			} else {
				ret = TRUE;
			}
		}
#endif /* DVD_VIDEO */

		s_entry = dpnt->contents;
		for (s_entry = dpnt->contents; s_entry;
						s_entry = s_entry->next) {
			/*
			 * If we already have an  extent for this entry, then
			 * don't assign a new one.  It must have come from a
			 * previous session on the disc.  Note that we don't
			 * end up scheduling the thing for writing either.
			 */
			if (get_733(s_entry->isorec.extent) != 0) {
				continue;
			}
			/*
			 * This saves some space if there are symlinks present
			 */
			s_hash = find_hash(s_entry->dev, s_entry->inode);
			if (s_hash) {
				if (verbose > 2) {
					fprintf(stderr, "Cache hit for '%s%s%s'\n", s_entry->filedir->de_name,
						SPATH_SEPARATOR,
						s_entry->name);
				}
				set_733((char *) s_entry->isorec.extent,
						s_hash->starting_block);
				set_733((char *) s_entry->isorec.size,
						s_hash->size);
#ifdef SORTING
				/* check for non-directory files */
				if (do_sort && ((s_entry->isorec.flags[0] & ISO_DIRECTORY) == 0)) {
					/* make sure the real file has the highest weighting */
					s_hash->de->sort = MAX(s_entry->sort, s_hash->de->sort);
					/* flag this as a potential non-sorted file */
					s_entry->sort = NOT_SORTED;
				}
#endif /* SORTING */
				continue;
			}
			/*
			 * If this is for a directory that is not a . or
			 * a .. entry, then look up the information for the
			 * entry.  We have already assigned extents for
			 * directories, so we just need to fill in the blanks
			 * here.
			 */
			if (strcmp(s_entry->name, ".") != 0 &&
					strcmp(s_entry->name, "..") != 0 &&
					s_entry->isorec.flags[0] & ISO_DIRECTORY) {
				finddir = dpnt->subdir;
				while (1 == 1) {
					if (finddir->self == s_entry)
						break;
					finddir = finddir->next;
					if (!finddir) {
#ifdef	DVD_VIDEO
						if (title_set_info != 0) {
							DVDFreeFileSet(title_set_info);
						}
#endif
						comerrno(EX_BAD,
							"Fatal goof - could not find dir entry for '%s'\n",
							s_entry->name);
					}
				}
				set_733((char *) s_entry->isorec.extent,
						finddir->extent);
				s_entry->starting_block = finddir->extent;
				s_entry->size = ISO_ROUND_UP(finddir->size);
				total_dir_size += s_entry->size;
				add_hash(s_entry);
				set_733((char *) s_entry->isorec.size,
						ISO_ROUND_UP(finddir->size));
				continue;
			}
			/*
			 * If this is . or .., then look up the relevant info
			 * from the tables.
			 */
			if (strcmp(s_entry->name, ".") == 0) {
				set_733((char *) s_entry->isorec.extent,
								dpnt->extent);

				/*
				 * Set these so that the hash table has the
				 * correct information
				 */
				s_entry->starting_block = dpnt->extent;
				s_entry->size = ISO_ROUND_UP(dpnt->size);

				add_hash(s_entry);
				s_entry->starting_block = dpnt->extent;
				set_733((char *) s_entry->isorec.size,
						ISO_ROUND_UP(dpnt->size));
				continue;
			}
			if (strcmp(s_entry->name, "..") == 0) {
				if (dpnt == root) {
					total_dir_size += root->size;
				}
				set_733((char *) s_entry->isorec.extent,
							dpnt->parent->extent);

				/*
				 * Set these so that the hash table has the
				 * correct information
				 */
				s_entry->starting_block = dpnt->parent->extent;
				s_entry->size =
					ISO_ROUND_UP(dpnt->parent->size);

				add_hash(s_entry);
				s_entry->starting_block = dpnt->parent->extent;
				set_733((char *) s_entry->isorec.size,
					ISO_ROUND_UP(dpnt->parent->size));
				continue;
			}
			/*
			 * Some ordinary non-directory file.  Just schedule
			 * the file to be written.  This is all quite
			 * straightforward, just make a list and assign
			 * extents as we go.  Once we get through writing all
			 * of the directories, we should be ready write out
			 * these files
			 */
			if (s_entry->size) {
				dwpnt = (struct deferred_write *)
					e_malloc(sizeof (struct deferred_write));
				/* save this directory entry for later use */
				dwpnt->s_entry = s_entry;
				/* set the initial padding to zero */
				dwpnt->pad = 0;
#ifdef DVD_VIDEO
				if (dvd_video && (title_set_info != 0)) {
					int pad;

					pad = DVDGetFilePad(title_set_info, s_entry->name);
					if (pad < 0) {
						errmsgno(EX_BAD,
						"Implementation botch. Video pad for file %s is %d\n",
						s_entry->name, pad),
						comerrno(EX_BAD,
						"Either the *.IFO file is bad or you found a genisoimage bug.\n");
					}
					dwpnt->pad = pad;
					if (verbose > 0 && pad != 0) {
						fprintf(stderr,
							"The pad was %d for file %s\n", dwpnt->pad, s_entry->name);
					}
				}
#endif /* DVD_VIDEO */
#ifdef APPLE_HYB
				/*
				 * maybe an offset to start of the real
				 * file/fork
				 */
				dwpnt->off = s_entry->hfs_off;
#else
				dwpnt->off = (off_t)0;
#endif	/* APPLE_HYB */
				if (dw_tail) {
					dw_tail->next = dwpnt;
					dw_tail = dwpnt;
				} else {
					dw_head = dwpnt;
					dw_tail = dwpnt;
				}
				if (s_entry->inode == TABLE_INODE) {
					dwpnt->table = s_entry->table;
					dwpnt->name = NULL;
					sprintf(whole_path, "%s%s%s",
						s_entry->filedir->whole_name,
						SPATH_SEPARATOR, trans_tbl);
				} else {
					dwpnt->table = NULL;
					strcpy(whole_path, s_entry->whole_name);
					dwpnt->name = strdup(whole_path);
				}
				dwpnt->next = NULL;
				dwpnt->size = s_entry->size;
				dwpnt->extent = last_extent;
				set_733((char *) s_entry->isorec.extent,
								last_extent);
				s_entry->starting_block = last_extent;
				add_hash(s_entry);
				last_extent += ISO_BLOCKS(s_entry->size);
#ifdef DVD_VIDEO
				/* Shouldn't we always add the pad info? */
				if (dvd_video) {
					last_extent += dwpnt->pad;
				}
#endif /* DVD_VIDEO */
				if (verbose > 2 && !do_sort) {
					fprintf(stderr, "%8d %8u %s\n",
						s_entry->starting_block,
						last_extent - 1, whole_path);
				}
#ifdef DBG_ISO
				if (ISO_BLOCKS(s_entry->size) > 500) {
					fprintf(stderr,
						"Warning: large file '%s'\n",
						whole_path);
					fprintf(stderr,
						"Starting block is %d\n",
						s_entry->starting_block);
					fprintf(stderr,
					"Reported file size is %lld\n",
						(Llong)s_entry->size);

				}
#endif
#ifdef	NOT_NEEDED	/* Never use this code if you like to create a DVD */

				if (last_extent > (800000000 >> 11)) {
					/* More than 800Mb? Punt */
					fprintf(stderr,
					"Extent overflow processing file '%s'\n",
						whole_path);
					fprintf(stderr,
						"Starting block is %d\n",
						s_entry->starting_block);
					fprintf(stderr,
					"Reported file size is %lld\n",
							(Llong)s_entry->size);
					exit(1);
				}
#endif
				continue;
			}
			/*
			 * This is for zero-length files.  If we leave the
			 * extent 0, then we get screwed, because many readers
			 * simply drop files that have an extent of zero.
			 * Thus we leave the size 0, and just assign the
			 * extent number.
			 */
			set_733((char *) s_entry->isorec.extent, last_extent);
		}
		if (dpnt->subdir) {
			if (assign_file_addresses(dpnt->subdir, TRUE))
				ret = TRUE;
		}
		dpnt = dpnt->next;
	}
#ifdef DVD_VIDEO
	if (title_set_info != NULL) {
		DVDFreeFileSet(title_set_info);
	}
	if (dvd_video && !ret && !isnest) {
		errmsgno(EX_BAD,
			"Could not find correct 'VIDEO_TS' directory.\n");
	}
#endif /* DVD_VIDEO */
	return (ret);
} /* assign_file_addresses(... */

static void
free_one_directory(struct directory *dpnt)
{
	struct directory_entry *s_entry;
	struct directory_entry *s_entry_d;

	s_entry = dpnt->contents;
	while (s_entry) {
		s_entry_d = s_entry;
		s_entry = s_entry->next;

		if (s_entry_d->rr_attributes) {
			free(s_entry_d->rr_attributes);
			s_entry_d->rr_attributes = NULL;
		}
		if (s_entry_d->name != NULL) {
			free(s_entry_d->name);
			s_entry_d->name = NULL;
		}
		if (s_entry_d->whole_name != NULL) {
			free(s_entry_d->whole_name);
			s_entry_d->whole_name = NULL;
		}
#ifdef APPLE_HYB
		if (apple_both && s_entry_d->hfs_ent && !s_entry_d->assoc)
			free(s_entry_d->hfs_ent);
#endif	/* APPLE_HYB */

		free(s_entry_d);
		s_entry_d = NULL;
	}
	dpnt->contents = NULL;
}/* free_one_directory(... */

static void
free_directories(struct directory *dpnt)
{
	while (dpnt) {
		free_one_directory(dpnt);
		if (dpnt->subdir)
			free_directories(dpnt->subdir);
		dpnt = dpnt->next;
	}
}

void
generate_one_directory(struct directory *dpnt, FILE *outfile)
{
	unsigned int	ce_address = 0;
	char		*ce_buffer;
	unsigned int	ce_index = 0;
	unsigned int	ce_size;
	unsigned int	dir_index;
	char		*directory_buffer;
	int		new_reclen;
	struct directory_entry *s_entry;
	struct directory_entry *s_entry_d;
	unsigned int	total_size;

	total_size = ISO_ROUND_UP(dpnt->size);
	directory_buffer = (char *) e_malloc(total_size);
	memset(directory_buffer, 0, total_size);
	dir_index = 0;

	ce_size = ISO_ROUND_UP(dpnt->ce_bytes);
	ce_buffer = NULL;

	if (ce_size > 0) {
		ce_buffer = (char *) e_malloc(ce_size);
		memset(ce_buffer, 0, ce_size);

		ce_index = 0;

		/* Absolute byte address of CE entries for this directory */
		ce_address = last_extent_written + (total_size >> 11);
		ce_address = ce_address << 11;
	}
	s_entry = dpnt->contents;
	while (s_entry) {
		/* skip if it's hidden */
		if (s_entry->de_flags & INHIBIT_ISO9660_ENTRY) {
			s_entry = s_entry->next;
			continue;
		}
		/*
		 * We do not allow directory entries to cross sector
		 * boundaries. Simply pad, and then start the next entry at
		 * the next sector
		 */
		new_reclen = s_entry->isorec.length[0];
		if ((dir_index & (SECTOR_SIZE - 1)) + new_reclen >=
								SECTOR_SIZE) {
			dir_index = ISO_ROUND_UP(dir_index);
		}
		memcpy(directory_buffer + dir_index, &s_entry->isorec,
			offsetof(struct iso_directory_record, name[0]) +
			s_entry->isorec.name_len[0]);
		dir_index += offsetof(struct iso_directory_record, name[0]) +
			s_entry->isorec.name_len[0];

		/* Add the Rock Ridge attributes, if present */
		if (s_entry->rr_attr_size) {
			if (dir_index & 1) {
				directory_buffer[dir_index++] = 0;
			}
			/*
			 * If the RR attributes were too long, then write the
			 * CE records, as required.
			 */
			if (s_entry->rr_attr_size != s_entry->total_rr_attr_size) {
				struct iso_xa_dir_record *xadp;
				unsigned char	*pnt;
				int		len,
						nbytes;

				/*
				 * Go through the entire record, first skip
				 * the XA record and then fix up the
				 * CE entries so that the extent and offset
				 * are correct
				 */
				pnt = s_entry->rr_attributes;
				len = s_entry->total_rr_attr_size;

				if (len >= 14) {
					xadp = (struct iso_xa_dir_record *)pnt;

					if (xadp->signature[0] == 'X' && xadp->signature[1] == 'A' &&
									xadp->reserved[0] == '\0') {
						len -= 14;
						pnt += 14;
					}
				}

				while (len > 3) {
#ifdef DEBUG
					if (ce_size <= 0) {
						fprintf(stderr,
						"Warning: ce_index(%d) && ce_address(%d) not initialized\n",
							ce_index, ce_address);
					}
#endif

					if (pnt[0] == 'C' && pnt[1] == 'E') {
						nbytes = get_733((char *) pnt + 20);

						if ((ce_index & (SECTOR_SIZE - 1)) + nbytes >=
							SECTOR_SIZE) {
							ce_index = ISO_ROUND_UP(ce_index);
						}
						set_733((char *) pnt + 4,
							(ce_address + ce_index) >> 11);
						set_733((char *) pnt + 12,
							(ce_address + ce_index) & (SECTOR_SIZE - 1));


						/*
						 * Now store the block in the
						 * ce buffer
						 */
						memcpy(ce_buffer + ce_index,
							pnt + pnt[2], nbytes);
						ce_index += nbytes;
						if (ce_index & 1) {
							ce_index++;
						}
					}
					len -= pnt[2];
					pnt += pnt[2];
				}

			}
			rockridge_size += s_entry->total_rr_attr_size;
			memcpy(directory_buffer + dir_index,
				s_entry->rr_attributes,
				s_entry->rr_attr_size);
			dir_index += s_entry->rr_attr_size;
		}
		if (dir_index & 1) {
			directory_buffer[dir_index++] = 0;
		}
		s_entry_d = s_entry;
		s_entry = s_entry->next;

		/*
		 * Joliet doesn't use the Rock Ridge attributes, so we free
		 * it here.
		 */
		if (s_entry_d->rr_attributes) {
			free(s_entry_d->rr_attributes);
			s_entry_d->rr_attributes = NULL;
		}
	}

	if (dpnt->size != dir_index) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD,
			"Unexpected directory length %lld expected: %d '%s'\n",
			(Llong)dpnt->size,
			dir_index, dpnt->de_name);
#else
		fprintf(stderr,
			"Unexpected directory length %lld expected: %d '%s'\n",
			(Llong)dpnt->size,
			dir_index, dpnt->de_name);
#endif
	}
	jtwrite(directory_buffer, total_size, 1, 0, FALSE);
	xfwrite(directory_buffer, total_size, 1, outfile, 0, FALSE);
	last_extent_written += total_size >> 11;
	free(directory_buffer);
	directory_buffer = NULL;

	if (ce_size > 0) {
		if (ce_index != dpnt->ce_bytes) {
#ifdef	USE_LIBSCHILY
			errmsgno(EX_BAD,
			"Continuation entry record length mismatch %d expected: %d.\n",
				ce_index, dpnt->ce_bytes);
#else
			fprintf(stderr,
			"Continuation entry record length mismatch %d expected: %d.\n",
				ce_index, dpnt->ce_bytes);
#endif
		}
		jtwrite(ce_buffer, ce_size, 1, 0, FALSE);
		xfwrite(ce_buffer, ce_size, 1, outfile, 0, FALSE);
		last_extent_written += ce_size >> 11;
		free(ce_buffer);
		ce_buffer = NULL;
	}
}/* generate_one_directory(... */

static void
build_pathlist(struct directory *node)
{
	struct directory *dpnt;

	dpnt = node;

	while (dpnt) {
		/* skip if it's hidden */
		if ((dpnt->dir_flags & INHIBIT_ISO9660_ENTRY) == 0)
			pathlist[dpnt->path_index] = dpnt;

		if (dpnt->subdir)
			build_pathlist(dpnt->subdir);
		dpnt = dpnt->next;
	}
}/* build_pathlist(... */

static int
compare_paths(void const *r, void const *l)
{
	struct directory const *ll = *(struct directory * const *) l;
	struct directory const *rr = *(struct directory * const *) r;

	if (rr->parent->path_index < ll->parent->path_index) {
		return (-1);
	}
	if (rr->parent->path_index > ll->parent->path_index) {
		return (1);
	}
	return (strcmp(rr->self->isorec.name, ll->self->isorec.name));

}/* compare_paths(... */

static int
generate_path_tables()
{
	struct directory_entry *de = NULL;
	struct directory *dpnt;
	int		fix;
	int		i;
	int		j;
	int		namelen;
	char		*npnt;
	char		*npnt1;
	int		tablesize;

	/* First allocate memory for the tables and initialize the memory */
	tablesize = path_blocks << 11;
	path_table_m = (char *) e_malloc(tablesize);
	path_table_l = (char *) e_malloc(tablesize);
	memset(path_table_l, 0, tablesize);
	memset(path_table_m, 0, tablesize);

	/*
	 * Now start filling in the path tables.  Start with root directory
	 */

	path_table_index = 0;
	pathlist = (struct directory **) e_malloc(sizeof (struct directory *)
		* next_path_index);
	memset(pathlist, 0, sizeof (struct directory *) * next_path_index);
	build_pathlist(root);

	do {
		fix = 0;
#ifdef PROTOTYPES
		qsort(&pathlist[1], next_path_index - 1,
			sizeof (struct directory *),
			(int (*) (const void *, const void *)) compare_paths);
#else
		qsort(&pathlist[1], next_path_index - 1,
			sizeof (struct directory *),
			compare_paths);
#endif

		for (j = 1; j < next_path_index; j++) {
			if (pathlist[j]->path_index != j) {
				pathlist[j]->path_index = j;
				fix++;
			}
		}
	} while (fix);

	for (j = 1; j < next_path_index; j++) {
		dpnt = pathlist[j];
		if (!dpnt) {
#ifdef	USE_LIBSCHILY
			comerrno(EX_BAD, "Entry %d not in path tables\n", j);
#else
			fprintf(stderr, "Entry %d not in path tables\n", j);
			exit(1);
#endif
		}
		npnt = dpnt->de_name;

		/* So the root comes out OK */
		if ((*npnt == 0) || (dpnt == root)) {
			npnt = ".";
		}
		npnt1 = strrchr(npnt, PATH_SEPARATOR);
		if (npnt1) {
			npnt = npnt1 + 1;
		}
		de = dpnt->self;
		if (!de) {
#ifdef	USE_LIBSCHILY
			comerrno(EX_BAD,
			"Fatal ISO9660 goof - directory has amnesia\n");
#else
			fprintf(stderr,
			"Fatal ISO9660 goof - directory has amnesia\n");
			exit(1);
#endif
		}
		namelen = de->isorec.name_len[0];

		path_table_l[path_table_index] = namelen;
		path_table_m[path_table_index] = namelen;
		path_table_index += 2;

		set_731(path_table_l + path_table_index, dpnt->extent);
		set_732(path_table_m + path_table_index, dpnt->extent);
		path_table_index += 4;

		if (dpnt->parent->path_index > 0xffff) {
#ifdef	USE_LIBSCHILY
			comerrno(EX_BAD,
			"Unable to generate sane path tables - too many directories (%d)\n",
				dpnt->parent->path_index);
#else
			fprintf(stderr,
			"Unable to generate sane path tables - too many directories (%d)\n",
				dpnt->parent->path_index);
			exit(1);
#endif
		}

		set_721(path_table_l + path_table_index,
			dpnt->parent->path_index);
		set_722(path_table_m + path_table_index,
			dpnt->parent->path_index);
		path_table_index += 2;

		for (i = 0; i < namelen; i++) {
			path_table_l[path_table_index] = de->isorec.name[i];
			path_table_m[path_table_index] = de->isorec.name[i];
			path_table_index++;
		}
		if (path_table_index & 1) {
			path_table_index++;	/* For odd lengths we pad */
		}
	}

	free(pathlist);
	pathlist = NULL;
	if (path_table_index != path_table_size) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD,
			"Path table lengths do not match %d expected: %d\n",
			path_table_index,
			path_table_size);
#else
		fprintf(stderr,
			"Path table lengths do not match %d expected: %d\n",
			path_table_index,
			path_table_size);
#endif
	}
	return (0);
}/* generate_path_tables(... */

void
memcpy_max(char *to, char *from, int max)
{
	int	n = strlen(from);

	if (n > max) {
		n = max;
	}
	memcpy(to, from, n);

}/* memcpy_max(... */

void
outputlist_insert(struct output_fragment *frag)
{
	struct output_fragment *nfrag;

	nfrag = e_malloc(sizeof (*frag));
	movebytes(frag, nfrag, sizeof (*frag));
	nfrag->of_start_extent = 0;

	if (out_tail == NULL) {
		out_list = out_tail = nfrag;
	} else {
		out_tail->of_next = nfrag;
		out_tail = nfrag;
	}
}

static int
file_write(FILE *outfile)
{
	Uint	should_write;

#ifdef APPLE_HYB
	char	buffer[SECTOR_SIZE];

	memset(buffer, 0, sizeof (buffer));

	if (apple_hyb) {

		int	i;

		/*
		 * write out padding to round up to HFS allocation block
		 */
		for (i = 0; i < hfs_pad; i++) {
			jtwrite(buffer, sizeof (buffer), 1, 0, FALSE);
			xfwrite(buffer, sizeof (buffer), 1, outfile, 0, FALSE);
			last_extent_written++;
		}
	}
#endif	/* APPLE_HYB */

	/*
	 * OK, all done with that crap.  Now write out the directories. This is
	 * where the fur starts to fly, because we need to keep track of each
	 * file as we find it and keep track of where we put it.
	 */
	should_write = last_extent - session_start;

	if (verbose > 2) {
#ifdef DBG_ISO
		fprintf(stderr,
			"Total directory extents being written = %d\n",
							last_extent);
#endif

#ifdef APPLE_HYB
		if (apple_hyb)
			fprintf(stderr,
			"Total extents scheduled to be written (inc HFS) = %d\n",
				last_extent - session_start);
		else
#endif	/* APPLE_HYB */

			fprintf(stderr,
				"Total extents scheduled to be written = %u\n",
				last_extent - session_start);
	}
	/* Now write all of the files that we need. */
	write_files(outfile);

#ifdef APPLE_HYB
	/* write out extents/catalog/dt file */
	if (apple_hyb) {

		jtwrite(hce->hfs_ce, HFS_BLOCKSZ, hce->hfs_tot_size, 0, FALSE);
		xfwrite(hce->hfs_ce, HFS_BLOCKSZ, hce->hfs_tot_size, outfile, 0, FALSE);

		/* round up to a whole CD block */
		if (HFS_ROUND_UP(hce->hfs_tot_size) -
					hce->hfs_tot_size * HFS_BLOCKSZ) {
			jtwrite(buffer,
				HFS_ROUND_UP(hce->hfs_tot_size) -
				hce->hfs_tot_size * HFS_BLOCKSZ, 1, 0, FALSE);
			xfwrite(buffer,
				HFS_ROUND_UP(hce->hfs_tot_size) -
				hce->hfs_tot_size * HFS_BLOCKSZ, 1, outfile, 0, FALSE);
		}
		last_extent_written += ISO_ROUND_UP(hce->hfs_tot_size *
						HFS_BLOCKSZ) / SECTOR_SIZE;

		/* write out HFS boot block */
		if (mac_boot.name)
			write_one_file(mac_boot.name, mac_boot.size, outfile,
								mac_boot.off);
	}
#endif	/* APPLE_HYB */

	/* The rest is just fluff. */
	if (verbose == 0) {
		return (0);
	}
#ifdef APPLE_HYB
	if (apple_hyb) {
		fprintf(stderr,
			"Total extents actually written (inc HFS) = %d\n",
			last_extent_written - session_start);
		fprintf(stderr, "(Size of ISO volume = %d, HFS extra = %d)\n",
			last_extent_written - session_start - hfs_extra,
			hfs_extra);
	} else
#else
	fprintf(stderr, "Total extents actually written = %d\n",
		last_extent_written - session_start);
#endif	/* APPLE_HYB */

	/* Hard links throw us off here */
	if (should_write != (last_extent - session_start)) {
		fprintf(stderr,
		"Number of extents written not what was predicted.  Please fix.\n");
		fprintf(stderr, "Predicted = %d, written = %d\n",
						should_write, last_extent);
	}
	fprintf(stderr, "Total translation table size: %d\n", table_size);
	fprintf(stderr, "Total rockridge attributes bytes: %d\n",
						rockridge_size);
	fprintf(stderr, "Total directory bytes: %d\n", total_dir_size);
	fprintf(stderr, "Path table size(bytes): %d\n", path_table_size);

#ifdef DEBUG
	fprintf(stderr,
		"next extent, last_extent, last_extent_written %d %d %d\n",
		next_extent, last_extent, last_extent_written);
#endif

	return (0);

}/* iso_write(... */

/*
 * Function to write the PVD for the disc.
 */
static int
pvd_write(FILE *outfile)
{
	char		iso_time[17];
	int		should_write;
	struct tm	local;
	struct tm	gmt;


	time(&begun);

	local = *localtime(&begun);
	gmt = *gmtime(&begun);

	/*
	 * There was a comment here about breaking in the year 2000.
	 * That's not true, in 2000 tm_year == 100, so 1900+tm_year == 2000.
	 */
	sprintf(iso_time, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d00",
		1900 + local.tm_year,
		local.tm_mon + 1, local.tm_mday,
		local.tm_hour, local.tm_min, local.tm_sec);

	local.tm_min -= gmt.tm_min;
	local.tm_hour -= gmt.tm_hour;
	local.tm_yday -= gmt.tm_yday;
	if (local.tm_yday < -2)		/* Hit new-year limit	*/
		local.tm_yday = 1;	/* Local is GMT + 1 day	*/
	iso_time[16] = (local.tm_min + 60 *
				(local.tm_hour + 24 * local.tm_yday)) / 15;

	/* Next we write out the primary descriptor for the disc */
	memset(&vol_desc, 0, sizeof (vol_desc));
	vol_desc.type[0] = ISO_VD_PRIMARY;
	memcpy(vol_desc.id, ISO_STANDARD_ID, sizeof (ISO_STANDARD_ID));
	vol_desc.version[0] = 1;

	memset(vol_desc.system_id, ' ', sizeof (vol_desc.system_id));
	memcpy_max(vol_desc.system_id, system_id, strlen(system_id));

	memset(vol_desc.volume_id, ' ', sizeof (vol_desc.volume_id));
	memcpy_max(vol_desc.volume_id, volume_id, strlen(volume_id));

	should_write = last_extent - session_start;
	set_733((char *) vol_desc.volume_space_size, should_write);
	set_723(vol_desc.volume_set_size, volume_set_size);
	set_723(vol_desc.volume_sequence_number, volume_sequence_number);
	set_723(vol_desc.logical_block_size, SECTOR_SIZE);

	/*
	 * The path tables are used by DOS based machines to cache directory
	 * locations
	 */
	set_733((char *) vol_desc.path_table_size, path_table_size);
	set_731(vol_desc.type_l_path_table, path_table[0]);
	set_731(vol_desc.opt_type_l_path_table, path_table[1]);
	set_732(vol_desc.type_m_path_table, path_table[2]);
	set_732(vol_desc.opt_type_m_path_table, path_table[3]);

	/* Now we copy the actual root directory record */
	memcpy(vol_desc.root_directory_record, &root_record,
		offsetof(struct iso_directory_record, name[0]) + 1);

	/*
	 * The rest is just fluff.  It looks nice to fill in many of these
	 * fields, though.
	 */
	FILL_SPACE(volume_set_id);
	if (volset_id)
		memcpy_max(vol_desc.volume_set_id, volset_id, strlen(volset_id));

	FILL_SPACE(publisher_id);
	if (publisher)
		memcpy_max(vol_desc.publisher_id, publisher, strlen(publisher));

	FILL_SPACE(preparer_id);
	if (preparer)
		memcpy_max(vol_desc.preparer_id, preparer, strlen(preparer));

	FILL_SPACE(application_id);
	if (appid)
		memcpy_max(vol_desc.application_id, appid, strlen(appid));

	FILL_SPACE(copyright_file_id);
	if (copyright)
		memcpy_max(vol_desc.copyright_file_id, copyright,
			strlen(copyright));

	FILL_SPACE(abstract_file_id);
	if (abstract)
		memcpy_max(vol_desc.abstract_file_id, abstract,
			strlen(abstract));

	FILL_SPACE(bibliographic_file_id);
	if (biblio)
		memcpy_max(vol_desc.bibliographic_file_id, biblio,
			strlen(biblio));

	FILL_SPACE(creation_date);
	FILL_SPACE(modification_date);
	FILL_SPACE(expiration_date);
	FILL_SPACE(effective_date);
	vol_desc.file_structure_version[0] = 1;
	FILL_SPACE(application_data);

	memcpy(vol_desc.creation_date, iso_time, 17);
	memcpy(vol_desc.modification_date, iso_time, 17);
	memcpy(vol_desc.expiration_date, "0000000000000000", 17);
	memcpy(vol_desc.effective_date, iso_time, 17);

	if (use_XA) {
		char	*xap = &((char *)&vol_desc)[1024];

		memcpy(&xap[0], "CD-XA001", 8);			/* XA Sign.  */
		memcpy(&xap[8], "\0\0", 2);			/* XA flags  */
		memcpy(&xap[10], "\0\0\0\0\0\0\0\0", 8);	/* Start dir */
		memcpy(&xap[18], "\0\0\0\0\0\0\0\0", 8);	/* Reserved  */
	}

	/* if not a bootable cd do it the old way */
	jtwrite(&vol_desc, SECTOR_SIZE, 1, 0, FALSE);
	xfwrite(&vol_desc, SECTOR_SIZE, 1, outfile, 0, FALSE);
	last_extent_written++;
	return (0);
}

/*
 * Function to write the Extended PVD for the disc.
 */
static int
xpvd_write(FILE *outfile)
{
	vol_desc.type[0] = ISO_VD_SUPPLEMENTARY;
	vol_desc.version[0] = 2;
	vol_desc.file_structure_version[0] = 2;

	/* if not a bootable cd do it the old way */
	jtwrite(&vol_desc, SECTOR_SIZE, 1, 0, FALSE);
	xfwrite(&vol_desc, SECTOR_SIZE, 1, outfile, 0, FALSE);
	last_extent_written++;
	return (0);
}

/*
 * Function to write the EVD for the disc.
 */
static int
evd_write(FILE *outfile)
{
	struct iso_primary_descriptor evol_desc;

	/*
	 * Now write the end volume descriptor.  Much simpler than the other
	 * one
	 */
	memset(&evol_desc, 0, sizeof (evol_desc));
	evol_desc.type[0] = (unsigned char) ISO_VD_END;
	memcpy(evol_desc.id, ISO_STANDARD_ID, sizeof (ISO_STANDARD_ID));
	evol_desc.version[0] = 1;
	jtwrite(&evol_desc, SECTOR_SIZE, 1, 0, TRUE);
	xfwrite(&evol_desc, SECTOR_SIZE, 1, outfile, 0, TRUE);
	last_extent_written += 1;
	return (0);
}

/*
 * Function to write the version information for the disc.
 */
static int
vers_write(FILE *outfile)
{
	char		vers[SECTOR_SIZE+1];
	int		X_ac;
	char		**X_av;
	char		*cp;
	int		i;
	int		idx = 4;
	int		len;
	extern char	version_string[];
	extern int	path_ind;

	/* Now write the version descriptor. */
	memset(vers, 0, sizeof (vers));
	strcpy(vers, "MKI ");

	cp = vers;
	X_ac = saved_ac();
	X_av = saved_av();
	strcpy(&cp[idx], ctime(&begun));
	idx += 25;
	strcpy(&cp[idx], version_string);
	idx += strlen(version_string);
	for (i = 1; i < X_ac; i++) {
		len = strlen(X_av[i]);
		if ((idx + len + 2) >= SECTOR_SIZE)
			break;
		cp[idx++] = ' ';
		/*
		 * Do not give away secret information when not in debug mode.
		 */
		if (debug)
			strcpy(&cp[idx], X_av[i]);
		else if (i >= path_ind)
			len = graftcp(&cp[idx], X_av[i], &vers[SECTOR_SIZE-1]);
		else if (X_av[i][0] == '/')
			len = pathcp(&cp[idx], X_av[i], &vers[SECTOR_SIZE-1]);
		else
			strcpy(&cp[idx], X_av[i]);
		idx += len;
	}

	cp[SECTOR_SIZE - 1] = '\0';
 	/* Per default: keep privacy. Blackout the version and arguments. */
	if(getenv("ISODEBUG")) {
		jtwrite(vers, SECTOR_SIZE, 1, 0, TRUE);
		xfwrite(vers, SECTOR_SIZE, 1, outfile, 0, TRUE);
	} else {
		jtwrite(calloc(SECTOR_SIZE, 1), SECTOR_SIZE, 1, 0, TRUE);
		xfwrite(calloc(SECTOR_SIZE, 1), SECTOR_SIZE, 1, outfile, 0, TRUE);
	}
    last_extent_written += 1;
	return (0);
}

/*
 * Avoid to write unwanted information into the version info string.
 */
static int
graftcp(char *to, char *from, char *ep)
{
	int	len = strlen(from);
	char	*node = NULL;

	if (use_graft_ptrs)
		node = findgequal(from);

	if (node == NULL) {
		len = 0;
		node = from;
	} else {
		len = node - from;
		*node = '\0';
		strncpy(to, from, ep - to);
		*node++ = '=';
		to += len++;
		*to++ = '=';
	}
	return (len + pathcp(to, node, ep));
}

static int
pathcp(char *to, char *from, char *ep)
{
	int	len = strlen(from);
	char	*p;

	p = strrchr(from, '/');
	if (p == NULL) {
		strncpy(to, from, ep - to);
	} else {
		if (p[1] == '\0') {
			--p;
			while (p > from && *p != '/')
				--p;
		}
		len = 0;
		if (*p == '/') {
			strncpy(to, "...", ep - to);
			to += 3;
			len = 3;
		}
		if (to < ep) {
			strncpy(to, p, ep - to);
			len += strlen(to);
		}
	}
	return (len);
}


/*
 * Function to write the path table for the disc.
 */
static int
pathtab_write(FILE *outfile)
{
	/* Next we write the path tables */
	jtwrite(path_table_l, path_blocks << 11, 1, 0, FALSE);
	xfwrite(path_table_l, path_blocks << 11, 1, outfile, 0, FALSE);
	last_extent_written += path_blocks;
	jtwrite(path_table_m, path_blocks << 11, 1, 0, FALSE);
	xfwrite(path_table_m, path_blocks << 11, 1, outfile, 0, FALSE);
	last_extent_written += path_blocks;
	free(path_table_l);
	free(path_table_m);
	path_table_l = NULL;
	path_table_m = NULL;
	return (0);
}

static int
exten_write(FILE *outfile)
{
	jtwrite(extension_record, SECTOR_SIZE, 1, 0, FALSE);
	xfwrite(extension_record, SECTOR_SIZE, 1, outfile, 0, FALSE);
	last_extent_written++;
	return (0);
}

/*
 * Functions to describe padding block at the start of the disc.
 */
int
oneblock_size(int starting_extent)
{
	last_extent++;
	return (0);
}

/*
 * Functions to describe path table size.
 */
static int
pathtab_size(int starting_extent)
{
	path_table[0] = starting_extent;

	path_table[1] = 0;
	path_table[2] = path_table[0] + path_blocks;
	path_table[3] = 0;
	last_extent += 2 * path_blocks;
	return (0);
}

/*
 * Functions to describe padding blocks before PVD.
 */
static int
startpad_size(int starting_extent)
{
	last_extent = session_start + 16;
	return (0);
}

/*
 * Functions to describe padding blocks between sections.
 */
static int
interpad_size(int starting_extent)
{
	int	emod = 0;

#ifdef	needed
	starting_extent += 16;	/* First add 16 pad blocks */
#endif
	if ((emod = starting_extent % 16) != 0) {
		starting_extent += 16 - emod;	/* Now pad to mod 16 #	   */
	}
	last_extent = starting_extent;
	return (0);
}

/*
 * Functions to describe padding blocks at end of disk.
 */
static int
endpad_size(int starting_extent)
{
	starting_extent += 150;			/* 150 pad blocks (post gap) */
	last_extent = starting_extent;
	return (0);
}

static int
file_gen()
{
#ifdef APPLE_HYB
	int	start_extent = last_extent;	/* orig ISO files start */

#endif	/* APPLE_HYB */

	if (!assign_file_addresses(root, FALSE)) {
#ifdef DVD_VIDEO
		if (dvd_video) {
			comerrno(EX_BAD, "Unable to make a DVD-Video image.\n"
            "Possible reasons:\n"
            "  - VIDEO_TS subdirectory was not found on specified location\n"
            "  - VIDEO_TS has invalid contents\n"
            );
		}
#else
		;	/* EMPTY */
#endif
	}


#ifdef SORTING
	if (do_sort) {
		if (sort_file_addresses() == 0)
			reassign_link_addresses(root);
	}
#endif /* SORTING */

#ifdef APPLE_HYB
	/*
	 * put this here for the time being - may when I've worked out how to
	 * use Eric's new system for creating/writing parts of the image it
	 * may move to it's own routine
	 */
	if (apple_hyb)
		hfs_file_gen(start_extent);
#ifdef PREP_BOOT
	else if (use_prep_boot || use_chrp_boot)
		gen_prepboot();
#endif	/* PREP_BOOT */
#endif	/* APPLE_HYB */

	return (0);
}

static int
dirtree_dump()
{
	if (verbose > 2) {
		dump_tree(root);
	}
	return (0);
}

static int
dirtree_fixup(int starting_extent)
{
	if (use_RockRidge && reloc_dir)
		finish_cl_pl_entries();

	if (use_RockRidge)
		update_nlink_field(root);
	return (0);
}

static int
dirtree_size(int starting_extent)
{
	assign_directory_addresses(root);
	return (0);
}

static int
ext_size(int starting_extent)
{
	extern int		extension_record_size;
	struct directory_entry *s_entry;

	extension_record_extent = starting_extent;
	s_entry = root->contents;
	set_733((char *) s_entry->rr_attributes + s_entry->rr_attr_size - 24,
		extension_record_extent);
	set_733((char *) s_entry->rr_attributes + s_entry->rr_attr_size - 8,
		extension_record_size);
	last_extent++;
	return (0);
}

static int
dirtree_write(FILE *outfile)
{
	generate_iso9660_directories(root, outfile);
	return (0);
}

static int
dirtree_cleanup(FILE *outfile)
{
	free_directories(root);
	return (0);
}

static int
startpad_write(FILE *outfile)
{
	char	buffer[SECTOR_SIZE];
	int	i;
	int	npad;

	memset(buffer, 0, sizeof (buffer));

	npad = session_start + 16 - last_extent_written;

	for (i = 0; i < npad; i++) {
		jtwrite(buffer, sizeof (buffer), 1, 0, FALSE);
		xfwrite(buffer, sizeof (buffer), 1, outfile, 0, FALSE);
		last_extent_written++;
	}

	return (0);
}

static int
interpad_write(FILE *outfile)
{
	char	buffer[SECTOR_SIZE];
	int	i;
	int	npad = 0;

	memset(buffer, 0, sizeof (buffer));

#ifdef	needed
	npad = 16;
#endif
	if ((i = last_extent_written % 16) != 0)
		npad += 16 - i;

	for (i = 0; i < npad; i++) {
		jtwrite(buffer, sizeof (buffer), 1, 0, FALSE);
		xfwrite(buffer, sizeof (buffer), 1, outfile, 0, FALSE);
		last_extent_written++;
	}

	return (0);
}

static int
endpad_write(FILE *outfile)
{
	char	buffer[SECTOR_SIZE];
	int	i;

	memset(buffer, 0, sizeof (buffer));

	for (i = 0; i < 150; i++) {
		jtwrite(buffer, sizeof (buffer), 1, 0, FALSE);
		xfwrite(buffer, sizeof (buffer), 1, outfile, 0, FALSE);
		last_extent_written++;
	}

	return (0);
}

#ifdef APPLE_HYB

/*
 *	hfs_get_parms:	get HFS parameters from the command line
 */

static int
hfs_get_parms(char *key)
{
	int	ret = 0;
	char	*p;

	if (hfs_parms == NULL)
		return (ret);

	if ((p = strstr(hfs_parms, key)) != NULL) {
		p += strlen(key) + 1;
		sscanf(p, "%d", &ret);
	}

	return (ret);
}

/*
 *	hfs_file_gen:	set up "fake" HFS volume using the ISO9660 tree
 */
static void
hfs_file_gen(int start_extent)
{
	int	Csize;	/* clump size for HFS vol */
	int	loop;
	int	last_extent_save = last_extent;
	char	*p;

	/* allocate memory for the libhfs/genisoimage extra info */
	hce = (hce_mem *) e_malloc(sizeof (hce_mem));

	hce->error = (char *) e_malloc(1024);

	/* mark as unallocated for use later */
	hce->hfs_ce = hce->hfs_hdr = hce->hfs_map = 0;

	/* reserve space for the label partition - if it is needed */
#ifdef PREP_BOOT
	/* a PReP bootable partition needs the map.. */
	if (gen_pt || use_prep_boot || use_chrp_boot)
#else
	if (gen_pt)
#endif	/* PREP_BOOT */
		hce->hfs_map_size = HFS_MAP_SIZE;
	else
		hce->hfs_map_size = 0;

	/* set the HFS parameter string to upper case */
	if (hfs_parms) {
		for (p = hfs_parms; *p; p++)
			*p = toupper(*p);
	}

	/* set the initial factor to increase Catalog file size */
	if ((hce->ctc_size = hfs_get_parms("CTC")) == 0)
		hce->ctc_size = CTC;

	/* set the max size of the Catalog file */
	if ((hce->max_XTCsize = hfs_get_parms("MAX_XTCSIZE")) == 0)
		hce->max_XTCsize = MAX_XTCSIZE;

	/* set the number of time to try to make an HFS volume */
	if ((loop = hfs_get_parms("CTC_LOOP")) == 0)
		loop = CTC_LOOP;

	/*
	 * "create" the HFS volume (just the header, catalog/extents files) if
	 * there's a problem with the Catalog file being too small, we keep on
	 * increasing the size (up to CTC_LOOP) times and try again.
	 * Unfortunately I don't know enough about the inner workings of HFS,
	 * so I can't workout the size of the Catalog file in advance (and I
	 * don't want to "grow" as is is normally allowed to), therefore, this
	 * approach is a bit over the top as it involves throwing away the
	 * "volume" we have created and trying again ...
	 */
	do {
		hce->error[0] = '\0';

		/* attempt to create the Mac volume */
		Csize = make_mac_volume(root, start_extent);

		/* if we have a problem ... */
		if (Csize < 0) {
			/*
			 * we've made too many attempts, or got some other
			 * error
			 */
			if (loop == 0 || errno != HCE_ERROR) {
				/* HCE_ERROR is not a valid errno value */
				if (errno == HCE_ERROR)
					errno = 0;

				/* exit with the error */
				if (*hce->error)
					fprintf(stderr, "%s\n", hce->error);
				perr(hfs_error);
			} else {
				/* increase Catalog file size factor */
				hce->ctc_size *= CTC;

				/*
				 * reset the initial "last_extent" and try
				 * again
				 */
				last_extent = last_extent_save;
			}
		} else {
			/* everything OK - just carry on ... */
			loop = 0;
		}
	}
	while (loop--);

	hfs_extra = HFS_ROUND_UP(hce->hfs_tot_size) / SECTOR_SIZE;

	last_extent += hfs_extra;

	/* generate the Mac label and HFS partition maps */
	mac_boot.name = hfs_boot_file;

	/*
	 * only generate the partition tables etc. if we are making a bootable
	 * CD - or if the -part option is given
	 */
	if (gen_pt) {
		if (gen_mac_label(&mac_boot)) {
			if (*hce->error)
				fprintf(stderr, "%s\n", hce->error);
			perr(hfs_error);
		}
	}
	/* set Autostart filename if required */
	if (autoname) {
		if (autostart())
			perr("Autostart filename must less than 12 characters");
	}
	/* finished with any HFS type errors */
	free(hce->error);
	hce->error = 0;

	/*
	 * the ISO files need to start on a multiple of the HFS allocation
	 * blocks, so find out how much padding we need
	 */

	/*
	 * take in accout alignment of files wrt HFS volume start - remove any
	 * previous session as well
	 */
	start_extent -= session_start;
	hfs_pad = ROUND_UP(start_extent*SECTOR_SIZE +
			(hce->hfs_hdr_size + hce->hfs_map_size) * HFS_BLOCKSZ,
							Csize) / SECTOR_SIZE;

	hfs_pad -= (start_extent + (hce->hfs_hdr_size + hce->hfs_map_size) /
							HFS_BLK_CONV);

#ifdef PREP_BOOT
	gen_prepboot_label(hce->hfs_map);
#endif	/* PREP_BOOT */

}

#ifdef PREP_BOOT
static void
gen_prepboot()
{
	/*
	 * we need to allocate the hce struct since hce->hfs_map is used to
	 * generate the fdisk partition map required for PReP booting
	 */
	hce = (hce_mem *) e_malloc(sizeof (hce_mem));

	/* mark as unallocated for use later */
	hce->hfs_ce = hce->hfs_hdr = hce->hfs_map = 0;

	/* reserve space for the label partition - if it is needed */
	hce->hfs_map_size = HFS_MAP_SIZE;

	hce->hfs_map = (unsigned char *) e_malloc(hce->hfs_map_size * HFS_BLOCKSZ);
	gen_prepboot_label(hce->hfs_map);
}

#endif	/* PREP_BOOT */

/*
 *	get_adj_size:	get the ajusted size of the volume with the HFS
 *			allocation block size for each file
 */
Ulong
get_adj_size(int Csize)
{
	struct deferred_write *dw;
	Ulong		size = 0;
	int		count = 0;

	/* loop through all the files finding the new total size */
	for (dw = dw_head; dw; dw = dw->next) {
		size += (ROUND_UP(dw->size, Csize)/HFS_BLOCKSZ);
		count++;
	}

	/*
	 * crude attempt to prevent overflows - HFS can only cope with a
	 * maximum of about 65536 forks (actually less) - this will trap cases
	 * when we have far too many files
	 */

	if (count >= 65536)
		return (-1);
	else
		return (size);
}

/*
 *	adj_size:	adjust the ISO record entries for all files
 *			based on the HFS allocation block size
 */
int
adj_size(int Csize, int start_extent, int extra)
{
	struct deferred_write *dw;
	struct directory_entry *s_entry;
	int		size;

	/* get the adjusted start_extent (with padding) */
	/* take in accout alignment of files wrt HFS volume start */

	start_extent -= session_start;

	start_extent = ROUND_UP(start_extent*SECTOR_SIZE + extra*HFS_BLOCKSZ,
						Csize) / SECTOR_SIZE;

	start_extent -= (extra / HFS_BLK_CONV);

	start_extent += session_start;

	/* initialise file hash */
	flush_hash();

	/*
	 * loop through all files changing their starting blocks and finding
	 * any padding needed to written out latter
	 */
	for (dw = dw_head; dw; dw = dw->next) {
		s_entry = dw->s_entry;
		s_entry->starting_block = dw->extent = start_extent;
		set_733((char *) s_entry->isorec.extent, start_extent);
		size = ROUND_UP(dw->size, Csize) / SECTOR_SIZE;
		dw->pad = size - ISO_ROUND_UP(dw->size) / SECTOR_SIZE;

		/*
		 * cache non-HFS files - as there may be multiple links to
		 * these files (HFS files can't have multiple links). We will
		 * need to change the starting extent of the other links later
		 */
		if (!s_entry->hfs_ent)
			add_hash(s_entry);

		start_extent += size;
	}

	return (start_extent);
}

/*
 *	adj_size_other:	adjust any non-HFS files that may be linked
 *			to an existing file (i.e. not have a deferred_write
 *			entry of it's own
 */
void
adj_size_other(struct directory *dpnt)
{
	struct directory_entry *s_entry;
	struct file_hash *s_hash;

	while (dpnt) {
		s_entry = dpnt->contents;
		for (s_entry = dpnt->contents; s_entry;
						s_entry = s_entry->next) {
			/*
			 * if it's an HFS file or a directory - then ignore
			 * (we're after non-HFS files)
			 */
			if (s_entry->hfs_ent ||
			    (s_entry->isorec.flags[0] & ISO_DIRECTORY))
				continue;

			/*
			 * find any cached entry and assign new starting
			 * extent
			 */
			s_hash = find_hash(s_entry->dev, s_entry->inode);
			if (s_hash) {
				set_733((char *) s_entry->isorec.extent,
						s_hash->starting_block);
				/* not vital - but tidy */
				s_entry->starting_block =
							s_hash->starting_block;
			}
		}
		if (dpnt->subdir) {
			adj_size_other(dpnt->subdir);
		}
		dpnt = dpnt->next;
	}

	/* clear file hash */
	flush_hash();
}

/*
 *	hfs_hce_write:	write out the HFS header stuff
 */
static int
hfs_hce_write(FILE *outfile)
{
	char	buffer[SECTOR_SIZE];
	int	n = 0;
	int	r;	/* HFS hdr output */
	int	tot_size = hce->hfs_map_size + hce->hfs_hdr_size;

	memset(buffer, 0, sizeof (buffer));

	/*
	 * hack time ... if the tot_size is greater than 32Kb then
	 * it won't fit in the first 16 blank SECTORS (64 512 byte
	 * blocks, as most of this is padding, we just truncate this
	 * data to 64x4xHFS_BLOCKSZ ... hope this is OK ...
	 */

	if (tot_size > 64) tot_size = 64;

	/* get size in CD blocks == 4xHFS_BLOCKSZ == 2048 */
	n = tot_size / HFS_BLK_CONV;
	r = tot_size % HFS_BLK_CONV;

	/* write out HFS volume header info */
	jtwrite(hce->hfs_map, HFS_BLOCKSZ, tot_size, 0, FALSE);
	xfwrite(hce->hfs_map, HFS_BLOCKSZ, tot_size, outfile, 0, FALSE);

	/* fill up to a complete CD block */
	if (r) {
		jtwrite(buffer, HFS_BLOCKSZ, HFS_BLK_CONV - r, 0, FALSE);
		xfwrite(buffer, HFS_BLOCKSZ, HFS_BLK_CONV - r, outfile, 0, FALSE);
		n++;
	}
	last_extent_written += n;
	return (0);
}

/*
 *	insert_padding_file : insert a dumy file to make volume at least
 *				800k
 *
 *	XXX If we ever need to write more then 2 GB, make size off_t
 */
int
insert_padding_file(int size)
{
	struct deferred_write *dwpnt;

	/* get the size in bytes */
	size *= HFS_BLOCKSZ;

	dwpnt = (struct deferred_write *)
		e_malloc(sizeof (struct deferred_write));
	dwpnt->s_entry = 0;
	/* set the padding to zero */
	dwpnt->pad = 0;
	/* set offset to zero */
	dwpnt->off = (off_t)0;

	/*
	 * don't need to wory about the s_entry stuff as it won't be touched#
	 * at this point onwards
	 */

	/* insert the entry in the list */
	if (dw_tail) {
		dw_tail->next = dwpnt;
		dw_tail = dwpnt;
	} else {
		dw_head = dwpnt;
		dw_tail = dwpnt;
	}

	/* aloocate memory as a "Table" file */
	dwpnt->table = e_malloc(size);
	dwpnt->name = NULL;

	dwpnt->next = NULL;
	dwpnt->size = size;
	dwpnt->extent = last_extent;
	last_extent += ISO_BLOCKS(size);

	/* retune the size in HFS blocks */
	return (ISO_ROUND_UP(size) / HFS_BLOCKSZ);
}

struct output_fragment hfs_desc		= {NULL, NULL, NULL, hfs_hce_write, "HFS volume header"};

#endif	/* APPLE_HYB */

struct output_fragment startpad_desc	= {NULL, startpad_size,	NULL,	  startpad_write, "Initial Padblock"};
struct output_fragment voldesc_desc	= {NULL, oneblock_size,	root_gen, pvd_write,	  "Primary Volume Descriptor"};
struct output_fragment xvoldesc_desc	= {NULL, oneblock_size,	NULL,	  xpvd_write,	  "Enhanced Volume Descriptor"};
struct output_fragment end_vol		= {NULL, oneblock_size,	NULL,	  evd_write,	  "End Volume Descriptor" };
struct output_fragment version_desc	= {NULL, oneblock_size,	NULL,	  vers_write,	  "Version block" };
struct output_fragment pathtable_desc	= {NULL, pathtab_size,	generate_path_tables, pathtab_write, "Path table"};
struct output_fragment dirtree_desc	= {NULL, dirtree_size,	NULL,	  dirtree_write,  "Directory tree" };
struct output_fragment dirtree_clean	= {NULL, dirtree_fixup,	dirtree_dump, dirtree_cleanup, "Directory tree cleanup" };
struct output_fragment extension_desc	= {NULL, ext_size,	NULL,	  exten_write,	  "Extension record" };
struct output_fragment files_desc	= {NULL, NULL,		file_gen, file_write,	  "The File(s)"};
struct output_fragment interpad_desc	= {NULL, interpad_size,	NULL,	  interpad_write, "Intermediate Padblock"};
struct output_fragment endpad_desc	= {NULL, endpad_size,	NULL,	  endpad_write,	  "Ending Padblock"};