summaryrefslogtreecommitdiff
path: root/support/dktools/dk-ls.c
blob: 6ec01d987c8abe09b9da4d33ac722e6b1498f4a5 (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
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk-ls.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk-ls.c The dk-ls module.
*/


#line 87 "dk-ls.ctr"

#include "dk4conf.h"

#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define WINDOWS_H_INCLUDED
#endif
#endif

#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define SYS_TYPES_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define	UNISTD_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_PROCESS_H
#ifndef PROCESS_H_INCLUDED
#include <process.h>
#define	PROCESS_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_IO_H
#ifndef IO_H_INCLUDED
#include <io.h>
#define	IO_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_FCNTL_H
#ifndef FCNTL_H_INCLUDED
#include <fcntl.h>
#define	FCNTL_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SIGNAL_H
#ifndef SIGNAL_H_INCLUDED
#include <signal.h>
#define SIGNAL_H_INCLUDED 1
#endif
#endif

#include "dk4types.h"
#include "dk4const.h"
#include "dk4vers.h"
#include "dk4app.h"
#include "dk4aopt.h"
#include "dk4mem.h"
#include "dk4mema.h"
#include "dk4fput.h"
#include "dk-ls.h"
#include "dk4mpl.h"
#include "dk4stat.h"
#include "dk4statd.h"
#include "dk4strd.h"
#include "dk4fileit.h"
#include "dk4filei.h"
#include "dk4fileia.h"
#include "dk4masz.h"
#include "dk4maadu.h"
#include "dk4maodd.h"
#include "dk4maodh.h"
#include "dk4numl.h"
#include "dk4dira.h"
#include "dk4pathd.h"
#include "dk4fopda.h"
#include "dk4symlink.h"





#line 168 "dk-ls.ctr"



/**	Default help text, used if help text file is not found.
*/
static const dkChar * const	dk_ls_help_text[] = {
dkT(""),
dkT("List files"),
dkT(""),
dkT("dk-ls [<options>] [<file(s)>]"),
dkT(""),
dkT("Options:"),
dkT("-p ...    Output column order, see below.  -r        Recursive listing."),
dkT("-t ...    File types to list.              -m ...    Message digest type."),
dkT("-f        Stay on current file system.     -s        Show summary at end."),
dkT("--help    Show this short help text.       --manual  *** SHOW FULL MANUAL. ***"),
dkT("--version Show version information.        --license Show license information.  "),
dkT(""),
dkT("Column order characters: n=name, s=size, t=type, p=permissions/attributes,"),
dkT("c=creation, m=modification, a=access, l=links, u=UID, g=GID, d=device,"),
dkT("i=inode, r=relative device, w=reparse tag (Widnows), x=message digest."),
dkT("Default: ``mtpn'' (``mtn'' on Windows)."),
dkT(""),
dkT("File type characters: d=directories, f=non-directory entries."),
dkT(""),
dkT("Message digest types: md5, sha-1, sha-224, sha-256, sha-384, sha-512,"),
dkT("ripemd-160. Optionally add encoding hex or a85, i.e. ``sha-1.hex''."),
dkT(""),
dkT("http://dktools.sourceforge.net"),
dkT(""),
NULL


#line 200 "dk-ls.ctr"
};



/**	License conditions.
*/
static const dkChar * const	dk_ls_license_text[] = {
dkT("This software uses code from the following projects, either directly or as"),
dkT("a library:"),
dkT(""),
dkT("dktools\t\tDirk Krause's tools and libraries."),
dkT("\t\tSee http://dktools.sourceforge.net/ for more information."),
#if DK4_HAVE_ZLIB_H
dkT(""),
dkT("zlib\t\tData compression library."),
dkT("\t\tSee http://www.zlib.net/ for more information."),
#endif
#if DK4_HAVE_BZLIB_H
dkT(""),
dkT("bzip2\t\tData compression program and library."),
dkT("\t\tSee http://www.bzip.org/ for more information."),
#endif
#if DK4_HAVE_OPENSSL_MD5_H && DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_OPENSSL_RIPEMD_H
dkT(""),
dkT("OpenSSL\t\tCryptographic toolkit, used to build file checksums here."),
dkT("\t\tSee http://www.openssl.org/ for more information."),
dkT(""),
dkT("This product includes software developed by the OpenSSL Project for use in"),
dkT("the OpenSSL Toolkit (http://www.openssl.org/)."),
dkT("This product includes cryptographic software written by"),
dkT("Eric Young (eay@cryptsoft.com)."),
dkT("This product includes software written by Tim Hudson (tjh@cryptsoft.com)."),
#endif
dkT(""),
dkT("All the licenses below apply to the program."),
dkT("Licenses for used libraries are shown as found on my Scientific Linux 6.x"),
dkT("computer in the /usr/share/doc directory on 2015-04-01. Check the project"),
dkT("homepages of the used libraries for additional information and/or updated"),
dkT("license terms."),
dkT(""),
dkT(""),
dkT("DK tools and libraries license"),
dkT("=============================="),
dkT("Copyright (c) 2015-2016, Dirk Krause"),
dkT("All rights reserved."),
dkT(""),
dkT("Redistribution and use in source and binary forms, with or without"),
dkT("modification, are permitted provided that the following conditions are met:"),
dkT(""),
dkT("* Redistributions of source code must retain the above copyright notice,"),
dkT("  this list of conditions and the following disclaimer."),
dkT("* Redistributions in binary form must reproduce the above copyright"),
dkT("  notice, this list of conditions and the following disclaimer in the"),
dkT("  documentation and/or other materials provided with the distribution."),
dkT("* Neither the name of the Dirk Krause nor the names of contributors may be"),
dkT("  used to endorse or promote products derived from this software without"),
dkT("  specific prior written permission."),
dkT(""),
dkT("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\""),
dkT("AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE"),
dkT("IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE"),
dkT("ARE DISCLAIMED."),
dkT(""),
dkT("IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,"),
dkT("INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,"),
dkT("BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,"),
dkT("DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY"),
dkT("OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING"),
dkT("NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,"),
dkT("EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."),
#if DK4_HAVE_ZLIB_H
dkT(""),
dkT(""),
dkT("Zlib license"),
dkT("============"),
dkT("(C) 1995-2004 Jean-loup Gailly and Mark Adler"),
dkT(""),
dkT("This software is provided 'as-is', without any express or implied"),
dkT("warranty.  In no event will the authors be held liable for any damages"),
dkT("arising from the use of this software."),
dkT(""),
dkT("Permission is granted to anyone to use this software for any purpose,"),
dkT("including commercial applications, and to alter it and redistribute it"),
dkT("freely, subject to the following restrictions:"),
dkT(""),
dkT("1. The origin of this software must not be misrepresented; you must not"),
dkT("   claim that you wrote the original software. If you use this software"),
dkT("   in a product, an acknowledgment in the product documentation would be"),
dkT("   appreciated but is not required."),
dkT("2. Altered source versions must be plainly marked as such, and must not be"),
dkT("   misrepresented as being the original software."),
dkT("3. This notice may not be removed or altered from any source distribution."),
dkT(""),
dkT("Jean-loup Gailly        Mark Adler"),
dkT("jloup@gzip.org          madler@alumni.caltech.edu"),
#endif
#if DK4_HAVE_BZLIB_H
dkT(""),
dkT(""),
dkT("Bzip2 and libbzip2 library license"),
dkT("=================================="),
dkT("This program, \"bzip2\", the associated library \"libbzip2\", and all"),
dkT("documentation, are copyright (C) 1996-2007 Julian R Seward.  All"),
dkT("rights reserved."),
dkT(""),
dkT("Redistribution and use in source and binary forms, with or without"),
dkT("modification, are permitted provided that the following conditions"),
dkT("are met:"),
dkT(""),
dkT("1. Redistributions of source code must retain the above copyright"),
dkT("   notice, this list of conditions and the following disclaimer."),
dkT(""),
dkT("2. The origin of this software must not be misrepresented; you must "),
dkT("   not claim that you wrote the original software.  If you use this "),
dkT("   software in a product, an acknowledgment in the product "),
dkT("   documentation would be appreciated but is not required."),
dkT(""),
dkT("3. Altered source versions must be plainly marked as such, and must"),
dkT("   not be misrepresented as being the original software."),
dkT(""),
dkT("4. The name of the author may not be used to endorse or promote "),
dkT("   products derived from this software without specific prior written "),
dkT("   permission."),
dkT(""),
dkT("THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS"),
dkT("OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED"),
dkT("WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE"),
dkT("ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY"),
dkT("DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL"),
dkT("DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE"),
dkT("GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS"),
dkT("INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,"),
dkT("WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING"),
dkT("NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS"),
dkT("SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."),
dkT(""),
dkT("Julian Seward, jseward@bzip.org"),
dkT("bzip2/libbzip2 version 1.0.5 of 10 December 2007"),
#endif
#if DK4_HAVE_OPENSSL_MD5_H && DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_OPENSSL_RIPEMD_H
dkT(""),
dkT(""),
dkT("OpenSSL license"),
dkT("==============="),
dkT(""),
dkT("LICENSE ISSUES"),
dkT("--------------"),
dkT(""),
dkT("The OpenSSL toolkit stays under a dual license, i.e. both the conditions of"),
dkT("the OpenSSL License and the original SSLeay license apply to the toolkit."),
dkT("See below for the actual license texts. Actually both licenses are BSD-style"),
dkT("Open Source licenses. In case of any license issues related to OpenSSL"),
dkT("please contact openssl-core@openssl.org."),
dkT(""),
dkT("OpenSSL License"),
dkT("---------------"),
dkT(""),
dkT("Copyright (c) 1998-2011 The OpenSSL Project.  All rights reserved."),
dkT(""),
dkT("Redistribution and use in source and binary forms, with or without"),
dkT("modification, are permitted provided that the following conditions"),
dkT("are met:"),
dkT(""),
dkT("1. Redistributions of source code must retain the above copyright"),
dkT("   notice, this list of conditions and the following disclaimer. "),
dkT(""),
dkT("2. Redistributions in binary form must reproduce the above copyright"),
dkT("   notice, this list of conditions and the following disclaimer in"),
dkT("   the documentation and/or other materials provided with the"),
dkT("   distribution."),
dkT(""),
dkT("3. All advertising materials mentioning features or use of this"),
dkT("   software must display the following acknowledgment:"),
dkT("   \"This product includes software developed by the OpenSSL Project"),
dkT("   for use in the OpenSSL Toolkit. (http://www.openssl.org/)\""),
dkT(""),
dkT("4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to"),
dkT("   endorse or promote products derived from this software without"),
dkT("   prior written permission. For written permission, please contact"),
dkT("   openssl-core@openssl.org."),
dkT(""),
dkT("5. Products derived from this software may not be called \"OpenSSL\""),
dkT("   nor may \"OpenSSL\" appear in their names without prior written"),
dkT("   permission of the OpenSSL Project."),
dkT(""),
dkT("6. Redistributions of any form whatsoever must retain the following"),
dkT("   acknowledgment:"),
dkT("   \"This product includes software developed by the OpenSSL Project"),
dkT("   for use in the OpenSSL Toolkit (http://www.openssl.org/)\""),
dkT(""),
dkT("THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY"),
dkT("EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE"),
dkT("IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR"),
dkT("PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR"),
dkT("ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,"),
dkT("SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT"),
dkT("NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;"),
dkT("LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)"),
dkT("HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,"),
dkT("STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)"),
dkT("ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED"),
dkT("OF THE POSSIBILITY OF SUCH DAMAGE."),
dkT(""),
dkT("This product includes cryptographic software written by Eric Young"),
dkT("(eay@cryptsoft.com).  This product includes software written by Tim"),
dkT("Hudson (tjh@cryptsoft.com)."),
dkT(""),
dkT(""),
dkT("Original SSLeay License"),
dkT("-----------------------"),
dkT(""),
dkT("Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)"),
dkT("All rights reserved."),
dkT(""),
dkT("This package is an SSL implementation written"),
dkT("by Eric Young (eay@cryptsoft.com)."),
dkT("The implementation was written so as to conform with Netscapes SSL."),
dkT(""),
dkT("This library is free for commercial and non-commercial use as long as"),
dkT("the following conditions are aheared to.  The following conditions"),
dkT("apply to all code found in this distribution, be it the RC4, RSA,"),
dkT("lhash, DES, etc., code; not just the SSL code.  The SSL documentation"),
dkT("included with this distribution is covered by the same copyright terms"),
dkT("except that the holder is Tim Hudson (tjh@cryptsoft.com)."),
dkT(""),
dkT("Copyright remains Eric Young's, and as such any Copyright notices in"),
dkT("the code are not to be removed."),
dkT("If this package is used in a product, Eric Young should be given attribution"),
dkT("as the author of the parts of the library used."),
dkT("This can be in the form of a textual message at program startup or"),
dkT("in documentation (online or textual) provided with the package."),
dkT(""),
dkT("Redistribution and use in source and binary forms, with or without"),
dkT("modification, are permitted provided that the following conditions"),
dkT("are met:"),
dkT("1. Redistributions of source code must retain the copyright"),
dkT("   notice, this list of conditions and the following disclaimer."),
dkT("2. Redistributions in binary form must reproduce the above copyright"),
dkT("   notice, this list of conditions and the following disclaimer in the"),
dkT("   documentation and/or other materials provided with the distribution."),
dkT("3. All advertising materials mentioning features or use of this software"),
dkT("   must display the following acknowledgement:"),
dkT("   \"This product includes cryptographic software written by"),
dkT("    Eric Young (eay@cryptsoft.com)\""),
dkT("   The word 'cryptographic' can be left out if the rouines from the library"),
dkT("   being used are not cryptographic related :-)."),
dkT("4. If you include any Windows specific code (or a derivative thereof) from "),
dkT("   the apps directory (application code) you must include an acknowledgement:"),
dkT("   \"This product includes software written by Tim Hudson (tjh@cryptsoft.com)\""),
dkT(""),
dkT("THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND"),
dkT("ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE"),
dkT("IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE"),
dkT("ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE"),
dkT("FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL"),
dkT("DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS"),
dkT("OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)"),
dkT("HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT"),
dkT("LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY"),
dkT("OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF"),
dkT("SUCH DAMAGE."),
dkT(""),
dkT("The licence and distribution terms for any publically available version or"),
dkT("derivative of this code cannot be changed.  i.e. this code cannot simply be"),
dkT("copied and put under another distribution licence"),
dkT("[including the GNU Public Licence.]"),
dkT(""),
#endif
dkT(""),
NULL


#line 471 "dk-ls.ctr"
};



/**	Constant texts used by program, not localized.
*/
static const dkChar * const	dk_ls_kwnl[] = {
/* 0 */
dkT("dktools"),

/* 1 */
dkT("dk-ls.txt"),

/* 2 */
dkT("dk-ls.str"),

/* 3 */
dkT("mtpn"),

/* 4 */
dkT("."),

/* 5 */
dkT("\\"),

/* 6 */
dkT("/"),

/* 7 */
dkT(" -> "),

/* 8 */
dkT("mtn"),

/* 9 */
dkT("message.digest"),

/* 10 */
dkT("print.order"),

/* 11 */
dkT("ERROR: Application startup failed!\n"),

NULL


#line 529 "dk-ls.ctr"
};



/**	Constant texts used by program, replaced by localized texts
	if available.
*/
static const dkChar * const     dk_ls_kw_def[] = {
/* 0 */
dkT("Failed to set up signal handlers!"),

/* 1 */
dkT("Failed to restore previous signal handlers!"),

/* 2 */
dkT("OVERFLOW"),

/* 3 */
dkT("Failed to obtain file size for symbolic link:\n\t\""),

/* 4 */
dkT("Failed to obtain file size for target:\n\t\""),

/* 5 */
dkT("\"!\n\tInformation not available!"),

/* 6 */
dkT("Path grows too long while recursing directory!"),

/* 7 */
dkT("Failed to obtain timestamp for symbolic link:\n\t\""),

/* 8 */
dkT("Failed to obtain timestamp for target:\n\t\""),

/* 9 */
dkT("\"!\nInformation not available!"),

/* 10 */
dkT("Failed to obtain file type and attributes (bug)!"),

/* 11 */
dkT("Invalid text in print order!"),

/* 12 */
dkT("Failed to find target for symbolic link:\n\t\""),

/* 13 */
dkT("\"!"),

/* 14 */
dkT("Failed to obtain unique file identifier:\n\t\""),

/* 15 */
dkT("\"!"),

/* 16 */
dkT("File names too long for expansion:\n\t\""),

/* 17 */
dkT("\",\n\t\""),

/* 18 */
dkT("\"!"),

/* 19 */
dkT("Message digest type not supported!"),

/* 20 */
dkT("Message digest type not supported: \""),

/* 21 */
dkT("\"!"),

/* 22 */
dkT("Not a message digest type name: \""),

/* 23 */
dkT("\"!"),

/* 24 */
dkT("Invalid encoding for message digest: \""),

/* 25 */
dkT("\"!"),

/* 26 */
dkT("Failed to initialize digest context (error from library function)!"),

/* 27 */
dkT("Failed to initialize message digest context!"),

/* 28 */
dkT("Failed to add data to message digest!"),

/* 29 */
dkT("Failed to finish message digest!"),

/* 30 */
dkT("# Number of directories: "),

/* 31 */
dkT("# Number of files:       "),

/* 32 */
dkT("# Total number of bytes: "),

NULL


#line 632 "dk-ls.ctr"
};



#if DK4_ON_WINDOWS
/**	Buffer to store symbolic link destination.
*/
static dkChar	dk_ls_sl_target[32784];
#else
/**	Buffer to store symbolic link destination.
*/
static dkChar	dk_ls_sl_target[DK4_MAX_PATH];
#endif



/**	Options used by program.
*/
static const dk4_option_specification_t	dk_ls_options[] = {

  /*	Reset (ignore preferences).
  */
  { dkT('R'),	dkT("reset"),			DK4_OPT_ARG_NONE },

  /*	Print order.
  */
  { dkT('p'),	dkT("print-order"),		DK4_OPT_ARG_STRING },

  /*	Recursive listing.
  */
  { dkT('r'),	dkT("recursive"),		DK4_OPT_ARG_NONE },

  /*	For recursive listings, stay on the current file system.
  */
  { dkT('f'),	dkT("stay-on-filesystem"),	DK4_OPT_ARG_NONE },

  /*	File types to show (all others are ignored).
  */
  { dkT('t'),	dkT("types"),			DK4_OPT_ARG_STRING },

  { dkT('s'),	dkT("summary"),			DK4_OPT_ARG_NONE },

#if DK4_HAVE_OPENSSL_MD5_H && DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_OPENSSL_RIPEMD_H
  { dkT('m'),	dkT("message-digest"),		DK4_OPT_ARG_STRING },
#endif
};



/**	Buffer for print order from command line.
*/
static dkChar		 po_buf[32];



/**	Application structure.
*/
static dk4_app_t	*app	=	NULL;



/**	Print order.
*/
static const dkChar	*print_order	=	NULL;


/**	Message texts, uses localized texts if possible.
*/
static const dkChar * const	*dk_ls_msg	= dk_ls_kw_def;


/**	Error report for bytes counting.
*/
static	dk4_er_t	 er_bytes;


/**	Error report for files counting.
*/
static	dk4_er_t	 er_files;


/**	Error report for directory counting.
*/
static	dk4_er_t	 er_dirs;



/**	Size found so far.
*/
static	dk4_um_t	 size_bytes	=	(dk4_um_t)0UL;



/**	Number of files shown.
*/
static	dk4_um_t	 num_files	=	(dk4_um_t)0UL;



/**	Number of directories shown.
*/
static	dk4_um_t	 num_dirs	=	(dk4_um_t)0UL;




/**	Number of elements in the dk_ls_kw_def array and in dk_ls_msg.
*/
static size_t	dk_ls_sz_msg	=
(sizeof(dk_ls_kw_def)/sizeof(DK4_PDKCHAR) - 1);



#if DK4_ON_WINDOWS
/**	Limit for incrementing path construction buffer.
	On Windows systems a special notation allows very long path names
	up to 32767 characters.
*/
static const size_t	dk_ls_max_path	=	32784U;
#else
/**	Limit for incrementing path construction buffer.
	On non-Windows systems the path is restricted to DK4_MAX_PATH.
*/
static const size_t	dk_ls_max_path	=	2 * DK4_MAX_PATH;
#endif



/**	Number of elements in the dk_ls_options array.
*/
static const size_t dk_ls_sz_options =
sizeof(dk_ls_options)/sizeof(dk4_option_specification_t);



/**	Flag: Size output wanted.
*/
static int		 size_wanted	=	0;



/**	Flag: Name output wanted.
*/
static int		 name_wanted	=	0;



/**	Flag: Show non-directories.
*/
static int		 show_files	=	1;



/**	Flag: Show directories.
*/
static int		 show_dirs	=	1;



/**	Flag: Recursive listing.
*/
static int		 recursive	=	0;



/**	Flag: Show summary.
*/
static int		 show_summary	=	0;



/**	Flag: Stay on file system when recursing directories.
*/
static int		 stay_on_fs	=	0;



/**	Message digest type.
*/
static int		 digest_type	=	DK4_MD_SHA1;



/**	Message digest encoding.
*/
static int		 digest_enco	=	DK4_BINARY_TO_TEXT_ENCODING_HEX;



/**	Flag: Unsupported digest type already reported.
*/
static int		 digest_erep	 = 0;



/**	Flag: Write error occured while writing to standard output.
*/
static int	write_error	=	0;



/**	Exit status code.
*/
static int	exval	=	EXIT_FAILURE;



/**	Flag: Illegal characters in print order reported.
*/
static int	print_order_error_reported	=	0;



#ifdef SIGPIPE
/**	Indicator: SIGPIPE signal received.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t	sig_had_pipe	=	0;
#endif

/**	Indicator: SIGINT signal received.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t	sig_had_int	=	0;

/**	Indicator: SIGTERM signal received.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t	sig_had_term	=	0;



/**	Pass a volatile pointer to an atomic integer.
	This function is necessary as some compilers mis-optimize
	direct access to volatile variables (at least if you believe
	one of the coding standards).
	@param	ptr	Address of atomic integer variable.
	@return	The unmodified pointer.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t *
sig_pass_pointer(DK4_VOLATILE dk4_sig_atomic_t *ptr)
{
  return ptr;
}



#ifdef SIGPIPE
/**	Handler for SIGPIPE signal.
	@param	signo	Signal number (always SIGPIPE, ignored).
*/
static
void
sig_handler_pipe(int signo)
{
  *sig_pass_pointer(&sig_had_pipe) = 1;
}
#endif

/**	Handler for SIGINT signal.
	@param	signo	Signal number (always SIGINT, ignored).
*/
static
void
sig_handler_int(int signo)
{
  *sig_pass_pointer(&sig_had_int) = 1;
}

/**	Handler for SIGTERM signal.
	@param	signo	Signal number (always SIGTERM, ignored).
*/
static
void
sig_handler_term(int signo)
{
  *sig_pass_pointer(&sig_had_term) = 1;
}

/**	Read value from volatile atomic type.
	This function is necessary as some compilers mis-optimize
	direct access to volatile variables (at least if you believe
	one of the coding standards).
	@param	ap	Pointer to volatile atomic variable.
	@return	Contents of the variable.
*/
static
dk4_sig_atomic_t
sig_read_atomic(DK4_VOLATILE dk4_sig_atomic_t *ap)
{
  return (*ap);
}

/**	Check whether we can continue or if a signal was received.
	@param	check_pipe	Flag: Check for occured SIGPIPE signal too.
	@return	1 if the program can continue, 0 if a signal was received.
*/
static
int
sig_can_continue(int check_pipe)
{
  int		back = 1;
#ifdef SIGPIPE
  if (0 != check_pipe) {
    if (0 != sig_read_atomic(&sig_had_pipe)) { back = 0; }
  }
#endif
  if (0 != sig_read_atomic(&sig_had_int )) { back = 0; }
  if (0 != sig_read_atomic(&sig_had_term)) { back = 0; }
  return back;
}



/**	Check whether we can continue.
	@return	1 for yes, 0 for no.
*/
static
int
dk_ls_can_continue(void)
{
  int		back	=	1;
  if (0 == sig_can_continue(1)) {
    back = 0;
  }
  return back;
}



/**	Node type for a directory.
*/
typedef struct _dk_ls_node_t_ {
  struct _dk_ls_node_t_	*parent;	/**< Pointer to parent node. */
  struct _dk_ls_node_t_	*child;		/**< Pointer to current child. */
  dk4_dir_t		*dirptr;	/**< Directory. */
  dk4_file_info_t	 fi;		/**< File information. */
  dk4_um_t		 size;		/**< Directory size. */
  int			 level;		/**< Processing level. */
  int			 ovsize;	/**< Flag: Overflow in size calc. */
} dk_ls_node_t;



/**	Destroy node object, release memory.
	@param	cn	Node to destroy.
*/
static
void
dk_ls_delete_node(dk_ls_node_t *cn)
{
  

#line 989 "dk-ls.ctr"
  if (NULL != cn) {
    cn->parent = NULL;
    cn->child = NULL;
    if (NULL != cn->dirptr) {
      dk4dir_close(cn->dirptr);
    }
    cn->dirptr = NULL;
    dk4mem_free(cn);
  } 

#line 998 "dk-ls.ctr"
}



/**	Create new new directory node.
	@param	dn	Directory name for node.
	@param	pa	Parent node.
	@param	level	Processing level.
	@return	Pointer to new node on success, NULL on error.
*/
static
dk_ls_node_t *
dk_ls_new_node(const dkChar *dn, dk_ls_node_t *pa, int level)
{
  dk4_er_t	 er;
  dk_ls_node_t	*back	=	NULL;
  int		 om	=	DK4_DIR_OPEN_SORTED;
  int		 ch	=	DK4_FILE_INFO_CONTENTS_DATA_TARGET;
  

#line 1017 "dk-ls.ctr"
  back = dk4mem_new_app(dk_ls_node_t,1,app);
  if (NULL != back) {
    DK4_MEMRES(back, sizeof(dk_ls_node_t));
    back->parent = pa;
    back->child  = NULL;
    back->dirptr = NULL;
    back->level  = level;
    back->size   = (dk4_um_t)0UL;
    back->ovsize = 0;
    if (3 < back->level) { back->level = 3; }
    om |= DK4_DIR_SYMLINK_DIR_AS_FILE;
    dk4error_init(&er);
    back->dirptr = dk4dir_open_app(dn, om, app);
    if (NULL == back->dirptr) {			

#line 1031 "dk-ls.ctr"
      exval = EXIT_FAILURE;
      dk_ls_delete_node(back);
      back = NULL;
    }
    if (NULL != back) {			

#line 1036 "dk-ls.ctr"
      dk4fileinfo_init(&(back->fi));
      if (0 != dk4fileinfo_app(&(back->fi), dn, app)) {
        if (0 == dk4fileinfo_size(&(back->size), &(back->fi), ch, NULL)) {
	  back->size = (dk4_um_t)0UL;	

#line 1040 "dk-ls.ctr"
	  /* ERROR: Failed to retrieve directory size */
	  dk_ls_report_fileinfo_size_error(app,dn,ch,dk_ls_msg,dk_ls_sz_msg);
	  exval = EXIT_FAILURE;
	}
      } else {				

#line 1045 "dk-ls.ctr"
        /* ERROR: Failed to retrieve file information, already reported */
	exval = EXIT_FAILURE;
      }
    } else {				

#line 1049 "dk-ls.ctr"
      /* ERROR: Failed to open directory, already reported */
      exval = EXIT_FAILURE;
    }
  } else {				

#line 1053 "dk-ls.ctr"
    /* ERROR: Allocation failed, already reported */
    exval = EXIT_FAILURE;
  }
  

#line 1057 "dk-ls.ctr"
  return back;
}



/**	Construct a path name consisting of directory and
	file or subdirectory name into an existing buffer.
	@param	bufptr		Buffer address.
	@param	szbuf		Size of buffer.
	@param	dirname		Leading part of path name.
	@param	filename	Trailing part of path name.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error (buffer too short).
*/
static
int
dk_ls_dir_and_file(
  dkChar		*bufptr,
  size_t		 szbuf,
  const dkChar		*dirname,
  const dkChar		*filename
)
{
  int		 back = 0;
  int		 nsep = 1;	/* Flag: Separator needed */
  

#line 1083 "dk-ls.ctr"
  if (NULL != dirname) {
    if (0 != dk4str_cpy_s(bufptr, szbuf, dirname, NULL)) {
      while(dkT('\0') != *dirname) {
#if DK4_ON_WINDOWS
        if (dkT('\\') == *dirname)
#else
	if (dkT('/') == *dirname)
#endif
	{
	  nsep = 0;
	} else {
	  nsep = 1;
	}
        dirname++;
      }
      if (0 != nsep) {
#if DK4_ON_WINDOWS
	if (0 != dk4str_cat_s(bufptr, szbuf, dk_ls_kwnl[5], NULL))
#else
	if (0 != dk4str_cat_s(bufptr, szbuf, dk_ls_kwnl[6], NULL))
#endif
	{
	  back = 1;
	}
      } else {
        back = 1;
      }
      if (0 != back) {
        back = 0;
	if (0 != dk4str_cat_s(bufptr, szbuf, filename, NULL)) {
	  back = 1;
	} else {		

#line 1115 "dk-ls.ctr"
	  /* BUG: Buffer too small */
	  exval = EXIT_FAILURE;
	}
      } else {			

#line 1119 "dk-ls.ctr"
        /* BUG: Buffer too small */
        exval = EXIT_FAILURE;
      }
    } else {			

#line 1123 "dk-ls.ctr"
      /* BUG: Buffer too small */
      exval = EXIT_FAILURE;
    }
  } else {
    back = dk4str_cpy_s(bufptr, szbuf, filename, NULL);
    if (0 == back) {		

#line 1129 "dk-ls.ctr"
      /* BUG: Buffer too small */
      exval = EXIT_FAILURE;
    }
  } 

#line 1133 "dk-ls.ctr"
  return back;
}



/**	Construct a file or directory name, resize buffer if
	necessary.
	@param	bufptr		Address of pointer to buffer.
	@param	szbufptr	Address of buffer size variable.
	@param	dirname		Directory part of path to construct.
	@param	filename	File name part of path to construct.
	@return	1 on success, 0 on error.
*/
static
int
dk_ls_construct_path(
  dkChar		**bufptr,
  size_t		 *szbufptr,
  const dkChar		 *dirname,
  const dkChar		 *filename
)
{
  dk4_er_t	 er;		/* Error code */
  dkChar	*nb;		/* New buffer */
  size_t	 lgt;		/* Required buffer size */
  int		 back = 0;
  

#line 1160 "dk-ls.ctr"
  dk4error_init(&er);
  lgt = dk4str_len(filename);
  if (NULL != dirname) {
    lgt = dk4ma_size_t_add(lgt, dk4str_len(dirname), &er);
    lgt = dk4ma_size_t_add(lgt, 2, &er);
  }
  if (DK4_E_NONE == er.ec) {
    if (lgt <= *szbufptr) {
      /*
      	Keep current buffer.
      */
      

#line 1172 "dk-ls.ctr"
      back = dk_ls_dir_and_file(*bufptr, *szbufptr, dirname, filename);
    } else {
      if (dk_ls_max_path > lgt) {
        /*
      	  Allocate new larger buffer.
        */
        

#line 1179 "dk-ls.ctr"
        nb = dk4mem_new_app(dkChar, lgt, app);
        if (NULL != nb) {			

#line 1181 "dk-ls.ctr"
          dk4mem_release(*bufptr);
	  *bufptr = nb;
	  *szbufptr = lgt;
	  back = dk_ls_dir_and_file(*bufptr, *szbufptr, dirname, filename);
        } else {				

#line 1186 "dk-ls.ctr"
          /* ERROR: Memory allocation failed, already reported */
	  exval = EXIT_FAILURE;
        }
      } else {
        /* ERROR: Path size too long */
	dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 6);
	exval = EXIT_FAILURE;
      }
    }
  } else {
    /* ERROR: Overflow in size calculation */
    dk4app_log_base1(app, DK4_LL_ERROR, 82);
    exval = EXIT_FAILURE;
  }
  

#line 1201 "dk-ls.ctr"
  return back;
}



static
void
dk_ls_write_decimal_number(dk4_um_t value, size_t width)
{
  dkChar	buf[16 + 8 * sizeof(dk4_um_t)];
  dk4_er_t	er;
  size_t	sl;
  int		res;
  dk4error_init(&er);
  res = dk4ma_write_decimal_unsigned(
    buf, DK4_SIZEOF(buf,dkChar), value, 0, &er
  );
  if (0 != res) {
    sl = dk4str_len(buf);
    while (sl++ < width) {
      if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
    if (0 == dk4fputs(buf, stdout, NULL)) {
      write_error = 1; exval = EXIT_FAILURE;
    }
  } else {			

#line 1229 "dk-ls.ctr"
    /* ERROR: Failed to write decimal number to buffer (bug) */
    exval = EXIT_FAILURE;
  }
}



static
void
dk_ls_write_hexadecimal_number(dk4_um_t value, size_t width)
{
  dkChar	buf[16 + 8 * sizeof(dk4_um_t)];
  dk4_er_t	er;
  size_t	sl;
  int		res;
  

#line 1245 "dk-ls.ctr"
  dk4error_init(&er);
  res = dk4ma_write_hex_unsigned(
    buf, DK4_SIZEOF(buf,dkChar), value, width, &er
  );
  if (0 != res) {				

#line 1250 "dk-ls.ctr"
    sl = dk4str_len(buf);
    while (sl++ < width) {			

#line 1252 "dk-ls.ctr"
      if (0 == dk4fputc(dkT('0'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
    if (0 == dk4fputs(buf, stdout, NULL)) {	

#line 1257 "dk-ls.ctr"
      write_error = 1; exval = EXIT_FAILURE;
    }
  } else {					

#line 1260 "dk-ls.ctr"
    /* BUG: Failed to write hexadecimal number to buffer */
    exval = EXIT_FAILURE;
  }
  

#line 1264 "dk-ls.ctr"
}



/**	Write file timestamp.
	@param	fiptr	File information.
	@param	fn	File name.
	@param	what	Choose the timestamp.
	@param	issl	Flag: Object is symbolic link.
*/
static
void
dk_ls_write_timestamp(
  dk4_file_info_t *fiptr, const dkChar *fn, int what, int issl
)
{
  dk4_er_t	er;
  dkChar	buf[64];
  int		ch;
  int		res;

  dk4error_init(&er);
  ch = (
    (0 != issl)
    ? DK4_FILE_INFO_CONTENTS_DATA_LINK
    : DK4_FILE_INFO_CONTENTS_DATA_TARGET
  );
  res = dk4fileinfo_timestamp(buf,DK4_SIZEOF(buf,dkChar),fiptr,ch,what,&er);
  if (0 != res) {
    if (0 == dk4fputs(buf, stdout, &er)) {
      write_error = 1; exval = EXIT_FAILURE;
    }
  } else {
    /* ERRROR: Failed to obtain time */
    dk_ls_report_fileinfo_timestamp_error(
      app, fn, ch, dk_ls_msg, dk_ls_sz_msg
    );
    exval = EXIT_FAILURE;
  }
}



/**	Write indicator for unkown data.
	@param	width	Field width.
	@param	compl	Fill complete field.
*/
static
void
dk_ls_write_unknown(size_t width, int compl)
{
  if (0 != compl) {
    while (0 < width--) {
      if (0 == dk4fputc(dkT('?'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
  } else {
    while (1 < width--) {
      if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
    if (0 == dk4fputc(dkT('?'), stdout, NULL)) {
      write_error = 1; exval = EXIT_FAILURE;
    }
  }
}



static
int
dk_ls_can_create_md(const dkChar *fn, dk4_file_info_t *fiptr)
{
  int back = 0;
  if (0 == dk4fileinfo_is_symlink(fiptr)) {
#if DK4_ON_WINDOWS
    if (0 == (FILE_ATTRIBUTE_REPARSE_POINT & (fiptr->fattr))) {
      if (0 == (FILE_ATTRIBUTE_DIRECTORY & (fiptr->fattr))) {
        if (0 == (FILE_ATTRIBUTE_DEVICE & (fiptr->fattr))) {
	  back = 1;
	}
      }
    }
#else
    if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & (fiptr->contents))) {
      if (S_IFREG == (S_IFMT & ((fiptr->tstb).st_mode))) {
        back = 1;
      }
    }
#endif
  }
  return back;
}



/**	Write message digest.
	@param	fn		File name.
	@param	fiptr		File information pointer.
	@param	digest_type	Message digest type.
	@param	digest_enco	Message digest encoding.
	@param	app		Application structure for diagnostics.
	@param	dk_ls_msg	Message texts array.
	@param	dk_ls_sz_msg	Size of message texts error.
*/
static
void
dk_ls_write_md(
  const dkChar		*fn,
  dk4_file_info_t	*fiptr,
  int			 digest_type,
  int			 digest_enco,
  dk4_app_t		*app,
  const dkChar * const	*dk_ls_msg,
  size_t		 dk_ls_sz_msg
)
{
  char		 buf[4096];		/* Buffer to read file */
  dk4_md_ctx_t	 ctx;			/* Message digest context */
  dk4_er_t	 er;			/* Error report */
  FILE		*fipo;			/* Input file to process */
  const char	*resptr;		/* Pointer to encoded digest buffer */
  size_t	 rdbytes;		/* Number of bytes read */
  int		 mdok = 0;		/* Flag: Digesting ok so far */
  if (0 != dk_ls_can_create_md(fn, fiptr)) {	

#line 1391 "dk-ls.ctr"
    dk4error_init(&er);
    if (0 != dk4md_ctx_init(&ctx, digest_type, digest_enco, &er)) {
      

#line 1394 "dk-ls.ctr"
      fipo = dk4fopen_app(fn, dkT("rb"), DK4_FOPEN_SC_IS_REGULAR, app);
      if (NULL != fipo) {			

#line 1396 "dk-ls.ctr"
        mdok = 1;
        do {
	  if (0 != dk_ls_can_continue())  {
	    rdbytes = fread(buf, 1, sizeof(buf), fipo);
	    if (0 < rdbytes) {			

#line 1401 "dk-ls.ctr"
	      dk4error_init(&er);
	      if (1 != dk4md_ctx_add(&ctx, buf, rdbytes, &er)) {
	        mdok = 0;				

#line 1404 "dk-ls.ctr"
	        /* ERROR: Digest failed */
	        switch (er.ec) {
	          case DK4_E_NOT_SUPPORTED : {
		    dk4app_log_1(app,dk_ls_msg,dk_ls_sz_msg,DK4_LL_ERROR,19);
		  } break;
		  default : {
		    dk4app_log_1(app,dk_ls_msg,dk_ls_sz_msg,DK4_LL_ERROR,28);
		  } break;
	        }
	      }
	    }
	  } else {
	    mdok = 0;
	    rdbytes = 0;
	  }
	} while((0 < rdbytes) && (1 == mdok));
        fclose(fipo);
	if (1 == mdok) {			

#line 1422 "dk-ls.ctr"
	  resptr = NULL; rdbytes = 0;
	  dk4error_init(&er);
	  if (1 == dk4md_ctx_finish(&resptr, &rdbytes, &ctx, &er)) {
	    

#line 1426 "dk-ls.ctr"
	    if ((NULL != resptr) && (0 < rdbytes)) {	

#line 1427 "dk-ls.ctr"
	      while(rdbytes--) {
	        if (0 == dk4fputc((dkChar)(*(resptr++)), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE; 

#line 1430 "dk-ls.ctr"
		}
	      }
	    } else {				

#line 1433 "dk-ls.ctr"
	      mdok = 0;
	      /* ERROR: No digest obtained */
	      dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 29);
	    }
	  } else {				

#line 1438 "dk-ls.ctr"
	    mdok = 0;
	    /* ERROR: Failed to obtain digest */
	    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 29);
	  }
	} else {				

#line 1443 "dk-ls.ctr"
	}
      } else {					

#line 1445 "dk-ls.ctr"
        /* ERROR: Failed to open file, already reported */
      }
    } else {					

#line 1448 "dk-ls.ctr"
      /* ERROR: Failed to initialize digest ctx */
      switch (er.ec) {
        case DK4_E_NOT_SUPPORTED : {
	  dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 19);
	} break;
	case DK4_E_SYSTEM : {
	  dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 26);
	} break;
	default : {
	  dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 27);
	} break;
      }
    }
  } else {					

#line 1462 "dk-ls.ctr"
  }
  if (0 == mdok) {
    rdbytes = dk4md_size(digest_type, digest_enco, NULL);
    while (rdbytes--) {
      if (0 == dk4fputc(dkT('x'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
  }
}


/**	Write information about file to standard output.
	@param	fn		File name.
	@param	fiptr		File information.
	@param	use_size	Flag: Use specified size.
	@param	size		Size to use.
	@param	ovsize		Flag: Numeric overflow in size calculation.
*/
static
void
dk_ls_process_item_information(
  const	dkChar		*fn,
  dk4_file_info_t	*fiptr,
  int			 use_size,
  dk4_um_t		 size,
  int			 ovsize
)
{
  dkChar		 abuf[8*sizeof(dk4_um_t) + 16];	/* Attributes */
  dkChar		 buf[8*sizeof(dk4_um_t) + 16];	/* Numbers */
  dk4_er_t		 er;				/* Error report */
  const dkChar		*piptr;				/* Print order */
  dk4_um_t		 fs;				/* File size */
  size_t		 numl;				/* Number length */
  size_t		 slen;				/* String length */
  size_t		 tlen;				/* First part length */
  size_t		 i;				/* Index variable */
  int			 issl;			/* Flag: Is symbolic link */
  int			 res;		/* Result from information retrieval */
  int			 showi;		/* Flag: Show this item */
  int			 habuf;		/* Flag: Data in abuf */
  int			 last_was_n;	/* Flag: n was last character */
  dkChar		 lasti;		/* Last information printed */
  

#line 1507 "dk-ls.ctr"
  last_was_n = habuf = 0;
  if ((NULL != fn) && (NULL != fiptr)) {		

#line 1509 "dk-ls.ctr"
    showi = 1;
    res = dk4fileinfo_type_attributes(
      buf, DK4_SIZEOF(buf,dkChar), fiptr, &er
    );
    if (0 != res) {
      showi = 0;
      if (dkT('d') == buf[1]) {
        if (0 != show_dirs) {
	  showi = 1;
	  if (0 != show_summary) {
	     num_dirs = dk4ma_um_add(num_dirs, 1, &er_dirs);
	  }
	}
      } else {
        if (0 != show_files) {
	  showi = 1;
	  if (0 != show_summary) {
	    num_files = dk4ma_um_add(num_files, 1, &er_files);
	  }
	}
      }
    } else {			

#line 1531 "dk-ls.ctr"
      /* BUG: Failed to write file attributes to buffer */
      dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 10);
      exval = EXIT_FAILURE;
    }
    if (0 != showi) {		

#line 1536 "dk-ls.ctr"
      issl = dk4fileinfo_is_symlink(fiptr);
      lasti = dkT('\0');
      piptr = print_order;
      while (dkT('\0') != *piptr) {		

#line 1540 "dk-ls.ctr"
        last_was_n = 0;
        if ((int)(*piptr) <= 255) {		

#line 1542 "dk-ls.ctr"
          switch ((char)(*piptr)) {
            case 'n' : {			

#line 1544 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
	          write_error = 1; exval = EXIT_FAILURE;
	        }
	      }
	      if (0 == dk4fputs(fn, stdout, NULL)) {
	        write_error = 1; exval = EXIT_FAILURE;
	      }
	      last_was_n = 1;
	    } break;
            case 'N' : {			

#line 1555 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
	          write_error = 1; exval = EXIT_FAILURE;
	        }
	      }
	      if (0 == dk4fputs(fn, stdout, NULL)) {
	        write_error = 1; exval = EXIT_FAILURE;
	      }
	    } break;
	    case 's' : {					

#line 1565 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
	          write_error = 1; exval = EXIT_FAILURE;
	        }
	      }
	      res = 0;
	      if (0 != use_size) {
	        if (0 != ovsize) {
	          /* Show Overflow */
		  dk4error_set_simple_error_code(&er_bytes, DK4_E_MATH_OVERFLOW);
		  slen = dk4str_len(dk_ls_msg[2]);
	          numl = dk4numlength(sizeof(dk4_um_t), 0);
		  tlen = (numl - slen) / 2;
		  for (i = 0; i < tlen; i++) {
		    if (0 == dk4fputc(dkT('-'), stdout, NULL)) {
		      write_error = 1; exval = EXIT_FAILURE;
		    }
		  }
		  if (0 == dk4fputs(dk_ls_msg[2], stdout, NULL)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
		  for (i = (tlen + slen); i < numl; i++) {
		    if (0 == dk4fputc(dkT('-'), stdout, NULL)) {
		      write_error = 1; exval = EXIT_FAILURE;
		    }
		  }
	        } else {
	          /* Show size */
		  fs = size;
		  res = 1;
	        }
	      } else {
	        dk4error_init(&er);
	        res = dk4fileinfo_size(
	          &fs, fiptr,
		  (
		    (0 != issl)
		    ? (DK4_FILE_INFO_CONTENTS_DATA_LINK)
		    : (DK4_FILE_INFO_CONTENTS_DATA_TARGET)
		  ),
		  &er
	        );
	        if (0 == res) {
	          /* ERROR: Failed to obtain size */
		  

#line 1610 "dk-ls.ctr"
		  dk_ls_report_fileinfo_size_error(
		    app, fn,
		    (
		      (0 != issl)
		      ? (DK4_FILE_INFO_CONTENTS_DATA_LINK)
		      : (DK4_FILE_INFO_CONTENTS_DATA_TARGET)
		    ),
		    dk_ls_msg, dk_ls_sz_msg
		  );
	        }
	      }
	      if (0 != res) {
	        numl = dk4numlength(sizeof(dk4_um_t), 0);
	        dk_ls_write_decimal_number(fs, numl);
		if (0 != show_summary) {
		  size_bytes = dk4ma_um_add(size_bytes, fs, &er_bytes);
		}
	      }
	    } break;
	    case 't' : {			

#line 1630 "dk-ls.ctr"
	      dk4error_init(&er);
	      if (0 == habuf) {
	        habuf = dk4fileinfo_type_attributes(
	          abuf, DK4_SIZEOF(abuf,dkChar), fiptr, &er
	        );
	      }
	      if (0 != habuf) {
	        if (dkT('\0') != lasti) {
	          if (0 == dk4fputc(dkT(' '), stdout, &er)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
	        }
	        if (0 == dk4fputc(abuf[0], stdout, &er)) {
	          write_error = 1; exval = EXIT_FAILURE;
	        }
		if (0 == dk4fputc(abuf[1], stdout, &er)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      } else {
	        /* ERROR: Failed to obtain file type */
		dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 10);
	      }
	    } break;
	    case 'p' : {
	      if (0 == habuf) {
	        habuf = dk4fileinfo_type_attributes(
		  abuf, DK4_SIZEOF(abuf,dkChar), fiptr, &er
		);
	      }
	      if (0 != habuf) {
	        if ((dkT('\0') != lasti) && (dkT('t') != lasti)) {
		  if (0 == dk4fputc(dkT(' '), stdout, &er)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
		}
		if (0 == dk4fputs(&(abuf[2]), stdout, &er)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      } else {
	        /* ERROR: Failed to obtain file type */
		dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 10);
	      }
	    } break;
	    case 'l' : {			

#line 1674 "dk-ls.ctr"
	      fs = (dk4_um_t)1UL;
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
		{
#if DK4_ON_WINDOWS
		  fs = (dk4_um_t)((fiptr->linfo).nNumberOfLinks);
#else
		  fs = (dk4_um_t)((fiptr->lstb).st_nlink);
#endif
		}
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
		{
#if DK4_ON_WINDOWS
		  fs = (dk4_um_t)((fiptr->tinfo).nNumberOfLinks);
#else
		  fs = (dk4_um_t)((fiptr->tstb).st_nlink);
#endif
		}
	      }
#if DK4_ON_WINDOWS
	      if ((DWORD)0UL == fiptr->resio) {
#endif
	        dk_ls_write_decimal_number(
	          fs,
#if DK4_ON_WINDOWS
		  dk4numlength(sizeof(DWORD), 0)
#else
		  dk4numlength(sizeof(nlink_t), 0)
#endif
	        );
#if DK4_ON_WINDOWS
	      } else {
	        slen = dk4numlength(sizeof(DWORD), 0);
		for (i = 1; i < slen; i++) {
		  if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
		}
		if (0 == dk4fputc(dkT('?'), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
#endif
	    } break;
	    case 'c' : {			

#line 1720 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
	      dk_ls_write_timestamp(fiptr, fn, DK4_FILE_INFO_TIME_CREATE, issl);
	    } break;
	    case 'm' : {			

#line 1728 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
	      dk_ls_write_timestamp(fiptr, fn, DK4_FILE_INFO_TIME_MODIFY, issl);
	    } break;
	    case 'a' : {			

#line 1736 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
	      dk_ls_write_timestamp(fiptr, fn, DK4_FILE_INFO_TIME_ACCESS, issl);
	    } break;
#if !DK4_ON_WINDOWS
	    case 'u' : {			

#line 1745 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
		{
		  dk_ls_write_decimal_number(
		    (fiptr->lstb).st_uid,
		    dk4numlength(sizeof(uid_t), 0)
		  );
		}
		else
		{
		  dk_ls_write_unknown(dk4numlength(sizeof(uid_t), 0), 0);
		}
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
		{
		  dk_ls_write_decimal_number(
		    (fiptr->tstb).st_uid,
		    dk4numlength(sizeof(uid_t), 0)
		  );
		}
		else
		{
		  dk_ls_write_unknown(dk4numlength(sizeof(uid_t), 0), 0);
		}
	      }
	    } break;
#endif
#if !DK4_ON_WINDOWS
	    case 'g' : {			

#line 1779 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
		{
		  dk_ls_write_decimal_number(
		    (fiptr->lstb).st_gid,
		    dk4numlength(sizeof(gid_t), 0)
		  );
		}
		else
		{
		  dk_ls_write_unknown(dk4numlength(sizeof(gid_t), 0), 0);
		}
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
		{
		  dk_ls_write_decimal_number(
		    (fiptr->tstb).st_gid,
		    dk4numlength(sizeof(gid_t), 0)
		  );
		}
		else
		{
		  dk_ls_write_unknown(dk4numlength(sizeof(gid_t), 0), 0);
		}
	      }
	    } break;
#endif
	    case 'd' : {			

#line 1812 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
#if DK4_ON_WINDOWS
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
	        {
		  if ((DWORD)0UL == fiptr->resio) {
		    dk_ls_write_hexadecimal_number(
		      (fiptr->linfo).dwVolumeSerialNumber, 2*sizeof(DWORD)
		    );
		  } else {
		    dk_ls_write_unknown(2*sizeof(DWORD), 1);
		  }
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(DWORD), 1);
	        }
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
	        {
		  if ((DWORD)0UL == fiptr->resio) {
		    dk_ls_write_hexadecimal_number(
		      (fiptr->tinfo).dwVolumeSerialNumber, 2*sizeof(DWORD)
		    );
		  } else {
		    dk_ls_write_unknown(2*sizeof(DWORD), 1);
		  }
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(DWORD), 1);
	        }
	      }
#else
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
	        {
		  dk_ls_write_hexadecimal_number(
		    (fiptr->lstb).st_dev, 2*sizeof(dev_t)
		  );
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(dev_t), 1);
	        }
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
	        {
		  dk_ls_write_hexadecimal_number(
		    (fiptr->tstb).st_dev, 2*sizeof(dev_t)
		  );
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(dev_t), 1);
	        }
	      }
#endif
	    } break;
	    case 'r' : {			

#line 1876 "dk-ls.ctr"
#if DK4_ON_WINDOWS
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
	        {
		  if ((DWORD)0UL == fiptr->resio) {
		    dk_ls_write_hexadecimal_number(
		      (fiptr->linfo).dwVolumeSerialNumber, 2*sizeof(DWORD)
		    );
		  } else {
		    dk_ls_write_unknown(2*sizeof(DWORD), 1);
		  }
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(DWORD), 1);
	        }
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
	        {
		  if ((DWORD)0UL == fiptr->resio) {
		    dk_ls_write_hexadecimal_number(
		      (fiptr->tinfo).dwVolumeSerialNumber, 2*sizeof(DWORD)
		    );
		  } else {
		    dk_ls_write_unknown(2*sizeof(DWORD), 1);
		  }
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(DWORD), 1);
	        }
	      }
#else
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
	        {
		  dk_ls_write_hexadecimal_number(
		    (fiptr->lstb).st_rdev, 2*sizeof(dev_t)
		  );
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(dev_t), 1);
	        }
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
	        {
		  dk_ls_write_hexadecimal_number(
		    (fiptr->tstb).st_rdev, 2*sizeof(dev_t)
		  );
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(dev_t), 1);
	        }
	      }
#endif
	    } break;
	    case 'i' : {			

#line 1935 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (dkT('d') != lasti) {
	          if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
		} else {
	          if (0 == dk4fputc(dkT(':'), stdout, NULL)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
		}
	      }
#if DK4_ON_WINDOWS
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
	        {
		  if ((DWORD)0UL == fiptr->resio) {
		    dk_ls_write_hexadecimal_number(
		      (fiptr->linfo).nFileIndexHigh, 2*sizeof(DWORD)
		    );
		    dk_ls_write_hexadecimal_number(
		      (fiptr->linfo).nFileIndexLow, 2*sizeof(DWORD)
		    );
		  } else {
		    dk_ls_write_unknown(4*sizeof(DWORD), 1);
		  }
	        }
	        else
	        {
	          dk_ls_write_unknown(4*sizeof(DWORD), 1);
	        }
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
	        {
		  if ((DWORD)0UL == fiptr->resio) {
		    dk_ls_write_hexadecimal_number(
		      (fiptr->tinfo).nFileIndexHigh, 2*sizeof(DWORD)
		    );
		    dk_ls_write_hexadecimal_number(
		      (fiptr->tinfo).nFileIndexLow, 2*sizeof(DWORD)
		    );
		  } else {
		    dk_ls_write_unknown(4*sizeof(DWORD), 1);
		  }
	        }
	        else
	        {
	          dk_ls_write_unknown(4*sizeof(DWORD), 1);
	        }
	      }
#else
	      if (0 != issl) {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_LINK & fiptr->contents))
	        {
		  dk_ls_write_hexadecimal_number(
		    (fiptr->lstb).st_ino, 2*sizeof(ino_t)
		  );
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(ino_t), 1);
	        }
	      } else {
	        if (0 != (DK4_FILE_INFO_CONTENTS_DATA_TARGET & fiptr->contents))
	        {
		  dk_ls_write_hexadecimal_number(
		    (fiptr->tstb).st_ino, 2*sizeof(ino_t)
		  );
	        }
	        else
	        {
	          dk_ls_write_unknown(2*sizeof(ino_t), 1);
	        }
	      }
#endif
	    } break;
#if DK4_ON_WINDOWS
	    case 'w' : {			

#line 2012 "dk-ls.ctr"
	      if (dkT('\0') != lasti) {
	        if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		  write_error = 1; exval = EXIT_FAILURE;
		}
	      }
	      dk_ls_write_hexadecimal_number(
	        fiptr->rppnt, 2*sizeof(DWORD)
	      );
	    } break;
#endif
	    case 'x' : {
	      if (0 != dk4md_check_type(digest_type)) {
	        if (dkT('\0') != lasti) {
		  if (0 == dk4fputc(dkT(' '), stdout, NULL)) {
		    write_error = 1; exval = EXIT_FAILURE;
		  }
		}
		dk_ls_write_md(
		  fn, fiptr, digest_type, digest_enco, app,
		  dk_ls_msg, dk_ls_sz_msg
		);
	      } else {
	        /* ERROR (once) Message digest type not supported */
		if (0 == digest_erep) {
		  dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 19);
		  digest_erep = 1;
		}
	      }
	    } break;
	    default : {
	      /* ERROR (once): Invalid text in -p argument */
	      if (0 == print_order_error_reported) {
		dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 11);
		print_order_error_reported = 1;
	      }
	      exval = EXIT_FAILURE;
	    } break;
          }
        } else {
	  /* ERROR (once): Invalid text in -p argument */
	  if (0 == print_order_error_reported) {
	    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 11);
	    print_order_error_reported = 1;
	  }
	  exval = EXIT_FAILURE;
	}
        lasti = *(piptr++);
      }
      if (0 != last_was_n) {
        if (0 != dk4fileinfo_is_symlink(fiptr)) {
	  res = dk4symlink_target(
	    dk_ls_sl_target, DK4_SIZEOF(dk_ls_sl_target,dkChar), fn, NULL
	  );
	  if (0 != res) {
	    if (0 == dk4fputs(dk_ls_kwnl[7], stdout, NULL)) {
	      write_error = 1; exval = EXIT_FAILURE;
	    }
	    if (0 == dk4fputs(dk_ls_sl_target, stdout, NULL)) {
	      write_error = 1; exval = EXIT_FAILURE;
	    }
	  } else {
	    /* ERROR: Failed to find symlink target */
	    dk4app_log_3(app,dk_ls_msg,dk_ls_sz_msg,DK4_LL_ERROR,12,13,fn);
	    exval = EXIT_FAILURE;
	  }
        }
      }
      if (0 == dk4fputc(dkT('\n'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    } else {						

#line 2083 "dk-ls.ctr"
    }
  } else {						

#line 2085 "dk-ls.ctr"
  }
  

#line 2087 "dk-ls.ctr"
}



/**	Process non-directory file.
	@param	fn	File name.
	@param	level	Processing level (0=cmd line arg, 1=direct child,
	2 and above=further indicrect siblings).
*/
static
void
dk_ls_process_non_directory(const dkChar *fn, int level)
{
  dk4_file_info_t	fili;

  dk4fileinfo_init(&fili);
  if (0 != dk4fileinfo_app(&fili, fn, app)) {
    dk_ls_process_item_information(fn, &fili, 0, (dk4_um_t)0UL, 0);
  } else {
    /* ERROR: Failed to obtain information about file, already reported */
    exval = EXIT_FAILURE;
  }
}



/**	Process one directory item.
	@param	dirnode		Node for the items parent directory.
	@param	filename	Path name for item.
*/
static
void
dk_ls_process_item_for_node(
  dk_ls_node_t		*dirnode,
  const dkChar		*filename
)
{
  dk4_file_info_t	fili;
  dk4_er_t		er;
  dk4_um_t		fs;
  int			ch	= DK4_FILE_INFO_CONTENTS_DATA_TARGET;
  

#line 2129 "dk-ls.ctr"
  if ((0 >= dirnode->level) || (0 != recursive) || (0 != size_wanted))
  {
    dk4error_init(&er);
    dk4fileinfo_init(&fili);
    if (0 != dk4fileinfo_app(&fili, filename, app)) {
      if (0 != dk4fileinfo_is_symlink(&fili)) {
        ch = DK4_FILE_INFO_CONTENTS_DATA_LINK ;
      }
      if (0 != dk4fileinfo_size(&fs, &fili, ch, &er)) {
        dirnode->size = dk4ma_um_add(dirnode->size, fs, &er);
	if (DK4_E_MATH_OVERFLOW == er.ec) {
	  dirnode->ovsize = 1;
	}
      } else {
        /* ERROR: Failed to retrieve size */
	

#line 2145 "dk-ls.ctr"
	dk_ls_report_fileinfo_size_error(
	  app, filename, ch, dk_ls_msg, dk_ls_sz_msg
	);
	fs = (dk4_um_t)0UL;
	exval = EXIT_FAILURE;
      }
      if ((0 >= dirnode->level ) || (0 != recursive)) {
        /* Show file information */
	dk_ls_process_item_information(
	  filename, &fili, 0, (dk4_um_t)0UL, 0
	);
      }
    } else {
      /* ERROR: Failed to obtain file information, already reported */
      exval = EXIT_FAILURE;
    }
  }
  

#line 2163 "dk-ls.ctr"
}



/**	Process directory. Unless required otherwise by a non-zero
	value for recursive, show contents and directory for level=0,
	directory itself for level=1.
	@param	dn	Directory name.
	@param	level	Processing level (0=cmd line arg, 1=direct child,
	2 and above=further siblings).
*/
static
void
dk_ls_process_directory(const dkChar *dn, int level)
{
  dk4_ufi_t		 pdufi;	/* UFI for parent directory */
  dk4_ufi_t		 sdufi;	/* UFI for sub directory */
  dk4_er_t		 er;	/* Error report for size addition */
  dkChar		*mybu;	/* Buffer for path name construction */
#if VERSION_BEFORE_20150821
  dk_ls_node_t		*rn;	/* Root node */
#endif
  dk_ls_node_t		*cn;	/* Current node */
  dk_ls_node_t		*on;	/* Other node */
  const dkChar		*pa;	/* Directory path */
  const dkChar		*ndn;	/* Next directory name */
  size_t		 szmb;	/* Size of mybu (number of dkChar) */
  int			 gosub;	/* Flag: Go into sub directory */
  int			 ufich;	/* Channel to retrieve UFI */
  

#line 2193 "dk-ls.ctr"
  /*
  	Allocate path name construction buffer.
  */
  ufich = DK4_FILE_INFO_CONTENTS_DATA_TARGET;
  szmb = DK4_MAX_PATH;
  mybu = dk4mem_new_app(dkChar,szmb,app);
  /*
  	If path name construction buffer available, run normally.
  */
  if (NULL != mybu) {
#if VERSION_BEFORE_20150821
    rn = cn = dk_ls_new_node(dn, NULL, level);
#else
    cn = dk_ls_new_node(dn, NULL, level);
#endif
    while (NULL != cn) {			

#line 2209 "dk-ls.ctr"
      if (0 != dk_ls_can_continue()) {		

#line 2210 "dk-ls.ctr"
        ndn = dk4dir_next_dir(cn->dirptr);
	if (NULL != ndn) {
	  /*
	  	Handle sub directory.
	  */
	  pa = dk4dir_get_path(cn->dirptr);
	  if (0 != dk_ls_construct_path(&mybu, &szmb, pa, ndn)) {
	    /*
	    	Decide whether or not to go into subdirectory
	    */
	    gosub = 0;
#if 0
	    /*	2015-04-14
		There is no reason to open a node for the subdirectory
		unless recursive output or size calculation is wanted.
	    */
	    if (0 == cn->level) {	/* Processing cmd line argument */
	      gosub = 1;
	    }
#endif
	    if (0 != recursive) {	/* Recursive output wanted */
	      gosub = 1;
	    }
	    if (0 != size_wanted) {	/* Size calculation required */
	      if (0 != show_dirs) {	/* ... and directory information wanted */
	        gosub = 1;
	      }
	    }
	    if (0 != gosub) {
	      if (0 != dk4ufi_get(&sdufi, mybu, NULL)) {
	        if (0 != stay_on_fs) {	/* Check for same filesystem */
		  if (0 != dk4fileinfo_ufi(&pdufi, &(cn->fi), ufich, NULL)) {
#if DK4_ON_WINDOWS
		    if (pdufi.ser != sdufi.ser) {
		      gosub = 0;
		    }
#else
		    if (pdufi.dev != sdufi.dev) {
		      gosub = 0;
		    }
#endif
		  } else {
		    /* ERROR: Failed to obtain file identifier */
		    dk4app_log_3(
		      app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 14, 15,
		      dk4dir_get_path(cn->dirptr)
		    );
		    gosub = 0;
		    exval = EXIT_FAILURE;
		  }
	        }
	        if (0 != gosub) {	/* Check for symlink loop */
		  on = cn;
		  while ((0 != gosub) && (NULL != on)) {
		    if (0 != dk4fileinfo_ufi(&pdufi, &(on->fi), ufich, NULL)) {
#if DK4_ON_WINDOWS
		      if (pdufi.ser == sdufi.ser) {
		        if (pdufi.inh == sdufi.inh) {
			  if (pdufi.inl == sdufi.inl) {
			    gosub = 0;
			  }
			}
		      }
#else
		      if (pdufi.dev == sdufi.dev) {
		        if (pdufi.ino == sdufi.ino) {
			  gosub = 0;
			}
		      }
#endif
		    } else {
		      /* ERROR: Failed to retrieve UFI for directory */
		      dk4app_log_3(
		        app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 14, 15,
		        dk4dir_get_path(on->dirptr)
		      );
		      exval = EXIT_FAILURE;
		      gosub = 0;
		    }
		    on = on->parent;
		  }
	        }
	      } else {
	        /* ERROR: Failed to retrieve unique file identifier */
		dk4app_log_3(
		  app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 14, 15,
		  mybu
		);
		exval = EXIT_FAILURE;
	        gosub = 0;
	      }
	    }
	    if (0 != gosub) {
	      /*
	      	Go into subdirectory.
	      */
	      on = dk_ls_new_node(
	        mybu, cn, ((3 >= cn->level) ? (1 + cn->level) : 3)
	      );
	      if (NULL != on) {
	        cn->child = on;
		cn = on;
	      } else {
	        /* ERROR: Failed to create node, already reported */
		exval = EXIT_FAILURE;
	      }
	    } else {
	      /*
	      	Do not go into subdirectory, probably show directory details.
	      */
	      dk_ls_process_item_for_node(cn, mybu);
	    }
	  } else {
	    /* ERROR: Failed to construct path, already reported */
	    exval = EXIT_FAILURE;
	  }
	} else {
	  /*
	  	Finalize node, report non-directory entries and the
		directories, finally move to parent node.
	  */
	  if ((0 != size_wanted) || (0 != show_files)) {
	    if (0 != dk_ls_can_continue()) {
	      do {
	        ndn = dk4dir_next_file(cn->dirptr);
	        if (NULL != ndn) {	

#line 2336 "dk-ls.ctr"
	          pa = dk4dir_get_path(cn->dirptr);
	          if (0 != dk_ls_construct_path(&mybu, &szmb, pa, ndn)) {
	            dk_ls_process_item_for_node(cn, mybu);
	          } else {
	            /* ERROR: Failed to construct path, already reported */
		    exval = EXIT_FAILURE;
	          }
	        }
	      } while((0 != dk_ls_can_continue()) && (NULL != ndn)) ;
	    }
	  }
          if (0 != dk_ls_can_continue()) {		

#line 2348 "dk-ls.ctr"
	    if ((0 != size_wanted) || (0 != show_dirs)) {
	      if ((1 >= cn->level) || (0 != recursive)) {
	        dk_ls_process_item_information(
	          dk4dir_get_path(cn->dirptr), &(cn->fi),
		  1, cn->size, cn->ovsize
	        );
	      }
	    }
	  }
	  on = cn->parent;
	  if (NULL != on) {
	    on->child = NULL;
	    dk4error_init(&er);
	    on->size = dk4ma_size_t_add(on->size, cn->size, &er);
	    if (DK4_E_NONE != er.ec) {
	      on->ovsize = 1;
	    }
	    if (0 != cn->ovsize) {
	      on->ovsize = 1;
	    }
	  }
	  dk_ls_delete_node(cn);
	  cn = on;
	}
      } else {
        /*
      	  Abort: Close current node and move to parent. No output.
        */
	

#line 2377 "dk-ls.ctr"
        on = cn->parent;
        if (NULL != on) { on->child = NULL; }
        dk_ls_delete_node(cn);
        cn = on;
      }
    }
  } else {					

#line 2384 "dk-ls.ctr"
    /* ERROR: Allocation failed, already reported */
    exval = EXIT_FAILURE;
  }
  /*
  	Release path name construction buffer.
  */
  if (NULL != mybu) {
    dk4mem_release(mybu);
  }
  

#line 2394 "dk-ls.ctr"
}



/**	Check whether the specified file name is a directory.
	@param	fn	File name.
	@return	1 for directories, 0 for non-directory entries,
	-1 on errors.
*/
static
int
dk_ls_is_directory(const dkChar *fn)
{
#if DK4_ON_WINDOWS
  DWORD 	dwattr;
  HANDLE	ha;
  LONG		lasterror;
#if DK4_CHAR_SIZE > 1
  WIN32_FIND_DATAW	finddata;
#else
  WIN32_FIND_DATAA	finddata;
#endif
#else
  dk4_stat_t	stb;
  dk4_er_t	er;
#endif
  int	back = -1;
  

#line 2422 "dk-ls.ctr"
  if (NULL != fn) {
#if DK4_ON_WINDOWS
#if DK4_CHAR_SIZE > 1
    dwattr = GetFileAttributesW(fn);
#else
    dwattr = GetFileAttributesA(fn);
#endif
    if (INVALID_FILE_ATTRIBUTES != dwattr) {
      if (0 != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) {
        back = 1;
      } else {
        back = 0;
      }
    } else {
      lasterror = GetLastError();
#if DK4_CHAR_SIZE > 1
      ha = FindFirstFileW(fn, &finddata);
#else
      ha = FindFirstFileA(fn, &finddata);
#endif
      if (INVALID_HANDLE_VALUE != ha) {
        if (0 != (FILE_ATTRIBUTE_DIRECTORY & (finddata.dwFileAttributes))) {
	  back = 1;
	} else {
	  back = 0;
	}
        FindClose(ha);
      } else {
        /* ERROR: Failed to retrieve file attributes */
        dk4fileinfo_report_win_failed(app, fn, lasterror);
        exval = EXIT_FAILURE;
      }
    }
#else
    dk4error_init(&er);
    if (0 != dk4stat(&stb, fn, &er)) {
      if (0 != dk4stat_is_directory(&stb, &er)) {
        back = 1;
      } else {
        back = 0;
      }
    } else {
      /* ERROR: Failed to retrieve file information */
      dk4fileinfo_report_stat_failed(app, fn, er.dt.iDetails1);
      exval = EXIT_FAILURE;
    }
#endif
  } 

#line 2470 "dk-ls.ctr"
  return back;
}



/*	!!!!!
	For files explicitly mentioned on the command line
	processing starts here, probably after name expansion.
*/

/**	Run for one file or directory directly named on the command line.
	@param	arg	Path name.
*/
static
void
run_for_one_name(const dkChar *arg)
{
  

#line 2488 "dk-ls.ctr"
  switch (dk_ls_is_directory(arg)) {
    case 1 : {		

#line 2490 "dk-ls.ctr"
      dk_ls_process_directory(arg, 0);
    } break;
    case 0 : {		

#line 2493 "dk-ls.ctr"
      if (0 != show_files) {
        dk_ls_process_non_directory(arg, 0);
      }
    } break;
    default : {		

#line 2498 "dk-ls.ctr"
      /* ERROR: Failed to obtain information about file, already reported */
      exval = EXIT_FAILURE;
    } break;
  }
  

#line 2503 "dk-ls.ctr"
}



/**	Process command line arguments.
	@param	xargc	Number of command line arguments.
*/
static
void
run_for_command_line_arguments(int xargc)
{
#if DK4_ON_WINDOWS
  dkChar	 buf[DK4_MAX_PATH];
  dk4_dir_t	*fne;
  const dkChar	*pth;
  const	dkChar	*fn;
  int		 cc2;
#endif
  const dkChar	*xargv;
  int		 i;
  int		 cc	= 1;
  

#line 2525 "dk-ls.ctr"
  for (i = 0; ((i < xargc) && (1 == cc)); i++) {	

#line 2526 "dk-ls.ctr"
    if (0 != dk_ls_can_continue()) {			

#line 2527 "dk-ls.ctr"
      xargv = dk4app_get_argv(app, i);		

#line 2528 "dk-ls.ctr"
#if DK4_ON_WINDOWS
      if (0 != dk4path_must_expand(xargv)) {
        /*	!!!!!
		For file names we need to expand
		processing starts here.
	*/
        fne = dk4app_fne_open(xargv, app);
	if (NULL != fne) {
	  cc2 = 1;
	  while (1 == cc2) {
	    if (0 != dk_ls_can_continue()) {
	      fn = dk4dir_next_dir(fne);
	      if (NULL != fn) {
	        pth = dk4dir_get_path(fne);
		if (NULL != pth) {
		  if (0 != dk4str_cpy_s(buf,DK4_SIZEOF(buf,dkChar),pth,NULL)) {
		  if (0!=dk4str_cat_s(buf,DK4_SIZEOF(buf,dkChar),dk_ls_kwnl[5],NULL))
		  {
		  if (0 != dk4str_cat_s(buf,DK4_SIZEOF(buf,dkChar),fn,NULL)) {
		    dk_ls_process_directory(buf, 0);
		  } else {
		    /* ERROR: File name too long! */
		    dk4app_log_5(
		      app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 16, 17, 18,
		      pth, fn
		    );
		    exval = EXIT_FAILURE;
		  }
		  } else {
		    /* ERROR: File name too long! */
		    dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, pth);
		    exval = EXIT_FAILURE;
		  }
		  } else {
		    /* ERROR: File name too long! */
		    dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, pth);
		    exval = EXIT_FAILURE;
		  }
		} else {
		  run_for_one_name(fn);
		}
	      } else {
	        cc2 = 0;
	      }
	    } else {
	      cc2 = 0;
	    }
	  }
	  cc2 = 1;
	  while (1 == cc2) {
	    if (0 != dk_ls_can_continue()) {
	      fn = dk4dir_next_file(fne);
	      if (NULL != fn) {
	        pth = dk4dir_get_path(fne);
		if (NULL != pth) {
		  if (0 != dk4str_cpy_s(buf,DK4_SIZEOF(buf,dkChar),pth,NULL)) {
		  if (0!=dk4str_cat_s(buf,DK4_SIZEOF(buf,dkChar),dk_ls_kwnl[5],NULL))
		  {
		  if (0 != dk4str_cat_s(buf,DK4_SIZEOF(buf,dkChar),fn,NULL)) {
		    dk_ls_process_non_directory(buf, 0);
		  } else {
		    /* ERROR: File name too long! */
		    dk4app_log_5(
		      app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 16, 17, 18,
		      pth, fn
		    );
		    exval = EXIT_FAILURE;
		  }
		  } else {
		    /* ERROR: File name too long! */
		    dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, pth);
		    exval = EXIT_FAILURE;
		  }
		  } else {
		    /* ERROR: File name too long! */
		    dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, pth);
		    exval = EXIT_FAILURE;
		  }
		} else {
		  run_for_one_name(fn);
		}
	      } else {
	        cc2 = 0;
	      }
	    } else {
	      cc2 = 0;
	    }
	  }
	  dk4dir_close(fne);
	} else {
	  /* ERROR: Failed to expand file name, already reported */
	  exval = EXIT_FAILURE;
	}
      } else {
#endif
	

#line 2624 "dk-ls.ctr"
        run_for_one_name(xargv);
#if DK4_ON_WINDOWS
      }
#endif
    } else {
      cc = 0;
    }
  }
  

#line 2633 "dk-ls.ctr"
}



/*	!!!!!
	If no file names was given on the command line processing
	starts here.
*/

/**	Show contents of current directory.
*/
static
void
run_for_current_dir_contents(void)
{
  dk4_er_t		 er;
  dk4_dir_t		*dirptr;
  const dkChar		*fn;
  int			 cc;
  int			 om;
  

#line 2654 "dk-ls.ctr"
  dk4error_init(&er);
  om = DK4_DIR_OPEN_SORTED | DK4_DIR_SYMLINK_DIR_AS_FILE;
  dirptr = dk4dir_open_app(dk_ls_kwnl[4], om, app);
  if (NULL != dirptr) {
    if ((0 != recursive) || (0 != show_dirs)) {
      cc = 1;
      while(1 == cc) {
        if (0 != dk_ls_can_continue()) {
          fn = dk4dir_next_dir(dirptr);
	  if (NULL != fn) {
	    if ((0 != recursive) || (0 != size_wanted)) {
	      dk_ls_process_directory(fn, 1);
	    } else {
	      dk_ls_process_non_directory(fn, 1);
	    }
	  } else {
	    cc = 0;
	  }
        } else {
          cc = 0;
        }
      }
    }
    if (0 != show_files) {
      cc = 1;
      while (1 == cc) {
        if (0 != dk_ls_can_continue()) {
          fn = dk4dir_next_file(dirptr);
	  if (NULL != fn) {
	    dk_ls_process_non_directory(fn, 1);
	  } else {
	    cc = 0;
	  }
        } else {
          cc = 0;
        }
      }
    }
    dk4dir_close(dirptr);
  } else {
    /* ERROR: Failed to open current directory, already reported */
    exval = EXIT_FAILURE;
  }
  

#line 2698 "dk-ls.ctr"
}



/**	Process command line arguments (if any).
*/
static
void
dk_ls_normal_run(void)
{
  dkChar	 mdbuf[32];
  dkChar	*bptr;
  int		 res;
  int		 xargc;
  int		 mdset;
  

#line 2714 "dk-ls.ctr"
  exval = EXIT_SUCCESS;

  /*	Process options.
  */
  if (0 != dk4app_opt_is_set_short(app, dkT('p'), NULL)) {
    res = dk4app_opt_get_string_short(
      po_buf, DK4_SIZEOF(po_buf,dkChar), app, dkT('p'), NULL
    );
    if (0 != res) {
      print_order = po_buf;
    }
  }
  if (NULL == print_order) {
    if (0 == dk4app_opt_is_set_short(app, dkT('R'), NULL)) {
      res = dk4app_pref_get(
        po_buf, DK4_SIZEOF(po_buf,dkChar), app, dk_ls_kwnl[10], 0
      );
      if (0 != res) {
        dk4str_rtwh(po_buf, NULL);
        print_order = dk4str_start(po_buf, NULL);
      }
    }
  }
  if (NULL == print_order) {
#if DK4_ON_WINDOWS
    print_order = dk_ls_kwnl[8];
#else
    print_order = dk_ls_kwnl[3];
#endif
  }
  if (NULL != dk4str_chr(print_order, dkT('s'))) {
    size_wanted = 1;
  }
  if (NULL != dk4str_chr(print_order, dkT('n'))) {
    name_wanted = 1;
  }
  if (0 != dk4app_opt_is_set_short(app, dkT('r'), NULL)) {
    recursive = 1;
  }
  if (0 != dk4app_opt_is_set_short(app, dkT('f'), NULL)) {
    stay_on_fs = 1;
  }
  if (0 != dk4app_opt_is_set_short(app, dkT('t'), NULL)) {
    res = dk4app_opt_get_string_short(
      mdbuf, DK4_SIZEOF(mdbuf,dkChar), app, dkT('t'), NULL
    );
    if (0 != res) {
      bptr = mdbuf;
      show_files = 0; show_dirs = 0;
      while (dkT('\0') != *bptr) {
        if (dkT('d') == *bptr) { show_dirs = 1; }
	if (dkT('f') == *bptr) { show_files = 1; }
        bptr++;
      }
    }
  }
  mdset = 0;
  if (0 != dk4app_opt_is_set_short(app, dkT('m'), NULL)) {
    res = dk4app_opt_get_string_short(
      mdbuf, DK4_SIZEOF(mdbuf,dkChar), app, dkT('m'), NULL
    );
    if (0 != res) {
      /* Process string, find digest and encoding */
      mdset = 1;
      dk_ls_set_md(
        &digest_type, &digest_enco, mdbuf, app, dk_ls_msg, dk_ls_sz_msg
      );
    }
  }
  if (0 != dk4app_opt_is_set_short(app, dkT('s'), NULL)) {
    show_summary = 1;
  }
  if ((0 == mdset) && (0 == dk4app_opt_is_set_short(app, dkT('R'), NULL))) {
    res = dk4app_pref_get(
      mdbuf, DK4_SIZEOF(mdbuf,dkChar), app, dk_ls_kwnl[9], 0
    );
    if (0 != res) {
      dk_ls_set_md(
        &digest_type, &digest_enco, mdbuf, app, dk_ls_msg, dk_ls_sz_msg
      );
    }
  }

  /*	Process command line arguments or current directory contents.
  */
  xargc = dk4app_get_argc(app);
  if (0 < xargc) {	

#line 2801 "dk-ls.ctr"
    run_for_command_line_arguments(xargc);
  } else {		

#line 2803 "dk-ls.ctr"
    run_for_current_dir_contents();
  }
  if (0 != show_summary) {
    if (((dk4_um_t)0UL != num_dirs) && (DK4_E_NONE == er_dirs.ec)) {
      if (0 == dk4fputs(dk_ls_msg[30], stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
      dk_ls_write_decimal_number(num_dirs,dk4numlength(sizeof(dk4_um_t),0));
      if (0 == dk4fputc(dkT('\n'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
    if (((dk4_um_t)0UL != num_files) && (DK4_E_NONE == er_files.ec)) {
      if (0 == dk4fputs(dk_ls_msg[31], stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
      dk_ls_write_decimal_number(num_files,dk4numlength(sizeof(dk4_um_t),0));
      if (0 == dk4fputc(dkT('\n'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
    if (((dk4_um_t)0UL != size_bytes) && (DK4_E_NONE == er_bytes.ec)) {
      if (0 == dk4fputs(dk_ls_msg[32], stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
      dk_ls_write_decimal_number(size_bytes,dk4numlength(sizeof(dk4_um_t),0));
      if (0 == dk4fputc(dkT('\n'), stdout, NULL)) {
        write_error = 1; exval = EXIT_FAILURE;
      }
    }
  }
  

#line 2835 "dk-ls.ctr"
}




#if	DK4_HAVE_SIGACTION
/**	Set signal handlers and run.
*/
static
void
dk_ls_run_with_signal_handlers(void)
{
#ifdef SIGPIPE
  struct sigaction opipe;
#endif
  struct sigaction oint;
#ifdef SIGPIPE
  struct sigaction npipe;
#endif
  struct sigaction nint;
  struct sigaction oterm;
  struct sigaction nterm;
  int	 success = 0;

#ifdef SIGPIPE
  /*	Set up signal handling for SIGPIPE.
  */
  DK4_MEMRES(&npipe, sizeof(npipe));
  npipe.sa_handler = sig_handler_pipe;
  npipe.sa_flags = 0;
  if (0 != sigemptyset(&npipe.sa_mask)) {
    /* ERROR: Failed to set up masked signal set for SIGPIPE */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto finished;
  }
  if (0 != sigaddset(&npipe.sa_mask, SIGPIPE)) {
    /* ERROR: Failed to set up masked signal set for SIGPIPE */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto finished;
  }
  if (0 != sigaction(SIGPIPE, &npipe, &opipe)) {
    /* ERROR: Failed to set up signal handler for SIGPIPE */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto finished;
  }
#endif

  /*	Set up signal handling for SIGINT.
  */
  DK4_MEMRES(&nint, sizeof(nint));
  nint.sa_handler = sig_handler_int;
  nint.sa_flags = 0;
  if (0 != sigemptyset(&nint.sa_mask)) {
    /* ERROR: Failed to set up masked signal set for SIGINT */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto restore_old_pipe;
  }
  if (0 != sigaddset(&nint.sa_mask, SIGINT)) {
    /* ERROR: Failed to set up masked signal set for SIGINT */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto restore_old_pipe;
  }
  if (0 != sigaction(SIGINT, &nint, &oint)) {
    /* ERROR: Failed to set up signal handler for SIGINT */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto restore_old_pipe;
  }

  /*	Set up signal handling for SIGTERM
  */
  DK4_MEMRES(&nterm, sizeof(nterm));
  nterm.sa_handler = sig_handler_term;
  nterm.sa_flags = 0;
  if (0 != sigemptyset(&nterm.sa_mask)) {
    /* ERROR: Failed to set up masked signal set for SIGTERM */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto restore_old_int;
  }
  if (0 != sigaddset(&nterm.sa_mask, SIGTERM)) {
    /* ERROR: Failed to set up masked signal set for SIGTERM */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto restore_old_int;
  }
  if (0 != sigaction(SIGTERM, &nterm, &oterm)) {
    /* ERROR: Failed to set up signal handler for SIGTERM */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 0);
    goto restore_old_int;
  }

  success = 1;
  dk_ls_normal_run();

  /*	Restore signal handling for SIGTERM.
  */
  if (0 != sigaction(SIGTERM, &oterm, NULL)) {
    /* ERROR: Failed to restore old SIGTERM settings */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 1);
    success = 0;
  }

  /*	Restore signal handling for SIGINT.
  */
  restore_old_int:
  if (0 != sigaction(SIGINT, &oint, NULL)) {
    /* ERROR: Failed to restore old SIGPIPE settings */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 1);
    success = 0;
  }

#ifdef SIGPIPE
  /*	Restore signal handling for SIGPIPE.
  */
  restore_old_pipe:
  if (0 != sigaction(SIGPIPE, &opipe, NULL)) {
    /* ERROR: Failed to restore old SIGPIPE settings */
    dk4app_log_1(app, dk_ls_msg, dk_ls_sz_msg, DK4_LL_ERROR, 1);
    success = 0;
  }
#endif

  /*	Set exit status code if error occured.
  */
  finished:
  if (0 == success) { exval = EXIT_FAILURE; }
}
#else
#if	DK4_HAVE_SIGSET
/**	Set signal handlers and run.
*/
static
void
dk_ls_run_with_signal_handlers(void)
{
#ifdef SIGPIPE
  dk4_sig_handler_t	*oldpipe = NULL;
#endif
  dk4_sig_handler_t	*oldint  = NULL;
  dk4_sig_handler_t	*oldterm = NULL;

#ifdef SIGPIPE
  oldpipe = sigset(SIGPIPE, sig_handler_pipe);
#endif
  oldint  = sigset(SIGINT,  sig_handler_int);
  oldterm = sigset(SIGTERM, sig_handler_term);
  dk_ls_normal_run();
  sigset(SIGTERM, oldterm);
  sigset(SIGINT,  oldint);
#ifdef SIGPIPE
  sigset(SIGPIPE, oldpipe);
#endif
}
#else
#if	DK4_HAVE_SIGNAL
/**	Set signal handlers and run.
*/
static
void
dk_ls_run_with_signal_handlers(void)
{
#ifdef SIGPIPE
  dk4_sig_handler_t	*oldpipe = NULL;
#endif
  dk4_sig_handler_t	*oldint  = NULL;
  dk4_sig_handler_t	*oldterm = NULL;

#ifdef SIGPIPE
  oldpipe = signal(SIGPIPE, sig_handler_pipe);
#endif
  oldint  = signal(SIGINT,  sig_handler_int);
  oldterm = signal(SIGTERM, sig_handler_term);
  dk_ls_normal_run();
  signal(SIGTERM, oldterm);
  signal(SIGINT,  oldint);
#ifdef SIGPIPE
  signal(SIGPIPE, oldpipe);
#endif
}
#else
/**	Set signal handlers and run.
*/
static
void
dk_ls_run_with_signal_handlers(void)
{
  dk_ls_normal_run();
}
#endif
#endif
#endif



/**	Main function.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	0 on success, all other values indicate errors.
*/
#if DK4_CHAR_SIZE > 1
int wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char *argv[])
#endif
{
  

#line 3039 "dk-ls.ctr"
  

#line 3040 "dk-ls.ctr"
  dk4fput_initialize_stdout();
  dk4fput_initialize_stderr();
  dk4error_init(&er_dirs);
  dk4error_init(&er_files);
  dk4error_init(&er_bytes);
  app = dk4app_open_cmd(
    argc, argv, dk_ls_options, dk_ls_sz_options,
    dk_ls_kwnl[0], DKT_VERSION_DK,
    dk_ls_kwnl[1], dk_ls_help_text, dk_ls_license_text
  );
  if (NULL != app) {					

#line 3051 "dk-ls.ctr"
    dk_ls_sz_msg = dk4app_string_table_size(dk_ls_kw_def);
    dk_ls_msg    = dk4app_string_table(app, dk_ls_kwnl[2], dk_ls_kw_def);
    if (0 != dk4app_can_run_normally(app)) {		

#line 3054 "dk-ls.ctr"
      dk_ls_run_with_signal_handlers();
    } else {
      if (0 != dk4app_help_version_license(app)) {	

#line 3057 "dk-ls.ctr"
        exval = EXIT_SUCCESS;
      }
    }
    dk4app_close(app);
  } else {						

#line 3062 "dk-ls.ctr"
    dk4fputs(dk_ls_kwnl[11], stdout, NULL);
    fflush(stdout);
  }
  fflush(stdout);
  fflush(stderr);
  dk4fput_cleanup_stderr();
  dk4fput_cleanup_stdout();
  

#line 3070 "dk-ls.ctr"
  

#line 3071 "dk-ls.ctr"
  exit(exval); return exval;
}