summaryrefslogtreecommitdiff
path: root/fileio.c
blob: 1847e62eed2ed731b724f264414b801611581c9b (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
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
/*
  fileio.c - Zip 3

  Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2007-Mar-4 or later
  (the contents of which are also included in zip.h) for terms of use.
  If, for some reason, all these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*
 *  fileio.c by Mark Adler
 */
#define __FILEIO_C

#include "zip.h"
#include "crc32.h"

#ifdef MACOS
#  include "helpers.h"
#endif

#ifdef VMS
#  include "vms/vms.h"
#endif /* def VMS */

#include <time.h>

#ifdef NO_MKTIME
time_t mktime OF((struct tm *));
#endif

#ifdef OSF
#define EXDEV 18   /* avoid a bug in the DEC OSF/1 header files. */
#else
#include <errno.h>
#endif

#ifdef NO_ERRNO
extern int errno;
#endif

/* -----------------------
   For long option support
   ----------------------- */
#include <ctype.h>


#if defined(VMS) || defined(TOPS20)
#  define PAD 5
#else
#  define PAD 0
#endif

#ifdef NO_RENAME
int rename OF((ZCONST char *, ZCONST char *));
#endif


/* Local functions */
local int optionerr OF((char *, ZCONST char *, int, int));
local unsigned long get_shortopt OF((char **, int, int *, int *, char **, int *, int));
local unsigned long get_longopt OF((char **, int, int *, int *, char **, int *, int));

#ifdef UNICODE_SUPPORT
local int utf8_char_bytes OF((ZCONST char *utf8));
local long ucs4_char_from_utf8 OF((ZCONST char **utf8 ));
local int utf8_from_ucs4_char OF((char *utf8buf, ulg ch));
local int utf8_to_ucs4_string OF((ZCONST char *utf8, ulg *usc4buf,
                                  int buflen));
local int ucs4_string_to_utf8 OF((ZCONST ulg *ucs4, char *utf8buf,
                                  int buflen));
#if 0
  local int utf8_chars OF((ZCONST char *utf8));
#endif
#endif /* UNICODE_SUPPORT */

#ifndef UTIL    /* the companion #endif is a bit of ways down ... */

local int fqcmp  OF((ZCONST zvoid *, ZCONST zvoid *));
local int fqcmpz OF((ZCONST zvoid *, ZCONST zvoid *));


/* Local module level variables. */
char *label = NULL;                /* global, but only used in `system'.c */
local z_stat zipstatb;             /* now use z_stat globally - 7/24/04 EG */
#if defined(UNICODE_SUPPORT) && defined(WIN32)
 local zw_stat zipstatbw;
#endif
#if (!defined(MACOS) && !defined(WINDLL))
local int zipstate = -1;
#else
int zipstate;
#endif
/* -1 unknown, 0 old zip file exists, 1 new zip file */

#if 0
char *getnam(n, fp)
char *n;                /* where to put name (must have >=FNMAX+1 bytes) */
#endif

/* converted to return string pointer from malloc to avoid
   size limitation - 11/8/04 EG */
#define GETNAM_MAX 9000 /* hopefully big enough for now */
char *getnam(fp)
  FILE *fp;
  /* Read a \n or \r delimited name from stdin into n, and return
     n.  If EOF, then return NULL.  Also, if problem return NULL. */
{
  char name[GETNAM_MAX + 1];
  int c;                /* last character read */
  char *p;              /* pointer into name area */


  p = name;
  while ((c = getc(fp)) == '\n' || c == '\r')
    ;
  if (c == EOF)
    return NULL;
  do {
    if (p - name >= GETNAM_MAX)
      return NULL;
    *p++ = (char) c;
    c = getc(fp);
  } while (c != EOF && (c != '\n' && c != '\r'));
#ifdef WIN32
/*
 * WIN32 strips off trailing spaces and periods in filenames
 * XXX what about a filename that only consists of spaces ?
 *     Answer: on WIN32, a filename must contain at least one non-space char
 */
  while (p > name) {
    if ((c = p[-1]) != ' ' && c != '.')
      break;
    --p;
  }
#endif
  *p = 0;
  /* malloc a copy */
  if ((p = malloc(strlen(name) + 1)) == NULL) {
    return NULL;
  }
  strcpy(p, name);
  return p;
}

struct flist far *fexpel(f)
struct flist far *f;    /* entry to delete */
/* Delete the entry *f in the doubly-linked found list.  Return pointer to
   next entry to allow stepping through list. */
{
  struct flist far *t;  /* temporary variable */

  t = f->nxt;
  *(f->lst) = t;                        /* point last to next, */
  if (t != NULL)
    t->lst = f->lst;                    /* and next to last */
  if (f->name != NULL)                  /* free memory used */
    free((zvoid *)(f->name));
  if (f->zname != NULL)
    free((zvoid *)(f->zname));
  if (f->iname != NULL)
    free((zvoid *)(f->iname));
#ifdef UNICODE_SUPPORT
  if (f->uname)
    free((zvoid *)f->uname);
# ifdef WIN32
  if (f->namew)
    free((zvoid *)f->namew);
  if (f->inamew)
    free((zvoid *)f->inamew);
  if (f->znamew)
    free((zvoid *)f->znamew);
# endif
#endif
  farfree((zvoid far *)f);
  fcount--;                             /* decrement count */
  return t;                             /* return pointer to next */
}

local int fqcmp(a, b)
  ZCONST zvoid *a, *b;          /* pointers to pointers to found entries */
/* Used by qsort() to compare entries in the found list by name. */
{
  return strcmp((*(struct flist far **)a)->name,
                (*(struct flist far **)b)->name);
}

local int fqcmpz(a, b)
  ZCONST zvoid *a, *b;          /* pointers to pointers to found entries */
/* Used by qsort() to compare entries in the found list by iname. */
{
  return strcmp((*(struct flist far **)a)->iname,
                (*(struct flist far **)b)->iname);
}

char *last(p, c)
  char *p;                /* sequence of path components */
  int c;                  /* path components separator character */
/* Return a pointer to the start of the last path component. For a directory
 * name terminated by the character in c, the return value is an empty string.
 */
{
  char *t;              /* temporary variable */

  if ((t = strrchr(p, c)) != NULL)
    return t + 1;
  else
#ifndef AOS_VS
    return p;
#else
/* We want to allow finding of end of path in either AOS/VS-style pathnames
 * or Unix-style pathnames.  This presents a few little problems ...
 */
  {
    if (*p == '='  ||  *p == '^')      /* like ./ and ../ respectively */
      return p + 1;
    else
      return p;
  }
#endif
}

#if defined(UNICODE_SUPPORT) && defined(WIN32)
wchar_t *lastw(pw, c)
  wchar_t *pw;            /* sequence of path components */
  wchar_t c;              /* path components separator character */
/* Return a pointer to the start of the last path component. For a directory
 * name terminated by the character in c, the return value is an empty string.
 */
{
  wchar_t *tw;            /* temporary variable */

  if ((tw = wcsrchr(pw, c)) != NULL)
    return tw + 1;
  else
# ifndef AOS_VS
    return pw;
# else
/* We want to allow finding of end of path in either AOS/VS-style pathnames
 * or Unix-style pathnames.  This presents a few little problems ...
 */
  {
    if (*pw == (wchar_t)'='  ||  *pw == (wchar_t)'^')      /* like ./ and ../ respectively */
      return pw + 1;
    else
      return pw;
  }
# endif
}
#endif


char *msname(n)
  char *n;
/* Reduce all path components to MSDOS upper case 8.3 style names. */
{
  int c;                /* current character */
  int f;                /* characters in current component */
  char *p;              /* source pointer */
  char *q;              /* destination pointer */

  p = q = n;
  f = 0;
  while ((c = (unsigned char)*POSTINCSTR(p)) != 0)
    if (c == ' ' || c == ':' || c == '"' || c == '*' || c == '+' ||
        c == ',' || c == ';' || c == '<' || c == '=' || c == '>' ||
        c == '?' || c == '[' || c == ']' || c == '|')
      continue;                         /* char is discarded */
    else if (c == '/')
    {
      *POSTINCSTR(q) = (char)c;
      f = 0;                            /* new component */
    }
#ifdef __human68k__
    else if (ismbblead(c) && *p)
    {
      if (f == 7 || f == 11)
        f++;
      else if (*p && f < 12 && f != 8)
      {
        *q++ = c;
        *q++ = *p++;
        f += 2;
      }
    }
#endif /* __human68k__ */
    else if (c == '.')
    {
      if (f == 0)
        continue;                       /* leading dots are discarded */
      else if (f < 9)
      {
        *POSTINCSTR(q) = (char)c;
        f = 9;                          /* now in file type */
      }
      else
        f = 12;                         /* now just excess characters */
    }
    else
      if (f < 12 && f != 8)
      {
        f += CLEN(p);                   /* do until end of name or type */
        *POSTINCSTR(q) = (char)(to_up(c));
      }
  *q = 0;
  return n;
}

#ifdef UNICODE_SUPPORT
wchar_t *msnamew(nw)
  wchar_t *nw;
/* Reduce all path components to MSDOS upper case 8.3 style names. */
{
  wchar_t c;            /* current character */
  int f;                /* characters in current component */
  wchar_t *pw;          /* source pointer */
  wchar_t *qw;          /* destination pointer */

  pw = qw = nw;
  f = 0;
  while ((c = (unsigned char)*pw++) != 0)
    if (c == ' ' || c == ':' || c == '"' || c == '*' || c == '+' ||
        c == ',' || c == ';' || c == '<' || c == '=' || c == '>' ||
        c == '?' || c == '[' || c == ']' || c == '|')
      continue;                         /* char is discarded */
    else if (c == '/')
    {
      *qw++ = c;
      f = 0;                            /* new component */
    }
#ifdef __human68k__
    else if (ismbblead(c) && *pw)
    {
      if (f == 7 || f == 11)
        f++;
      else if (*pw && f < 12 && f != 8)
      {
        *qw++ = c;
        *qw++ = *pw++;
        f += 2;
      }
    }
#endif /* __human68k__ */
    else if (c == '.')
    {
      if (f == 0)
        continue;                       /* leading dots are discarded */
      else if (f < 9)
      {
        *qw++ = c;
        f = 9;                          /* now in file type */
      }
      else
        f = 12;                         /* now just excess characters */
    }
    else
      if (f < 12 && f != 8)
      {
        f++;                            /* do until end of name or type */
        *qw++ = towupper(c);
      }
  *qw = 0;
  return nw;
}
#endif


int proc_archive_name(n, caseflag)
  char *n;             /* name to process */
  int caseflag;         /* true to force case-sensitive match */
/* Process a name or sh expression in existing archive to operate
   on (or exclude).  Return an error code in the ZE_ class. */
{
  int m;                /* matched flag */
  char *p;              /* path for recursion */
  struct zlist far *z;  /* steps through zfiles list */

  if (strcmp(n, "-") == 0) {   /* if compressing stdin */
    zipwarn("Cannot select stdin when selecting archive entries", "");
    return ZE_MISS;
  }
  else
  {
    /* Search for shell expression in zip file */
    p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
    m = 1;
    for (z = zfiles; z != NULL; z = z->nxt) {
      if (MATCH(p, z->iname, caseflag))
      {
        z->mark = pcount ? filter(z->zname, caseflag) : 1;
        if (verbose)
            fprintf(mesg, "zip diagnostic: %scluding %s\n",
               z->mark ? "in" : "ex", z->oname);
        m = 0;
      }
    }
#ifdef UNICODE_SUPPORT
    /* also check escaped Unicode names */
    for (z = zfiles; z != NULL; z = z->nxt) {
      if (z->zuname) {
#ifdef WIN32
        /* It seems something is lost in going from a listed
           name from zip -su in a console window to using that
           name in a command line.  This kluge may fix it
           and just takes zuname, converts to oem (i.e. ouname),
           then converts it back which ends up not the same as
           started with.
         */
        char *zuname = z->wuname;
#else
        char *zuname = z->zuname;
#endif
        if (MATCH(p, zuname, caseflag))
        {
          z->mark = pcount ? filter(zuname, caseflag) : 1;
          if (verbose) {
              fprintf(mesg, "zip diagnostic: %scluding %s\n",
                 z->mark ? "in" : "ex", z->oname);
              fprintf(mesg, "     Escaped Unicode:  %s\n",
                 z->ouname);
          }
          m = 0;
        }
      }
    }
#endif
    free((zvoid *)p);
    return m ? ZE_MISS : ZE_OK;
  }
}


int check_dup()
/* Sort the found list and remove duplicates.
   Return an error code in the ZE_ class. */
{
  struct flist far *f;          /* steps through found linked list */
  extent j, k;                  /* indices for s */
  struct flist far **s;         /* sorted table */
  struct flist far **nodup;     /* sorted table without duplicates */

  /* sort found list, remove duplicates */
  if (fcount)
  {
    extent fl_size = fcount * sizeof(struct flist far *);
    if ((fl_size / sizeof(struct flist far *)) != fcount ||
        (s = (struct flist far **)malloc(fl_size)) == NULL)
      return ZE_MEM;
    for (j = 0, f = found; f != NULL; f = f->nxt)
      s[j++] = f;
    /* Check names as given (f->name) */
    qsort((char *)s, fcount, sizeof(struct flist far *), fqcmp);
    for (k = j = fcount - 1; j > 0; j--)
      if (strcmp(s[j - 1]->name, s[j]->name) == 0)
        /* remove duplicate entry from list */
        fexpel(s[j]);           /* fexpel() changes fcount */
      else
        /* copy valid entry into destination position */
        s[k--] = s[j];
    s[k] = s[0];                /* First entry is always valid */
    nodup = &s[k];              /* Valid entries are at end of array s */

    /* sort only valid items and check for unique internal names (f->iname) */
    qsort((char *)nodup, fcount, sizeof(struct flist far *), fqcmpz);
    for (j = 1; j < fcount; j++)
      if (strcmp(nodup[j - 1]->iname, nodup[j]->iname) == 0)
      {
        char tempbuf[FNMAX+4081];

        sprintf(errbuf, "  first full name: %s\n", nodup[j - 1]->name);
        sprintf(tempbuf, " second full name: %s\n", nodup[j]->name);
        strcat(errbuf, "                     ");
        strcat(errbuf, tempbuf);
#ifdef EBCDIC
        strtoebc(nodup[j]->iname, nodup[j]->iname);
#endif
        sprintf(tempbuf, "name in zip file repeated: %s", nodup[j]->iname);
        strcat(errbuf, "                     ");
        strcat(errbuf, tempbuf);
        if (pathput == 0) {
          strcat(errbuf, "\n                     this may be a result of using -j");
        }
#ifdef EBCDIC
        strtoasc(nodup[j]->iname, nodup[j]->iname);
#endif
        zipwarn(errbuf, "");
        return ZE_PARMS;
      }
    free((zvoid *)s);
  }
  return ZE_OK;
}

int filter(name, casesensitive)
  char *name;
  int casesensitive;
  /* Scan the -R, -i and -x lists for matches to the given name.
     Return TRUE if the name must be included, FALSE otherwise.
     Give precedence to -x over -i and -R.
     Note that if both R and i patterns are given then must
     have a match for both.
     This routine relies on the following global variables:
       patterns                 array of match pattern structures
       pcount                   total number of patterns
       icount                   number of -i patterns
       Rcount                   number of -R patterns
     These data are set up by the command line parsing code.
   */
{
   unsigned int n;
   int slashes;
   char *p, *q;
   /* without -i patterns, every name matches the "-i select rules" */
   int imatch = (icount == 0);
   /* without -R patterns, every name matches the "-R select rules" */
   int Rmatch = (Rcount == 0);

   if (pcount == 0) return TRUE;

   for (n = 0; n < pcount; n++) {
      if (!patterns[n].zname[0])        /* it can happen... */
         continue;
      p = name;
      switch (patterns[n].select) {
       case 'R':
         if (Rmatch)
            /* one -R match is sufficient, skip this pattern */
            continue;
         /* With -R patterns, if the pattern has N path components (that is,
            N-1 slashes), then we test only the last N components of name.
          */
         slashes = 0;
         for (q = patterns[n].zname; (q = MBSCHR(q, '/')) != NULL; MB_NEXTCHAR(q))
            slashes++;
         /* The name may have M path components (M-1 slashes) */
         for (q = p; (q = MBSCHR(q, '/')) != NULL; MB_NEXTCHAR(q))
            slashes--;
         /* Now, "slashes" contains the difference "N-M" between the number
            of path components in the pattern (N) and in the name (M).
          */
         if (slashes < 0)
            /* We found "M > N"
                --> skip the first (M-N) path components of the name.
             */
            for (q = p; (q = MBSCHR(q, '/')) != NULL; MB_NEXTCHAR(q))
               if (++slashes == 0) {
                  p = q + 1;    /* q points at '/', mblen("/") is 1 */
                  break;
               }
         break;
       case 'i':
         if (imatch)
            /* one -i match is sufficient, skip this pattern */
            continue;
         break;
      }
      if (MATCH(patterns[n].zname, p, casesensitive)) {
         switch (patterns[n].select) {
            case 'x':
               /* The -x match takes precedence over everything else */
               return FALSE;
            case 'R':
               Rmatch = TRUE;
               break;
            default:
               /* this must be a type -i match */
               imatch = TRUE;
               break;
         }
      }
   }
   return imatch && Rmatch;
}


#ifdef UNICODE_SUPPORT
# ifdef WIN32

int newnamew(namew, isdir, casesensitive)
  wchar_t *namew;             /* name to add (or exclude) */
  int  isdir;                 /* true for a directory */
  int  casesensitive;         /* true for case-sensitive matching */
/* Add (or exclude) the name of an existing disk file.  Return an error
   code in the ZE_ class. */
{
  wchar_t *inamew = NULL;     /* internal name */
  wchar_t *znamew = NULL;     /* external version of iname */
  wchar_t *undosmw = NULL;    /* zname version with "-j" and "-k" options disabled */
  char *oname = NULL;         /* iname converted for display */
  char *name = NULL;
  char *iname = NULL;
  char *zname = NULL;
  char *zuname = NULL;
  char *undosm = NULL;
  struct flist far *f;        /* where in found, or new found entry */
  struct zlist far *z;        /* where in zfiles (if found) */
  int dosflag;

  /* Scanning files ...
   *
   * After 5 seconds output Scanning files...
   * then a dot every 2 seconds
   */
  if (noisy) {
    /* If find files then output message after delay */
    if (scan_count == 0) {
      time_t current = time(NULL);
      scan_start = current;
    }
    scan_count++;
    if (scan_count % 100 == 0) {
      time_t current = time(NULL);

      if (current - scan_start > scan_delay) {
        if (scan_last == 0) {
          zipmessage_nl("Scanning files ", 0);
          scan_last = current;
        }
        if (current - scan_last > scan_dot_time) {
          scan_last = current;
          fprintf(mesg, ".");
          fflush(mesg);
        }
      }
    }
  }

  /* Search for name in zip file.  If there, mark it, else add to
     list of new names to do (or remove from that list). */
  if ((inamew = ex2inw(namew, isdir, &dosflag)) == NULL)
    return ZE_MEM;

  /* Discard directory names with zip -rj */
  if (*inamew == (wchar_t)'\0') {

 /* If extensions needs to be swapped, we will have empty directory names
    instead of the original directory. For example, zipping 'c.', 'c.main'
    should zip only 'main.c' while 'c.' will be converted to '\0' by ex2in. */

    if (pathput && !recurse) error("empty name without -j or -r");
    free((zvoid *)inamew);
    return ZE_OK;
  }

  if (dosflag || !pathput) {
    int save_dosify = dosify, save_pathput = pathput;
    dosify = 0;
    pathput = 1;
    /* zname is temporarly mis-used as "undosmode" iname pointer */
    if ((znamew = ex2inw(namew, isdir, NULL)) != NULL) {
      undosmw = in2exw(znamew);
      free(znamew);
    }
    dosify = save_dosify;
    pathput = save_pathput;
  }
  if ((znamew = in2exw(inamew)) == NULL)
    return ZE_MEM;

  /* Convert names from wchar_t to char */

  name = wchar_to_local_string(namew);
  iname = wchar_to_local_string(inamew);
  zname = wchar_to_local_string(znamew);

  oname = local_to_display_string(zname);

  zuname = wchar_to_local_string(znamew);

  if (undosmw == NULL)
    undosmw = znamew;
  undosm = wchar_to_local_string(undosmw);

  if ((z = zsearch(zuname)) != NULL) {
    if (pcount && !filter(undosm, casesensitive)) {
      /* Do not clear z->mark if "exclude", because, when "dosify || !pathput"
       * is in effect, two files with different filter options may hit the
       * same z entry.
       */
      if (verbose)
        fprintf(mesg, "excluding %s\n", oname);
    } else {
      z->mark = 1;
      if ((z->name = malloc(strlen(name) + 1 + PAD)) == NULL) {
        if (undosmw != znamew)
          free(undosmw);
        if (undosm) free(undosm);
        if (inamew) free(inamew);
        if (znamew) free(znamew);
        if (name) free(name);
        if (iname) free(iname);
        if (zname) free(zname);
        if (oname) free(oname);
        if (zuname) free(zuname);
        return ZE_MEM;
      }
      strcpy(z->name, name);
      z->oname = oname;
      oname = NULL;
      z->dosflag = dosflag;

#ifdef FORCE_NEWNAME
      free((zvoid *)(z->iname));
      z->iname = iname;
      iname = NULL;
#else
      /* Better keep the old name. Useful when updating on MSDOS a zip file
       * made on Unix.
       */
#endif /* ? FORCE_NEWNAME */
    }

    if ((z->namew = (wchar_t *)malloc((wcslen(namew) + 1) * sizeof(wchar_t))) == NULL) {
      if (undosmw != znamew)
        free(undosmw);
      if (undosm) free(undosm);
      if (inamew) free(inamew);
      if (znamew) free(znamew);
      if (name) free(name);
      if (iname) free(iname);
      if (zname) free(zname);
      if (oname) free(oname);
      if (zuname) free(zuname);
      return ZE_MEM;
    }
    wcscpy(z->namew, namew);
    z->inamew = inamew;
    inamew = NULL;
    z->znamew = znamew;
    znamew = NULL;
    z->uname = wchar_to_utf8_string(z->inamew);
    if (name == label) {
       label = z->name;
    }
  } else if (pcount == 0 || filter(undosm, casesensitive)) {

    /* Check that we are not adding the zip file to itself. This
     * catches cases like "zip -m foo ../dir/foo.zip".
     */
/* Version of stat() for CMS/MVS isn't complete enough to see if       */
/* files match.  Just let ZIP.C compare the filenames.  That's good    */
/* enough for CMS anyway since there aren't paths to worry about.      */
    zw_stat statbw;     /* need for wide stat */
    wchar_t *zipfilew = local_to_wchar_string(zipfile);

    if (zipstate == -1)
       zipstate = strcmp(zipfile, "-") != 0 &&
                   zwstat(zipfilew, &zipstatbw) == 0;
    free(zipfilew);

    if (zipstate == 1 && (statbw = zipstatbw, zwstat(namew, &statbw) == 0
      && zipstatbw.st_mode  == statbw.st_mode
      && zipstatbw.st_ino   == statbw.st_ino
      && zipstatbw.st_dev   == statbw.st_dev
      && zipstatbw.st_uid   == statbw.st_uid
      && zipstatbw.st_gid   == statbw.st_gid
      && zipstatbw.st_size  == statbw.st_size
      && zipstatbw.st_mtime == statbw.st_mtime
      && zipstatbw.st_ctime == statbw.st_ctime)) {
      /* Don't compare a_time since we are reading the file */
        if (verbose)
          fprintf(mesg, "file matches zip file -- skipping\n");
        if (undosmw != znamew)
          free(undosmw);
        if (undosm) free(undosm);
        if (inamew) free(inamew);
        if (znamew) free(znamew);
        if (name) free(name);
        if (iname) free(iname);
        if (zname) free(zname);
        if (oname) free(oname);
        if (zuname) free(zuname);
        return ZE_OK;
    }

    /* allocate space and add to list */
    if ((f = (struct flist far *)farmalloc(sizeof(struct flist))) == NULL ||
        fcount + 1 < fcount ||
        (f->name = malloc(strlen(name) + 1 + PAD)) == NULL)
    {
      if (f != NULL)
        farfree((zvoid far *)f);
      if (undosmw != znamew)
        free(undosmw);
      if (undosm) free(undosm);
      if (inamew) free(inamew);
      if (znamew) free(znamew);
      if (name) free(name);
      if (iname) free(iname);
      if (zname) free(zname);
      if (oname) free(oname);
      if (zuname) free(zuname);
      return ZE_MEM;
    }
    if (undosmw != znamew)
      free((zvoid *)undosmw);
    strcpy(f->name, name);
    f->iname = iname;
    iname = NULL;
    f->zname = zname;
    zname = NULL;
    /* Unicode */
    if ((f->namew = (wchar_t *)malloc((wcslen(namew) + 1) * sizeof(wchar_t))) == NULL) {
      if (f != NULL)
        farfree((zvoid far *)f);
      if (undosmw != znamew)
        free(undosmw);
      if (undosm) free(undosm);
      if (inamew) free(inamew);
      if (znamew) free(znamew);
      if (name) free(name);
      if (iname) free(iname);
      if (zname) free(zname);
      if (oname) free(oname);
      if (zuname) free(zuname);
      return ZE_MEM;
    }
    wcscpy(f->namew, namew);
    f->znamew = znamew;
    znamew = NULL;
    f->uname = wchar_to_utf8_string(inamew);
    f->inamew = inamew;
    inamew = NULL;
    f->oname = oname;
    oname = NULL;
    f->dosflag = dosflag;
    *fnxt = f;
    f->lst = fnxt;
    f->nxt = NULL;
    fnxt = &f->nxt;
    fcount++;
    if (name == label) {
      label = f->name;
    }
  }
  if (undosm) free(undosm);
  if (inamew) free(inamew);
  if (znamew) free(znamew);
  if (name) free(name);
  if (iname) free(iname);
  if (zname) free(zname);
  if (oname) free(oname);
  if (zuname) free(zuname);
  return ZE_OK;
}

# endif
#endif

int newname(name, isdir, casesensitive)
  char *name;           /* name to add (or exclude) */
  int  isdir;           /* true for a directory */
  int  casesensitive;   /* true for case-sensitive matching */
/* Add (or exclude) the name of an existing disk file.  Return an error
   code in the ZE_ class. */
{
  char *iname, *zname;  /* internal name, external version of iname */
  char *undosm;         /* zname version with "-j" and "-k" options disabled */
  char *oname;          /* iname converted for display */
  struct flist far *f;  /* where in found, or new found entry */
  struct zlist far *z;  /* where in zfiles (if found) */
  int dosflag;

  /* Scanning files ...
   *
   * After 5 seconds output Scanning files...
   * then a dot every 2 seconds
   */
  if (noisy) {
    /* If find files then output message after delay */
    if (scan_count == 0) {
      time_t current = time(NULL);
      scan_start = current;
    }
    scan_count++;
    if (scan_count % 100 == 0) {
      time_t current = time(NULL);

      if (current - scan_start > scan_delay) {
        if (scan_last == 0) {
          zipmessage_nl("Scanning files ", 0);
          scan_last = current;
        }
        if (current - scan_last > scan_dot_time) {
          scan_last = current;
          fprintf(mesg, ".");
          fflush(mesg);
        }
      }
    }
  }

  /* Search for name in zip file.  If there, mark it, else add to
     list of new names to do (or remove from that list). */
  if ((iname = ex2in(name, isdir, &dosflag)) == NULL)
    return ZE_MEM;

  /* Discard directory names with zip -rj */
  if (*iname == '\0') {
#ifndef AMIGA
/* A null string is a legitimate external directory name in AmigaDOS; also,
 * a command like "zip -r zipfile FOO:" produces an empty internal name.
 */
# ifndef RISCOS
 /* If extensions needs to be swapped, we will have empty directory names
    instead of the original directory. For example, zipping 'c.', 'c.main'
    should zip only 'main.c' while 'c.' will be converted to '\0' by ex2in. */

    if (pathput && !recurse) error("empty name without -j or -r");

# endif /* !RISCOS */
#endif /* !AMIGA */
    free((zvoid *)iname);
    return ZE_OK;
  }
  undosm = NULL;
  if (dosflag || !pathput) {
    int save_dosify = dosify, save_pathput = pathput;
    dosify = 0;
    pathput = 1;
    /* zname is temporarly mis-used as "undosmode" iname pointer */
    if ((zname = ex2in(name, isdir, NULL)) != NULL) {
      undosm = in2ex(zname);
      free(zname);
    }
    dosify = save_dosify;
    pathput = save_pathput;
  }
  if ((zname = in2ex(iname)) == NULL)
    return ZE_MEM;
#ifdef UNICODE_SUPPORT
  /* Convert name to display or OEM name */
  oname = local_to_display_string(iname);
#else
  if ((oname = malloc(strlen(zname) + 1)) == NULL)
    return ZE_MEM;
  strcpy(oname, zname);
#endif
  if (undosm == NULL)
    undosm = zname;
  if ((z = zsearch(zname)) != NULL) {
    if (pcount && !filter(undosm, casesensitive)) {
      /* Do not clear z->mark if "exclude", because, when "dosify || !pathput"
       * is in effect, two files with different filter options may hit the
       * same z entry.
       */
      if (verbose)
        fprintf(mesg, "excluding %s\n", oname);
      free((zvoid *)iname);
      free((zvoid *)zname);
    } else {
      z->mark = 1;
      if ((z->name = malloc(strlen(name) + 1 + PAD)) == NULL) {
        if (undosm != zname)
          free((zvoid *)undosm);
        free((zvoid *)iname);
        free((zvoid *)zname);
        return ZE_MEM;
      }
      strcpy(z->name, name);
      z->oname = oname;
      z->dosflag = dosflag;

#ifdef FORCE_NEWNAME
      free((zvoid *)(z->iname));
      z->iname = iname;
#else
      /* Better keep the old name. Useful when updating on MSDOS a zip file
       * made on Unix.
       */
      free((zvoid *)iname);
      free((zvoid *)zname);
#endif /* ? FORCE_NEWNAME */
    }
#if defined(UNICODE_SUPPORT) && defined(WIN32)
    z->namew = NULL;
    z->inamew = NULL;
    z->znamew = NULL;
#endif
    if (name == label) {
       label = z->name;
    }
  } else if (pcount == 0 || filter(undosm, casesensitive)) {

    /* Check that we are not adding the zip file to itself. This
     * catches cases like "zip -m foo ../dir/foo.zip".
     */
#ifndef CMS_MVS
/* Version of stat() for CMS/MVS isn't complete enough to see if       */
/* files match.  Just let ZIP.C compare the filenames.  That's good    */
/* enough for CMS anyway since there aren't paths to worry about.      */
    z_stat statb;      /* now use structure z_stat and function zstat globally 7/24/04 EG */

    if (zipstate == -1)
       zipstate = strcmp(zipfile, "-") != 0 &&
                   zstat(zipfile, &zipstatb) == 0;

    if (zipstate == 1 && (statb = zipstatb, zstat(name, &statb) == 0
      && zipstatb.st_mode  == statb.st_mode
#ifdef VMS
      && memcmp(zipstatb.st_ino, statb.st_ino, sizeof(statb.st_ino)) == 0
      && strcmp(zipstatb.st_dev, statb.st_dev) == 0
      && zipstatb.st_uid   == statb.st_uid
#else /* !VMS */
      && zipstatb.st_ino   == statb.st_ino
      && zipstatb.st_dev   == statb.st_dev
      && zipstatb.st_uid   == statb.st_uid
      && zipstatb.st_gid   == statb.st_gid
#endif /* ?VMS */
      && zipstatb.st_size  == statb.st_size
      && zipstatb.st_mtime == statb.st_mtime
      && zipstatb.st_ctime == statb.st_ctime)) {
      /* Don't compare a_time since we are reading the file */
         if (verbose)
           fprintf(mesg, "file matches zip file -- skipping\n");
         if (undosm != zname)
           free((zvoid *)zname);
         if (undosm != iname)
           free((zvoid *)undosm);
         free((zvoid *)iname);
         free(oname);
         return ZE_OK;
    }
#endif  /* CMS_MVS */

    /* allocate space and add to list */
    if ((f = (struct flist far *)farmalloc(sizeof(struct flist))) == NULL ||
        fcount + 1 < fcount ||
        (f->name = malloc(strlen(name) + 1 + PAD)) == NULL)
    {
      if (f != NULL)
        farfree((zvoid far *)f);
      if (undosm != zname)
        free((zvoid *)undosm);
      free((zvoid *)iname);
      free((zvoid *)zname);
      free(oname);
      return ZE_MEM;
    }
    strcpy(f->name, name);
    f->iname = iname;
    f->zname = zname;
#ifdef UNICODE_SUPPORT
    /* Unicode */
    f->uname = local_to_utf8_string(iname);
#ifdef WIN32
    f->namew = NULL;
    f->inamew = NULL;
    f->znamew = NULL;
    if (strcmp(f->name, "-") == 0) {
      f->namew = local_to_wchar_string(f->name);
    }
#endif

#endif
    f->oname = oname;
    f->dosflag = dosflag;

    *fnxt = f;
    f->lst = fnxt;
    f->nxt = NULL;
    fnxt = &f->nxt;
    fcount++;
    if (name == label) {
      label = f->name;
    }
  }
  if (undosm != zname)
    free((zvoid *)undosm);
  return ZE_OK;
}

ulg dostime(y, n, d, h, m, s)
int y;                  /* year */
int n;                  /* month */
int d;                  /* day */
int h;                  /* hour */
int m;                  /* minute */
int s;                  /* second */
/* Convert the date y/n/d and time h:m:s to a four byte DOS date and
   time (date in high two bytes, time in low two bytes allowing magnitude
   comparison). */
{
  return y < 1980 ? DOSTIME_MINIMUM /* dostime(1980, 1, 1, 0, 0, 0) */ :
        (((ulg)y - 1980) << 25) | ((ulg)n << 21) | ((ulg)d << 16) |
        ((ulg)h << 11) | ((ulg)m << 5) | ((ulg)s >> 1);
}


ulg unix2dostime(t)
time_t *t;              /* unix time to convert */
/* Return the Unix time t in DOS format, rounded up to the next two
   second boundary. */
{
  time_t t_even;
  struct tm *s;         /* result of localtime() */

  t_even = (time_t)(((unsigned long)(*t) + 1) & (~1));
                                /* Round up to even seconds. */
  s = localtime(&t_even);       /* Use local time since MSDOS does. */
  if (s == (struct tm *)NULL) {
      /* time conversion error; use current time as emergency value
         (assuming that localtime() does at least accept this value!) */
      t_even = (time_t)(((unsigned long)time(NULL) + 1) & (~1));
      s = localtime(&t_even);
  }
  return dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
                 s->tm_hour, s->tm_min, s->tm_sec);
}

int issymlnk(a)
ulg a;                  /* Attributes returned by filetime() */
/* Return true if the attributes are those of a symbolic link */
{
#ifndef QDOS
#ifdef S_IFLNK
#ifdef __human68k__
  int *_dos_importlnenv(void);

  if (_dos_importlnenv() == NULL)
    return 0;
#endif
  return ((a >> 16) & S_IFMT) == S_IFLNK;
#else /* !S_IFLNK */
  return (int)a & 0;    /* avoid warning on unused parameter */
#endif /* ?S_IFLNK */
#else
  return 0;
#endif
}

#endif /* !UTIL */


#if (!defined(UTIL) && !defined(ZP_NEED_GEN_D2U_TIME))
   /* There is no need for dos2unixtime() in the ZipUtils' code. */
#  define ZP_NEED_GEN_D2U_TIME
#endif
#if ((defined(OS2) || defined(VMS)) && defined(ZP_NEED_GEN_D2U_TIME))
   /* OS/2 and VMS use a special solution to handle time-stams of files. */
#  undef ZP_NEED_GEN_D2U_TIME
#endif
#if (defined(W32_STATROOT_FIX) && !defined(ZP_NEED_GEN_D2U_TIME))
   /* The Win32 stat()-bandaid to fix stat'ing root directories needs
    * dos2unixtime() to calculate the time-stamps. */
#  define ZP_NEED_GEN_D2U_TIME
#endif

#ifdef ZP_NEED_GEN_D2U_TIME

time_t dos2unixtime(dostime)
ulg dostime;            /* DOS time to convert */
/* Return the Unix time_t value (GMT/UTC time) for the DOS format (local)
 * time dostime, where dostime is a four byte value (date in most significant
 * word, time in least significant word), see dostime() function.
 */
{
  struct tm *t;         /* argument for mktime() */
  ZCONST time_t clock = time(NULL);

  t = localtime(&clock);
  t->tm_isdst = -1;     /* let mktime() determine if DST is in effect */
  /* Convert DOS time to UNIX time_t format */
  t->tm_sec  = (((int)dostime) <<  1) & 0x3e;
  t->tm_min  = (((int)dostime) >>  5) & 0x3f;
  t->tm_hour = (((int)dostime) >> 11) & 0x1f;
  t->tm_mday = (int)(dostime >> 16) & 0x1f;
  t->tm_mon  = ((int)(dostime >> 21) & 0x0f) - 1;
  t->tm_year = ((int)(dostime >> 25) & 0x7f) + 80;

  return mktime(t);
}

#undef ZP_NEED_GEN_D2U_TIME
#endif /* ZP_NEED_GEN_D2U_TIME */


#ifndef MACOS
int destroy(f)
  char *f;             /* file to delete */
/* Delete the file *f, returning non-zero on failure. */
{
  return unlink(f);
}


int replace(d, s)
char *d, *s;            /* destination and source file names */
/* Replace file *d by file *s, removing the old *s.  Return an error code
   in the ZE_ class. This function need not preserve the file attributes,
   this will be done by setfileattr() later.
 */
{
  z_stat t;         /* results of stat() */
#if defined(CMS_MVS)
  /* cmsmvs.h defines FOPW_TEMP as memory(hiperspace).  Since memory is
   * lost at end of run, always do copy instead of rename.
   */
  int copy = 1;
#else
  int copy = 0;
#endif
  int d_exists;

#if defined(VMS) || defined(CMS_MVS)
  /* stat() is broken on VMS remote files (accessed through Decnet).
   * This patch allows creation of remote zip files, but is not sufficient
   * to update them or compress remote files */
  unlink(d);
#else /* !(VMS || CMS_MVS) */
  d_exists = (LSTAT(d, &t) == 0);
  if (d_exists)
  {
    /*
     * respect existing soft and hard links!
     */
    if (t.st_nlink > 1
# ifdef S_IFLNK
        || (t.st_mode & S_IFMT) == S_IFLNK
# endif
        )
       copy = 1;
    else if (unlink(d))
       return ZE_CREAT;                 /* Can't erase zip file--give up */
  }
#endif /* ?(VMS || CMS_MVS) */
#ifndef CMS_MVS
  if (!copy) {
      if (rename(s, d)) {               /* Just move s on top of d */
          copy = 1;                     /* failed ? */
#if !defined(VMS) && !defined(ATARI) && !defined(AZTEC_C)
#if !defined(CMS_MVS) && !defined(RISCOS) && !defined(QDOS)
    /* For VMS, ATARI, AMIGA Aztec, VM_CMS, MVS, RISCOS,
       always assume that failure is EXDEV */
          if (errno != EXDEV
#  ifdef THEOS
           && errno != EEXIST
#  else
#    ifdef ENOTSAM
           && errno != ENOTSAM /* Used at least on Turbo C */
#    endif
#  endif
              ) return ZE_CREAT;
#endif /* !CMS_MVS && !RISCOS */
#endif /* !VMS && !ATARI && !AZTEC_C */
      }
  }
#endif /* !CMS_MVS */

  if (copy) {
    FILE *f, *g;        /* source and destination files */
    int r;              /* temporary variable */

#ifdef RISCOS
    if (SWI_OS_FSControl_26(s,d,0xA1)!=NULL) {
#endif

    /* Use zfopen for almost all opens where fopen is used.  For
       most OS that support large files we use the 64-bit file
       environment and zfopen maps to fopen, but this allows
       tweeking ports that don't do that.  7/24/04 */
    if ((f = zfopen(s, FOPR)) == NULL) {
      fprintf(mesg," replace: can't open %s\n", s);
      return ZE_TEMP;
    }
    if ((g = zfopen(d, FOPW)) == NULL)
    {
      fclose(f);
      return ZE_CREAT;
    }

    r = fcopy(f, g, (ulg)-1L);
    fclose(f);
    if (fclose(g) || r != ZE_OK)
    {
      unlink(d);
      return r ? (r == ZE_TEMP ? ZE_WRITE : r) : ZE_WRITE;
    }
    unlink(s);
#ifdef RISCOS
    }
#endif
  }
  return ZE_OK;
}
#endif /* !MACOS */


int getfileattr(f)
char *f;                /* file path */
/* Return the file attributes for file f or 0 if failure */
{
#ifdef __human68k__
  struct _filbuf buf;

  return _dos_files(&buf, f, 0xff) < 0 ? 0x20 : buf.atr;
#else
  z_stat s;

  return SSTAT(f, &s) == 0 ? (int) s.st_mode : 0;
#endif
}


int setfileattr(f, a)
char *f;                /* file path */
int a;                  /* attributes returned by getfileattr() */
/* Give the file f the attributes a, return non-zero on failure */
{
#if defined(TOPS20) || defined (CMS_MVS)
  return 0;
#else
#ifdef __human68k__
  return _dos_chmod(f, a) < 0 ? -1 : 0;
#else
  return chmod(f, a);
#endif
#endif
}


/* tempname */

#ifndef VMS /* VMS-specific function is in VMS.C. */

char *tempname(zip)
  char *zip;              /* path name of zip file to generate temp name for */

/* Return a temporary file name in its own malloc'ed space, using tempath. */
{
  char *t = zip;   /* malloc'ed space for name (use zip to avoid warning) */

# ifdef CMS_MVS
  if ((t = malloc(strlen(tempath) + L_tmpnam + 2)) == NULL)
    return NULL;

#  ifdef VM_CMS
  tmpnam(t);
  /* Remove filemode and replace with tempath, if any. */
  /* Otherwise A-disk is used by default */
  *(strrchr(t, ' ')+1) = '\0';
  if (tempath!=NULL)
     strcat(t, tempath);
  return t;
#  else   /* !VM_CMS */
  /* For MVS */
  tmpnam(t);
  if (tempath != NULL)
  {
    int l1 = strlen(t);
    char *dot;
    if (*t == '\'' && *(t+l1-1) == '\'' && (dot = strchr(t, '.')))
    {
      /* MVS and not OE.  tmpnam() returns quoted string of 5 qualifiers.
       * First is HLQ, rest are timestamps.  User can only replace HLQ.
       */
      int l2 = strlen(tempath);
      if (strchr(tempath, '.') || l2 < 1 || l2 > 8)
        ziperr(ZE_PARMS, "On MVS and not OE, tempath (-b) can only be HLQ");
      memmove(t+1+l2, dot, l1+1-(dot-t));  /* shift dot ready for new hlq */
      memcpy(t+1, tempath, l2);            /* insert new hlq */
    }
    else
    {
      /* MVS and probably OE.  tmpnam() returns filename based on TMPDIR,
       * no point in even attempting to change it.  User should modify TMPDIR
       * instead.
       */
      zipwarn("MVS, assumed to be OE, change TMPDIR instead of option -b: ",
              tempath);
    }
  }
  return t;
#  endif  /* !VM_CMS */

# else /* !CMS_MVS */

#  ifdef TANDEM
  char cur_subvol [FILENAME_MAX];
  char temp_subvol [FILENAME_MAX];
  char *zptr;
  char *ptr;
  char *cptr = &cur_subvol[0];
  char *tptr = &temp_subvol[0];
  short err;
  FILE *tempf;
  int attempts;

  t = (char *)malloc(NAMELEN); /* malloc here as you cannot free */
                               /* tmpnam allocated storage later */

  zptr = strrchr(zip, TANDEM_DELIMITER);

  if (zptr != NULL) {
    /* ZIP file specifies a Subvol so make temp file there so it can just
       be renamed at end */

    *tptr = *cptr = '\0';
    strcat(cptr, getenv("DEFAULTS"));

    strncat(tptr, zip, _min(FILENAME_MAX, (zptr - zip)) ); /* temp subvol */
    strncat(t, zip, _min(NAMELEN, ((zptr - zip) + 1)) );   /* temp stem   */

    err = chvol(tptr);
    ptr = t + strlen(t);  /* point to end of stem */
  }
  else
    ptr = t;

  /* If two zips are running in same subvol then we can get contention problems
     with the temporary filename.  As a work around we attempt to create
     the file here, and if it already exists we get a new temporary name */

  attempts = 0;
  do {
    attempts++;
    tmpnam(ptr);  /* Add filename */
    tempf = zfopen(ptr, FOPW_TMP);    /* Attempt to create file */
  } while (tempf == NULL && attempts < 100);

  if (attempts >= 100) {
    ziperr(ZE_TEMP, "Could not get unique temp file name");
  }

  fclose(tempf);

  if (zptr != NULL) {
    err = chvol(cptr);  /* Put ourself back to where we came in */
  }

  return t;

#  else /* !CMS_MVS && !TANDEM */
/*
 * Do something with TMPDIR, TMP, TEMP ????
 */
  if (tempath != NULL)
  {
    if ((t = malloc(strlen(tempath) + 12)) == NULL)
      return NULL;
    strcpy(t, tempath);

#   if (!defined(VMS) && !defined(TOPS20))
#    ifdef MSDOS
    {
      char c = (char)lastchar(t);
      if (c != '/' && c != ':' && c != '\\')
        strcat(t, "/");
    }
#    else

#     ifdef AMIGA
    {
      char c = (char)lastchar(t);
      if (c != '/' && c != ':')
        strcat(t, "/");
    }
#     else /* !AMIGA */
#      ifdef RISCOS
    if (lastchar(t) != '.')
      strcat(t, ".");
#      else /* !RISCOS */

#       ifdef QDOS
    if (lastchar(t) != '_')
      strcat(t, "_");
#       else
    if (lastchar(t) != '/')
      strcat(t, "/");
#       endif /* ?QDOS */
#      endif /* ?RISCOS */
#     endif  /* ?AMIGA */
#    endif /* ?MSDOS */
#   endif /* !VMS && !TOPS20 */
  }
  else
  {
    if ((t = malloc(12)) == NULL)
      return NULL;
    *t = 0;
  }
#   ifdef NO_MKTEMP
  {
    char *p = t + strlen(t);
    sprintf(p, "%08lx", (ulg)time(NULL));
    return t;
  }
#   else
  strcat(t, "ziXXXXXX"); /* must use lowercase for Linux dos file system */
#     if defined(UNIX) && !defined(NO_MKSTEMP)
  /* tempname should not be called */
  return t;
#     else
  return mktemp(t);
#     endif
#   endif /* NO_MKTEMP */
#  endif /* TANDEM */
# endif /* CMS_MVS */
}
#endif /* !VMS */

int fcopy(f, g, n)
  FILE *f, *g;            /* source and destination files */
  /* now use uzoff_t for all file sizes 5/14/05 CS */
  uzoff_t n;               /* number of bytes to copy or -1 for all */
/* Copy n bytes from file *f to file *g, or until EOF if (zoff_t)n == -1.
   Return an error code in the ZE_ class. */
{
  char *b;              /* malloc'ed buffer for copying */
  extent k;             /* result of fread() */
  uzoff_t m;            /* bytes copied so far */

  if ((b = malloc(CBSZ)) == NULL)
    return ZE_MEM;
  m = 0;
  while (n == (uzoff_t)(-1L) || m < n)
  {
    if ((k = fread(b, 1, n == (uzoff_t)(-1) ?
                   CBSZ : (n - m < CBSZ ? (extent)(n - m) : CBSZ), f)) == 0)
    {
      if (ferror(f))
      {
        free((zvoid *)b);
        return ZE_READ;
      }
      else
        break;
    }
    if (fwrite(b, 1, k, g) != k)
    {
      free((zvoid *)b);
      fprintf(mesg," fcopy: write error\n");
      return ZE_TEMP;
    }
    m += k;
  }
  free((zvoid *)b);
  return ZE_OK;
}


/* from zipfile.c */

#ifdef THEOS
 /* Macros cause stack overflow in compiler */
 ush SH(uch* p) { return ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)); }
 ulg LG(uch* p) { return ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16)); }
#else /* !THEOS */
 /* Macros for converting integers in little-endian to machine format */
# define SH(a) ((ush)(((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8)))
# define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
# ifdef ZIP64_SUPPORT           /* zip64 support 08/31/2003 R.Nausedat */
#  define LLG(a) ((zoff_t)LG(a) | ((zoff_t)LG((a)+4) << 32))
# endif
#endif /* ?THEOS */


/* always copies from global in_file to global output file y */
int bfcopy(n)
  /* now use uzoff_t for all file sizes 5/14/05 CS */
  uzoff_t n;               /* number of bytes to copy or -1 for all */
/* Copy n bytes from in_file to out_file, or until EOF if (zoff_t)n == -1.

   Normally we have the compressed size from either the central directory
   entry or the local header.

   If n != -1 and EOF, close current split and open next and continue
   copying.

   If n == -2, copy until find the extended header (data descriptor).  Only
   used for -FF when no size available.

   If fix == 1 calculate CRC of input entry and verify matches.

   If fix == 2 and this entry using data descriptor keep a sliding
   window in the buffer for looking for signature.

   Return an error code in the ZE_ class. */
{
  char *b;              /* malloc'ed buffer for copying */
  extent k;             /* result of fread() */
  uzoff_t m;            /* bytes copied so far */
  extent brd;           /* bytes to read */
  zoff_t data_start = 0;
  zoff_t des_start = 0;
  char *split_path;
  extent kk;
  int i;
  char sbuf[4];         /* buffer for sliding signature window for fix = 2 */
  int des = 0;          /* this entry has data descriptor to find */

  if ((b = malloc(CBSZ)) == NULL)
    return ZE_MEM;

  if (copy_only && !display_globaldots) {
    /* initialize dot count */
    dot_count = -1;
  }

  if (fix == 2 && n == (uzoff_t) -2) {
    data_start = zftello(in_file);
    for (kk = 0; kk < 4; kk++)
      sbuf[kk] = 0;
    des = 1;
  }

  des_good = 0;

  m = 0;
  while (des || n == (uzoff_t)(-1L) || m < n)
  {
    if (des || n == (uzoff_t)(-1))
      brd = CBSZ;
    else
      brd = (n - m < CBSZ ? (extent)(n - m) : CBSZ);

    des_start = zftello(in_file);

    if ((k = fread(b, 1, brd, in_file)) == 0)
    {
      if (fix == 2 && k < brd) {
        free((zvoid *)b);
        return ZE_READ;
      }
      else if (ferror(in_file))
      {
        free((zvoid *)b);
        return ZE_READ;
      }
      else {
        break;
      }
    }


    /* end at extended local header (data descriptor) signature */
    if (des) {
      des_crc = 0;
      des_csize = 0;
      des_usize = 0;

      /* If first 4 bytes in buffer are data descriptor signature then
         try to read the data descriptor.
         If not, scan for signature and break if found, let bfwrite flush
         the data and then next read should put the data descriptor at
         the beginning of the buffer.
       */

      if (
          (b[0] != 0x50 /*'P' except EBCDIC*/ ||
           b[1] != 0x4b /*'K' except EBCDIC*/ ||
           b[2] != '\07' ||
           b[3] != '\010')) {
        /* buffer is not start of data descriptor */

        for (kk = 0; kk < k; kk++) {
          /* add byte to end of sbuf */
          for (i = 0; i < 3; i++)
            sbuf[i] = sbuf[i + 1];
          sbuf[3] = b[kk];

          /* see if this is signature */
          if (
              (sbuf[0] == 0x50 /*'P' except EBCDIC*/ &&
               sbuf[1] == 0x4b /*'K' except EBCDIC*/ &&
               sbuf[2] == '\07' &&
               sbuf[3] == '\010')) {
            kk -= 3;
            if (zfseeko(in_file, bytes_this_split + kk, SEEK_SET) != 0) {
              /* seek error */
              ZIPERR(ZE_READ, "seek failed reading descriptor");
            }
            des_start = zftello(in_file);
            k = kk;
            break;
          }
        }
      }
      else

      /* signature at start of buffer */
      {
        des_good = 0;

#ifdef ZIP64_SUPPORT
        if (zip64_entry) {

          /* read Zip64 data descriptor */
          if (k < 24) {
            /* not enough bytes, so can't be data descriptor
               as data descriptors can't be split across splits
             */
          }
          else
          {
            /* read the Zip64 descriptor */

            des_crc = LG(b + 4);
            des_csize = LLG(b + 8);
            des_usize = LLG(b + 16);

            /* if this is the right data descriptor then the sizes should match */
            if ((uzoff_t)des_start - (uzoff_t)data_start != des_csize) {
              /* apparently this signature does not go with this data so skip */

              /* write out signature as data */
              k = 4;
              if (zfseeko(in_file, des_start + k, SEEK_SET) != 0) {
                /* seek error */
                ZIPERR(ZE_READ, "seek failed reading descriptor");
              }
              if (bfwrite(b, 1, k, BFWRITE_DATA) != k)
              {
                free((zvoid *)b);
                fprintf(mesg," fcopy: write error\n");
                return ZE_TEMP;
              }
              m += k;
              continue;
            }
            else
            {
              /* apparently this is the correct data descriptor */

              /* we should check the CRC but would need to inflate
                 the data */

              /* skip descriptor as will write out later */
              des_good = 1;
              k = 24;
              data_start = zftello(in_file);
              if (zfseeko(in_file, des_start + k, SEEK_SET) != 0) {
                /* seek error */
                ZIPERR(ZE_READ, "seek failed reading descriptor");
              }
              data_start = zftello(in_file);
            }
          }

        }
        else
#endif
        {
          /* read standard data descriptor */

          if (k < 16) {
            /* not enough bytes, so can't be data descriptor
               as data descriptors can't be split across splits
             */
          }
          else
          {
            /* read the descriptor */

            des_crc = LG(b + 4);
            des_csize = LG(b + 8);
            des_usize = LG(b + 12);

            /* if this is the right data descriptor then the sizes should match */
            if ((uzoff_t)des_start - (uzoff_t)data_start != des_csize) {
              /* apparently this signature does not go with this data so skip */

              /* write out signature as data */
              k = 4;
              if (zfseeko(in_file, des_start + k, SEEK_SET) != 0) {
                /* seek error */
                ZIPERR(ZE_READ, "seek failed reading descriptor");
              }
              if (bfwrite(b, 1, k, BFWRITE_DATA) != k)
              {
                free((zvoid *)b);
                fprintf(mesg," fcopy: write error\n");
                return ZE_TEMP;
              }
              m += k;
              continue;
            }
            else
            {
              /* apparently this is the correct data descriptor */

              /* we should check the CRC but this does not work for
                 encrypted data */

              /* skip descriptor as will write out later */
              des_good = 1;
              data_start = zftello(in_file);
              k = 16;
              if (zfseeko(in_file, des_start + k, SEEK_SET) != 0) {
                /* seek error */
                ZIPERR(ZE_READ, "seek failed reading descriptor");
              }
              data_start = zftello(in_file);
            }
          }


        }
      }
    }


    if (des_good) {
      /* skip descriptor as will write out later */
    } else {
      /* write out apparently wrong descriptor as data */
      if (bfwrite(b, 1, k, BFWRITE_DATA) != k)
      {
        free((zvoid *)b);
        fprintf(mesg," fcopy: write error\n");
        return ZE_TEMP;
      }
      m += k;
    }

    if (copy_only && !display_globaldots) {
      if (dot_size > 0) {
        /* initial space */
        if (noisy && dot_count == -1) {
#ifndef WINDLL
          putc(' ', mesg);
          fflush(mesg);
#else
          fprintf(stdout,"%c",' ');
#endif
          dot_count++;
        }
        dot_count += k;
        if (dot_size <= dot_count) dot_count = 0;
      }
      if ((verbose || noisy) && dot_size && !dot_count) {
#ifndef WINDLL
        putc('.', mesg);
        fflush(mesg);
#else
        fprintf(stdout,"%c",'.');
#endif
        mesg_line_started = 1;
      }
    }

    if (des_good)
      break;

    if (des)
      continue;

    if ((des || n != (uzoff_t)(-1L)) && m < n && feof(in_file)) {
      /* open next split */
      current_in_disk++;

      if (current_in_disk >= total_disks) {
        /* done */
        break;

      } else if (current_in_disk == total_disks - 1) {
        /* last disk is archive.zip */
        if ((split_path = malloc(strlen(in_path) + 1)) == NULL) {
          zipwarn("reading archive: ", in_path);
          return ZE_MEM;
        }
        strcpy(split_path, in_path);
      } else {
        /* other disks are archive.z01, archive.z02, ... */
        split_path = get_in_split_path(in_path, current_in_disk);
      }

      fclose(in_file);

      /* open the split */
      while ((in_file = zfopen(split_path, FOPR)) == NULL) {
        int r = 0;

        /* could not open split */

        if (fix == 1 && skip_this_disk) {
          free(split_path);
          free((zvoid *)b);
          return ZE_FORM;
        }

        /* Ask for directory with split.  Updates in_path */
        r = ask_for_split_read_path(current_in_disk);
        if (r == ZE_ABORT) {
          zipwarn("could not find split: ", split_path);
          free(split_path);
          free((zvoid *)b);
          return ZE_ABORT;
        }
        if (r == ZE_EOF) {
          zipmessage_nl("", 1);
          zipwarn("user ended reading - closing archive", "");
          free(split_path);
          free((zvoid *)b);
          return ZE_EOF;
        }
        if (fix == 2 && skip_this_disk) {
          /* user asked to skip this disk */
          zipwarn("skipping split file: ", split_path);
          current_in_disk++;
        }

        if (current_in_disk == total_disks - 1) {
          /* last disk is archive.zip */
          if ((split_path = malloc(strlen(in_path) + 1)) == NULL) {
            zipwarn("reading archive: ", in_path);
            return ZE_MEM;
          }
          strcpy(split_path, in_path);
        } else {
          /* other disks are archive.z01, archive.z02, ... */
          split_path = get_in_split_path(zipfile, current_in_disk);
        }
      }
      if (fix == 2 && skip_this_disk) {
        /* user asked to skip this disk */
        free(split_path);
        free((zvoid *)b);
        return ZE_FORM;
      }
      free(split_path);
    }
  }
  free((zvoid *)b);
  return ZE_OK;
}



#ifdef NO_RENAME
int rename(from, to)
ZCONST char *from;
ZCONST char *to;
{
    unlink(to);
    if (link(from, to) == -1)
        return -1;
    if (unlink(from) == -1)
        return -1;
    return 0;
}

#endif /* NO_RENAME */


#ifdef ZMEM

/************************/
/*  Function memset()   */
/************************/

/*
 * memset - for systems without it
 *  bill davidsen - March 1990
 */

char *
memset(buf, init, len)
register char *buf;     /* buffer loc */
register int init;      /* initializer */
register unsigned int len;   /* length of the buffer */
{
    char *start;

    start = buf;
    while (len--) *(buf++) = init;
    return(start);
}


/************************/
/*  Function memcpy()   */
/************************/

char *
memcpy(dst,src,len)             /* v2.0f */
register char *dst, *src;
register unsigned int len;
{
    char *start;

    start = dst;
    while (len--)
        *dst++ = *src++;
    return(start);
}


/************************/
/*  Function memcmp()   */
/************************/

int
memcmp(b1,b2,len)                     /* jpd@usl.edu -- 11/16/90 */
register char *b1, *b2;
register unsigned int len;
{

    if (len) do {       /* examine each byte (if any) */
      if (*b1++ != *b2++)
        return (*((uch *)b1-1) - *((uch *)b2-1));  /* exit when miscompare */
    } while (--len);

    return(0);          /* no miscompares, yield 0 result */
}

#endif  /* ZMEM */


/*------------------------------------------------------------------
 * Split archives
 */


/* ask_for_split_read_path
 *
 * If the next split file is not in the current directory, ask
 * the user where it is.
 *
 * in_path is the base path for reading splits and is usually
 * the same as zipfile.  The path in in_path must be the archive
 * file ending in .zip as this is assumed by get_in_split_path().
 *
 * Updates in_path if changed.  Returns ZE_OK if OK or ZE_ABORT if
 * user cancels reading archive.
 *
 * If fix = 1 then allow skipping disk (user may not have it).
 */

#define SPLIT_MAXPATH (FNMAX + 4010)

int ask_for_split_read_path(current_disk)
  ulg current_disk;
{
  FILE *f;
  int is_readable = 0;
  int i;
  char *split_dir = NULL;
  char *archive_name = NULL;
  char *split_name = NULL;
  char *split_path = NULL;
  char buf[SPLIT_MAXPATH + 100];

  /* get split path */
  split_path = get_in_split_path(in_path, current_disk);

  /* get the directory */
  if ((split_dir = malloc(strlen(in_path) + 40)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  strcpy(split_dir, in_path);

  /* remove any name at end */
  for (i = strlen(split_dir) - 1; i >= 0; i--) {
    if (split_dir[i] == '/' || split_dir[i] == '\\'
          || split_dir[i] == ':') {
      split_dir[i + 1] = '\0';
      break;
    }
  }
  if (i < 0)
    split_dir[0] = '\0';

  /* get the name of the archive */
  if ((archive_name = malloc(strlen(in_path) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  if (strlen(in_path) == strlen(split_dir)) {
    archive_name[0] = '\0';
  } else {
    strcpy(archive_name, in_path + strlen(split_dir));
  }

  /* get the name of the split */
  if ((split_name = malloc(strlen(split_path) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  if (strlen(in_path) == strlen(split_dir)) {
    split_name[0] = '\0';
  } else {
    strcpy(split_name, split_path + strlen(split_dir));
  }
  if (i < 0) {
    strcpy(split_dir, "(current directory)");
  }

  fprintf(mesg, "\n\nCould not find:\n");
  fprintf(mesg, "  %s\n", split_path);
  /*
  fprintf(mesg, "Please enter the path directory (. for cur dir) where\n");
  fprintf(mesg, "  %s\n", split_name);
  fprintf(mesg, "is located\n");
  */
  for (;;) {
    if (is_readable) {
      fprintf(mesg, "\nHit c      (change path to where this split file is)");
      fprintf(mesg, "\n    q      (abort archive - quit)");
      fprintf(mesg, "\n or ENTER  (continue with this split): ");
    } else {
      if (fix == 1) {
        fprintf(mesg, "\nHit c      (change path to where this split file is)");
        fprintf(mesg, "\n    s      (skip this split)");
        fprintf(mesg, "\n    q      (abort archive - quit)");
        fprintf(mesg, "\n or ENTER  (try reading this split again): ");
      } else if (fix == 2) {
        fprintf(mesg, "\nHit c      (change path to where this split file is)");
        fprintf(mesg, "\n    s      (skip this split)");
        fprintf(mesg, "\n    q      (abort archive - quit)");
        fprintf(mesg, "\n    e      (end this archive - no more splits)");
        fprintf(mesg, "\n    z      (look for .zip split - the last split)");
        fprintf(mesg, "\n or ENTER  (try reading this split again): ");
      } else {
        fprintf(mesg, "\nHit c      (change path to where this split file is)");
        fprintf(mesg, "\n    q      (abort archive - quit)");
        fprintf(mesg, "\n or ENTER  (try reading this split again): ");
      }
    }
    fflush(mesg);
    fgets(buf, SPLIT_MAXPATH, stdin);
    /* remove any newline */
    for (i = 0; buf[i]; i++) {
      if (buf[i] == '\n') {
        buf[i] = '\0';
        break;
      }
    }
    if (toupper(buf[0]) == 'Q') {
      return ZE_ABORT;
    } else if ((fix == 1 || fix == 2) && toupper(buf[0]) == 'S') {
    /*
      fprintf(mesg, "\nSkip this split/disk?  (files in this split will not be recovered) [n/y] ");
      fflush(mesg);
      fgets(buf, SPLIT_MAXPATH, stdin);
      if (buf[0] == 'y' || buf[0] == 'Y') {
    */
      skip_this_disk = current_in_disk + 1;
      return ZE_FORM;
    } else if (toupper(buf[0]) == 'C') {
      fprintf(mesg, "\nEnter path where this split is (ENTER = same dir, . = current dir)");
      fprintf(mesg, "\n: ");
      fflush(mesg);
      fgets(buf, SPLIT_MAXPATH, stdin);
      is_readable = 0;
      /* remove any newline */
      for (i = 0; buf[i]; i++) {
        if (buf[i] == '\n') {
          buf[i] = '\0';
          break;
        }
      }
      if (buf[0] == '\0') {
        /* Hit ENTER so try old path again - could be removable media was changed */
        strcpy(buf, split_path);
      }
    } else if (fix == 2 && toupper(buf[0]) == 'E') {
      /* no more splits to read */
      return ZE_EOF;
    } else if (fix == 2 && toupper(buf[0]) == 'Z') {
      total_disks = current_disk + 1;
      free(split_path);
      split_path = get_in_split_path(in_path, current_disk);
      buf[0] = '\0';
      strncat(buf, split_path, SPLIT_MAXPATH);
    }
    if (strlen(buf) > 0) {
      /* changing path */

      /* check if user wants current directory */
      if (buf[0] == '.' && buf[1] == '\0') {
        buf[0] = '\0';
      }
      /* remove any name at end */
      for (i = strlen(buf); i >= 0; i--) {
        if (buf[i] == '/' || buf[i] == '\\'
             || buf[i] == ':') {
          buf[i + 1] = '\0';
          break;
        }
      }
      /* update base_path to newdir/split_name - in_path is the .zip file path */
      free(in_path);
      if (i < 0) {
        /* just name so current directory */
        strcpy(buf, "(current directory)");
        if (archive_name == NULL) {
          i = 0;
        } else {
          i = strlen(archive_name);
        }
        if ((in_path = malloc(strlen(archive_name) + 40)) == NULL) {
          ZIPERR(ZE_MEM, "split path");
        }
        strcpy(in_path, archive_name);
      } else {
        /* not the current directory */
        /* remove any name at end */
        for (i = strlen(buf); i >= 0; i--) {
          if (buf[i] == '/') {
            buf[i + 1] = '\0';
            break;
          }
        }
        if (i < 0) {
          buf[0] = '\0';
        }
        if ((in_path = malloc(strlen(buf) + strlen(archive_name) + 40)) == NULL) {
          ZIPERR(ZE_MEM, "split path");
        }
        strcpy(in_path, buf);
        strcat(in_path, archive_name);
      }

      free(split_path);

      /* get split path */
      split_path = get_in_split_path(in_path, current_disk);

      free(split_dir);
      if ((split_dir = malloc(strlen(in_path) + 40)) == NULL) {
        ZIPERR(ZE_MEM, "split path");
      }
      strcpy(split_dir, in_path);
      /* remove any name at end */
      for (i = strlen(split_dir); i >= 0; i--) {
        if (split_dir[i] == '/') {
          split_dir[i + 1] = '\0';
          break;
        }
      }

      /* try to open it */
      if ((f = fopen(split_path, "r")) == NULL) {
        fprintf(mesg, "\nCould not find or open\n");
        fprintf(mesg, "  %s\n", split_path);
        /*
        fprintf(mesg, "Please enter the path (. for cur dir) where\n");
        fprintf(mesg, "  %s\n", split_name);
        fprintf(mesg, "is located\n");
        */
        continue;
      }
      fclose(f);
      is_readable = 1;
      fprintf(mesg, "Found:  %s\n", split_path);
    } else {
      /* try to open it */
      if ((f = fopen(split_path, "r")) == NULL) {
        fprintf(mesg, "\nCould not find or open\n");
        fprintf(mesg, "  %s\n", split_path);
        /*
        fprintf(mesg, "Please enter the path (. for cur dir) where\n");
        fprintf(mesg, "  %s\n", split_name);
        fprintf(mesg, "is located\n");
        */
        continue;
      }
      fclose(f);
      is_readable = 1;
      fprintf(mesg, "\nFound:  %s\n", split_path);
      break;
    }
  }
  free(archive_name);
  free(split_dir);
  free(split_name);

  return ZE_OK;
}


/* ask_for_split_write_path
 *
 * Verify the directory for the next split.  Called
 * when -sp is used to pause between writing splits.
 *
 * Updates out_path and return 1 if OK or 0 if cancel
 */
int ask_for_split_write_path(current_disk)
  ulg current_disk;
{
  unsigned int num = (unsigned int)current_disk + 1;
  int i;
  char *split_dir = NULL;
  char *split_name = NULL;
  char buf[FNMAX + 40];

  /* get the directory */
  if ((split_dir = malloc(strlen(out_path) + 40)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  strcpy(split_dir, out_path);

  /* remove any name at end */
  for (i = strlen(split_dir); i >= 0; i--) {
    if (split_dir[i] == '/' || split_dir[i] == '\\'
          || split_dir[i] == ':') {
      split_dir[i + 1] = '\0';
      break;
    }
  }

  /* get the name of the split */
  if ((split_name = malloc(strlen(out_path) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  if (strlen(out_path) == strlen(split_dir)) {
    split_name[0] = '\0';
  } else {
    strcpy(split_name, out_path + strlen(split_dir));
  }
  if (i < 0) {
    strcpy(split_dir, "(current directory)");
  }
  if (mesg_line_started)
    fprintf(mesg, "\n");
  fprintf(mesg, "\nOpening disk %d\n", num);
  fprintf(mesg, "Hit ENTER to write to default path of\n");
  fprintf(mesg, "  %s\n", split_dir);
  fprintf(mesg, "or enter a new directory path (. for cur dir) and hit ENTER\n");
  for (;;) {
    fprintf(mesg, "\nPath (or hit ENTER to continue): ");
    fflush(mesg);
    fgets(buf, FNMAX, stdin);
    /* remove any newline */
    for (i = 0; buf[i]; i++) {
      if (buf[i] == '\n') {
        buf[i] = '\0';
        break;
      }
    }
    if (strlen(buf) > 0) {
      /* changing path */

      /* current directory */
      if (buf[0] == '.' && buf[1] == '\0') {
        buf[0] = '\0';
      }
      /* remove any name at end */
      for (i = strlen(buf); i >= 0; i--) {
        if (buf[i] == '/' || buf[i] == '\\'
             || buf[i] == ':') {
          buf[i + 1] = '\0';
          break;
        }
      }
      /* update out_path to newdir/split_name */
      free(out_path);
      if (i < 0) {
        /* just name so current directory */
        strcpy(buf, "(current directory)");
        if (split_name == NULL) {
          i = 0;
        } else {
          i = strlen(split_name);
        }
        if ((out_path = malloc(strlen(split_name) + 40)) == NULL) {
          ZIPERR(ZE_MEM, "split path");
        }
        strcpy(out_path, split_name);
      } else {
        /* not the current directory */
        /* remove any name at end */
        for (i = strlen(buf); i >= 0; i--) {
          if (buf[i] == '/') {
            buf[i + 1] = '\0';
            break;
          }
        }
        if (i < 0) {
          buf[0] = '\0';
        }
        if ((out_path = malloc(strlen(buf) + strlen(split_name) + 40)) == NULL) {
          ZIPERR(ZE_MEM, "split path");
        }
        strcpy(out_path, buf);
        strcat(out_path, split_name);
      }
      fprintf(mesg, "Writing to:\n  %s\n", buf);
      free(split_name);
      free(split_dir);
      if ((split_dir = malloc(strlen(out_path) + 40)) == NULL) {
        ZIPERR(ZE_MEM, "split path");
      }
      strcpy(split_dir, out_path);
      /* remove any name at end */
      for (i = strlen(split_dir); i >= 0; i--) {
        if (split_dir[i] == '/') {
          split_dir[i + 1] = '\0';
          break;
        }
      }
      if ((split_name = malloc(strlen(out_path) + 1)) == NULL) {
        ZIPERR(ZE_MEM, "split path");
      }
      strcpy(split_name, out_path + strlen(split_dir));
    } else {
      break;
    }
  }
  free(split_dir);
  free(split_name);

  /* for now no way out except Ctrl C */
  return 1;
}


/* split_name
 *
 * get name of split being read
 */
char *get_in_split_path(base_path, disk_number)
  char *base_path;
  ulg disk_number;
{
  char *split_path = NULL;
  int base_len = 0;
  int path_len = 0;
  ulg num = disk_number + 1;
  char ext[6];
#ifdef VMS
  int vers_len;                         /* File version length. */
  char *vers_ptr;                       /* File version string. */
#endif /* def VMS */

  /*
   * A split has extension z01, z02, ..., z99, z100, z101, ... z999
   * We currently support up to .z99999
   * WinZip will also read .100, .101, ... but AppNote 6.2.2 uses above
   * so use that.  Means on DOS can only have 100 splits.
   */

  if (num == total_disks) {
    /* last disk is base path */
    if ((split_path = malloc(strlen(base_path) + 1)) == NULL) {
      ZIPERR(ZE_MEM, "base path");
    }
    strcpy(split_path, base_path);

    return split_path;
  } else {
    if (num > 99999) {
      ZIPERR(ZE_BIG, "More than 99999 splits needed");
    }
    sprintf(ext, "z%02lu", num);
  }

  /* create path for this split - zip.c checked for .zip extension */
  base_len = strlen(base_path) - 3;
  path_len = base_len + strlen(ext);

#ifdef VMS
  /* On VMS, locate the file version, and adjust base_len accordingly.
     Note that path_len is correct, as-is.
  */
  vers_ptr = vms_file_version( base_path);
  vers_len = strlen( vers_ptr);
  base_len -= vers_len;
#endif /* def VMS */

  if ((split_path = malloc(path_len + 1)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  /* copy base_path except for end zip */
  strcpy(split_path, base_path);
  split_path[base_len] = '\0';
  /* add extension */
  strcat(split_path, ext);

#ifdef VMS
  /* On VMS, append (preserve) the file version. */
  strcat(split_path, vers_ptr);
#endif /* def VMS */

  return split_path;
}


/* split_name
 *
 * get name of split being written
 */
char *get_out_split_path(base_path, disk_number)
  char *base_path;
  ulg disk_number;
{
  char *split_path = NULL;
  int base_len = 0;
  int path_len = 0;
  ulg num = disk_number + 1;
  char ext[6];
#ifdef VMS
  int vers_len;                         /* File version length. */
  char *vers_ptr;                       /* File version string. */
#endif /* def VMS */

  /*
   * A split has extension z01, z02, ..., z99, z100, z101, ... z999
   * We currently support up to .z99999
   * WinZip will also read .100, .101, ... but AppNote 6.2.2 uses above
   * so use that.  Means on DOS can only have 100 splits.
   */

  if (num > 99999) {
    ZIPERR(ZE_BIG, "More than 99999 splits needed");
  }
  sprintf(ext, "z%02lu", num);

  /* create path for this split - zip.c checked for .zip extension */
  base_len = strlen(base_path) - 3;
  path_len = base_len + strlen(ext);

#ifdef VMS
  /* On VMS, locate the file version, and adjust base_len accordingly.
     Note that path_len is correct, as-is.
  */
  vers_ptr = vms_file_version( base_path);
  vers_len = strlen( vers_ptr);
  base_len -= vers_len;
#endif /* def VMS */

  if ((split_path = malloc(path_len + 1)) == NULL) {
    ZIPERR(ZE_MEM, "split path");
  }
  /* copy base_path except for end zip */
  strcpy(split_path, base_path);
  split_path[base_len] = '\0';
  /* add extension */
  strcat(split_path, ext);

#ifdef VMS
  /* On VMS, append (preserve) the file version. */
  strcat(split_path, vers_ptr);
#endif /* def VMS */

  return split_path;
}

/* close_split
 *
 * close a split - assume that the paths needed for the splits are
 * available.
 */
int close_split(disk_number, tempfile, temp_name)
  ulg disk_number;
  FILE *tempfile;
  char *temp_name;
{
  char *split_path = NULL;

  split_path = get_out_split_path(out_path, disk_number);

  if (noisy_splits) {
    zipmessage("\tClosing split ", split_path);
  }

  fclose(tempfile);

  rename_split(temp_name, split_path);
  set_filetype(split_path);

  return ZE_OK;
}

/* bfwrite
   Does the fwrite but also counts bytes and does splits */
size_t bfwrite(buffer, size, count, mode)
  ZCONST void *buffer;
  size_t size;
  size_t count;
  int mode;
{
  size_t bytes_written = 0;
  size_t r;
  size_t b = size * count;
  uzoff_t bytes_left_in_split = 0;
  size_t bytes_to_write = b;


  /* -------------------------------- */
  /* local header */
  if (mode == BFWRITE_LOCALHEADER) {
    /* writing local header - reset entry data count */
    bytes_this_entry = 0;
    /* save start of local header so we can rewrite later */
    current_local_file = y;
    current_local_disk = current_disk;
    current_local_offset = bytes_this_split;
  }

  if (split_size == 0)
    bytes_left_in_split = bytes_to_write;
  else
    bytes_left_in_split = split_size - bytes_this_split;

  if (bytes_to_write > bytes_left_in_split) {
    if (mode == BFWRITE_HEADER ||
        mode == BFWRITE_LOCALHEADER ||
        mode == BFWRITE_CENTRALHEADER) {
      /* if can't write entire header save for next split */
      bytes_to_write = 0;
    } else {
      /* normal data so fill the split */
      bytes_to_write = (size_t)bytes_left_in_split;
    }
  }

  /* -------------------------------- */
  /* central header */
  if (mode == BFWRITE_CENTRALHEADER) {
    /* set start disk for CD */
    if (cd_start_disk == (ulg)-1) {
      cd_start_disk = current_disk;
      cd_start_offset = bytes_this_split;
    }
    cd_entries_this_disk++;
    total_cd_entries++;
  }

  /* -------------------------------- */
  if (bytes_to_write > 0) {
    /* write out the bytes for this split */
    r = fwrite(buffer, size, bytes_to_write, y);
    bytes_written += r;
    bytes_to_write = b - r;
    bytes_this_split += r;
    if (mode == BFWRITE_DATA)
      /* if data descriptor do not include in count */
      bytes_this_entry += r;
  } else {
    bytes_to_write = b;
  }

  if (bytes_to_write > 0) {
    if (split_method) {
      /* still bytes to write so close split and open next split */
      bytes_prev_splits += bytes_this_split;

      if (split_method == 1 && ferror(y)) {
        /* if writing all splits to same place and have problem then bad */
        ZIPERR(ZE_WRITE, "Could not write split");
      }

      if (split_method == 2 && ferror(y)) {
        /* A split must be at least 64K except last .zip split */
        if (bytes_this_split < 64 * (uzoff_t)0x400) {
          ZIPERR(ZE_WRITE, "Not enough space to write split");
        }
      }

      /* close this split */
      if (split_method == 1 && current_local_disk == current_disk) {
        /* keep split open so can update it */
        current_local_tempname = tempzip;
      } else {
        /* close split */
        close_split(current_disk, y, tempzip);
        y = NULL;
        free(tempzip);
        tempzip = NULL;
      }
      cd_entries_this_disk = 0;
      bytes_this_split = 0;

      /* increment disk - disks are numbered 0, 1, 2, ... and
         splits are 01, 02, ... */
      current_disk++;

      if (split_method == 2 && split_bell) {
        /* bell when pause to ask for next split */
        putc('\007', mesg);
        fflush(mesg);
      }

      for (;;) {
        /* if method 2 pause and allow changing path */
        if (split_method == 2) {
          if (ask_for_split_write_path(current_disk) == 0) {
            ZIPERR(ZE_ABORT, "could not write split");
          }
        }

        /* open next split */
#if defined(UNIX) && !defined(NO_MKSTEMP)
        {
          int yd;
          int i;

          /* use mkstemp to avoid race condition and compiler warning */

          if (tempath != NULL)
          {
            /* if -b used to set temp file dir use that for split temp */
            if ((tempzip = malloc(strlen(tempath) + 12)) == NULL) {
              ZIPERR(ZE_MEM, "allocating temp filename");
            }
            strcpy(tempzip, tempath);
            if (lastchar(tempzip) != '/')
              strcat(tempzip, "/");
          }
          else
          {
            /* create path by stripping name and appending template */
            if ((tempzip = malloc(strlen(zipfile) + 12)) == NULL) {
            ZIPERR(ZE_MEM, "allocating temp filename");
            }
            strcpy(tempzip, zipfile);
            for(i = strlen(tempzip); i > 0; i--) {
              if (tempzip[i - 1] == '/')
                break;
            }
            tempzip[i] = '\0';
          }
          strcat(tempzip, "ziXXXXXX");

          if ((yd = mkstemp(tempzip)) == EOF) {
            ZIPERR(ZE_TEMP, tempzip);
          }
          if ((y = fdopen(yd, FOPW_TMP)) == NULL) {
            ZIPERR(ZE_TEMP, tempzip);
          }
        }
#else
        if ((tempzip = tempname(zipfile)) == NULL) {
          ZIPERR(ZE_MEM, "allocating temp filename");
        }
        if ((y = zfopen(tempzip, FOPW_TMP)) == NULL) {
          ZIPERR(ZE_TEMP, tempzip);
        }
#endif

        r = fwrite((char *)buffer + bytes_written, 1, bytes_to_write, y);
        bytes_written += r;
        bytes_this_split += r;
        if (!(mode == BFWRITE_HEADER ||
              mode == BFWRITE_LOCALHEADER ||
              mode == BFWRITE_CENTRALHEADER)) {
          bytes_this_entry += r;
        }
        if (bytes_to_write > r) {
          /* buffer bigger than split */
          if (split_method == 2) {
            /* let user choose another disk */
            zipwarn("Not enough room on disk", "");
            continue;
          } else {
            ZIPERR(ZE_WRITE, "Not enough room on disk");
          }
        }
        if (mode == BFWRITE_LOCALHEADER ||
            mode == BFWRITE_HEADER ||
            mode == BFWRITE_CENTRALHEADER) {
          if (split_method == 1 && current_local_file &&
              current_local_disk != current_disk) {
            /* We're opening a new split because the next header
               did not fit on the last split.  We need to now close
               the last split and update the pointers for
               the current split. */
            close_split(current_local_disk, current_local_file,
                        current_local_tempname);
            free(current_local_tempname);
          }
          current_local_tempname = tempzip;
          current_local_file = y;
          current_local_offset = 0;
          current_local_disk = current_disk;
        }
        break;
      }
    }
    else
    {
      /* likely have more than fits but no splits */

      /* probably already have error "no space left on device" */
      /* could let flush_outbuf() handle error but bfwrite() is called for
         headers also */
      if (ferror(y))
        ziperr(ZE_WRITE, "write error on zip file");
    }
  }


  /* display dots for archive instead of for each file */
  if (display_globaldots) {
    if (dot_size > 0) {
      /* initial space */
      if (dot_count == -1) {
#ifndef WINDLL
        putc(' ', mesg);
        fflush(mesg);
#else
        fprintf(stdout,"%c",' ');
#endif
        /* assume a header will be written first, so avoid 0 */
        dot_count = 1;
      }
      /* skip incrementing dot count for small buffers like for headers */
      if (size * count > 1000) {
        dot_count++;
        if (dot_size <= dot_count * (zoff_t)size * (zoff_t)count) dot_count = 0;
      }
    }
    if (dot_size && !dot_count) {
      dot_count++;
#ifndef WINDLL
      putc('.', mesg);
      fflush(mesg);
#else
      fprintf(stdout,"%c",'.');
#endif
      mesg_line_started = 1;
    }
  }


  return bytes_written;
}


#ifdef UNICODE_SUPPORT

/*---------------------------------------------
 * Unicode conversion functions
 *
 * Provided by Paul Kienitz
 *
 * Some modifications to work with Zip
 *
 *---------------------------------------------
 */

/*
   NOTES APPLICABLE TO ALL STRING FUNCTIONS:

   All of the x_to_y functions take parameters for an output buffer and
   its available length, and return an int.  The value returned is the
   length of the string that the input produces, which may be larger than
   the provided buffer length.  If the returned value is less than the
   buffer length, then the contents of the buffer will be null-terminated;
   otherwise, it will not be terminated and may be invalid, possibly
   stopping in the middle of a multibyte sequence.

   In all cases you may pass NULL as the buffer and/or 0 as the length, if
   you just want to learn how much space the string is going to require.

   The functions will return -1 if the input is invalid UTF-8 or cannot be
   encoded as UTF-8.
*/

/* utility functions for managing UTF-8 and UCS-4 strings */


/* utf8_char_bytes
 *
 * Returns the number of bytes used by the first character in a UTF-8
 * string, or -1 if the UTF-8 is invalid or null.
 */
local int utf8_char_bytes(utf8)
  ZCONST char *utf8;
{
  int      t, r;
  unsigned lead;

  if (!utf8)
    return -1;          /* no input */
  lead = (unsigned char) *utf8;
  if (lead < 0x80)
    r = 1;              /* an ascii-7 character */
  else if (lead < 0xC0)
    return -1;          /* error: trailing byte without lead byte */
  else if (lead < 0xE0)
    r = 2;              /* an 11 bit character */
  else if (lead < 0xF0)
    r = 3;              /* a 16 bit character */
  else if (lead < 0xF8)
    r = 4;              /* a 21 bit character (the most currently used) */
  else if (lead < 0xFC)
    r = 5;              /* a 26 bit character (shouldn't happen) */
  else if (lead < 0xFE)
    r = 6;              /* a 31 bit character (shouldn't happen) */
  else
    return -1;          /* error: invalid lead byte */
  for (t = 1; t < r; t++)
    if ((unsigned char) utf8[t] < 0x80 || (unsigned char) utf8[t] >= 0xC0)
      return -1;        /* error: not enough valid trailing bytes */
  return r;
}


/* ucs4_char_from_utf8
 *
 * Given a reference to a pointer into a UTF-8 string, returns the next
 * UCS-4 character and advances the pointer to the next character sequence.
 * Returns ~0 and does not advance the pointer when input is ill-formed.
 *
 * Since the Unicode standard says 32-bit values won't be used (just
 * up to the current 21-bit mappings) changed this to signed to allow -1 to
 * be returned.
 */
long ucs4_char_from_utf8(utf8)
  ZCONST char **utf8;
{
  ulg  ret;
  int  t, bytes;

  if (!utf8)
    return -1;                          /* no input */
  bytes = utf8_char_bytes(*utf8);
  if (bytes <= 0)
    return -1;                          /* invalid input */
  if (bytes == 1)
    ret = **utf8;                       /* ascii-7 */
  else
    ret = **utf8 & (0x7F >> bytes);     /* lead byte of a multibyte sequence */
  (*utf8)++;
  for (t = 1; t < bytes; t++)           /* consume trailing bytes */
    ret = (ret << 6) | (*((*utf8)++) & 0x3F);
  return (long) ret;
}


/* utf8_from_ucs4_char - Convert UCS char to UTF-8
 *
 * Returns the number of bytes put into utf8buf to represent ch, from 1 to 6,
 * or -1 if ch is too large to represent.  utf8buf must have room for 6 bytes.
 */
local int utf8_from_ucs4_char(utf8buf, ch)
  char *utf8buf;
  ulg ch;
{
  int trailing = 0;
  int leadmask = 0x80;
  int leadbits = 0x3F;
  ulg tch = ch;
  int ret;

  if (ch > 0x7FFFFFFF)
    return -1;                /* UTF-8 can represent 31 bits */
  if (ch < 0x7F)
  {
    *utf8buf++ = (char) ch;   /* ascii-7 */
    return 1;
  }
  do {
    trailing++;
    leadmask = (leadmask >> 1) | 0x80;
    leadbits >>= 1;
    tch >>= 6;
  } while (tch & ~leadbits);
  ret = trailing + 1;
  /* produce lead byte */
  *utf8buf++ = (char) (leadmask | (ch >> (6 * trailing)));
  /* produce trailing bytes */
  while (--trailing >= 0)
    *utf8buf++ = (char) (0x80 | ((ch >> (6 * trailing)) & 0x3F));
  return ret;
}


/*===================================================================*/

/* utf8_to_ucs4_string - convert UTF-8 string to UCS string
 *
 * Return UCS count.  Now returns int so can return -1.
 */
local int utf8_to_ucs4_string(utf8, ucs4buf, buflen)
  ZCONST char *utf8;
  ulg *ucs4buf;
  int buflen;
{
  int count = 0;

  for (;;)
  {
    long ch = ucs4_char_from_utf8(&utf8);
    if (ch == -1)
      return -1;
    else
    {
      if (ucs4buf && count < buflen)
        ucs4buf[count] = ch;
      if (ch == 0)
        return count;
      count++;
    }
  }
}


/* ucs4_string_to_utf8
 *
 *
 */
local int ucs4_string_to_utf8(ucs4, utf8buf, buflen)
  ZCONST ulg *ucs4;
  char *utf8buf;
  int buflen;
{
  char mb[6];
  int  count = 0;

  if (!ucs4)
    return -1;
  for (;;)
  {
    int mbl = utf8_from_ucs4_char(mb, *ucs4++);
    int c;
    if (mbl <= 0)
      return -1;
    /* We could optimize this a bit by passing utf8buf + count */
    /* directly to utf8_from_ucs4_char when buflen >= count + 6... */
    c = buflen - count;
    if (mbl < c)
      c = mbl;
    if (utf8buf && count < buflen)
      strncpy(utf8buf + count, mb, c);
    if (mbl == 1 && !mb[0])
      return count;           /* terminating nul */
    count += mbl;
  }
}


#if 0  /* currently unused */
/* utf8_chars
 *
 * Wrapper: counts the actual unicode characters in a UTF-8 string.
 */
local int utf8_chars(utf8)
  ZCONST char *utf8;
{
  return utf8_to_ucs4_string(utf8, NULL, 0);
}
#endif


/* --------------------------------------------------- */
/* Unicode Support
 *
 * These functions common for all Unicode ports.
 *
 * These functions should allocate and return strings that can be
 * freed with free().
 *
 * 8/27/05 EG
 *
 * Use zwchar for wide char which is unsigned long
 * in zip.h and 32 bits.  This avoids problems with
 * different sizes of wchar_t.
 */

#ifdef WIN32

zwchar *wchar_to_wide_string(wchar_string)
  wchar_t *wchar_string;
{
  int i;
  int wchar_len;
  zwchar *wide_string;

  wchar_len = wcslen(wchar_string);

  if ((wide_string = malloc((wchar_len + 1) * sizeof(zwchar))) == NULL) {
    ZIPERR(ZE_MEM, "wchar to wide conversion");
  }
  for (i = 0; i <= wchar_len; i++) {
    wide_string[i] = wchar_string[i];
  }

  return wide_string;
}

/* is_ascii_stringw
 * Checks if a wide string is all ascii
 */
int is_ascii_stringw(wstring)
  wchar_t *wstring;
{
  wchar_t *pw;
  wchar_t cw;

  if (wstring == NULL)
    return 0;

  for (pw = wstring; (cw = *pw) != '\0'; pw++) {
    if (cw > 0x7F) {
      return 0;
    }
  }
  return 1;
}

#endif

/* is_ascii_string
 * Checks if a string is all ascii
 */
int is_ascii_string(mbstring)
  char *mbstring;
{
  char *p;
  uch c;

  if (mbstring == NULL)
    return 0;

  for (p = mbstring; (c = (uch)*p) != '\0'; p++) {
    if (c > 0x7F) {
      return 0;
    }
  }
  return 1;
}

/* local to UTF-8 */
char *local_to_utf8_string(local_string)
  char *local_string;
{
  zwchar *wide_string = local_to_wide_string(local_string);
  char *utf8_string = wide_to_utf8_string(wide_string);

  free(wide_string);
  return utf8_string;
}

/* wide_char_to_escape_string
   provides a string that represents a wide char not in local char set

   An initial try at an algorithm.  Suggestions welcome.

   If not an ASCII char, probably need 2 bytes at least.  So if
   a 2-byte wide encode it as 4 hex digits with a leading #U.
   Since the Unicode standard has been frozen, it looks like 3 bytes
   should be enough for any large Unicode character.  In these cases
   prefix the string with #L.
   So
   #U1234
   is a 2-byte wide character with bytes 0x12 and 0x34 while
   #L123456
   is a 3-byte wide with bytes 0x12, 0x34, and 0x56.
   On Windows, wide that need two wide characters as a surrogate pair
   to represent them need to be converted to a single number.
  */

 /* set this to the max bytes an escape can be */
#define MAX_ESCAPE_BYTES 8

char *wide_char_to_escape_string(wide_char)
  zwchar wide_char;
{
  int i;
  zwchar w = wide_char;
  uch b[9];
  char e[7];
  int len;
  char *r;

  /* fill byte array with zeros */
  for (len = 0; len < sizeof(zwchar); len++) {
    b[len] = 0;
  }
  /* get bytes in right to left order */
  for (len = 0; w; len++) {
    b[len] = (char)(w % 0x100);
    w /= 0x100;
  }

  if ((r = malloc(MAX_ESCAPE_BYTES + 8)) == NULL) {
    ZIPERR(ZE_MEM, "wide_char_to_escape_string");
  }
  strcpy(r, "#");
  /* either 2 bytes or 4 bytes */
  if (len < 3) {
    len = 2;
    strcat(r, "U");
  } else {
    len = 3;
    strcat(r, "L");
  }
  for (i = len - 1; i >= 0; i--) {
    sprintf(e, "%02x", b[i]);
    strcat(r, e);
  }
  return r;
}

#if 0
/* returns the wide character represented by the escape string */
zwchar escape_string_to_wide(escape_string)
  char *escape_string;
{
  int i;
  zwchar w;
  char c;
  char u;
  int len;
  char *e = escape_string;

  if (e == NULL) {
    return 0;
  }
  if (e[0] != '#') {
    /* no leading # */
    return 0;
  }
  len = strlen(e);
  /* either #U1234 or #L123456 format */
  if (len != 6 && len != 8) {
    return 0;
  }
  w = 0;
  if (e[1] == 'L') {
    if (len != 8) {
      return 0;
    }
    /* 3 bytes */
    for (i = 2; i < 8; i++) {
      c = e[i];
      u = toupper(c);
      if (u >= 'A' && u <= 'F') {
        w = w * 0x10 + (zwchar)(u + 10 - 'A');
      } else if (c >= '0' && c <= '9') {
        w = w * 0x10 + (zwchar)(c - '0');
      } else {
        return 0;
      }
    }
  } else if (e[1] == 'U') {
    /* 2 bytes */
    for (i = 2; i < 6; i++) {
      c = e[i];
      u = toupper(c);
      if (u >= 'A' && u <= 'F') {
        w = w * 0x10 + (zwchar)(u + 10 - 'A');
      } else if (c >= '0' && c <= '9') {
        w = w * 0x10 + (zwchar)(c - '0');
      } else {
        return 0;
      }
    }
  }
  return w;
}
#endif


char *local_to_escape_string(local_string)
  char *local_string;
{
  zwchar *wide_string = local_to_wide_string(local_string);
  char *escape_string = wide_to_escape_string(wide_string);

  free(wide_string);
  return escape_string;
}

#ifdef WIN32
char *wchar_to_local_string(wstring)
  wchar_t *wstring;
{
  zwchar *wide_string = wchar_to_wide_string(wstring);
  char *local_string = wide_to_local_string(wide_string);

  free(wide_string);

  return local_string;
}
#endif


#ifndef WIN32   /* The Win32 port uses a system-specific variant. */
/* convert wide character string to multi-byte character string */
char *wide_to_local_string(wide_string)
  zwchar *wide_string;
{
  int i;
  wchar_t wc;
  int b;
  int state_dependent;
  int wsize = 0;
  int max_bytes = MB_CUR_MAX;
  char buf[9];
  char *buffer = NULL;
  char *local_string = NULL;

  for (wsize = 0; wide_string[wsize]; wsize++) ;

  if (MAX_ESCAPE_BYTES > max_bytes)
    max_bytes = MAX_ESCAPE_BYTES;

  if ((buffer = (char *)malloc(wsize * max_bytes + 1)) == NULL) {
    ZIPERR(ZE_MEM, "wide_to_local_string");
  }

  /* convert it */
  buffer[0] = '\0';
  /* set initial state if state-dependent encoding */
  wc = (wchar_t)'a';
  b = wctomb(NULL, wc);
  if (b == 0)
    state_dependent = 0;
  else
    state_dependent = 1;
  for (i = 0; i < wsize; i++) {
    if (sizeof(wchar_t) < 4 && wide_string[i] > 0xFFFF) {
      /* wchar_t probably 2 bytes */
      /* could do surrogates if state_dependent and wctomb can do */
      wc = zwchar_to_wchar_t_default_char;
    } else {
      wc = (wchar_t)wide_string[i];
    }
    b = wctomb(buf, wc);
    if (unicode_escape_all) {
      if (b == 1 && (uch)buf[0] <= 0x7f) {
        /* ASCII */
        strncat(buffer, buf, b);
      } else {
        /* use escape for wide character */
        char *e = wide_char_to_escape_string(wide_string[i]);
        strcat(buffer, e);
        free(e);
      }
    } else if (b > 0) {
      /* multi-byte char */
      strncat(buffer, buf, b);
    } else {
      /* no MB for this wide */
      if (use_wide_to_mb_default) {
        /* default character */
        strcat(buffer, wide_to_mb_default_string);
      } else {
        /* use escape for wide character */
        char *e = wide_char_to_escape_string(wide_string[i]);
        strcat(buffer, e);
        free(e);
      }
    }
  }
  if ((local_string = (char *)malloc(strlen(buffer) + 1)) == NULL) {
    free(buffer);
    ZIPERR(ZE_MEM, "wide_to_local_string");
  }
  strcpy(local_string, buffer);
  free(buffer);

  return local_string;
}
#endif /* !WIN32 */


/* convert wide character string to escaped string */
char *wide_to_escape_string(wide_string)
  zwchar *wide_string;
{
  int i;
  int wsize = 0;
  char buf[9];
  char *buffer = NULL;
  char *escape_string = NULL;

  for (wsize = 0; wide_string[wsize]; wsize++) ;

  if ((buffer = (char *)malloc(wsize * MAX_ESCAPE_BYTES + 1)) == NULL) {
    ZIPERR(ZE_MEM, "wide_to_escape_string");
  }

  /* convert it */
  buffer[0] = '\0';
  for (i = 0; i < wsize; i++) {
    if (wide_string[i] <= 0x7f && isprint((char)wide_string[i])) {
      /* ASCII */
      buf[0] = (char)wide_string[i];
      buf[1] = '\0';
      strcat(buffer, buf);
    } else {
      /* use escape for wide character */
      char *e = wide_char_to_escape_string(wide_string[i]);
      strcat(buffer, e);
      free(e);
    }
  }
  if ((escape_string = (char *)malloc(strlen(buffer) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "wide_to_escape_string");
  }
  strcpy(escape_string, buffer);
  free(buffer);

  return escape_string;
}


/* convert local string to display character set string */
char *local_to_display_string(local_string)
  char *local_string;
{
  char *temp_string;
  char *display_string;

  /* For Windows, OEM string should never be bigger than ANSI string, says
     CharToOem description.
     On UNIX, non-printable characters (0x00 - 0xFF) will be replaced by
     "^x", so more space may be needed.  Note that "^" itself is a valid
     name character, so this leaves an ambiguity, but UnZip displays
     names this way, too.  (0x00 is not possible, I hope.)
     For all other ports, just make a copy of local_string.
  */

#ifdef UNIX
  char *cp_dst;                 /* Character pointers used in the */
  char *cp_src;                 /*  copying/changing procedure.   */
#endif

  if ((temp_string = (char *)malloc(2 * strlen(local_string) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "local_to_display_string");
  }

#ifdef WIN32
  /* convert to OEM display character set */
  local_to_oem_string(temp_string, local_string);
#else
# ifdef UNIX
  /* Copy source string, expanding non-printable characters to "^x". */
  cp_dst = temp_string;
  cp_src = local_string;
  while (*cp_src != '\0') {
    if ((unsigned char)*cp_src < ' ') {
      *cp_dst++ = '^';
      *cp_dst++ = '@'+ *cp_src++;
    }
    else {
      *cp_dst++ = *cp_src++;
    }
  }
  *cp_dst = '\0';
# else /* not UNIX */
  strcpy(temp_string, local_string);
# endif /* UNIX */
#endif

#ifdef EBCDIC
  {
    char *ebc;

    if ((ebc = malloc(strlen(display_string) + 1)) ==  NULL) {
      ZIPERR(ZE_MEM, "local_to_display_string");
    }
    strtoebc(ebc, display_string);
    free(display_string);
    display_string = ebc;
  }
#endif

  if ((display_string = (char *)malloc(strlen(temp_string) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "local_to_display_string");
  }
  strcpy(display_string, temp_string);
  free(temp_string);

  return display_string;
}

/* UTF-8 to local */
char *utf8_to_local_string(utf8_string)
  char *utf8_string;
{
  zwchar *wide_string = utf8_to_wide_string(utf8_string);
  char *loc = wide_to_local_string(wide_string);
  if (wide_string)
    free(wide_string);
  return loc;
}

/* UTF-8 to local */
char *utf8_to_escape_string(utf8_string)
  char *utf8_string;
{
  zwchar *wide_string = utf8_to_wide_string(utf8_string);
  char *escape_string = wide_to_escape_string(wide_string);
  free(wide_string);
  return escape_string;
}

#ifndef WIN32   /* The Win32 port uses a system-specific variant. */
/* convert multi-byte character string to wide character string */
zwchar *local_to_wide_string(local_string)
  char *local_string;
{
  int wsize;
  wchar_t *wc_string;
  zwchar *wide_string;

  /* for now try to convert as string - fails if a bad char in string */
  wsize = mbstowcs(NULL, local_string, MB_CUR_MAX );
  if (wsize == (size_t)-1) {
    /* could not convert */
    return NULL;
  }

  /* convert it */
  if ((wc_string = (wchar_t *)malloc((wsize + 1) * sizeof(wchar_t))) == NULL) {
    ZIPERR(ZE_MEM, "local_to_wide_string");
  }
  wsize = mbstowcs(wc_string, local_string, strlen(local_string) + 1);
  wc_string[wsize] = (wchar_t) 0;

  /* in case wchar_t is not zwchar */
  if ((wide_string = (zwchar *)malloc((wsize + 1) * sizeof(zwchar))) == NULL) {
    ZIPERR(ZE_MEM, "local_to_wide_string");
  }
  for (wsize = 0; (wide_string[wsize] = (zwchar)wc_string[wsize]); wsize++) ;
  wide_string[wsize] = (zwchar)0;
  free(wc_string);

  return wide_string;
}
#endif /* !WIN32 */


#if 0
/* All wchar functions are only used by Windows and are
   now in win32zip.c so that the Windows functions can
   be used and multiple character wide characters can
   be handled easily. */
# ifndef WIN32
char *wchar_to_utf8_string(wstring)
  wchar_t *wstring;
{
  zwchar *wide_string = wchar_to_wide_string(wstring);
  char *local_string = wide_to_utf8_string(wide_string);

  free(wide_string);

  return local_string;
}
# endif
#endif


/* convert wide string to UTF-8 */
char *wide_to_utf8_string(wide_string)
  zwchar *wide_string;
{
  int mbcount;
  char *utf8_string;

  /* get size of utf8 string */
  mbcount = ucs4_string_to_utf8(wide_string, NULL, 0);
  if (mbcount == -1)
    return NULL;
  if ((utf8_string = (char *) malloc(mbcount + 1)) == NULL) {
    ZIPERR(ZE_MEM, "wide_to_utf8_string");
  }
  mbcount = ucs4_string_to_utf8(wide_string, utf8_string, mbcount + 1);
  if (mbcount == -1)
    return NULL;

  return utf8_string;
}

/* convert UTF-8 string to wide string */
zwchar *utf8_to_wide_string(utf8_string)
  char *utf8_string;
{
  int wcount;
  zwchar *wide_string;

  wcount = utf8_to_ucs4_string(utf8_string, NULL, 0);
  if (wcount == -1)
    return NULL;
  if ((wide_string = (zwchar *) malloc((wcount + 2) * sizeof(zwchar))) == NULL) {
    ZIPERR(ZE_MEM, "utf8_to_wide_string");
  }
  wcount = utf8_to_ucs4_string(utf8_string, wide_string, wcount + 1);

  return wide_string;
}


#endif /* UNICODE_SUPPORT */


/*---------------------------------------------------------------
 *  Long option support
 *  8/23/2003
 *
 *  Defines function get_option() to get and process the command
 *  line options and arguments from argv[].  The caller calls
 *  get_option() in a loop to get either one option and possible
 *  value or a non-option argument each loop.
 *
 *  This version does not include argument file support and can
 *  work directly on argv.  The argument file code complicates things and
 *  it seemed best to leave it out for now.  If argument file support (reading
 *  in command line arguments stored in a file and inserting into
 *  command line where @filename is found) is added later the arguments
 *  can change and a freeable copy of argv will be needed and can be
 *  created using copy_args in the left out code.
 *
 *  Supports short and long options as defined in the array options[]
 *  in zip.c, multiple short options in an argument (like -jlv), long
 *  option abbreviation (like --te for --temp-file if --te unique),
 *  short and long option values (like -b filename or --temp-file filename
 *  or --temp-file=filename), optional and required values, option negation
 *  by trailing - (like -S- to not include hidden and system files in MSDOS),
 *  value lists (like -x a b c), argument permuting (returning all options
 *  and values before any non-option arguments), and argument files (where any
 *  non-option non-value argument in form @path gets substituted with the
 *  white space separated arguments in the text file at path).  In this
 *  version argument file support has been removed to simplify development but
 *  may be added later.
 *
 *  E. Gordon
 */


/* message output - char casts are needed to handle constants */
#define oWARN(message) zipwarn((char *) message, "")
#define oERR(err,message) ZIPERR(err, (char *) message)


/* Although the below provides some support for multibyte characters
   the proper thing to do may be to use wide characters and support
   Unicode.  May get to it soon.  EG
 */

/* For now stay with muti-byte characters.  May support wide characters
   in Zip 3.1.
 */

/* multibyte character set support
   Multibyte characters use typically two or more sequential bytes
   to represent additional characters than can fit in a single byte
   character set.  The code used here is based on the ANSI mblen function. */
#ifdef MULTIBYTE_GETOPTNS
  int mb_clen(ptr)
    ZCONST char *ptr;
  {
    /* return the number of bytes that the char pointed to is.  Return 1 if
       null character or error like not start of valid multibyte character. */
    int cl;

    cl = mblen(ptr, MB_CUR_MAX);
    return (cl > 0) ? cl : 1;
  }
#endif


  /* moved to zip.h */
#if 0
#ifdef UNICODE_SUPPORT
# define MB_CLEN(ptr) (1)
# define MB_NEXTCHAR(ptr) ((ptr)++)
# ifdef MULTIBYTE_GETOPTNS
#    undef MULTIBYTE_GETOPTNS
# endif
#else
# ifdef _MBCS
#  ifndef MULTIBYTE_GETOPTNS
#    define MULTIBYTE_GETOPTNS
#  endif
# endif
/* multibyte character set support
   Multibyte characters use typically two or more sequential bytes
   to represent additional characters than can fit in a single byte
   character set.  The code used here is based on the ANSI mblen function. */
#  ifdef MULTIBYTE_GETOPTNS
  local int mb_clen OF((ZCONST char *));  /* declare proto first */
  local int mb_clen(ptr)
    ZCONST char *ptr;
  {
    /* return the number of bytes that the char pointed to is.  Return 1 if
       null character or error like not start of valid multibyte character. */
    int cl;

    cl = mblen(ptr, MB_CUR_MAX);
    return (cl > 0) ? cl : 1;
  }
#  define MB_CLEN(ptr) mb_clen(ptr)
#  define MB_NEXTCHAR(ptr) ((ptr) += MB_CLEN(ptr))
# else
#  define MB_CLEN(ptr) (1)
#  define MB_NEXTCHAR(ptr) ((ptr)++)
# endif
#endif
#endif


/* constants */

/* function get_args_from_arg_file() can return this in depth parameter */
#define ARG_FILE_ERR -1

/* internal settings for optchar */
#define SKIP_VALUE_ARG   -1
#define THIS_ARG_DONE    -2
#define START_VALUE_LIST -3
#define IN_VALUE_LIST    -4
#define NON_OPTION_ARG   -5
#define STOP_VALUE_LIST  -6
/* 7/25/04 EG */
#define READ_REST_ARGS_VERBATIM -7


/* global veriables */

int enable_permute = 1;                     /* yes - return options first */
/* 7/25/04 EG */
int doubledash_ends_options = 1;            /* when -- what follows are not options */

/* buffer for error messages (this sizing is a guess but must hold 2 paths) */
#define OPTIONERR_BUF_SIZE (FNMAX * 2 + 4000)
local char Far optionerrbuf[OPTIONERR_BUF_SIZE + 1];

/* error messages */
static ZCONST char Far op_not_neg_err[] = "option %s not negatable";
static ZCONST char Far op_req_val_err[] = "option %s requires a value";
static ZCONST char Far op_no_allow_val_err[] = "option %s does not allow a value";
static ZCONST char Far sh_op_not_sup_err[] = "short option '%c' not supported";
static ZCONST char Far oco_req_val_err[] = "option %s requires one character value";
static ZCONST char Far oco_no_mbc_err[] = "option %s does not support multibyte values";
static ZCONST char Far num_req_val_err[] = "option %s requires number value";
static ZCONST char Far long_op_ambig_err[] = "long option '%s' ambiguous";
static ZCONST char Far long_op_not_sup_err[] = "long option '%s' not supported";

static ZCONST char Far no_arg_files_err[] = "argument files not enabled\n";


/* below removed as only used for processing argument files */

/* get_nextarg */
/* get_args_from_string */
/* insert_args */
/* get_args_from_arg_file */


/* copy error, option name, and option description if any to buf */
local int optionerr(buf, err, optind, islong)
  char *buf;
  ZCONST char *err;
  int optind;
  int islong;
{
  char optname[50];

  if (options[optind].name && options[optind].name[0] != '\0') {
    if (islong)
      sprintf(optname, "'%s' (%s)", options[optind].longopt, options[optind].name);
    else
      sprintf(optname, "'%s' (%s)", options[optind].shortopt, options[optind].name);
  } else {
    if (islong)
      sprintf(optname, "'%s'", options[optind].longopt);
    else
      sprintf(optname, "'%s'", options[optind].shortopt);
  }
  sprintf(buf, err, optname);
  return 0;
}


/* copy_args
 *
 * Copy arguments in args, allocating storage with malloc.
 * Copies until a NULL argument is found or until max_args args
 * including args[0] are copied.  Set max_args to 0 to copy
 * until NULL.  Always terminates returned args[] with NULL arg.
 *
 * Any argument in the returned args can be freed with free().  Any
 * freed argument should be replaced with either another string
 * allocated with malloc or by NULL if last argument so that free_args
 * will properly work.
 */
char **copy_args(args, max_args)
  char **args;
  int max_args;
{
  int j;
  char **new_args;

  if (args == NULL) {
    return NULL;
  }

  /* count args */
  for (j = 0; args[j] && (max_args == 0 || j < max_args); j++) ;

  if ((new_args = (char **) malloc((j + 1) * sizeof(char *))) == NULL) {
    oERR(ZE_MEM, "ca");
  }

  for (j = 0; args[j] && (max_args == 0 || j < max_args); j++) {
    if (args[j] == NULL) {
      /* null argument is end of args */
      new_args[j] = NULL;
      break;
    }
    if ((new_args[j] = malloc(strlen(args[j]) + 1)) == NULL) {
      free_args(new_args);
      oERR(ZE_MEM, "ca");
    }
    strcpy(new_args[j], args[j]);
  }
  new_args[j] = NULL;

  return new_args;
}


/* free args - free args created with one of these functions */
int free_args(args)
  char **args;
{
  int i;

  if (args == NULL) {
    return 0;
  }

  for (i = 0; args[i]; i++) {
    free(args[i]);
  }
  free(args);
  return i;
}


/* insert_arg
 *
 * Insert the argument arg into the array args before argument at_arg.
 * Return the new count of arguments (argc).
 *
 * If free_args is true, this function frees the old args array
 * (but not the component strings).  DO NOT set free_args on original
 * argv but only on args allocated with malloc.
 */

int insert_arg(pargs, arg, at_arg, free_args)
   char ***pargs;
   ZCONST char *arg;
   int at_arg;
   int free_args;
{
   char *newarg = NULL;
   char **args;
   char **newargs = NULL;
   int argnum;
   int newargnum;
   int argcnt;
   int newargcnt;

   if (pargs == NULL) {
     return 0;
   }
   args = *pargs;

   /* count args */
   if (args == NULL) {
     argcnt = 0;
   } else {
     for (argcnt = 0; args[argcnt]; argcnt++) ;
   }
   if (arg == NULL) {
     /* done */
     return argcnt;
   }
   newargcnt = argcnt + 1;

   /* get storage for new args */
   if ((newargs = (char **) malloc((newargcnt + 1) * sizeof(char *))) == NULL) {
     oERR(ZE_MEM, "ia");
   }

   /* copy argument pointers from args to position at_arg, copy arg, then rest args */
   argnum = 0;
   newargnum = 0;
   if (args) {
     for (; args[argnum] && argnum < at_arg; argnum++) {
       newargs[newargnum++] = args[argnum];
     }
   }
   /* copy new arg */
   if ((newarg = (char *) malloc(strlen(arg) + 1)) == NULL) {
     oERR(ZE_MEM, "ia");
   }
   strcpy(newarg, arg);

   newargs[newargnum++] = newarg;
   if (args) {
     for ( ; args[argnum]; argnum++) {
       newargs[newargnum++] = args[argnum];
     }
   }
   newargs[newargnum] = NULL;

   /* free old args array but not component strings - this assumes that
      args was allocated with malloc as copy_args does.  DO NOT DO THIS
      on the original argv.
    */
   if (free_args)
     free(args);

   *pargs = newargs;

   return newargnum;
}

/* ------------------------------------- */




/* get_shortopt
 *
 * Get next short option from arg.  The state is stored in argnum, optchar, and
 * option_num so no static storage is used.  Returns the option_ID.
 *
 * parameters:
 *    args        - argv array of arguments
 *    argnum      - index of current arg in args
 *    optchar     - pointer to index of next char to process.  Can be 0 or
 *                  const defined at top of this file like THIS_ARG_DONE
 *    negated     - on return pointer to int set to 1 if option negated or 0 otherwise
 *    value       - on return pointer to string set to value of option if any or NULL
 *                  if none.  If value is returned then the caller should free()
 *                  it when not needed anymore.
 *    option_num  - pointer to index in options[] of returned option or
 *                  o_NO_OPTION_MATCH if none.  Do not change as used by
 *                  value lists.
 *    depth       - recursion depth (0 at top level, 1 or more in arg files)
 */
local unsigned long get_shortopt(args, argnum, optchar, negated, value,
                                 option_num, depth)
  char **args;
  int argnum;
  int *optchar;
  int *negated;
  char **value;
  int *option_num;
  int depth;
{
  char *shortopt;
  int clen;
  char *nextchar;
  char *s;
  char *start;
  int op;
  char *arg;
  int match = -1;


  /* get arg */
  arg = args[argnum];
  /* current char in arg */
  nextchar = arg + (*optchar);
  clen = MB_CLEN(nextchar);
  /* next char in arg */
  (*optchar) +=  clen;
  /* get first char of short option */
  shortopt = arg + (*optchar);
  /* no value */
  *value = NULL;

  if (*shortopt == '\0') {
    /* no more options in arg */
    *optchar = 0;
    *option_num = o_NO_OPTION_MATCH;
    return 0;
  }

  /* look for match in options */
  clen = MB_CLEN(shortopt);
  for (op = 0; options[op].option_ID; op++) {
    s = options[op].shortopt;
    if (s && s[0] == shortopt[0]) {
      if (s[1] == '\0' && clen == 1) {
        /* single char match */
        match = op;
      } else {
        /* 2 wide short opt.  Could support more chars but should use long opts instead */
        if (s[1] == shortopt[1]) {
          /* match 2 char short opt or 2 byte char */
          match = op;
          if (clen == 1) (*optchar)++;
          break;
        }
      }
    }
  }

  if (match > -1) {
    /* match */
    clen = MB_CLEN(shortopt);
    nextchar = arg + (*optchar) + clen;
    /* check for trailing dash negating option */
    if (*nextchar == '-') {
      /* negated */
      if (options[match].negatable == o_NOT_NEGATABLE) {
        if (options[match].value_type == o_NO_VALUE) {
          optionerr(optionerrbuf, op_not_neg_err, match, 0);
          if (depth > 0) {
            /* unwind */
            oWARN(optionerrbuf);
            return o_ARG_FILE_ERR;
          } else {
            oERR(ZE_PARMS, optionerrbuf);
          }
        }
      } else {
        *negated = 1;
        /* set up to skip negating dash */
        (*optchar) += clen;
        clen = 1;
      }
    }

    /* value */
    clen = MB_CLEN(arg + (*optchar));
    /* optional value, one char value, and number value must follow option */
    if (options[match].value_type == o_ONE_CHAR_VALUE) {
      /* one char value */
      if (arg[(*optchar) + clen]) {
        /* has value */
        if (MB_CLEN(arg + (*optchar) + clen) > 1) {
          /* multibyte value not allowed for now */
          optionerr(optionerrbuf, oco_no_mbc_err, match, 0);
          if (depth > 0) {
            /* unwind */
            oWARN(optionerrbuf);
            return o_ARG_FILE_ERR;
          } else {
            oERR(ZE_PARMS, optionerrbuf);
          }
        }
        if ((*value = (char *) malloc(2)) == NULL) {
          oERR(ZE_MEM, "gso");
        }
        (*value)[0] = *(arg + (*optchar) + clen);
        (*value)[1] = '\0';
        *optchar += clen;
        clen = 1;
      } else {
        /* one char values require a value */
        optionerr(optionerrbuf, oco_req_val_err, match, 0);
        if (depth > 0) {
          oWARN(optionerrbuf);
          return o_ARG_FILE_ERR;
        } else {
          oERR(ZE_PARMS, optionerrbuf);
        }
      }
    } else if (options[match].value_type == o_NUMBER_VALUE) {
      /* read chars until end of number */
      start = arg + (*optchar) + clen;
      if (*start == '+' || *start == '-') {
        start++;
      }
      s = start;
      for (; isdigit(*s); MB_NEXTCHAR(s)) ;
      if (s == start) {
        /* no digits */
        optionerr(optionerrbuf, num_req_val_err, match, 0);
        if (depth > 0) {
          oWARN(optionerrbuf);
          return o_ARG_FILE_ERR;
        } else {
          oERR(ZE_PARMS, optionerrbuf);
        }
      }
      start = arg + (*optchar) + clen;
      if ((*value = (char *) malloc((int)(s - start) + 1)) == NULL) {
        oERR(ZE_MEM, "gso");
      }
      *optchar += (int)(s - start);
      strncpy(*value, start, (int)(s - start));
      (*value)[(int)(s - start)] = '\0';
      clen = MB_CLEN(s);
    } else if (options[match].value_type == o_OPTIONAL_VALUE) {
      /* optional value */
      /* This seemed inconsistent so now if no value attached to argument look
         to the next argument if that argument is not an option for option
         value - 11/12/04 EG */
      if (arg[(*optchar) + clen]) {
        /* has value */
        /* add support for optional = - 2/6/05 EG */
        if (arg[(*optchar) + clen] == '=') {
          /* skip = */
          clen++;
        }
        if (arg[(*optchar) + clen]) {
          if ((*value = (char *)malloc(strlen(arg + (*optchar) + clen) + 1))
              == NULL) {
            oERR(ZE_MEM, "gso");
          }
          strcpy(*value, arg + (*optchar) + clen);
        }
        *optchar = THIS_ARG_DONE;
      } else if (args[argnum + 1] && args[argnum + 1][0] != '-') {
        /* use next arg for value */
        if ((*value = (char *)malloc(strlen(args[argnum + 1]) + 1)) == NULL) {
          oERR(ZE_MEM, "gso");
        }
        /* using next arg as value */
        strcpy(*value, args[argnum + 1]);
        *optchar = SKIP_VALUE_ARG;
      }
    } else if (options[match].value_type == o_REQUIRED_VALUE ||
               options[match].value_type == o_VALUE_LIST) {
      /* see if follows option */
      if (arg[(*optchar) + clen]) {
        /* has value following option as -ovalue */
        /* add support for optional = - 6/5/05 EG */
        if (arg[(*optchar) + clen] == '=') {
          /* skip = */
          clen++;
        }
          if ((*value = (char *)malloc(strlen(arg + (*optchar) + clen) + 1))
              == NULL) {
          oERR(ZE_MEM, "gso");
        }
        strcpy(*value, arg + (*optchar) + clen);
        *optchar = THIS_ARG_DONE;
      } else {
        /* use next arg for value */
        if (args[argnum + 1]) {
          if ((*value = (char *)malloc(strlen(args[argnum + 1]) + 1)) == NULL) {
            oERR(ZE_MEM, "gso");
          }
          strcpy(*value, args[argnum + 1]);
          if (options[match].value_type == o_VALUE_LIST) {
            *optchar = START_VALUE_LIST;
          } else {
            *optchar = SKIP_VALUE_ARG;
          }
        } else {
          /* no value found */
          optionerr(optionerrbuf, op_req_val_err, match, 0);
          if (depth > 0) {
            oWARN(optionerrbuf);
            return o_ARG_FILE_ERR;
          } else {
            oERR(ZE_PARMS, optionerrbuf);
          }
        }
      }
    }

    *option_num = match;
    return options[match].option_ID;
  }
  sprintf(optionerrbuf, sh_op_not_sup_err, *shortopt);
  if (depth > 0) {
    /* unwind */
    oWARN(optionerrbuf);
    return o_ARG_FILE_ERR;
  } else {
    oERR(ZE_PARMS, optionerrbuf);
  }
  return 0;
}


/* get_longopt
 *
 * Get the long option in args array at argnum.
 * Parameters same as for get_shortopt.
 */

local unsigned long get_longopt(args, argnum, optchar, negated, value,
                                option_num, depth)
  char **args;
  int argnum;
  int *optchar;
  int *negated;
  char **value;
  int *option_num;
  int depth;
{
  char *longopt;
  char *lastchr;
  char *valuestart;
  int op;
  char *arg;
  int match = -1;
  *value = NULL;

  if (args == NULL) {
    *option_num = o_NO_OPTION_MATCH;
    return 0;
  }
  if (args[argnum] == NULL) {
    *option_num = o_NO_OPTION_MATCH;
    return 0;
  }
  /* copy arg so can chop end if value */
  if ((arg = (char *)malloc(strlen(args[argnum]) + 1)) == NULL) {
    oERR(ZE_MEM, "glo");
  }
  strcpy(arg, args[argnum]);

  /* get option */
  longopt = arg + 2;
  /* no value */
  *value = NULL;

  /* find = */
  for (lastchr = longopt, valuestart = longopt;
       *valuestart && *valuestart != '=';
       lastchr = valuestart, MB_NEXTCHAR(valuestart)) ;
  if (*valuestart) {
    /* found =value */
    *valuestart = '\0';
    valuestart++;
  } else {
    valuestart = NULL;
  }

  if (*lastchr == '-') {
    /* option negated */
    *negated = 1;
    *lastchr = '\0';
  } else {
    *negated = 0;
  }

  /* look for long option match */
  for (op = 0; options[op].option_ID; op++) {
    if (options[op].longopt && strcmp(options[op].longopt, longopt) == 0) {
      /* exact match */
      match = op;
      break;
    }
    if (options[op].longopt && strncmp(options[op].longopt, longopt, strlen(longopt)) == 0) {
      if (match > -1) {
        sprintf(optionerrbuf, long_op_ambig_err, longopt);
        free(arg);
        if (depth > 0) {
          /* unwind */
          oWARN(optionerrbuf);
          return o_ARG_FILE_ERR;
        } else {
          oERR(ZE_PARMS, optionerrbuf);
        }
      }
      match = op;
    }
  }

  if (match == -1) {
    sprintf(optionerrbuf, long_op_not_sup_err, longopt);
    free(arg);
    if (depth > 0) {
      oWARN(optionerrbuf);
      return o_ARG_FILE_ERR;
    } else {
      oERR(ZE_PARMS, optionerrbuf);
    }
  }

  /* one long option an arg */
  *optchar = THIS_ARG_DONE;

  /* if negated then see if allowed */
  if (*negated && options[match].negatable == o_NOT_NEGATABLE) {
    optionerr(optionerrbuf, op_not_neg_err, match, 1);
    free(arg);
    if (depth > 0) {
      /* unwind */
      oWARN(optionerrbuf);
      return o_ARG_FILE_ERR;
    } else {
      oERR(ZE_PARMS, optionerrbuf);
    }
  }
  /* get value */
  if (options[match].value_type == o_OPTIONAL_VALUE) {
    /* optional value in form option=value */
    if (valuestart) {
      /* option=value */
      if ((*value = (char *)malloc(strlen(valuestart) + 1)) == NULL) {
        free(arg);
        oERR(ZE_MEM, "glo");
      }
      strcpy(*value, valuestart);
    }
  } else if (options[match].value_type == o_REQUIRED_VALUE ||
             options[match].value_type == o_NUMBER_VALUE ||
             options[match].value_type == o_ONE_CHAR_VALUE ||
             options[match].value_type == o_VALUE_LIST) {
    /* handle long option one char and number value as required value */
    if (valuestart) {
      /* option=value */
      if ((*value = (char *)malloc(strlen(valuestart) + 1)) == NULL) {
        free(arg);
        oERR(ZE_MEM, "glo");
      }
      strcpy(*value, valuestart);
    } else {
      /* use next arg */
      if (args[argnum + 1]) {
        if ((*value = (char *)malloc(strlen(args[argnum + 1]) + 1)) == NULL) {
          free(arg);
          oERR(ZE_MEM, "glo");
        }
        /* using next arg as value */
        strcpy(*value, args[argnum + 1]);
        if (options[match].value_type == o_VALUE_LIST) {
          *optchar = START_VALUE_LIST;
        } else {
          *optchar = SKIP_VALUE_ARG;
        }
      } else {
        /* no value found */
        optionerr(optionerrbuf, op_req_val_err, match, 1);
        free(arg);
        if (depth > 0) {
          /* unwind */
          oWARN(optionerrbuf);
          return o_ARG_FILE_ERR;
        } else {
          oERR(ZE_PARMS, optionerrbuf);
        }
      }
    }
  } else if (options[match].value_type == o_NO_VALUE) {
    /* this option does not accept a value */
    if (valuestart) {
      /* --option=value */
      optionerr(optionerrbuf, op_no_allow_val_err, match, 1);
      free(arg);
      if (depth > 0) {
        oWARN(optionerrbuf);
        return o_ARG_FILE_ERR;
      } else {
        oERR(ZE_PARMS, optionerrbuf);
      }
    }
  }
  free(arg);

  *option_num = match;
  return options[match].option_ID;
}



/* get_option
 *
 * Main interface for user.  Use this function to get options, values and
 * non-option arguments from a command line provided in argv form.
 *
 * To use get_option() first define valid options by setting
 * the global variable options[] to an array of option_struct.  Also
 * either change defaults below or make variables global and set elsewhere.
 * Zip uses below defaults.
 *
 * Call get_option() to get an option (like -b or --temp-file) and any
 * value for that option (like filename for -b) or a non-option argument
 * (like archive name) each call.  If *value* is not NULL after calling
 * get_option() it is a returned value and the caller should either store
 * the char pointer or free() it before calling get_option() again to avoid
 * leaking memory.  If a non-option non-value argument is returned get_option()
 * returns o_NON_OPTION_ARG and value is set to the entire argument.
 * When there are no more arguments get_option() returns 0.
 *
 * The parameters argnum (after set to 0 on initial call),
 * optchar, first_nonopt_arg, option_num, and depth (after initial
 * call) are set and maintained by get_option() and should not be
 * changed.  The parameters argc, negated, and value are outputs and
 * can be used by the calling program.  get_option() returns either the
 * option_ID for the current option, a special value defined in
 * zip.h, or 0 when no more arguments.
 *
 * The value returned by get_option() is the ID value in the options
 * table.  This value can be duplicated in the table if different
 * options are really the same option.  The index into the options[]
 * table is given by option_num, though the ID should be used as
 * option numbers change when the table is changed.  The ID must
 * not be 0 for any option as this ends the table.  If get_option()
 * finds an option not in the table it calls oERR to post an
 * error and exit.  Errors also result if the option requires a
 * value that is missing, a value is present but the option does
 * not take one, and an option is negated but is not
 * negatable.  Non-option arguments return o_NON_OPTION_ARG
 * with the entire argument in value.
 *
 * For Zip, permuting is on and all options and their values are
 * returned before any non-option arguments like archive name.
 *
 * The arguments "-" alone and "--" alone return as non-option arguments.
 * Note that "-" should not be used as part of a short option
 * entry in the table but can be used in the middle of long
 * options such as in the long option "a-long-option".  Now "--" alone
 * stops option processing, returning any arguments following "--" as
 * non-option arguments instead of options.
 *
 * Argument file support is removed from this version. It may be added later.
 *
 * After each call:
 *   argc       is set to the current size of args[] but should not change
 *                with argument file support removed,
 *   argnum     is the index of the current arg,
 *   value      is either the value of the returned option or non-option
 *                argument or NULL if option with no value,
 *   negated    is set if the option was negated by a trailing dash (-)
 *   option_num is set to either the index in options[] for the option or
 *                o_NO_OPTION_MATCH if no match.
 * Negation is checked before the value is read if the option is negatable so
 * that the - is not included in the value.  If the option is not negatable
 * but takes a value then the - will start the value.  If permuting then
 * argnum and first_nonopt_arg are unreliable and should not be used.
 *
 * Command line is read from left to right.  As get_option() finds non-option
 * arguments (arguments not starting with - and that are not values to options)
 * it moves later options and values in front of the non-option arguments.
 * This permuting is turned off by setting enable_permute to 0.  Then
 * get_option() will return options and non-option arguments in the order
 * found.  Currently permuting is only done after an argument is completely
 * processed so that any value can be moved with options they go with.  All
 * state information is stored in the parameters argnum, optchar,
 * first_nonopt_arg and option_num.  You should not change these after the
 * first call to get_option().  If you need to back up to a previous arg then
 * set argnum to that arg (remembering that args may have been permuted) and
 * set optchar = 0 and first_nonopt_arg to the first non-option argument if
 * permuting.  After all arguments are returned the next call to get_option()
 * returns 0.  The caller can then call free_args(args) if appropriate.
 *
 * get_option() accepts arguments in the following forms:
 *  short options
 *       of 1 and 2 characters, e.g. a, b, cc, d, and ba, after a single
 *       leading -, as in -abccdba.  In this example if 'b' is followed by 'a'
 *       it matches short option 'ba' else it is interpreted as short option
 *       b followed by another option.  The character - is not legal as a
 *       short option or as part of a 2 character short option.
 *
 *       If a short option has a value it immediately follows the option or
 *       if that option is the end of the arg then the next arg is used as
 *       the value.  So if short option e has a value, it can be given as
 *             -evalue
 *       or
 *             -e value
 *       and now
 *             -e=value
 *       but now that = is optional a leading = is stripped for the first.
 *       This change allows optional short option values to be defaulted as
 *             -e=
 *       Either optional or required values can be specified.  Optional values
 *       now use both forms as ignoring the later got confusing.  Any
 *       non-value short options can preceed a valued short option as in
 *             -abevalue
 *       Some value types (one_char and number) allow options after the value
 *       so if oc is an option that takes a character and n takes a number
 *       then
 *             -abocVccn42evalue
 *       returns value V for oc and value 42 for n.  All values are strings
 *       so programs may have to convert the "42" to a number.  See long
 *       options below for how value lists are handled.
 *
 *       Any short option can be negated by following it with -.  Any - is
 *       handled and skipped over before any value is read unless the option
 *       is not negatable but takes a value and then - starts the value.
 *
 *       If the value for an optional value is just =, then treated as no
 *       value.
 *
 *  long options
 *       of arbitrary length are assumed if an arg starts with -- but is not
 *       exactly --.  Long options are given one per arg and can be abbreviated
 *       if the abbreviation uniquely matches one of the long options.
 *       Exact matches always match before partial matches.  If ambiguous an
 *       error is generated.
 *
 *       Values are specified either in the form
 *             --longoption=value
 *       or can be the following arg if the value is required as in
 *             --longoption value
 *       Optional values to long options must be in the first form.
 *
 *       Value lists are specified by o_VALUE_LIST and consist of an option
 *       that takes a value followed by one or more value arguments.
 *       The two forms are
 *             --option=value
 *       or
 *             -ovalue
 *       for a single value or
 *             --option value1 value2 value3 ... --option2
 *       or
 *             -o value1 value2 value3 ...
 *       for a list of values.  The list ends at the next option, the
 *       end of the command line, or at a single "@" argument.
 *       Each value is treated as if it was preceeded by the option, so
 *             --option1 val1 val2
 *       with option1 value_type set to o_VALUE_LIST is the same as
 *             --option1=val1 --option1=val2
 *
 *       Long options can be negated by following the option with - as in
 *             --longoption-
 *       Long options with values can also be negated if this makes sense for
 *       the caller as:
 *             --longoption-=value
 *       If = is not followed by anything it is treated as no value.
 *
 *  @path
 *       When an argument in the form @path is encountered, the file at path
 *       is opened and white space separated arguments read from the file
 *       and inserted into the command line at that point as if the contents
 *       of the file were directly typed at that location.  The file can
 *       have options, files to zip, or anything appropriate at that location
 *       in the command line.  Since Zip has permuting enabled, options and
 *       files will propagate to the appropriate locations in the command
 *       line.
 *
 *       Argument files support has been removed from this version.  It may
 *       be added back later.
 *
 *  non-option argument
 *       is any argument not given above.  If enable_permute is 1 then
 *       these are returned after all options, otherwise all options and
 *       args are returned in order.  Returns option ID o_NON_OPTION_ARG
 *       and sets value to the argument.
 *
 *
 * Arguments to get_option:
 *  char ***pargs          - pointer to arg array in the argv form
 *  int *argc              - returns the current argc for args incl. args[0]
 *  int *argnum            - the index of the current argument (caller
 *                            should set = 0 on first call and not change
 *                            after that)
 *  int *optchar           - index of next short opt in arg or special
 *  int *first_nonopt_arg  - used by get_option to permute args
 *  int *negated           - option was negated (had trailing -)
 *  char *value            - value of option if any (free when done with it) or NULL
 *  int *option_num        - the index in options of the last option returned
 *                            (can be o_NO_OPTION_MATCH)
 *  int recursion_depth    - current depth of recursion
 *                            (always set to 0 by caller)
 *                            (always 0 with argument files support removed)
 *
 *  Caller should only read the returned option ID and the value, negated,
 *  and option_num (if required) parameters after each call.
 *
 *  Ed Gordon
 *  24 August 2003 (last updated 2 April 2008 EG)
 *
 */

unsigned long get_option(pargs, argc, argnum, optchar, value,
                         negated, first_nonopt_arg, option_num, recursion_depth)
  char ***pargs;
  int *argc;
  int *argnum;
  int *optchar;
  char **value;
  int *negated;
  int *first_nonopt_arg;
  int *option_num;
  int recursion_depth;
{
  char **args;
  unsigned long option_ID;

  int argcnt;
  int first_nonoption_arg;
  char *arg = NULL;
  int h;
  int optc;
  int argn;
  int j;
  int v;
  int read_rest_args_verbatim = 0;  /* 7/25/04 - ignore options and arg files for rest args */

  /* value is outdated.  The caller should free value before
     calling get_option again. */
  *value = NULL;

  /* if args is NULL then done */
  if (pargs == NULL) {
    *argc = 0;
    return 0;
  }
  args = *pargs;
  if (args == NULL) {
    *argc = 0;
    return 0;
  }

  /* count args */
  for (argcnt = 0; args[argcnt]; argcnt++) ;

  /* if no provided args then nothing to do */
  if (argcnt < 1 || (recursion_depth == 0 && argcnt < 2)) {
    *argc = argcnt;
    /* return 0 to note that no args are left */
    return 0;
  }

  *negated = 0;
  first_nonoption_arg = *first_nonopt_arg;
  argn = *argnum;
  optc = *optchar;

  if (optc == READ_REST_ARGS_VERBATIM) {
    read_rest_args_verbatim = 1;
  }

  if (argn == -1 || (recursion_depth == 0 && argn == 0)) {
    /* first call */
    /* if depth = 0 then args[0] is argv[0] so skip */
    *option_num = o_NO_OPTION_MATCH;
    optc = THIS_ARG_DONE;
    first_nonoption_arg = -1;
  }

  /* if option_num is set then restore last option_ID in case continuing value list */
  option_ID = 0;
  if (*option_num != o_NO_OPTION_MATCH) {
    option_ID = options[*option_num].option_ID;
  }

  /* get next option if any */
  for (;;)  {
    if (read_rest_args_verbatim) {
      /* rest of args after "--" are non-option args if doubledash_ends_options set */
      argn++;
      if (argn > argcnt || args[argn] == NULL) {
        /* done */
        option_ID = 0;
        break;
      }
      arg = args[argn];
      if ((*value = (char *)malloc(strlen(arg) + 1)) == NULL) {
        oERR(ZE_MEM, "go");
      }
      strcpy(*value, arg);
      *option_num = o_NO_OPTION_MATCH;
      option_ID = o_NON_OPTION_ARG;
      break;

    /* permute non-option args after option args so options are returned first */
    } else if (enable_permute) {
      if (optc == SKIP_VALUE_ARG || optc == THIS_ARG_DONE ||
          optc == START_VALUE_LIST || optc == IN_VALUE_LIST ||
          optc == STOP_VALUE_LIST) {
        /* moved to new arg */
        if (first_nonoption_arg > -1 && args[first_nonoption_arg]) {
          /* do the permuting - move non-options after this option */
          /* if option and value separate args or starting list skip option */
          if (optc == SKIP_VALUE_ARG || optc == START_VALUE_LIST) {
            v = 1;
          } else {
            v = 0;
          }
          for (h = first_nonoption_arg; h < argn; h++) {
            arg = args[first_nonoption_arg];
            for (j = first_nonoption_arg; j < argn + v; j++) {
              args[j] = args[j + 1];
            }
            args[j] = arg;
          }
          first_nonoption_arg += 1 + v;
        }
      }
    } else if (optc == NON_OPTION_ARG) {
      /* if not permuting then already returned arg */
      optc = THIS_ARG_DONE;
    }

    /* value lists */
    if (optc == STOP_VALUE_LIST) {
      optc = THIS_ARG_DONE;
    }

    if (optc == START_VALUE_LIST || optc == IN_VALUE_LIST) {
      if (optc == START_VALUE_LIST) {
        /* already returned first value */
        argn++;
        optc = IN_VALUE_LIST;
      }
      argn++;
      arg = args[argn];
      /* if end of args and still in list and there are non-option args then
         terminate list */
      if (arg == NULL && (optc == START_VALUE_LIST || optc == IN_VALUE_LIST)
          && first_nonoption_arg > -1) {
        /* terminate value list with @ */
        /* this is only needed for argument files */
        /* but is also good for show command line so command lines with lists
           can always be read back in */
        argcnt = insert_arg(&args, "@", first_nonoption_arg, 1);
        argn++;
        if (first_nonoption_arg > -1) {
          first_nonoption_arg++;
        }
      }

      arg = args[argn];
      if (arg && arg[0] == '@' && arg[1] == '\0') {
          /* inserted arguments terminator */
          optc = STOP_VALUE_LIST;
          continue;
      } else if (arg && arg[0] != '-') {  /* not option */
        /* - and -- are not allowed in value lists unless escaped */
        /* another value in value list */
        if ((*value = (char *)malloc(strlen(args[argn]) + 1)) == NULL) {
          oERR(ZE_MEM, "go");
        }
        strcpy(*value, args[argn]);
        break;

      } else {
        argn--;
        optc = THIS_ARG_DONE;
      }
    }

    /* move to next arg */
    if (optc == SKIP_VALUE_ARG) {
      argn += 2;
      optc = 0;
    } else if (optc == THIS_ARG_DONE) {
      argn++;
      optc = 0;
    }
    if (argn > argcnt) {
      break;
    }
    if (args[argn] == NULL) {
      /* done unless permuting and non-option args */
      if (first_nonoption_arg > -1 && args[first_nonoption_arg]) {
        /* return non-option arguments at end */
        if (optc == NON_OPTION_ARG) {
          first_nonoption_arg++;
        }
        /* after first pass args are permuted but skipped over non-option args */
        /* swap so argn points to first non-option arg */
        j = argn;
        argn = first_nonoption_arg;
        first_nonoption_arg = j;
      }
      if (argn > argcnt || args[argn] == NULL) {
        /* done */
        option_ID = 0;
        break;
      }
    }

    /* after swap first_nonoption_arg points to end which is NULL */
    if (first_nonoption_arg > -1 && (args[first_nonoption_arg] == NULL)) {
      /* only non-option args left */
      if (optc == NON_OPTION_ARG) {
        argn++;
      }
      if (argn > argcnt || args[argn] == NULL) {
        /* done */
        option_ID = 0;
        break;
      }
      if ((*value = (char *)malloc(strlen(args[argn]) + 1)) == NULL) {
        oERR(ZE_MEM, "go");
      }
      strcpy(*value, args[argn]);
      optc = NON_OPTION_ARG;
      option_ID = o_NON_OPTION_ARG;
      break;
    }

    arg = args[argn];

    /* is it an option */
    if (arg[0] == '-') {
      /* option */
      if (arg[1] == '\0') {
        /* arg = - */
        /* treat like non-option arg */
        *option_num = o_NO_OPTION_MATCH;
        if (enable_permute) {
          /* permute args to move all non-option args to end */
          if (first_nonoption_arg < 0) {
            first_nonoption_arg = argn;
          }
          argn++;
        } else {
          /* not permute args so return non-option args when found */
          if ((*value = (char *)malloc(strlen(arg) + 1)) == NULL) {
            oERR(ZE_MEM, "go");
          }
          strcpy(*value, arg);
          optc = NON_OPTION_ARG;
          option_ID = o_NON_OPTION_ARG;
          break;
        }

      } else if (arg[1] == '-') {
        /* long option */
        if (arg[2] == '\0') {
          /* arg = -- */
          if (doubledash_ends_options) {
            /* Now -- stops permuting and forces the rest of
               the command line to be read verbatim - 7/25/04 EG */

            /* never permute args after -- and return as non-option args */
            if (first_nonoption_arg < 1) {
              /* -- is first non-option argument - 8/7/04 EG */
              argn--;
            } else {
              /* go back to start of non-option args - 8/7/04 EG */
              argn = first_nonoption_arg - 1;
            }

            /* disable permuting and treat remaining arguments as not
               options */
            read_rest_args_verbatim = 1;
            optc = READ_REST_ARGS_VERBATIM;

          } else {
            /* treat like non-option arg */
            *option_num = o_NO_OPTION_MATCH;
            if (enable_permute) {
              /* permute args to move all non-option args to end */
              if (first_nonoption_arg < 0) {
                first_nonoption_arg = argn;
              }
              argn++;
            } else {
              /* not permute args so return non-option args when found */
              if ((*value = (char *)malloc(strlen(arg) + 1)) == NULL) {
                oERR(ZE_MEM, "go");
              }
              strcpy(*value, arg);
              optc = NON_OPTION_ARG;
              option_ID = o_NON_OPTION_ARG;
              break;
            }
          }

        } else {
          option_ID = get_longopt(args, argn, &optc, negated, value, option_num, recursion_depth);
          if (option_ID == o_ARG_FILE_ERR) {
            /* unwind as only get this if recursion_depth > 0 */
            return option_ID;
          }
          break;
        }

      } else {
        /* short option */
        option_ID = get_shortopt(args, argn, &optc, negated, value, option_num, recursion_depth);

        if (option_ID == o_ARG_FILE_ERR) {
          /* unwind as only get this if recursion_depth > 0 */
          return option_ID;
        }

        if (optc == 0) {
          /* if optc = 0 then ran out of short opts this arg */
          optc = THIS_ARG_DONE;
        } else {
          break;
        }
      }

#if 0
    /* argument file code left out
       so for now let filenames start with @
    */

    } else if (allow_arg_files && arg[0] == '@') {
      /* arg file */
      oERR(ZE_PARMS, no_arg_files_err);
#endif

    } else {
      /* non-option */
      if (enable_permute) {
        /* permute args to move all non-option args to end */
        if (first_nonoption_arg < 0) {
          first_nonoption_arg = argn;
        }
        argn++;
      } else {
        /* no permute args so return non-option args when found */
        if ((*value = (char *)malloc(strlen(arg) + 1)) == NULL) {
          oERR(ZE_MEM, "go");
        }
        strcpy(*value, arg);
        *option_num = o_NO_OPTION_MATCH;
        optc = NON_OPTION_ARG;
        option_ID = o_NON_OPTION_ARG;
        break;
      }

    }
  }

  *pargs = args;
  *argc = argcnt;
  *first_nonopt_arg = first_nonoption_arg;
  *argnum = argn;
  *optchar = optc;

  return option_ID;
}