1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
|
/* `dir', `vdir' and `ls' directory listing programs for GNU.
Copyright (C) 85, 88, 90, 91, 1995-2002 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* If ls_mode is LS_MULTI_COL,
the multi-column format is the default regardless
of the type of output device.
This is for the `dir' program.
If ls_mode is LS_LONG_FORMAT,
the long format is the default regardless of the
type of output device.
This is for the `vdir' program.
If ls_mode is LS_LS,
the output format depends on whether the output
device is a terminal.
This is for the `ls' program. */
/* Written by Richard Stallman and David MacKenzie. */
/* Color support by Peter Anvin <Peter.Anvin@linux.org> and Dennis
Flaherty <dennisf@denix.elk.miles.com> based on original patches by
Greg Lee <lee@uhunix.uhcc.hawaii.edu>. */
#ifdef _AIX
#pragma alloca
#endif
#include <config.h>
#include <sys/types.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#if HAVE_TERMIOS_H
# include <termios.h>
#endif
#ifdef GWINSZ_IN_SYS_IOCTL
# include <sys/ioctl.h>
#endif
#ifdef WINSIZE_IN_PTEM
# include <sys/stream.h>
# include <sys/ptem.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <setjmp.h>
#include <grp.h>
#include <pwd.h>
#include <getopt.h>
/* Get MB_CUR_MAX. */
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
/* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
#if HAVE_WCHAR_H
# include <wchar.h>
#endif
/* Get iswprint(). */
#if HAVE_WCTYPE_H
# include <wctype.h>
#endif
#if !defined iswprint && !HAVE_ISWPRINT
# define iswprint(wc) 1
#endif
#ifndef HAVE_DECL_WCWIDTH
"this configure-time declaration test was not run"
#endif
#if !HAVE_DECL_WCWIDTH
int wcwidth ();
#endif
/* If wcwidth() doesn't exist, assume all printable characters have
width 1. */
#ifndef wcwidth
# if !HAVE_WCWIDTH
# define wcwidth(wc) ((wc) == 0 ? 0 : iswprint (wc) ? 1 : -1)
# endif
#endif
#include "system.h"
#include <fnmatch.h>
#include "acl.h"
#include "argmatch.h"
#include "dirname.h"
#include "dirfd.h"
#include "error.h"
#include "hard-locale.h"
#include "hash.h"
#include "human.h"
#include "filemode.h"
#include "ls.h"
#include "mbswidth.h"
#include "obstack.h"
#include "path-concat.h"
#include "quote.h"
#include "quotearg.h"
#include "same.h"
#include "strverscmp.h"
#include "xstrtol.h"
#include "xreadlink.h"
#define PROGRAM_NAME (ls_mode == LS_LS ? "ls" \
: (ls_mode == LS_MULTI_COL \
? "dir" : "vdir"))
#define AUTHORS N_ ("Richard Stallman and David MacKenzie")
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
/* Return an int indicating the result of comparing two integers.
Subtracting doesn't always work, due to overflow. */
#define longdiff(a, b) ((a) < (b) ? -1 : (a) > (b))
/* The field width for inode numbers. On some hosts inode numbers are
64 bits, so columns won't line up exactly when a huge inode number
is encountered, but in practice 7 digits is usually enough. */
#ifndef INODE_DIGITS
# define INODE_DIGITS 7
#endif
#ifdef S_ISLNK
# define HAVE_SYMLINKS 1
#else
# define HAVE_SYMLINKS 0
#endif
/* Arrange to make lstat calls go through the wrapper function
on systems with an lstat function that does not dereference symlinks
that are specified with a trailing slash. */
#if ! LSTAT_FOLLOWS_SLASHED_SYMLINK
int rpl_lstat PARAMS((const char *, struct stat *));
# undef lstat
# define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
#endif
#if HAVE_STRUCT_DIRENT_D_TYPE && defined DTTOIF
# define DT_INIT(Val) = Val
#else
# define DT_INIT(Val) /* empty */
#endif
#ifdef ST_MTIM_NSEC
# define TIMESPEC_NS(timespec) ((timespec).ST_MTIM_NSEC)
#else
# define TIMESPEC_NS(timespec) 0
#endif
#if ! HAVE_STRUCT_STAT_ST_AUTHOR
# define st_author st_uid
#endif
/* Cray/Unicos DMF: use the file's migrated, not real, status */
#if HAVE_ST_DM_MODE
# define ST_DM_MODE(Stat_buf) ((Stat_buf).st_dm_mode)
#else
# define ST_DM_MODE(Stat_buf) ((Stat_buf).st_mode)
#endif
#ifndef LOGIN_NAME_MAX
# if _POSIX_LOGIN_NAME_MAX
# define LOGIN_NAME_MAX _POSIX_LOGIN_NAME_MAX
# else
# define LOGIN_NAME_MAX 17
# endif
#endif
/* The maximum length of a string representation of a user or group ID,
not counting any terminating NUL byte. */
#define ID_LENGTH_MAX \
MAX (LOGIN_NAME_MAX - 1, LONGEST_HUMAN_READABLE)
enum filetype
{
unknown DT_INIT (DT_UNKNOWN),
fifo DT_INIT (DT_FIFO),
chardev DT_INIT (DT_CHR),
directory DT_INIT (DT_DIR),
blockdev DT_INIT (DT_BLK),
normal DT_INIT (DT_REG),
symbolic_link DT_INIT (DT_LNK),
sock DT_INIT (DT_SOCK),
arg_directory DT_INIT (2 * (DT_UNKNOWN | DT_FIFO | DT_CHR | DT_DIR | DT_BLK
| DT_REG | DT_LNK | DT_SOCK))
};
struct fileinfo
{
/* The file name. */
char *name;
struct stat stat;
/* For symbolic link, name of the file linked to, otherwise zero. */
char *linkname;
/* For symbolic link and long listing, st_mode of file linked to, otherwise
zero. */
mode_t linkmode;
/* For symbolic link and color printing, 1 if linked-to file
exists, otherwise 0. */
int linkok;
enum filetype filetype;
#if HAVE_ACL
/* For long listings, true if the file has an access control list. */
bool have_acl;
#endif
};
#if HAVE_ACL
# define FILE_HAS_ACL(F) ((F)->have_acl)
#else
# define FILE_HAS_ACL(F) 0
#endif
#define LEN_STR_PAIR(s) sizeof (s) - 1, s
/* Null is a valid character in a color indicator (think about Epson
printers, for example) so we have to use a length/buffer string
type. */
struct bin_str
{
int len; /* Number of bytes */
const char *string; /* Pointer to the same */
};
#ifndef STDC_HEADERS
time_t time ();
#endif
size_t nstrftime PARAMS ((char *, size_t, char const *,
struct tm const *, int, int));
char *getgroup ();
char *getuser ();
static size_t quote_name PARAMS ((FILE *out, const char *name,
struct quoting_options const *options));
static char *make_link_path PARAMS ((const char *path, const char *linkname));
static int decode_switches PARAMS ((int argc, char **argv));
static int file_interesting PARAMS ((const struct dirent *next));
static uintmax_t gobble_file PARAMS ((const char *name, enum filetype type,
int explicit_arg, const char *dirname));
static void print_color_indicator PARAMS ((const char *name, mode_t mode,
int linkok));
static void put_indicator PARAMS ((const struct bin_str *ind));
static int length_of_file_name_and_frills PARAMS ((const struct fileinfo *f));
static void add_ignore_pattern PARAMS ((const char *pattern));
static void attach PARAMS ((char *dest, const char *dirname, const char *name));
static void clear_files PARAMS ((void));
static void extract_dirs_from_files PARAMS ((const char *dirname,
int ignore_dot_and_dot_dot));
static void get_link_name PARAMS ((const char *filename, struct fileinfo *f));
static void indent PARAMS ((int from, int to));
static void init_column_info PARAMS ((void));
static void print_current_files PARAMS ((void));
static void print_dir PARAMS ((const char *name, const char *realname));
static void print_file_name_and_frills PARAMS ((const struct fileinfo *f));
static void print_horizontal PARAMS ((void));
static void print_long_format PARAMS ((const struct fileinfo *f));
static void print_many_per_line PARAMS ((void));
static void print_name_with_quoting PARAMS ((const char *p, mode_t mode,
int linkok,
struct obstack *stack));
static void prep_non_filename_text PARAMS ((void));
static void print_type_indicator PARAMS ((mode_t mode));
static void print_with_commas PARAMS ((void));
static void queue_directory PARAMS ((const char *name, const char *realname));
static void sort_files PARAMS ((void));
static void parse_ls_color PARAMS ((void));
void usage PARAMS ((int status));
/* The name the program was run with, stripped of any leading path. */
char *program_name;
/* Initial size of hash table.
Most hierarchies are likely to be shallower than this. */
#define INITIAL_TABLE_SIZE 30
/* The set of `active' directories, from the current command-line argument
to the level in the hierarchy at which files are being listed.
A directory is represented by its device and inode numbers.
A directory is added to this set when ls begins listing it or its
entries, and it is removed from the set just after ls has finished
processing it. This set is used solely to detect loops, e.g., with
mkdir loop; cd loop; ln -s ../loop sub; ls -RL */
static Hash_table *active_dir_set;
#define LOOP_DETECT (!!active_dir_set)
/* An entry in the active_dir_set. */
struct dev_ino
{
dev_t st_dev;
ino_t st_ino;
};
/* The table of files in the current directory:
`files' points to a vector of `struct fileinfo', one per file.
`nfiles' is the number of elements space has been allocated for.
`files_index' is the number actually in use. */
/* Address of block containing the files that are described. */
static struct fileinfo *files; /* FIXME: rename this to e.g. cwd_file */
/* Length of block that `files' points to, measured in files. */
static int nfiles; /* FIXME: rename this to e.g. cwd_n_alloc */
/* Index of first unused in `files'. */
static int files_index; /* FIXME: rename this to e.g. cwd_n_used */
/* When nonzero, in a color listing, color each symlink name according to the
type of file it points to. Otherwise, color them according to the `ln'
directive in LS_COLORS. Dangling (orphan) symlinks are treated specially,
regardless. This is set when `ln=target' appears in LS_COLORS. */
static int color_symlink_as_referent;
/* mode of appropriate file for colorization */
#define FILE_OR_LINK_MODE(File) \
((color_symlink_as_referent && (File)->linkok) \
? (File)->linkmode : (File)->stat.st_mode)
/* Record of one pending directory waiting to be listed. */
struct pending
{
char *name;
/* If the directory is actually the file pointed to by a symbolic link we
were told to list, `realname' will contain the name of the symbolic
link, otherwise zero. */
char *realname;
struct pending *next;
};
static struct pending *pending_dirs;
/* Current time in seconds and nanoseconds since 1970, updated as
needed when deciding whether a file is recent. */
static time_t current_time = TYPE_MINIMUM (time_t);
static int current_time_ns = -1;
/* The number of digits to use for block sizes.
4, or more if needed for bigger numbers. */
static int block_size_size;
/* Option flags */
/* long_format for lots of info, one per line.
one_per_line for just names, one per line.
many_per_line for just names, many per line, sorted vertically.
horizontal for just names, many per line, sorted horizontally.
with_commas for just names, many per line, separated by commas.
-l (and other options that imply -l), -1, -C, -x and -m control
this parameter. */
enum format
{
long_format, /* -l and other options that imply -l */
one_per_line, /* -1 */
many_per_line, /* -C */
horizontal, /* -x */
with_commas /* -m */
};
static enum format format;
/* `full-iso' uses full ISO-style dates and times. `iso' uses shorter
ISO-style time stamps. `locale' uses locale-dependent time stamps.
`posix-iso' uses traditional POSIX-locale-style dates where
POSIX requires it, ISO-style dates otherwise. */
enum time_style
{
full_iso_time_style, /* --time-style=full-iso */
iso_time_style, /* --time-style=iso */
locale_time_style, /* --time-style=locale */
posix_iso_time_style /* --time-style=posix-iso (default) */
};
static char const *const time_style_args[] =
{
"full-iso", "iso", "locale", "posix-iso", 0
};
static enum time_style const time_style_types[] =
{
full_iso_time_style, iso_time_style,
locale_time_style, posix_iso_time_style, 0
};
/* Type of time to print or sort by. Controlled by -c and -u. */
enum time_type
{
time_mtime, /* default */
time_ctime, /* -c */
time_atime /* -u */
};
static enum time_type time_type;
/* The file characteristic to sort by. Controlled by -t, -S, -U, -X, -v. */
enum sort_type
{
sort_none, /* -U */
sort_name, /* default */
sort_extension, /* -X */
sort_time, /* -t */
sort_size, /* -S */
sort_version /* -v */
};
static enum sort_type sort_type;
/* Direction of sort.
0 means highest first if numeric,
lowest first if alphabetic;
these are the defaults.
1 means the opposite order in each case. -r */
static int sort_reverse;
/* Nonzero means to display owner information. -g turns this off. */
static int print_owner = 1;
/* Nonzero means to display author information. */
static bool print_author;
/* Nonzero means to display group information. -G and -o turn this off. */
static int print_group = 1;
/* Nonzero means print the user and group id's as numbers rather
than as names. -n */
static int numeric_ids;
/* Nonzero means mention the size in blocks of each file. -s */
static int print_block_size;
/* If positive, the units to use when printing sizes;
if negative, the human-readable base. */
static int output_block_size;
/* Precede each line of long output (per file) with a string like `m,n:'
where M is the number of characters after the `:' and before the
filename and N is the length of the filename. Using this format,
Emacs' dired mode starts up twice as fast, and can handle all
strange characters in file names. */
static int dired;
/* `none' means don't mention the type of files.
`classify' means mention file types and mark executables.
`file_type' means mention only file types.
Controlled by -F, -p, and --indicator-style. */
enum indicator_style
{
none, /* --indicator-style=none */
classify, /* -F, --indicator-style=classify */
file_type /* -p, --indicator-style=file-type */
};
static enum indicator_style indicator_style;
/* Names of indicator styles. */
static char const *const indicator_style_args[] =
{
"none", "classify", "file-type", 0
};
static enum indicator_style const indicator_style_types[]=
{
none, classify, file_type
};
/* Nonzero means use colors to mark types. Also define the different
colors as well as the stuff for the LS_COLORS environment variable.
The LS_COLORS variable is now in a termcap-like format. */
static int print_with_color;
enum color_type
{
color_never, /* 0: default or --color=never */
color_always, /* 1: --color=always */
color_if_tty /* 2: --color=tty */
};
enum Dereference_symlink
{
DEREF_UNDEFINED = 1,
DEREF_NEVER,
DEREF_COMMAND_LINE_ARGUMENTS, /* -H */
DEREF_ALWAYS /* -L */
};
enum indicator_no
{
C_LEFT, C_RIGHT, C_END, C_NORM, C_FILE, C_DIR, C_LINK, C_FIFO, C_SOCK,
C_BLK, C_CHR, C_MISSING, C_ORPHAN, C_EXEC, C_DOOR
};
static const char *const indicator_name[]=
{
"lc", "rc", "ec", "no", "fi", "di", "ln", "pi", "so",
"bd", "cd", "mi", "or", "ex", "do", NULL
};
struct color_ext_type
{
struct bin_str ext; /* The extension we're looking for */
struct bin_str seq; /* The sequence to output when we do */
struct color_ext_type *next; /* Next in list */
};
static struct bin_str color_indicator[] =
{
{ LEN_STR_PAIR ("\033[") }, /* lc: Left of color sequence */
{ LEN_STR_PAIR ("m") }, /* rc: Right of color sequence */
{ 0, NULL }, /* ec: End color (replaces lc+no+rc) */
{ LEN_STR_PAIR ("0") }, /* no: Normal */
{ LEN_STR_PAIR ("0") }, /* fi: File: default */
{ LEN_STR_PAIR ("01;34") }, /* di: Directory: bright blue */
{ LEN_STR_PAIR ("01;36") }, /* ln: Symlink: bright cyan */
{ LEN_STR_PAIR ("33") }, /* pi: Pipe: yellow/brown */
{ LEN_STR_PAIR ("01;35") }, /* so: Socket: bright magenta */
{ LEN_STR_PAIR ("01;33") }, /* bd: Block device: bright yellow */
{ LEN_STR_PAIR ("01;33") }, /* cd: Char device: bright yellow */
{ 0, NULL }, /* mi: Missing file: undefined */
{ 0, NULL }, /* or: Orphanned symlink: undefined */
{ LEN_STR_PAIR ("01;32") }, /* ex: Executable: bright green */
{ LEN_STR_PAIR ("01;35") } /* do: Door: bright magenta */
};
/* FIXME: comment */
static struct color_ext_type *color_ext_list = NULL;
/* Buffer for color sequences */
static char *color_buf;
/* Nonzero means to check for orphaned symbolic link, for displaying
colors. */
static int check_symlink_color;
/* Nonzero means mention the inode number of each file. -i */
static int print_inode;
/* What to do with symbolic links. Affected by -d, -F, -H, -l (and
other options that imply -l), and -L. */
static enum Dereference_symlink dereference;
/* Nonzero means when a directory is found, display info on its
contents. -R */
static int recursive;
/* Nonzero means when an argument is a directory name, display info
on it itself. -d */
static int immediate_dirs;
/* Nonzero means don't omit files whose names start with `.'. -A */
static int all_files;
/* Nonzero means don't omit files `.' and `..'
This flag implies `all_files'. -a */
static int really_all_files;
/* A linked list of shell-style globbing patterns. If a non-argument
file name matches any of these patterns, it is omitted.
Controlled by -I. Multiple -I options accumulate.
The -B option adds `*~' and `.*~' to this list. */
struct ignore_pattern
{
const char *pattern;
struct ignore_pattern *next;
};
static struct ignore_pattern *ignore_patterns;
/* Nonzero means output nongraphic chars in file names as `?'.
(-q, --hide-control-chars)
qmark_funny_chars and the quoting style (-Q, --quoting-style=WORD) are
independent. The algorithm is: first, obey the quoting style to get a
string representing the file name; then, if qmark_funny_chars is set,
replace all nonprintable chars in that string with `?'. It's necessary
to replace nonprintable chars even in quoted strings, because we don't
want to mess up the terminal if control chars get sent to it, and some
quoting methods pass through control chars as-is. */
static int qmark_funny_chars;
/* Quoting options for file and dir name output. */
static struct quoting_options *filename_quoting_options;
static struct quoting_options *dirname_quoting_options;
/* The number of chars per hardware tab stop. Setting this to zero
inhibits the use of TAB characters for separating columns. -T */
static int tabsize;
/* Nonzero means we are listing the working directory because no
non-option arguments were given. */
static int dir_defaulted;
/* Nonzero means print each directory name before listing it. */
static int print_dir_name;
/* The line length to use for breaking lines in many-per-line format.
Can be set with -w. */
static int line_length;
/* If nonzero, the file listing format requires that stat be called on
each file. */
static int format_needs_stat;
/* Similar to `format_needs_stat', but set if only the file type is
needed. */
static int format_needs_type;
/* strftime formats for non-recent and recent files, respectively, in
-l output. */
static char const *long_time_format[2] =
{
/* strftime format for non-recent files (older than 6 months), in
-l output when --time-style=locale is specified. This should
contain the year, month and day (at least), in an order that is
understood by people in your locale's territory.
Please try to keep the number of used screen columns small,
because many people work in windows with only 80 columns. But
make this as wide as the other string below, for recent files. */
N_("%b %e %Y"),
/* strftime format for recent files (younger than 6 months), in
-l output when --time-style=locale is specified. This should
contain the month, day and time (at least), in an order that is
understood by people in your locale's territory.
Please try to keep the number of used screen columns small,
because many people work in windows with only 80 columns. But
make this as wide as the other string above, for non-recent files. */
N_("%b %e %H:%M")
};
/* The exit status to use if we don't get any fatal errors. */
static int exit_status;
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
AUTHOR_OPTION = CHAR_MAX + 1,
BLOCK_SIZE_OPTION,
COLOR_OPTION,
FORMAT_OPTION,
FULL_TIME_OPTION,
INDICATOR_STYLE_OPTION,
QUOTING_STYLE_OPTION,
SHOW_CONTROL_CHARS_OPTION,
SI_OPTION,
SORT_OPTION,
TIME_OPTION,
TIME_STYLE_OPTION
};
static struct option const long_options[] =
{
{"all", no_argument, 0, 'a'},
{"escape", no_argument, 0, 'b'},
{"directory", no_argument, 0, 'd'},
{"dired", no_argument, 0, 'D'},
{"full-time", no_argument, 0, FULL_TIME_OPTION},
{"human-readable", no_argument, 0, 'h'},
{"inode", no_argument, 0, 'i'},
{"kilobytes", no_argument, 0, 'k'}, /* long form is obsolescent */
{"numeric-uid-gid", no_argument, 0, 'n'},
{"no-group", no_argument, 0, 'G'},
{"hide-control-chars", no_argument, 0, 'q'},
{"reverse", no_argument, 0, 'r'},
{"size", no_argument, 0, 's'},
{"width", required_argument, 0, 'w'},
{"almost-all", no_argument, 0, 'A'},
{"ignore-backups", no_argument, 0, 'B'},
{"classify", no_argument, 0, 'F'},
{"file-type", no_argument, 0, 'p'},
{"si", no_argument, 0, SI_OPTION},
{"dereference-command-line", no_argument, 0, 'H'},
{"ignore", required_argument, 0, 'I'},
{"indicator-style", required_argument, 0, INDICATOR_STYLE_OPTION},
{"dereference", no_argument, 0, 'L'},
{"literal", no_argument, 0, 'N'},
{"quote-name", no_argument, 0, 'Q'},
{"quoting-style", required_argument, 0, QUOTING_STYLE_OPTION},
{"recursive", no_argument, 0, 'R'},
{"format", required_argument, 0, FORMAT_OPTION},
{"show-control-chars", no_argument, 0, SHOW_CONTROL_CHARS_OPTION},
{"sort", required_argument, 0, SORT_OPTION},
{"tabsize", required_argument, 0, 'T'},
{"time", required_argument, 0, TIME_OPTION},
{"time-style", required_argument, 0, TIME_STYLE_OPTION},
{"color", optional_argument, 0, COLOR_OPTION},
{"block-size", required_argument, 0, BLOCK_SIZE_OPTION},
{"author", no_argument, 0, AUTHOR_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
static char const *const format_args[] =
{
"verbose", "long", "commas", "horizontal", "across",
"vertical", "single-column", 0
};
static enum format const format_types[] =
{
long_format, long_format, with_commas, horizontal, horizontal,
many_per_line, one_per_line
};
static char const *const sort_args[] =
{
"none", "time", "size", "extension", "version", 0
};
static enum sort_type const sort_types[] =
{
sort_none, sort_time, sort_size, sort_extension, sort_version
};
static char const *const time_args[] =
{
"atime", "access", "use", "ctime", "status", 0
};
static enum time_type const time_types[] =
{
time_atime, time_atime, time_atime, time_ctime, time_ctime
};
static char const *const color_args[] =
{
/* force and none are for compatibility with another color-ls version */
"always", "yes", "force",
"never", "no", "none",
"auto", "tty", "if-tty", 0
};
static enum color_type const color_types[] =
{
color_always, color_always, color_always,
color_never, color_never, color_never,
color_if_tty, color_if_tty, color_if_tty
};
/* Information about filling a column. */
struct column_info
{
int valid_len;
int line_len;
int *col_arr;
};
/* Array with information about column filledness. */
static struct column_info *column_info;
/* Maximum number of columns ever possible for this display. */
static int max_idx;
/* The minimum width of a colum is 3: 1 character for the name and 2
for the separating white space. */
#define MIN_COLUMN_WIDTH 3
/* This zero-based index is used solely with the --dired option.
When that option is in effect, this counter is incremented for each
character of output generated by this program so that the beginning
and ending indices (in that output) of every file name can be recorded
and later output themselves. */
static size_t dired_pos;
#define DIRED_PUTCHAR(c) do {putchar ((c)); ++dired_pos;} while (0)
/* Write S to STREAM and increment DIRED_POS by S_LEN. */
#define DIRED_FPUTS(s, stream, s_len) \
do {fputs ((s), (stream)); dired_pos += s_len;} while (0)
/* Like DIRED_FPUTS, but for use when S is a literal string. */
#define DIRED_FPUTS_LITERAL(s, stream) \
do {fputs ((s), (stream)); dired_pos += sizeof((s)) - 1;} while (0)
#define DIRED_INDENT() \
do \
{ \
/* FIXME: remove the `&& format == long_format' clause. */ \
if (dired && format == long_format) \
DIRED_FPUTS_LITERAL (" ", stdout); \
} \
while (0)
/* With --dired, store pairs of beginning and ending indices of filenames. */
static struct obstack dired_obstack;
/* With --dired, store pairs of beginning and ending indices of any
directory names that appear as headers (just before `total' line)
for lists of directory entries. Such directory names are seen when
listing hierarchies using -R and when a directory is listed with at
least one other command line argument. */
static struct obstack subdired_obstack;
/* Save the current index on the specified obstack, OBS. */
#define PUSH_CURRENT_DIRED_POS(obs) \
do \
{ \
/* FIXME: remove the `&& format == long_format' clause. */ \
if (dired && format == long_format) \
obstack_grow ((obs), &dired_pos, sizeof (dired_pos)); \
} \
while (0)
/* With -R, this stack is used to help detect directory cycles.
The device/inode pairs on this stack mirror the pairs in the
active_dir_set hash table. */
static struct obstack dev_ino_obstack;
/* Push a pair onto the device/inode stack. */
#define DEV_INO_PUSH(Dev, Ino) \
do \
{ \
struct dev_ino *di; \
obstack_blank (&dev_ino_obstack, sizeof (struct dev_ino)); \
di = -1 + (struct dev_ino *) obstack_next_free (&dev_ino_obstack); \
di->st_dev = (Dev); \
di->st_ino = (Ino); \
} \
while (0)
/* Pop a dev/ino struct off the global dev_ino_obstack
and return that struct. */
static struct dev_ino
dev_ino_pop (void)
{
assert (sizeof (struct dev_ino) <= obstack_object_size (&dev_ino_obstack));
obstack_blank (&dev_ino_obstack, -(int) (sizeof (struct dev_ino)));
return *(struct dev_ino*) obstack_next_free (&dev_ino_obstack);
}
#define ASSERT_MATCHING_DEV_INO(Name, Di) \
do \
{ \
struct stat sb; \
assert (Name); \
assert (0 <= stat (Name, &sb)); \
assert (sb.st_dev == Di.st_dev); \
assert (sb.st_ino == Di.st_ino); \
} \
while (0)
/* Write to standard output PREFIX, followed by the quoting style and
a space-separated list of the integers stored in OS all on one line. */
static void
dired_dump_obstack (const char *prefix, struct obstack *os)
{
int n_pos;
n_pos = obstack_object_size (os) / sizeof (dired_pos);
if (n_pos > 0)
{
int i;
size_t *pos;
pos = (size_t *) obstack_finish (os);
fputs (prefix, stdout);
for (i = 0; i < n_pos; i++)
printf (" %lu", (unsigned long) pos[i]);
fputs ("\n", stdout);
}
}
static unsigned int
dev_ino_hash (void const *x, unsigned int table_size)
{
struct dev_ino const *p = x;
return (uintmax_t) p->st_ino % table_size;
}
static bool
dev_ino_compare (void const *x, void const *y)
{
struct dev_ino const *a = x;
struct dev_ino const *b = y;
return SAME_INODE (*a, *b) ? true : false;
}
static void
dev_ino_free (void *x)
{
free (x);
}
/* Add the device/inode pair (P->st_dev/P->st_ino) to the set of
active directories. Return nonzero if there is already a matching
entry in the table. Otherwise, return zero. */
static int
visit_dir (dev_t dev, ino_t ino)
{
struct dev_ino *ent;
struct dev_ino *ent_from_table;
int found_match;
ent = XMALLOC (struct dev_ino, 1);
ent->st_ino = ino;
ent->st_dev = dev;
/* Attempt to insert this entry into the table. */
ent_from_table = hash_insert (active_dir_set, ent);
if (ent_from_table == NULL)
{
/* Insertion failed due to lack of memory. */
xalloc_die ();
}
found_match = (ent_from_table != ent);
if (found_match)
{
/* ent was not inserted, so free it. */
free (ent);
}
return found_match;
}
static void
free_pending_ent (struct pending *p)
{
if (p->name)
free (p->name);
if (p->realname)
free (p->realname);
free (p);
}
int
main (int argc, char **argv)
{
register int i;
register struct pending *thispend;
unsigned int n_files;
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
#define N_ENTRIES(Array) (sizeof Array / sizeof *(Array))
assert (N_ENTRIES (color_indicator) + 1 == N_ENTRIES (indicator_name));
exit_status = 0;
dir_defaulted = 1;
print_dir_name = 1;
pending_dirs = 0;
i = decode_switches (argc, argv);
if (print_with_color)
parse_ls_color ();
/* Test print_with_color again, because the call to parse_ls_color
may have just reset it -- e.g., if LS_COLORS is invalid. */
if (print_with_color)
{
prep_non_filename_text ();
/* Avoid following symbolic links when possible. */
if (color_indicator[C_ORPHAN].string != NULL
|| (color_indicator[C_MISSING].string != NULL
&& format == long_format))
check_symlink_color = 1;
}
if (dereference == DEREF_UNDEFINED)
dereference = ((immediate_dirs
|| indicator_style == classify
|| format == long_format)
? DEREF_NEVER
: DEREF_COMMAND_LINE_ARGUMENTS);
/* When using -R, initialize a data structure we'll use to
detect any directory cycles. */
if (recursive)
{
active_dir_set = hash_initialize (INITIAL_TABLE_SIZE, NULL,
dev_ino_hash,
dev_ino_compare,
dev_ino_free);
if (active_dir_set == NULL)
xalloc_die ();
obstack_init (&dev_ino_obstack);
}
format_needs_stat = sort_type == sort_time || sort_type == sort_size
|| format == long_format
|| dereference == DEREF_ALWAYS
|| recursive || print_block_size || print_inode;
format_needs_type = (format_needs_stat == 0
&& (print_with_color || indicator_style != none));
if (dired && format == long_format)
{
obstack_init (&dired_obstack);
obstack_init (&subdired_obstack);
}
nfiles = 100;
files = XMALLOC (struct fileinfo, nfiles);
files_index = 0;
clear_files ();
n_files = argc - i;
if (0 < n_files)
dir_defaulted = 0;
for (; i < argc; i++)
{
gobble_file (argv[i], unknown, 1, "");
}
if (dir_defaulted)
{
if (immediate_dirs)
gobble_file (".", directory, 1, "");
else
queue_directory (".", 0);
}
if (files_index)
{
sort_files ();
if (!immediate_dirs)
extract_dirs_from_files ("", 0);
/* `files_index' might be zero now. */
}
/* In the following if/else blocks, it is sufficient to test `pending_dirs'
(and not pending_dirs->name) because there may be no markers in the queue
at this point. A marker may be enqueued when extract_dirs_from_files is
called with a non-empty string or via print_dir. */
if (files_index)
{
print_current_files ();
if (pending_dirs)
DIRED_PUTCHAR ('\n');
}
else if (n_files <= 1 && pending_dirs && pending_dirs->next == 0)
print_dir_name = 0;
while (pending_dirs)
{
thispend = pending_dirs;
pending_dirs = pending_dirs->next;
if (LOOP_DETECT)
{
if (thispend->name == NULL)
{
/* thispend->name == NULL means this is a marker entry
indicating we've finished processing the directory.
Use its dev/ino numbers to remove the corresponding
entry from the active_dir_set hash table. */
struct dev_ino di = dev_ino_pop ();
struct dev_ino *found = hash_delete (active_dir_set, &di);
/* ASSERT_MATCHING_DEV_INO (thispend->realname, di); */
assert (found);
dev_ino_free (found);
free_pending_ent (thispend);
continue;
}
}
print_dir (thispend->name, thispend->realname);
free_pending_ent (thispend);
print_dir_name = 1;
}
if (dired && format == long_format)
{
/* No need to free these since we're about to exit. */
dired_dump_obstack ("//DIRED//", &dired_obstack);
dired_dump_obstack ("//SUBDIRED//", &subdired_obstack);
printf ("//DIRED-OPTIONS// --quoting-style=%s\n",
quoting_style_args[get_quoting_style (filename_quoting_options)]);
}
/* Restore default color before exiting */
if (print_with_color)
{
put_indicator (&color_indicator[C_LEFT]);
put_indicator (&color_indicator[C_RIGHT]);
}
if (LOOP_DETECT)
{
assert (hash_get_n_entries (active_dir_set) == 0);
hash_free (active_dir_set);
}
exit (exit_status);
}
/* Set all the option flags according to the switches specified.
Return the index of the first non-option argument. */
static int
decode_switches (int argc, char **argv)
{
int c;
char *time_style_option = 0;
/* Record whether there is an option specifying sort type. */
int sort_type_specified = 0;
qmark_funny_chars = 0;
/* initialize all switches to default settings */
switch (ls_mode)
{
case LS_MULTI_COL:
/* This is for the `dir' program. */
format = many_per_line;
set_quoting_style (NULL, escape_quoting_style);
break;
case LS_LONG_FORMAT:
/* This is for the `vdir' program. */
format = long_format;
set_quoting_style (NULL, escape_quoting_style);
break;
case LS_LS:
/* This is for the `ls' program. */
if (isatty (STDOUT_FILENO))
{
format = many_per_line;
/* See description of qmark_funny_chars, above. */
qmark_funny_chars = 1;
}
else
{
format = one_per_line;
qmark_funny_chars = 0;
}
break;
default:
abort ();
}
time_type = time_mtime;
sort_type = sort_name;
sort_reverse = 0;
numeric_ids = 0;
print_block_size = 0;
indicator_style = none;
print_inode = 0;
dereference = DEREF_UNDEFINED;
recursive = 0;
immediate_dirs = 0;
all_files = 0;
really_all_files = 0;
ignore_patterns = 0;
/* FIXME: put this in a function. */
{
char const *q_style = getenv ("QUOTING_STYLE");
if (q_style)
{
int i = ARGCASEMATCH (q_style, quoting_style_args, quoting_style_vals);
if (0 <= i)
set_quoting_style (NULL, quoting_style_vals[i]);
else
error (0, 0,
_("ignoring invalid value of environment variable QUOTING_STYLE: %s"),
quotearg (q_style));
}
}
human_block_size (getenv ("LS_BLOCK_SIZE"), 0, &output_block_size);
line_length = 80;
{
char const *p = getenv ("COLUMNS");
if (p && *p)
{
long int tmp_long;
if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
&& 0 < tmp_long && tmp_long <= INT_MAX)
{
line_length = (int) tmp_long;
}
else
{
error (0, 0,
_("ignoring invalid width in environment variable COLUMNS: %s"),
quotearg (p));
}
}
}
#ifdef TIOCGWINSZ
{
struct winsize ws;
if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0)
line_length = ws.ws_col;
}
#endif
/* Using the TABSIZE environment variable is not POSIX-approved.
Ignore it when POSIXLY_CORRECT is set. */
{
char const *p;
tabsize = 8;
if (!getenv ("POSIXLY_CORRECT") && (p = getenv ("TABSIZE")))
{
long int tmp_long;
if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
&& 0 <= tmp_long && tmp_long <= INT_MAX)
{
tabsize = (int) tmp_long;
}
else
{
error (0, 0,
_("ignoring invalid tab size in environment variable TABSIZE: %s"),
quotearg (p));
}
}
}
while ((c = getopt_long (argc, argv,
"abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
long_options, NULL)) != -1)
{
switch (c)
{
case 0:
break;
case 'a':
all_files = 1;
really_all_files = 1;
break;
case 'b':
set_quoting_style (NULL, escape_quoting_style);
break;
case 'c':
time_type = time_ctime;
break;
case 'd':
immediate_dirs = 1;
break;
case 'f':
/* Same as enabling -a -U and disabling -l -s. */
all_files = 1;
really_all_files = 1;
sort_type = sort_none;
sort_type_specified = 1;
/* disable -l */
if (format == long_format)
format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);
print_block_size = 0; /* disable -s */
print_with_color = 0; /* disable --color */
break;
case 'g':
format = long_format;
print_owner = 0;
break;
case 'h':
output_block_size = -1024;
break;
case 'i':
print_inode = 1;
break;
case 'k':
output_block_size = 1024;
break;
case 'l':
format = long_format;
break;
case 'm':
format = with_commas;
break;
case 'n':
numeric_ids = 1;
format = long_format;
break;
case 'o': /* Just like -l, but don't display group info. */
format = long_format;
print_group = 0;
break;
case 'p':
indicator_style = file_type;
break;
case 'q':
qmark_funny_chars = 1;
break;
case 'r':
sort_reverse = 1;
break;
case 's':
print_block_size = 1;
break;
case 't':
sort_type = sort_time;
sort_type_specified = 1;
break;
case 'u':
time_type = time_atime;
break;
case 'v':
sort_type = sort_version;
sort_type_specified = 1;
break;
case 'w':
{
long int tmp_long;
if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
|| tmp_long <= 0 || tmp_long > INT_MAX)
error (EXIT_FAILURE, 0, _("invalid line width: %s"),
quotearg (optarg));
line_length = (int) tmp_long;
break;
}
case 'x':
format = horizontal;
break;
case 'A':
really_all_files = 0;
all_files = 1;
break;
case 'B':
add_ignore_pattern ("*~");
add_ignore_pattern (".*~");
break;
case 'C':
format = many_per_line;
break;
case 'D':
dired = 1;
break;
case 'F':
indicator_style = classify;
break;
case 'G': /* inhibit display of group info */
print_group = 0;
break;
case 'H':
dereference = DEREF_COMMAND_LINE_ARGUMENTS;
break;
case 'I':
add_ignore_pattern (optarg);
break;
case 'L':
dereference = DEREF_ALWAYS;
break;
case 'N':
set_quoting_style (NULL, literal_quoting_style);
break;
case 'Q':
set_quoting_style (NULL, c_quoting_style);
break;
case 'R':
recursive = 1;
break;
case 'S':
sort_type = sort_size;
sort_type_specified = 1;
break;
case 'T':
{
long int tmp_long;
if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
|| tmp_long < 0 || tmp_long > INT_MAX)
error (EXIT_FAILURE, 0, _("invalid tab size: %s"),
quotearg (optarg));
tabsize = (int) tmp_long;
break;
}
case 'U':
sort_type = sort_none;
sort_type_specified = 1;
break;
case 'X':
sort_type = sort_extension;
sort_type_specified = 1;
break;
case '1':
/* -1 has no effect after -l. */
if (format != long_format)
format = one_per_line;
break;
case AUTHOR_OPTION:
print_author = true;
break;
case SORT_OPTION:
sort_type = XARGMATCH ("--sort", optarg, sort_args, sort_types);
sort_type_specified = 1;
break;
case TIME_OPTION:
time_type = XARGMATCH ("--time", optarg, time_args, time_types);
break;
case FORMAT_OPTION:
format = XARGMATCH ("--format", optarg, format_args, format_types);
break;
case FULL_TIME_OPTION:
format = long_format;
time_style_option = "full-iso";
break;
case COLOR_OPTION:
{
int i;
if (optarg)
i = XARGMATCH ("--color", optarg, color_args, color_types);
else
/* Using --color with no argument is equivalent to using
--color=always. */
i = color_always;
print_with_color = (i == color_always
|| (i == color_if_tty
&& isatty (STDOUT_FILENO)));
if (print_with_color)
{
/* Don't use TAB characters in output. Some terminal
emulators can't handle the combination of tabs and
color codes on the same line. */
tabsize = 0;
}
break;
}
case INDICATOR_STYLE_OPTION:
indicator_style = XARGMATCH ("--indicator-style", optarg,
indicator_style_args,
indicator_style_types);
break;
case QUOTING_STYLE_OPTION:
set_quoting_style (NULL,
XARGMATCH ("--quoting-style", optarg,
quoting_style_args,
quoting_style_vals));
break;
case TIME_STYLE_OPTION:
time_style_option = optarg;
break;
case SHOW_CONTROL_CHARS_OPTION:
qmark_funny_chars = 0;
break;
case BLOCK_SIZE_OPTION:
human_block_size (optarg, 1, &output_block_size);
break;
case SI_OPTION:
output_block_size = -1000;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
filename_quoting_options = clone_quoting_options (NULL);
if (get_quoting_style (filename_quoting_options) == escape_quoting_style)
set_char_quoting (filename_quoting_options, ' ', 1);
if (indicator_style != none)
{
char const *p;
for (p = "*=@|" + (int) indicator_style - 1; *p; p++)
set_char_quoting (filename_quoting_options, *p, 1);
}
dirname_quoting_options = clone_quoting_options (NULL);
set_char_quoting (dirname_quoting_options, ':', 1);
/* If -c or -u is specified and not -l (or any other option that implies -l),
and no sort-type was specified, then sort by the ctime (-c) or atime (-u).
The behavior of ls when using either -c or -u but with neither -l nor -t
appears to be unspecified by POSIX. So, with GNU ls, `-u' alone means
sort by atime (this is the one that's not specified by the POSIX spec),
-lu means show atime and sort by name, -lut means show atime and sort
by atime. */
if ((time_type == time_ctime || time_type == time_atime)
&& !sort_type_specified && format != long_format)
{
sort_type = sort_time;
}
if (format == long_format)
{
if (! time_style_option)
time_style_option = getenv ("TIME_STYLE");
if (time_style_option && *time_style_option == '+')
{
char *p0 = time_style_option + 1;
char *p1 = strchr (p0, '\n');
if (! p1)
p1 = p0;
else
{
if (strchr (p1 + 1, '\n'))
error (EXIT_FAILURE, 0, _("invalid time style format %s"),
quote (p0));
*p1++ = '\0';
}
long_time_format[0] = p0;
long_time_format[1] = p1;
}
else
switch (time_style_option
? XARGMATCH ("time style", time_style_option,
time_style_args,
time_style_types)
: posix_iso_time_style)
{
case full_iso_time_style:
long_time_format[0] = long_time_format[1] =
"%Y-%m-%d %H:%M:%S.%N %z";
break;
case posix_iso_time_style:
if (! hard_locale (LC_TIME))
break;
/* Fall through. */
case iso_time_style:
long_time_format[0] = "%Y-%m-%d ";
long_time_format[1] = "%m-%d %H:%M";
break;
case locale_time_style:
if (hard_locale (LC_TIME))
{
unsigned int i;
for (i = 0; i < 2; i++)
long_time_format[i] =
dcgettext (NULL, long_time_format[i], LC_TIME);
}
}
}
return optind;
}
/* Parse a string as part of the LS_COLORS variable; this may involve
decoding all kinds of escape characters. If equals_end is set an
unescaped equal sign ends the string, otherwise only a : or \0
does. Returns the number of characters output, or -1 on failure.
The resulting string is *not* null-terminated, but may contain
embedded nulls.
Note that both dest and src are char **; on return they point to
the first free byte after the array and the character that ended
the input string, respectively. */
static int
get_funky_string (char **dest, const char **src, int equals_end)
{
int num; /* For numerical codes */
int count; /* Something to count with */
enum {
ST_GND, ST_BACKSLASH, ST_OCTAL, ST_HEX, ST_CARET, ST_END, ST_ERROR
} state;
const char *p;
char *q;
p = *src; /* We don't want to double-indirect */
q = *dest; /* the whole darn time. */
count = 0; /* No characters counted in yet. */
num = 0;
state = ST_GND; /* Start in ground state. */
while (state < ST_END)
{
switch (state)
{
case ST_GND: /* Ground state (no escapes) */
switch (*p)
{
case ':':
case '\0':
state = ST_END; /* End of string */
break;
case '\\':
state = ST_BACKSLASH; /* Backslash scape sequence */
++p;
break;
case '^':
state = ST_CARET; /* Caret escape */
++p;
break;
case '=':
if (equals_end)
{
state = ST_END; /* End */
break;
}
/* else fall through */
default:
*(q++) = *(p++);
++count;
break;
}
break;
case ST_BACKSLASH: /* Backslash escaped character */
switch (*p)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
state = ST_OCTAL; /* Octal sequence */
num = *p - '0';
break;
case 'x':
case 'X':
state = ST_HEX; /* Hex sequence */
num = 0;
break;
case 'a': /* Bell */
num = 7; /* Not all C compilers know what \a means */
break;
case 'b': /* Backspace */
num = '\b';
break;
case 'e': /* Escape */
num = 27;
break;
case 'f': /* Form feed */
num = '\f';
break;
case 'n': /* Newline */
num = '\n';
break;
case 'r': /* Carriage return */
num = '\r';
break;
case 't': /* Tab */
num = '\t';
break;
case 'v': /* Vtab */
num = '\v';
break;
case '?': /* Delete */
num = 127;
break;
case '_': /* Space */
num = ' ';
break;
case '\0': /* End of string */
state = ST_ERROR; /* Error! */
break;
default: /* Escaped character like \ ^ : = */
num = *p;
break;
}
if (state == ST_BACKSLASH)
{
*(q++) = num;
++count;
state = ST_GND;
}
++p;
break;
case ST_OCTAL: /* Octal sequence */
if (*p < '0' || *p > '7')
{
*(q++) = num;
++count;
state = ST_GND;
}
else
num = (num << 3) + (*(p++) - '0');
break;
case ST_HEX: /* Hex sequence */
switch (*p)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num = (num << 4) + (*(p++) - '0');
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
num = (num << 4) + (*(p++) - 'a') + 10;
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
num = (num << 4) + (*(p++) - 'A') + 10;
break;
default:
*(q++) = num;
++count;
state = ST_GND;
break;
}
break;
case ST_CARET: /* Caret escape */
state = ST_GND; /* Should be the next state... */
if (*p >= '@' && *p <= '~')
{
*(q++) = *(p++) & 037;
++count;
}
else if (*p == '?')
{
*(q++) = 127;
++count;
}
else
state = ST_ERROR;
break;
default:
abort ();
}
}
*dest = q;
*src = p;
return state == ST_ERROR ? -1 : count;
}
static void
parse_ls_color (void)
{
const char *p; /* Pointer to character being parsed */
char *buf; /* color_buf buffer pointer */
int state; /* State of parser */
int ind_no; /* Indicator number */
char label[3]; /* Indicator label */
struct color_ext_type *ext; /* Extension we are working on */
if ((p = getenv ("LS_COLORS")) == NULL || *p == '\0')
return;
ext = NULL;
strcpy (label, "??");
/* This is an overly conservative estimate, but any possible
LS_COLORS string will *not* generate a color_buf longer than
itself, so it is a safe way of allocating a buffer in
advance. */
buf = color_buf = xstrdup (p);
state = 1;
while (state > 0)
{
switch (state)
{
case 1: /* First label character */
switch (*p)
{
case ':':
++p;
break;
case '*':
/* Allocate new extension block and add to head of
linked list (this way a later definition will
override an earlier one, which can be useful for
having terminal-specific defs override global). */
ext = XMALLOC (struct color_ext_type, 1);
ext->next = color_ext_list;
color_ext_list = ext;
++p;
ext->ext.string = buf;
state = (ext->ext.len =
get_funky_string (&buf, &p, 1)) < 0 ? -1 : 4;
break;
case '\0':
state = 0; /* Done! */
break;
default: /* Assume it is file type label */
label[0] = *(p++);
state = 2;
break;
}
break;
case 2: /* Second label character */
if (*p)
{
label[1] = *(p++);
state = 3;
}
else
state = -1; /* Error */
break;
case 3: /* Equal sign after indicator label */
state = -1; /* Assume failure... */
if (*(p++) == '=')/* It *should* be... */
{
for (ind_no = 0; indicator_name[ind_no] != NULL; ++ind_no)
{
if (STREQ (label, indicator_name[ind_no]))
{
color_indicator[ind_no].string = buf;
state = ((color_indicator[ind_no].len =
get_funky_string (&buf, &p, 0)) < 0 ? -1 : 1);
break;
}
}
if (state == -1)
error (0, 0, _("unrecognized prefix: %s"), quotearg (label));
}
break;
case 4: /* Equal sign after *.ext */
if (*(p++) == '=')
{
ext->seq.string = buf;
state = (ext->seq.len =
get_funky_string (&buf, &p, 0)) < 0 ? -1 : 1;
}
else
state = -1;
break;
}
}
if (state < 0)
{
struct color_ext_type *e;
struct color_ext_type *e2;
error (0, 0,
_("unparsable value for LS_COLORS environment variable"));
free (color_buf);
for (e = color_ext_list; e != NULL; /* empty */)
{
e2 = e;
e = e->next;
free (e2);
}
print_with_color = 0;
}
if (color_indicator[C_LINK].len == 6
&& !strncmp (color_indicator[C_LINK].string, "target", 6))
color_symlink_as_referent = 1;
}
/* Request that the directory named NAME have its contents listed later.
If REALNAME is nonzero, it will be used instead of NAME when the
directory name is printed. This allows symbolic links to directories
to be treated as regular directories but still be listed under their
real names. NAME == NULL is used to insert a marker entry for the
directory named in REALNAME.
If F is non-NULL, we use its dev/ino information to save
a call to stat -- when doing a recursive (-R) traversal. */
static void
queue_directory (const char *name, const char *realname)
{
struct pending *new;
new = XMALLOC (struct pending, 1);
new->realname = realname ? xstrdup (realname) : NULL;
new->name = name ? xstrdup (name) : NULL;
new->next = pending_dirs;
pending_dirs = new;
}
/* Read directory `name', and list the files in it.
If `realname' is nonzero, print its name instead of `name';
this is used for symbolic links to directories. */
static void
print_dir (const char *name, const char *realname)
{
register DIR *reading;
register struct dirent *next;
register uintmax_t total_blocks = 0;
static int first = 1;
errno = 0;
reading = opendir (name);
if (!reading)
{
error (0, errno, "%s", quotearg_colon (name));
exit_status = 1;
return;
}
if (LOOP_DETECT)
{
struct stat dir_stat;
int fd = dirfd (reading);
/* If dirfd failed, endure the overhead of using stat. */
if ((0 <= fd
? fstat (fd, &dir_stat)
: stat (name, &dir_stat)) < 0)
{
error (0, errno, _("cannot determine device and inode of %s"),
quotearg_colon (name));
exit_status = 1;
return;
}
/* If we've already visited this dev/inode pair, warn that
we've found a loop, and do not process this directory. */
if (visit_dir (dir_stat.st_dev, dir_stat.st_ino))
{
error (0, 0, _("not listing already-listed directory: %s"),
quotearg_colon (name));
return;
}
DEV_INO_PUSH (dir_stat.st_dev, dir_stat.st_ino);
}
/* Read the directory entries, and insert the subfiles into the `files'
table. */
clear_files ();
while ((next = readdir (reading)) != NULL)
if (file_interesting (next))
{
enum filetype type = unknown;
#if HAVE_STRUCT_DIRENT_D_TYPE
if (next->d_type == DT_DIR || next->d_type == DT_CHR
|| next->d_type == DT_BLK || next->d_type == DT_SOCK
|| next->d_type == DT_FIFO)
type = next->d_type;
#endif
total_blocks += gobble_file (next->d_name, type, 0, name);
}
if (CLOSEDIR (reading))
{
error (0, errno, "%s", quotearg_colon (name));
exit_status = 1;
/* Don't return; print whatever we got. */
}
/* Sort the directory contents. */
sort_files ();
/* If any member files are subdirectories, perhaps they should have their
contents listed rather than being mentioned here as files. */
if (recursive)
extract_dirs_from_files (name, 1);
if (recursive || print_dir_name)
{
if (!first)
DIRED_PUTCHAR ('\n');
first = 0;
DIRED_INDENT ();
PUSH_CURRENT_DIRED_POS (&subdired_obstack);
dired_pos += quote_name (stdout, realname ? realname : name,
dirname_quoting_options);
PUSH_CURRENT_DIRED_POS (&subdired_obstack);
DIRED_FPUTS_LITERAL (":\n", stdout);
}
if (format == long_format || print_block_size)
{
const char *p;
char buf[LONGEST_HUMAN_READABLE + 1];
DIRED_INDENT ();
p = _("total");
DIRED_FPUTS (p, stdout, strlen (p));
DIRED_PUTCHAR (' ');
p = human_readable_inexact (total_blocks, buf, ST_NBLOCKSIZE,
output_block_size, human_ceiling);
DIRED_FPUTS (p, stdout, strlen (p));
DIRED_PUTCHAR ('\n');
}
if (files_index)
print_current_files ();
}
/* Add `pattern' to the list of patterns for which files that match are
not listed. */
static void
add_ignore_pattern (const char *pattern)
{
register struct ignore_pattern *ignore;
ignore = XMALLOC (struct ignore_pattern, 1);
ignore->pattern = pattern;
/* Add it to the head of the linked list. */
ignore->next = ignore_patterns;
ignore_patterns = ignore;
}
/* Return nonzero if the file in `next' should be listed. */
static int
file_interesting (const struct dirent *next)
{
register struct ignore_pattern *ignore;
for (ignore = ignore_patterns; ignore; ignore = ignore->next)
if (fnmatch (ignore->pattern, next->d_name, FNM_PERIOD) == 0)
return 0;
if (really_all_files
|| next->d_name[0] != '.'
|| (all_files
&& next->d_name[1] != '\0'
&& (next->d_name[1] != '.' || next->d_name[2] != '\0')))
return 1;
return 0;
}
/* Enter and remove entries in the table `files'. */
/* Empty the table of files. */
static void
clear_files (void)
{
register int i;
for (i = 0; i < files_index; i++)
{
free (files[i].name);
if (files[i].linkname)
free (files[i].linkname);
}
files_index = 0;
block_size_size = 4;
}
/* Add a file to the current table of files.
Verify that the file exists, and print an error message if it does not.
Return the number of blocks that the file occupies. */
static uintmax_t
gobble_file (const char *name, enum filetype type, int explicit_arg,
const char *dirname)
{
register uintmax_t blocks;
register char *path;
if (files_index == nfiles)
{
nfiles *= 2;
files = XREALLOC (files, struct fileinfo, nfiles);
}
files[files_index].linkname = 0;
files[files_index].linkmode = 0;
files[files_index].linkok = 0;
/* FIXME: this use of ls: `mkdir a; touch a/{b,c,d}; ls -R a'
shouldn't require that ls stat b, c, and d -- at least
not on systems with usable d_type. The problem is that
format_needs_stat is set, because of the -R. */
if (explicit_arg || format_needs_stat
|| (format_needs_type && type == unknown))
{
/* `path' is the absolute pathname of this file. */
int val;
if (name[0] == '/' || dirname[0] == 0)
path = (char *) name;
else
{
path = (char *) alloca (strlen (name) + strlen (dirname) + 2);
attach (path, dirname, name);
}
val = (DEREF_ALWAYS <= dereference + explicit_arg
? stat (path, &files[files_index].stat)
: lstat (path, &files[files_index].stat));
if (val < 0)
{
error (0, errno, "%s", quotearg_colon (path));
exit_status = 1;
return 0;
}
#if HAVE_ACL
if (format == long_format)
{
int n = file_has_acl (path, &files[files_index].stat);
files[files_index].have_acl = (0 < n);
if (n < 0)
error (0, errno, "%s", quotearg_colon (path));
}
#endif
if (S_ISLNK (files[files_index].stat.st_mode)
&& (format == long_format || check_symlink_color))
{
char *linkpath;
struct stat linkstats;
get_link_name (path, &files[files_index]);
linkpath = make_link_path (path, files[files_index].linkname);
/* Avoid following symbolic links when possible, ie, when
they won't be traced and when no indicator is needed. */
if (linkpath
&& (indicator_style != none || check_symlink_color)
&& stat (linkpath, &linkstats) == 0)
{
files[files_index].linkok = 1;
/* Symbolic links to directories that are mentioned on the
command line are automatically traced if not being
listed as files. */
if (explicit_arg && format != long_format
&& S_ISDIR (linkstats.st_mode))
{
/* Substitute the linked-to directory's name, but
save the real name in `linkname' for printing. */
if (!immediate_dirs)
{
const char *tempname = name;
name = linkpath;
linkpath = files[files_index].linkname;
files[files_index].linkname = (char *) tempname;
}
files[files_index].stat = linkstats;
}
else
{
/* Get the linked-to file's mode for the filetype indicator
in long listings. */
files[files_index].linkmode = linkstats.st_mode;
files[files_index].linkok = 1;
}
}
if (linkpath)
free (linkpath);
}
if (S_ISLNK (files[files_index].stat.st_mode))
files[files_index].filetype = symbolic_link;
else if (S_ISDIR (files[files_index].stat.st_mode))
{
if (explicit_arg && !immediate_dirs)
files[files_index].filetype = arg_directory;
else
files[files_index].filetype = directory;
}
else
files[files_index].filetype = normal;
blocks = ST_NBLOCKS (files[files_index].stat);
{
char buf[LONGEST_HUMAN_READABLE + 1];
int len = strlen (human_readable_inexact (blocks, buf, ST_NBLOCKSIZE,
output_block_size,
human_ceiling));
if (block_size_size < len)
block_size_size = len < 7 ? len : 7;
}
}
else
{
files[files_index].filetype = type;
#if HAVE_STRUCT_DIRENT_D_TYPE
files[files_index].stat.st_mode = DTTOIF (type);
#endif
blocks = 0;
}
files[files_index].name = xstrdup (name);
files_index++;
return blocks;
}
#if HAVE_SYMLINKS
/* Put the name of the file that `filename' is a symbolic link to
into the `linkname' field of `f'. */
static void
get_link_name (const char *filename, struct fileinfo *f)
{
f->linkname = xreadlink (filename);
if (f->linkname == NULL)
{
error (0, errno, _("cannot read symbolic link %s"),
quotearg_colon (filename));
exit_status = 1;
}
}
/* If `linkname' is a relative path and `path' contains one or more
leading directories, return `linkname' with those directories
prepended; otherwise, return a copy of `linkname'.
If `linkname' is zero, return zero. */
static char *
make_link_path (const char *path, const char *linkname)
{
char *linkbuf;
size_t bufsiz;
if (linkname == 0)
return 0;
if (*linkname == '/')
return xstrdup (linkname);
/* The link is to a relative path. Prepend any leading path
in `path' to the link name. */
linkbuf = strrchr (path, '/');
if (linkbuf == 0)
return xstrdup (linkname);
bufsiz = linkbuf - path + 1;
linkbuf = xmalloc (bufsiz + strlen (linkname) + 1);
strncpy (linkbuf, path, bufsiz);
strcpy (linkbuf + bufsiz, linkname);
return linkbuf;
}
#endif
/* Return nonzero if base_name (NAME) ends in `.' or `..'
This is so we don't try to recurse on `././././. ...' */
static int
basename_is_dot_or_dotdot (const char *name)
{
char const *base = base_name (name);
return DOT_OR_DOTDOT (base);
}
/* Remove any entries from `files' that are for directories,
and queue them to be listed as directories instead.
`dirname' is the prefix to prepend to each dirname
to make it correct relative to ls's working dir.
If IGNORE_DOT_AND_DOT_DOT is nonzero don't treat `.' and `..' as dirs.
This is desirable when processing directories recursively. */
static void
extract_dirs_from_files (const char *dirname, int ignore_dot_and_dot_dot)
{
register int i, j;
if (*dirname && LOOP_DETECT)
{
/* Insert a marker entry first. When we dequeue this marker entry,
we'll know that DIRNAME has been processed and may be removed
from the set of active directories. */
queue_directory (NULL, dirname);
}
/* Queue the directories last one first, because queueing reverses the
order. */
for (i = files_index - 1; i >= 0; i--)
if ((files[i].filetype == directory || files[i].filetype == arg_directory)
&& (!ignore_dot_and_dot_dot
|| !basename_is_dot_or_dotdot (files[i].name)))
{
if (files[i].name[0] == '/' || dirname[0] == 0)
{
queue_directory (files[i].name, files[i].linkname);
}
else
{
char *path = path_concat (dirname, files[i].name, NULL);
queue_directory (path, files[i].linkname);
free (path);
}
if (files[i].filetype == arg_directory)
free (files[i].name);
}
/* Now delete the directories from the table, compacting all the remaining
entries. */
for (i = 0, j = 0; i < files_index; i++)
if (files[i].filetype != arg_directory)
files[j++] = files[i];
files_index = j;
}
/* Use strcoll to compare strings in this locale. If an error occurs,
report an error and longjmp to failed_strcoll. */
static jmp_buf failed_strcoll;
static int
xstrcoll (char const *a, char const *b)
{
int diff;
errno = 0;
diff = strcoll (a, b);
if (errno)
{
error (0, errno, _("cannot compare file names %s and %s"),
quote_n (0, a), quote_n (1, b));
exit_status = 1;
longjmp (failed_strcoll, 1);
}
return diff;
}
/* Comparison routines for sorting the files. */
typedef void const *V;
static inline int
cmp_ctime (struct fileinfo const *a, struct fileinfo const *b,
int (*cmp) PARAMS ((char const *, char const *)))
{
int diff = CTIME_CMP (b->stat, a->stat);
return diff ? diff : cmp (a->name, b->name);
}
static int compare_ctime (V a, V b) { return cmp_ctime (a, b, xstrcoll); }
static int compstr_ctime (V a, V b) { return cmp_ctime (a, b, strcmp); }
static int rev_cmp_ctime (V a, V b) { return compare_ctime (b, a); }
static int rev_str_ctime (V a, V b) { return compstr_ctime (b, a); }
static inline int
cmp_mtime (struct fileinfo const *a, struct fileinfo const *b,
int (*cmp) PARAMS((char const *, char const *)))
{
int diff = MTIME_CMP (b->stat, a->stat);
return diff ? diff : cmp (a->name, b->name);
}
static int compare_mtime (V a, V b) { return cmp_mtime (a, b, xstrcoll); }
static int compstr_mtime (V a, V b) { return cmp_mtime (a, b, strcmp); }
static int rev_cmp_mtime (V a, V b) { return compare_mtime (b, a); }
static int rev_str_mtime (V a, V b) { return compstr_mtime (b, a); }
static inline int
cmp_atime (struct fileinfo const *a, struct fileinfo const *b,
int (*cmp) PARAMS ((char const *, char const *)))
{
int diff = ATIME_CMP (b->stat, a->stat);
return diff ? diff : cmp (a->name, b->name);
}
static int compare_atime (V a, V b) { return cmp_atime (a, b, xstrcoll); }
static int compstr_atime (V a, V b) { return cmp_atime (a, b, strcmp); }
static int rev_cmp_atime (V a, V b) { return compare_atime (b, a); }
static int rev_str_atime (V a, V b) { return compstr_atime (b, a); }
static inline int
cmp_size (struct fileinfo const *a, struct fileinfo const *b,
int (*cmp) PARAMS ((char const *, char const *)))
{
int diff = longdiff (b->stat.st_size, a->stat.st_size);
return diff ? diff : cmp (a->name, b->name);
}
static int compare_size (V a, V b) { return cmp_size (a, b, xstrcoll); }
static int compstr_size (V a, V b) { return cmp_size (a, b, strcmp); }
static int rev_cmp_size (V a, V b) { return compare_size (b, a); }
static int rev_str_size (V a, V b) { return compstr_size (b, a); }
static inline int
cmp_version (struct fileinfo const *a, struct fileinfo const *b)
{
return strverscmp (a->name, b->name);
}
static int compare_version (V a, V b) { return cmp_version (a, b); }
static int rev_cmp_version (V a, V b) { return compare_version (b, a); }
static inline int
cmp_name (struct fileinfo const *a, struct fileinfo const *b,
int (*cmp) PARAMS ((char const *, char const *)))
{
return cmp (a->name, b->name);
}
static int compare_name (V a, V b) { return cmp_name (a, b, xstrcoll); }
static int compstr_name (V a, V b) { return cmp_name (a, b, strcmp); }
static int rev_cmp_name (V a, V b) { return compare_name (b, a); }
static int rev_str_name (V a, V b) { return compstr_name (b, a); }
/* Compare file extensions. Files with no extension are `smallest'.
If extensions are the same, compare by filenames instead. */
static inline int
cmp_extension (struct fileinfo const *a, struct fileinfo const *b,
int (*cmp) PARAMS ((char const *, char const *)))
{
char const *base1 = strrchr (a->name, '.');
char const *base2 = strrchr (b->name, '.');
int diff = cmp (base1 ? base1 : "", base2 ? base2 : "");
return diff ? diff : cmp (a->name, b->name);
}
static int compare_extension (V a, V b) { return cmp_extension (a, b, xstrcoll); }
static int compstr_extension (V a, V b) { return cmp_extension (a, b, strcmp); }
static int rev_cmp_extension (V a, V b) { return compare_extension (b, a); }
static int rev_str_extension (V a, V b) { return compstr_extension (b, a); }
/* Sort the files now in the table. */
static void
sort_files (void)
{
int (*func) PARAMS ((V, V));
switch (sort_type)
{
case sort_none:
return;
case sort_time:
switch (time_type)
{
case time_ctime:
func = sort_reverse ? rev_cmp_ctime : compare_ctime;
break;
case time_mtime:
func = sort_reverse ? rev_cmp_mtime : compare_mtime;
break;
case time_atime:
func = sort_reverse ? rev_cmp_atime : compare_atime;
break;
default:
abort ();
}
break;
case sort_name:
func = sort_reverse ? rev_cmp_name : compare_name;
break;
case sort_extension:
func = sort_reverse ? rev_cmp_extension : compare_extension;
break;
case sort_size:
func = sort_reverse ? rev_cmp_size : compare_size;
break;
case sort_version:
func = sort_reverse ? rev_cmp_version : compare_version;
break;
default:
abort ();
}
/* Try strcoll. If it fails, fall back on strcmp. We can't safely
ignore strcoll failures, as a failing strcoll might be a
comparison function that is not a total order, and if we ignored
the failure this might cause qsort to dump core. */
if (setjmp (failed_strcoll))
{
switch (sort_type)
{
case sort_time:
switch (time_type)
{
case time_ctime:
func = sort_reverse ? rev_str_ctime : compstr_ctime;
break;
case time_mtime:
func = sort_reverse ? rev_str_mtime : compstr_mtime;
break;
case time_atime:
func = sort_reverse ? rev_str_atime : compstr_atime;
break;
default:
abort ();
}
break;
case sort_name:
func = sort_reverse ? rev_str_name : compstr_name;
break;
case sort_extension:
func = sort_reverse ? rev_str_extension : compstr_extension;
break;
case sort_size:
func = sort_reverse ? rev_str_size : compstr_size;
break;
default:
abort ();
}
}
qsort (files, files_index, sizeof (struct fileinfo), func);
}
/* List all the files now in the table. */
static void
print_current_files (void)
{
register int i;
switch (format)
{
case one_per_line:
for (i = 0; i < files_index; i++)
{
print_file_name_and_frills (files + i);
putchar ('\n');
}
break;
case many_per_line:
init_column_info ();
print_many_per_line ();
break;
case horizontal:
init_column_info ();
print_horizontal ();
break;
case with_commas:
print_with_commas ();
break;
case long_format:
for (i = 0; i < files_index; i++)
{
print_long_format (files + i);
DIRED_PUTCHAR ('\n');
}
break;
}
}
/* Return the expected number of columns in a long-format time stamp,
or zero if it cannot be calculated. */
static int
long_time_expected_width (void)
{
static int width = -1;
if (width < 0)
{
time_t epoch = 0;
struct tm const *tm = localtime (&epoch);
char const *fmt = long_time_format[0];
char initbuf[100];
char *buf = initbuf;
size_t bufsize = sizeof initbuf;
size_t len;
for (;;)
{
*buf = '\1';
len = nstrftime (buf, bufsize, fmt, tm, 0, 0);
if (len || ! *buf)
break;
buf = alloca (bufsize *= 2);
}
width = mbsnwidth (buf, len, 0);
if (width < 0)
width = 0;
}
return width;
}
/* Get the current time. */
static void
get_current_time (void)
{
#if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
{
struct timespec timespec;
if (clock_gettime (CLOCK_REALTIME, ×pec) == 0)
{
current_time = timespec.tv_sec;
current_time_ns = timespec.tv_nsec;
return;
}
}
#endif
/* The clock does not have nanosecond resolution, so get the maximum
possible value for the current time that is consistent with the
reported clock. That way, files are not considered to be in the
future merely because their time stamps have higher resolution
than the clock resolution. */
#if HAVE_GETTIMEOFDAY
{
struct timeval timeval;
if (gettimeofday (&timeval, NULL) == 0)
{
current_time = timeval.tv_sec;
current_time_ns = timeval.tv_usec * 1000 + 999;
return;
}
}
#endif
current_time = time (NULL);
current_time_ns = 999999999;
}
/* Format into BUFFER the name or id of the user with id U. Return
the length of the formatted buffer, not counting the terminating
null. */
static size_t
format_user (char *buffer, uid_t u)
{
char const *name = (numeric_ids ? NULL : getuser (u));
if (name)
sprintf (buffer, "%-8s ", name);
else
sprintf (buffer, "%-8lu ", (unsigned long) u);
return strlen (buffer);
}
/* Print information about F in long format. */
static void
print_long_format (const struct fileinfo *f)
{
char modebuf[12];
char init_bigbuf
[LONGEST_HUMAN_READABLE + 1 /* inode */
+ LONGEST_HUMAN_READABLE + 1 /* size in blocks */
+ sizeof (modebuf) - 1 + 1 /* mode string */
+ LONGEST_HUMAN_READABLE + 1 /* st_nlink */
+ ID_LENGTH_MAX + 1 /* owner name */
+ ID_LENGTH_MAX + 1 /* group name */
+ ID_LENGTH_MAX + 1 /* author name */
+ LONGEST_HUMAN_READABLE + 1 /* major device number */
+ LONGEST_HUMAN_READABLE + 1 /* minor device number */
+ 35 + 1 /* usual length of time/date -- may be longer; see below */
];
char *buf = init_bigbuf;
size_t bufsize = sizeof (init_bigbuf);
size_t s;
char *p;
time_t when;
int when_ns IF_LINT (= 0);
struct tm *when_local;
/* Compute mode string. On most systems, it's based on st_mode.
On systems with migration (via the stat.st_dm_mode field), use
the file's migrated status. */
mode_string (ST_DM_MODE (f->stat), modebuf);
modebuf[10] = (FILE_HAS_ACL (f) ? '+' : ' ');
modebuf[11] = '\0';
switch (time_type)
{
case time_ctime:
when = f->stat.st_ctime;
when_ns = TIMESPEC_NS (f->stat.st_ctim);
break;
case time_mtime:
when = f->stat.st_mtime;
when_ns = TIMESPEC_NS (f->stat.st_mtim);
break;
case time_atime:
when = f->stat.st_atime;
when_ns = TIMESPEC_NS (f->stat.st_atim);
break;
}
p = buf;
if (print_inode)
{
char hbuf[LONGEST_HUMAN_READABLE + 1];
sprintf (p, "%*s ", INODE_DIGITS,
human_readable ((uintmax_t) f->stat.st_ino, hbuf, 1, 1));
p += strlen (p);
}
if (print_block_size)
{
char hbuf[LONGEST_HUMAN_READABLE + 1];
sprintf (p, "%*s ", block_size_size,
human_readable_inexact ((uintmax_t) ST_NBLOCKS (f->stat), hbuf,
ST_NBLOCKSIZE, output_block_size,
human_ceiling));
p += strlen (p);
}
/* The last byte of the mode string is the POSIX
"optional alternate access method flag". */
sprintf (p, "%s %3lu ", modebuf, (unsigned long) f->stat.st_nlink);
p += strlen (p);
if (print_owner)
p += format_user (p, f->stat.st_uid);
if (print_group)
{
char const *group_name = (numeric_ids ? NULL : getgroup (f->stat.st_gid));
if (group_name)
sprintf (p, "%-8s ", group_name);
else
sprintf (p, "%-8lu ", (unsigned long) f->stat.st_gid);
p += strlen (p);
}
if (print_author)
p += format_user (p, f->stat.st_author);
if (S_ISCHR (f->stat.st_mode) || S_ISBLK (f->stat.st_mode))
sprintf (p, "%3lu, %3lu ",
(unsigned long) major (f->stat.st_rdev),
(unsigned long) minor (f->stat.st_rdev));
else
{
char hbuf[LONGEST_HUMAN_READABLE + 1];
uintmax_t size = f->stat.st_size;
/* POSIX requires that the size be printed without a sign, even
when negative. Assume the typical case where negative sizes
are actually positive values that have wrapped around. */
size += (f->stat.st_size < 0) * ((uintmax_t) OFF_T_MAX - OFF_T_MIN + 1);
sprintf (p, "%8s ",
human_readable (size, hbuf, 1,
output_block_size < 0 ? output_block_size : 1));
}
p += strlen (p);
if ((when_local = localtime (&when)))
{
time_t six_months_ago;
int recent;
char const *fmt;
/* If the file appears to be in the future, update the current
time, in case the file happens to have been modified since
the last time we checked the clock. */
if (current_time < when
|| (current_time == when && current_time_ns < when_ns))
{
/* Note that get_current_time calls gettimeofday which, on some non-
compliant systems, clobbers the buffer used for localtime's result.
But it's ok here, because we use a gettimeofday wrapper that
saves and restores the buffer around the gettimeofday call. */
get_current_time ();
}
/* Consider a time to be recent if it is within the past six
months. A Gregorian year has 365.2425 * 24 * 60 * 60 ==
31556952 seconds on the average. Write this value as an
integer constant to avoid floating point hassles. */
six_months_ago = current_time - 31556952 / 2;
recent = (six_months_ago <= when
&& (when < current_time
|| (when == current_time && when_ns <= current_time_ns)));
fmt = long_time_format[recent];
for (;;)
{
char *newbuf;
*p = '\1';
s = nstrftime (p, buf + bufsize - p - 1, fmt,
when_local, 0, when_ns);
if (s || ! *p)
break;
newbuf = alloca (bufsize *= 2);
memcpy (newbuf, buf, p - buf);
p = newbuf + (p - buf);
buf = newbuf;
}
p += s;
*p++ = ' ';
/* NUL-terminate the string -- fputs (via DIRED_FPUTS) requires it. */
*p = '\0';
}
else
{
/* The time cannot be represented as a local time;
print it as a huge integer number of seconds. */
char hbuf[LONGEST_HUMAN_READABLE + 1];
int width = long_time_expected_width ();
if (when < 0)
{
const char *num = human_readable (- (uintmax_t) when, hbuf, 1, 1);
int sign_width = width - strlen (num);
sprintf (p, "%*s%s ", sign_width < 0 ? 0 : sign_width, "-", num);
}
else
sprintf (p, "%*s ", width,
human_readable ((uintmax_t) when, hbuf, 1, 1));
p += strlen (p);
}
DIRED_INDENT ();
DIRED_FPUTS (buf, stdout, p - buf);
print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok,
&dired_obstack);
if (f->filetype == symbolic_link)
{
if (f->linkname)
{
DIRED_FPUTS_LITERAL (" -> ", stdout);
print_name_with_quoting (f->linkname, f->linkmode, f->linkok - 1,
NULL);
if (indicator_style != none)
print_type_indicator (f->linkmode);
}
}
else if (indicator_style != none)
print_type_indicator (f->stat.st_mode);
}
/* Output to OUT a quoted representation of the file name NAME,
using OPTIONS to control quoting. Produce no output if OUT is NULL.
Return the number of screen columns occupied by NAME's quoted
representation. */
static size_t
quote_name (FILE *out, const char *name, struct quoting_options const *options)
{
char smallbuf[BUFSIZ];
size_t len = quotearg_buffer (smallbuf, sizeof smallbuf, name, -1, options);
char *buf;
int displayed_width;
if (len < sizeof smallbuf)
buf = smallbuf;
else
{
buf = (char *) alloca (len + 1);
quotearg_buffer (buf, len + 1, name, -1, options);
}
if (qmark_funny_chars)
{
#if HAVE_MBRTOWC
if (MB_CUR_MAX > 1)
{
char const *p = buf;
char const *plimit = buf + len;
char *q = buf;
displayed_width = 0;
while (p < plimit)
switch (*p)
{
case ' ': case '!': case '"': case '#': case '%':
case '&': case '\'': case '(': case ')': case '*':
case '+': case ',': case '-': case '.': case '/':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case ':': case ';': case '<': case '=': case '>':
case '?':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
case '[': case '\\': case ']': case '^': case '_':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z': case '{': case '|': case '}': case '~':
/* These characters are printable ASCII characters. */
*q++ = *p++;
displayed_width += 1;
break;
default:
/* If we have a multibyte sequence, copy it until we
reach its end, replacing each non-printable multibyte
character with a single question mark. */
{
mbstate_t mbstate;
memset (&mbstate, 0, sizeof mbstate);
do
{
wchar_t wc;
size_t bytes;
int w;
bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
if (bytes == (size_t) -1)
{
/* An invalid multibyte sequence was
encountered. Skip one input byte, and
put a question mark. */
p++;
*q++ = '?';
displayed_width += 1;
break;
}
if (bytes == (size_t) -2)
{
/* An incomplete multibyte character
at the end. Replace it entirely with
a question mark. */
p = plimit;
*q++ = '?';
displayed_width += 1;
break;
}
if (bytes == 0)
/* A null wide character was encountered. */
bytes = 1;
w = wcwidth (wc);
if (w >= 0)
{
/* A printable multibyte character.
Keep it. */
for (; bytes > 0; --bytes)
*q++ = *p++;
displayed_width += w;
}
else
{
/* An unprintable multibyte character.
Replace it entirely with a question
mark. */
p += bytes;
*q++ = '?';
displayed_width += 1;
}
}
while (! mbsinit (&mbstate));
}
break;
}
/* The buffer may have shrunk. */
len = q - buf;
}
else
#endif
{
char *p = buf;
char const *plimit = buf + len;
while (p < plimit)
{
if (! ISPRINT ((unsigned char) *p))
*p = '?';
p++;
}
displayed_width = len;
}
}
else
{
/* Assume unprintable characters have a displayed_width of 1. */
#if HAVE_MBRTOWC
if (MB_CUR_MAX > 1)
displayed_width = mbsnwidth (buf, len, 0);
else
#endif
displayed_width = len;
}
if (out != NULL)
fwrite (buf, 1, len, out);
return displayed_width;
}
static void
print_name_with_quoting (const char *p, mode_t mode, int linkok,
struct obstack *stack)
{
if (print_with_color)
print_color_indicator (p, mode, linkok);
if (stack)
PUSH_CURRENT_DIRED_POS (stack);
dired_pos += quote_name (stdout, p, filename_quoting_options);
if (stack)
PUSH_CURRENT_DIRED_POS (stack);
if (print_with_color)
prep_non_filename_text ();
}
static void
prep_non_filename_text (void)
{
if (color_indicator[C_END].string != NULL)
put_indicator (&color_indicator[C_END]);
else
{
put_indicator (&color_indicator[C_LEFT]);
put_indicator (&color_indicator[C_NORM]);
put_indicator (&color_indicator[C_RIGHT]);
}
}
/* Print the file name of `f' with appropriate quoting.
Also print file size, inode number, and filetype indicator character,
as requested by switches. */
static void
print_file_name_and_frills (const struct fileinfo *f)
{
char buf[LONGEST_HUMAN_READABLE + 1];
if (print_inode)
printf ("%*s ", INODE_DIGITS,
human_readable ((uintmax_t) f->stat.st_ino, buf, 1, 1));
if (print_block_size)
printf ("%*s ", block_size_size,
human_readable_inexact ((uintmax_t) ST_NBLOCKS (f->stat), buf,
ST_NBLOCKSIZE, output_block_size,
human_ceiling));
print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok, NULL);
if (indicator_style != none)
print_type_indicator (f->stat.st_mode);
}
static void
print_type_indicator (mode_t mode)
{
int c;
if (S_ISREG (mode))
{
if (indicator_style == classify && (mode & S_IXUGO))
c ='*';
else
c = 0;
}
else
{
if (S_ISDIR (mode))
c = '/';
else if (S_ISLNK (mode))
c = '@';
else if (S_ISFIFO (mode))
c = '|';
else if (S_ISSOCK (mode))
c = '=';
else if (S_ISDOOR (mode))
c = '>';
else
c = 0;
}
if (c)
DIRED_PUTCHAR (c);
}
static void
print_color_indicator (const char *name, mode_t mode, int linkok)
{
int type = C_FILE;
struct color_ext_type *ext; /* Color extension */
size_t len; /* Length of name */
/* Is this a nonexistent file? If so, linkok == -1. */
if (linkok == -1 && color_indicator[C_MISSING].string != NULL)
{
ext = NULL;
type = C_MISSING;
}
else
{
if (S_ISDIR (mode))
type = C_DIR;
else if (S_ISLNK (mode))
type = ((!linkok && color_indicator[C_ORPHAN].string)
? C_ORPHAN : C_LINK);
else if (S_ISFIFO (mode))
type = C_FIFO;
else if (S_ISSOCK (mode))
type = C_SOCK;
else if (S_ISBLK (mode))
type = C_BLK;
else if (S_ISCHR (mode))
type = C_CHR;
else if (S_ISDOOR (mode))
type = C_DOOR;
if (type == C_FILE && (mode & S_IXUGO) != 0)
type = C_EXEC;
/* Check the file's suffix only if still classified as C_FILE. */
ext = NULL;
if (type == C_FILE)
{
/* Test if NAME has a recognized suffix. */
len = strlen (name);
name += len; /* Pointer to final \0. */
for (ext = color_ext_list; ext != NULL; ext = ext->next)
{
if ((size_t) ext->ext.len <= len
&& strncmp (name - ext->ext.len, ext->ext.string,
ext->ext.len) == 0)
break;
}
}
}
put_indicator (&color_indicator[C_LEFT]);
put_indicator (ext ? &(ext->seq) : &color_indicator[type]);
put_indicator (&color_indicator[C_RIGHT]);
}
/* Output a color indicator (which may contain nulls). */
static void
put_indicator (const struct bin_str *ind)
{
register int i;
register const char *p;
p = ind->string;
for (i = ind->len; i > 0; --i)
putchar (*(p++));
}
static int
length_of_file_name_and_frills (const struct fileinfo *f)
{
register int len = 0;
if (print_inode)
len += INODE_DIGITS + 1;
if (print_block_size)
len += 1 + block_size_size;
len += quote_name (NULL, f->name, filename_quoting_options);
if (indicator_style != none)
{
mode_t filetype = f->stat.st_mode;
if (S_ISREG (filetype))
{
if (indicator_style == classify
&& (f->stat.st_mode & S_IXUGO))
len += 1;
}
else if (S_ISDIR (filetype)
|| S_ISLNK (filetype)
|| S_ISFIFO (filetype)
|| S_ISSOCK (filetype)
|| S_ISDOOR (filetype)
)
len += 1;
}
return len;
}
static void
print_many_per_line (void)
{
struct column_info *line_fmt;
int filesno; /* Index into files. */
int row; /* Current row. */
int max_name_length; /* Length of longest file name + frills. */
int name_length; /* Length of each file name + frills. */
int pos; /* Current character column. */
int cols; /* Number of files across. */
int rows; /* Maximum number of files down. */
int max_cols;
/* Normally the maximum number of columns is determined by the
screen width. But if few files are available this might limit it
as well. */
max_cols = max_idx > files_index ? files_index : max_idx;
/* Compute the maximum number of possible columns. */
for (filesno = 0; filesno < files_index; ++filesno)
{
int i;
name_length = length_of_file_name_and_frills (files + filesno);
for (i = 0; i < max_cols; ++i)
{
if (column_info[i].valid_len)
{
int idx = filesno / ((files_index + i) / (i + 1));
int real_length = name_length + (idx == i ? 0 : 2);
if (real_length > column_info[i].col_arr[idx])
{
column_info[i].line_len += (real_length
- column_info[i].col_arr[idx]);
column_info[i].col_arr[idx] = real_length;
column_info[i].valid_len = column_info[i].line_len < line_length;
}
}
}
}
/* Find maximum allowed columns. */
for (cols = max_cols; cols > 1; --cols)
{
if (column_info[cols - 1].valid_len)
break;
}
line_fmt = &column_info[cols - 1];
/* Calculate the number of rows that will be in each column except possibly
for a short column on the right. */
rows = files_index / cols + (files_index % cols != 0);
for (row = 0; row < rows; row++)
{
int col = 0;
filesno = row;
pos = 0;
/* Print the next row. */
while (1)
{
print_file_name_and_frills (files + filesno);
name_length = length_of_file_name_and_frills (files + filesno);
max_name_length = line_fmt->col_arr[col++];
filesno += rows;
if (filesno >= files_index)
break;
indent (pos + name_length, pos + max_name_length);
pos += max_name_length;
}
putchar ('\n');
}
}
static void
print_horizontal (void)
{
struct column_info *line_fmt;
int filesno;
int max_name_length;
int name_length;
int cols;
int pos;
int max_cols;
/* Normally the maximum number of columns is determined by the
screen width. But if few files are available this might limit it
as well. */
max_cols = max_idx > files_index ? files_index : max_idx;
/* Compute the maximum file name length. */
max_name_length = 0;
for (filesno = 0; filesno < files_index; ++filesno)
{
int i;
name_length = length_of_file_name_and_frills (files + filesno);
for (i = 0; i < max_cols; ++i)
{
if (column_info[i].valid_len)
{
int idx = filesno % (i + 1);
int real_length = name_length + (idx == i ? 0 : 2);
if (real_length > column_info[i].col_arr[idx])
{
column_info[i].line_len += (real_length
- column_info[i].col_arr[idx]);
column_info[i].col_arr[idx] = real_length;
column_info[i].valid_len = column_info[i].line_len < line_length;
}
}
}
}
/* Find maximum allowed columns. */
for (cols = max_cols; cols > 1; --cols)
{
if (column_info[cols - 1].valid_len)
break;
}
line_fmt = &column_info[cols - 1];
pos = 0;
/* Print first entry. */
print_file_name_and_frills (files);
name_length = length_of_file_name_and_frills (files);
max_name_length = line_fmt->col_arr[0];
/* Now the rest. */
for (filesno = 1; filesno < files_index; ++filesno)
{
int col = filesno % cols;
if (col == 0)
{
putchar ('\n');
pos = 0;
}
else
{
indent (pos + name_length, pos + max_name_length);
pos += max_name_length;
}
print_file_name_and_frills (files + filesno);
name_length = length_of_file_name_and_frills (files + filesno);
max_name_length = line_fmt->col_arr[col];
}
putchar ('\n');
}
static void
print_with_commas (void)
{
int filesno;
int pos, old_pos;
pos = 0;
for (filesno = 0; filesno < files_index; filesno++)
{
old_pos = pos;
pos += length_of_file_name_and_frills (files + filesno);
if (filesno + 1 < files_index)
pos += 2; /* For the comma and space */
if (old_pos != 0 && pos >= line_length)
{
putchar ('\n');
pos -= old_pos;
}
print_file_name_and_frills (files + filesno);
if (filesno + 1 < files_index)
{
putchar (',');
putchar (' ');
}
}
putchar ('\n');
}
/* Assuming cursor is at position FROM, indent up to position TO.
Use a TAB character instead of two or more spaces whenever possible. */
static void
indent (int from, int to)
{
while (from < to)
{
if (tabsize > 0 && to / tabsize > (from + 1) / tabsize)
{
putchar ('\t');
from += tabsize - from % tabsize;
}
else
{
putchar (' ');
from++;
}
}
}
/* Put DIRNAME/NAME into DEST, handling `.' and `/' properly. */
/* FIXME: maybe remove this function someday. See about using a
non-malloc'ing version of path_concat. */
static void
attach (char *dest, const char *dirname, const char *name)
{
const char *dirnamep = dirname;
/* Copy dirname if it is not ".". */
if (dirname[0] != '.' || dirname[1] != 0)
{
while (*dirnamep)
*dest++ = *dirnamep++;
/* Add '/' if `dirname' doesn't already end with it. */
if (dirnamep > dirname && dirnamep[-1] != '/')
*dest++ = '/';
}
while (*name)
*dest++ = *name++;
*dest = 0;
}
static void
init_column_info (void)
{
int i;
int allocate = 0;
max_idx = line_length / MIN_COLUMN_WIDTH;
if (max_idx == 0)
max_idx = 1;
if (column_info == NULL)
{
column_info = XMALLOC (struct column_info, max_idx);
allocate = 1;
}
for (i = 0; i < max_idx; ++i)
{
int j;
column_info[i].valid_len = 1;
column_info[i].line_len = (i + 1) * MIN_COLUMN_WIDTH;
if (allocate)
column_info[i].col_arr = XMALLOC (int, i + 1);
for (j = 0; j <= i; ++j)
column_info[i].col_arr[j] = MIN_COLUMN_WIDTH;
}
}
void
usage (int status)
{
if (status != 0)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
fputs (_("\
List information about the FILEs (the current directory by default).\n\
Sort entries alphabetically if none of -cftuSUX nor --sort.\n\
\n\
"), stdout);
fputs (_("\
Mandatory arguments to long options are mandatory for short options too.\n\
"), stdout);
fputs (_("\
-a, --all do not hide entries starting with .\n\
-A, --almost-all do not list implied . and ..\n\
--author print the author of each file\n\
-b, --escape print octal escapes for nongraphic characters\n\
"), stdout);
fputs (_("\
--block-size=SIZE use SIZE-byte blocks\n\
-B, --ignore-backups do not list implied entries ending with ~\n\
-c with -lt: sort by, and show, ctime (time of last\n\
modification of file status information)\n\
with -l: show ctime and sort by name\n\
otherwise: sort by ctime\n\
"), stdout);
fputs (_("\
-C list entries by columns\n\
--color[=WHEN] control whether color is used to distinguish file\n\
types. WHEN may be `never', `always', or `auto'\n\
-d, --directory list directory entries instead of contents\n\
-D, --dired generate output designed for Emacs' dired mode\n\
"), stdout);
fputs (_("\
-f do not sort, enable -aU, disable -lst\n\
-F, --classify append indicator (one of */=@|) to entries\n\
--format=WORD across -x, commas -m, horizontal -x, long -l,\n\
single-column -1, verbose -l, vertical -C\n\
--full-time like -l --time-style=full-iso\n\
"), stdout);
fputs (_("\
-g like -l, but do not list owner\n\
-G, --no-group inhibit display of group information\n\
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\n\
--si likewise, but use powers of 1000 not 1024\n\
-H, --dereference-command-line follow symbolic links on the command line\n\
"), stdout);
fputs (_("\
--indicator-style=WORD append indicator with style WORD to entry names:\n\
none (default), classify (-F), file-type (-p)\n\
-i, --inode print index number of each file\n\
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN\n\
-k like --block-size=1K\n\
"), stdout);
fputs (_("\
-l use a long listing format\n\
-L, --dereference when showing file information for a symbolic\n\
link, show information for the file the link\n\
references rather than for the link itself\n\
-m fill width with a comma separated list of entries\n\
"), stdout);
fputs (_("\
-n, --numeric-uid-gid like -l, but list numeric UIDs and GIDs\n\
-N, --literal print raw entry names (don't treat e.g. control\n\
characters specially)\n\
-o like -l, but do not list group information\n\
-p, --file-type append indicator (one of /=@|) to entries\n\
"), stdout);
fputs (_("\
-q, --hide-control-chars print ? instead of non graphic characters\n\
--show-control-chars show non graphic characters as-is (default\n\
unless program is `ls' and output is a terminal)\n\
-Q, --quote-name enclose entry names in double quotes\n\
--quoting-style=WORD use quoting style WORD for entry names:\n\
literal, locale, shell, shell-always, c, escape\n\
"), stdout);
fputs (_("\
-r, --reverse reverse order while sorting\n\
-R, --recursive list subdirectories recursively\n\
-s, --size print size of each file, in blocks\n\
"), stdout);
fputs (_("\
-S sort by file size\n\
--sort=WORD extension -X, none -U, size -S, time -t,\n\
version -v\n\
status -c, time -t, atime -u, access -u, use -u\n\
--time=WORD show time as WORD instead of modification time:\n\
atime, access, use, ctime or status; use\n\
specified time as sort key if --sort=time\n\
"), stdout);
fputs (_("\
--time-style=WORD show times using style WORD:\n\
full-iso, iso, locale, posix-iso, +FORMAT\n\
FORMAT is interpreted like `date'; if FORMAT is\n\
FORMAT1<newline>FORMAT2, FORMAT1 applies to\n\
non-recent files and FORMAT2 to recent files\n\
-t sort by modification time\n\
-T, --tabsize=COLS assume tab stops at each COLS instead of 8\n\
"), stdout);
fputs (_("\
-u with -lt: sort by, and show, access time\n\
with -l: show access time and sort by name\n\
otherwise: sort by access time\n\
-U do not sort; list entries in directory order\n\
-v sort by version\n\
"), stdout);
fputs (_("\
-w, --width=COLS assume screen width instead of current value\n\
-x list entries by lines instead of by columns\n\
-X sort alphabetically by entry extension\n\
-1 list one file per line\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\n\
SIZE may be (or may be an integer optionally followed by) one of following:\n\
kB 1000, K 1024, MB 1,000,000, M 1,048,576, and so on for G, T, P, E, Z, Y.\n\
"), stdout);
fputs (_("\
\n\
By default, color is not used to distinguish types of files. That is\n\
equivalent to using --color=none. Using the --color option without the\n\
optional WHEN argument is equivalent to using --color=always. With\n\
--color=auto, color codes are output only if standard output is connected\n\
to a terminal (tty).\n\
"), stdout);
puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
}
exit (status);
}
|