|
root / base / usr / src / cmd / fs.d / df.c
df.c C 2113 lines 53.3 KB
   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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/


/*
 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Copyright 2016 Jason King
 * Copyright 2019 Nexenta Systems, Inc.  All rights reserved.
 */

#include <dlfcn.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <stdlib.h>
#include <ftw.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/mnttab.h>
#include <sys/mntent.h>
#include <sys/vfstab.h>
#include <sys/wait.h>
#include <sys/mkdev.h>
#include <sys/int_limits.h>
#include <sys/zone.h>
#include <sys/debug.h>
#include <libzfs.h>
#include <libcmdutils.h>

#include "fslib.h"

extern char *default_fstype(char *);

/*
 * General notice:
 * String pointers in this code may point to statically allocated memory
 * or dynamically allocated memory. Furthermore, a dynamically allocated
 * string may be pointed to by more than one pointer. This does not pose
 * a problem because malloc'ed memory is never free'd (so we don't need
 * to remember which pointers point to malloc'ed memory).
 */

/*
 * TRANSLATION_NOTE
 * Only strings passed as arguments to the TRANSLATE macro need to
 * be translated.
 */

#ifndef MNTTYPE_LOFS
#define	MNTTYPE_LOFS		"lofs"
#endif

#define	EQ(s1, s2)		(strcmp(s1, s2) == 0)
#define	NEW(type)		xmalloc(sizeof (type))
#define	CLEAR(var)		(void) memset(&(var), 0, sizeof (var))
#define	MAX(a, b)		((a) > (b) ? (a) : (b))
#define	MAX3(a, b, c)		MAX(a, MAX(b, c))
#define	TRANSLATE(s)		new_string(gettext(s))

#define	MAX_OPTIONS		36
#define	N_FSTYPES		20
#define	MOUNT_TABLE_ENTRIES	40	/* initial allocation */
#define	MSGBUF_SIZE		1024
#define	LINEBUF_SIZE		256	/* either input or output lines */

#define	BLOCK_SIZE		512	/* when reporting in terms of blocks */

#define	DEVNM_CMD		"devnm"
#define	FS_LIBPATH		"/usr/lib/fs/"
#define	MOUNT_TAB		"/etc/mnttab"
#define	VFS_TAB			"/etc/vfstab"
#define	REMOTE_FS		"/etc/dfs/fstypes"

#define	NUL			'\0'
#define	FALSE			0
#define	TRUE			1

/*
 * Formatting constants
 */
#define	IBCS2_FILESYSTEM_WIDTH	15	/* Truncate to match ISC/SCO */
#define	IBCS2_MOUNT_POINT_WIDTH	10	/* Truncate to match ISC/SCO */
#define	FILESYSTEM_WIDTH	20
#define	MOUNT_POINT_WIDTH	19
#define	SPECIAL_DEVICE_WIDTH	18
#define	FSTYPE_WIDTH		8
#define	BLOCK_WIDTH		8
#define	NFILES_WIDTH		8
#define	KBYTE_WIDTH		11
#define	AVAILABLE_WIDTH		10
#define	SCALED_WIDTH		6
#define	CAPACITY_WIDTH		9
#define	BSIZE_WIDTH		6
#define	FRAGSIZE_WIDTH		7
#define	FSID_WIDTH		7
#define	FLAG_WIDTH		8
#define	NAMELEN_WIDTH		7
#define	MNT_SPEC_WIDTH		MOUNT_POINT_WIDTH + SPECIAL_DEVICE_WIDTH + 2

/*
 * Flags for the errmsg() function
 */
#define	ERR_NOFLAGS		0x0
#define	ERR_NONAME		0x1	/* don't include the program name */
					/* as a prefix */
#define	ERR_FATAL		0x2	/* call exit after printing the */
					/* message */
#define	ERR_PERROR		0x4	/* append an errno explanation to */
					/* the message */
#define	ERR_USAGE		0x8	/* print the usage line after the */
					/* message */

#define	NUMBER_WIDTH		40
CTASSERT(NUMBER_WIDTH >= NN_NUMBUF_SZ);

/*
 * A numbuf_t is used when converting a number to a string representation
 */
typedef char numbuf_t[ NUMBER_WIDTH ];

/*
 * We use bool_int instead of int to make clear which variables are
 * supposed to be boolean
 */
typedef int bool_int;

struct mtab_entry {
	bool_int	mte_dev_is_valid;
	dev_t		mte_dev;
	bool_int	mte_ignore;	/* the "ignore" option was set */
	struct extmnttab	*mte_mount;
};


struct df_request {
	bool_int		dfr_valid;
	char			*dfr_cmd_arg;	/* what the user specified */
	struct mtab_entry	*dfr_mte;
	char			*dfr_fstype;
	int			dfr_index;	/* to make qsort stable	*/
};

#define	DFR_MOUNT_POINT(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_mountp
#define	DFR_SPECIAL(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_special
#define	DFR_FSTYPE(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_fstype
#define	DFR_ISMOUNTEDFS(dfrp)	((dfrp)->dfr_mte != NULL)

#define	DFRP(p)			((struct df_request *)(p))

typedef void (*output_func)(struct df_request *, struct statvfs64 *);

struct df_output {
	output_func	dfo_func;	/* function that will do the output */
	int		dfo_flags;
};

/*
 * Output flags
 */
#define	DFO_NOFLAGS	0x0
#define	DFO_HEADER	0x1		/* output preceded by header */
#define	DFO_STATVFS	0x2		/* must do a statvfs64(2) */


static char	*program_name;
static char	df_options[MAX_OPTIONS] = "-";
static size_t	df_options_len = 1;
static char	*o_option_arg;			/* arg to the -o option */
static char	*FSType;
static char	*remote_fstypes[N_FSTYPES+1];	/* allocate an extra one */
						/* to use as a terminator */

/*
 * The following three variables support an in-memory copy of the mount table
 * to speedup searches.
 */
static struct mtab_entry	*mount_table;	/* array of mtab_entry's */
static size_t			mount_table_entries;
static size_t			mount_table_allocated_entries;

static bool_int		F_option;
static bool_int		V_option;
static bool_int		P_option;	/* Added for XCU4 compliance */
static bool_int		Z_option;
static bool_int		v_option;
static bool_int		a_option;
static bool_int		b_option;
static bool_int		e_option;
static bool_int		g_option;
static bool_int		h_option;
static bool_int		k_option;
static bool_int		l_option;
static bool_int		m_option;
static bool_int		n_option;
static bool_int		t_option;
static bool_int		o_option;

static bool_int		tty_output;
static bool_int		use_scaling;

static void usage(void);
static void do_devnm(int, char **);
static void do_df(int, char **)	__NORETURN;
static void parse_options(int, char **);
static char *basename(char *);

static libzfs_handle_t *(*_libzfs_init)(void);
static zfs_handle_t *(*_zfs_open)(libzfs_handle_t *, const char *, int);
static void (*_zfs_close)(zfs_handle_t *);
static uint64_t (*_zfs_prop_get_int)(zfs_handle_t *, zfs_prop_t);
static libzfs_handle_t *g_zfs;

/*
 * Dynamically check for libzfs, in case the user hasn't installed the SUNWzfs
 * packages.  A basic utility such as df shouldn't depend on optional
 * filesystems.
 */
static boolean_t
load_libzfs(void)
{
	void *hdl;

	if (_libzfs_init != NULL)
		return (g_zfs != NULL);

	if ((hdl = dlopen("libzfs.so", RTLD_LAZY)) != NULL) {
		_libzfs_init = (libzfs_handle_t *(*)(void))dlsym(hdl,
		    "libzfs_init");
		_zfs_open = (zfs_handle_t *(*)())dlsym(hdl, "zfs_open");
		_zfs_close = (void (*)())dlsym(hdl, "zfs_close");
		_zfs_prop_get_int = (uint64_t (*)())
		    dlsym(hdl, "zfs_prop_get_int");

		if (_libzfs_init != NULL) {
			assert(_zfs_open != NULL);
			assert(_zfs_close != NULL);
			assert(_zfs_prop_get_int != NULL);

			g_zfs = _libzfs_init();
		}
	}

	return (g_zfs != NULL);
}

int
main(int argc, char *argv[])
{
	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	program_name = basename(argv[0]);

	if (EQ(program_name, DEVNM_CMD))
		do_devnm(argc, argv);

	parse_options(argc, argv);

	/*
	 * The k_option implies SunOS 4.x compatibility: when the special
	 * device name is too long the line will be split except when the
	 * output has been redirected.
	 * This is also valid for the -h option.
	 */

	if (use_scaling || k_option || P_option || v_option)
		tty_output = isatty(1);

	do_df(argc - optind, &argv[optind]);
	/* NOTREACHED */
}


/*
 * Prints an error message to stderr.
 */
/* VARARGS2 */
static void
errmsg(int flags, char *fmt, ...)
{
	char buf[MSGBUF_SIZE];
	va_list ap;
	int cc;
	int offset;

	if (flags & ERR_NONAME)
		offset = 0;
	else
		offset = sprintf(buf, "%s: ", program_name);

	va_start(ap, fmt);
	cc = vsprintf(&buf[offset], gettext(fmt), ap);
	offset += cc;
	va_end(ap);

	if (flags & ERR_PERROR) {
		if (buf[offset-1] != ' ')
			(void) strcat(buf, " ");
		(void) strcat(buf, strerror(errno));
	}
	(void) fprintf(stderr, "%s\n", buf);
	if (flags & ERR_USAGE)
		usage();
	if (flags & ERR_FATAL)
		exit(1);
}


static void
usage(void)
{
	errmsg(ERR_NONAME,
	    "Usage: %s [-F FSType] [-abeghklmntPVvZ]"
	    " [-o FSType-specific_options]"
	    " [directory | block_device | resource]", program_name);
	exit(1);
	/* NOTREACHED */
}


static char *
new_string(char *s)
{
	char *p = NULL;

	if (s) {
		p = strdup(s);
		if (p)
			return (p);
		errmsg(ERR_FATAL, "out of memory");
		/* NOTREACHED */
	}
	return (p);
}


/*
 * Allocate memory using malloc but terminate if the allocation fails
 */
static void *
xmalloc(size_t size)
{
	void *p = malloc(size);

	if (p)
		return (p);
	errmsg(ERR_FATAL, "out of memory");
	/* NOTREACHED */
	return (NULL);
}


/*
 * Allocate memory using realloc but terminate if the allocation fails
 */
static void *
xrealloc(void *ptr, size_t size)
{
	void *p = realloc(ptr, size);

	if (p)
		return (p);
	errmsg(ERR_FATAL, "out of memory");
	/* NOTREACHED */
	return (NULL);
}


/*
 * fopen the specified file for reading but terminate if the fopen fails
 */
static FILE *
xfopen(char *file)
{
	FILE *fp = fopen(file, "r");

	if (fp == NULL)
		errmsg(ERR_FATAL + ERR_PERROR, "failed to open %s:", file);
	return (fp);
}


/*
 * Read remote file system types from REMOTE_FS into the
 * remote_fstypes array.
 */
static void
init_remote_fs(void)
{
	FILE	*fp;
	char	line_buf[LINEBUF_SIZE];
	size_t	fstype_index = 0;

	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
		errmsg(ERR_NOFLAGS,
		    "Warning: can't open %s, ignored", REMOTE_FS);
		return;
	}

	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
		char buf[LINEBUF_SIZE];

		(void) sscanf(line_buf, "%s", buf);
		remote_fstypes[fstype_index++] = new_string(buf);

		if (fstype_index == N_FSTYPES)
			break;
	}
	(void) fclose(fp);
}


/*
 * Returns TRUE if fstype is a remote file system type;
 * otherwise, returns FALSE.
 */
static int
is_remote_fs(char *fstype)
{
	char **p;
	static bool_int remote_fs_initialized;

	if (! remote_fs_initialized) {
		init_remote_fs();
		remote_fs_initialized = TRUE;
	}

	for (p = remote_fstypes; *p; p++)
		if (EQ(fstype, *p))
			return (TRUE);
	return (FALSE);
}


static char *
basename(char *s)
{
	char *p = strrchr(s, '/');

	return (p ? p+1 : s);
}


/*
 * Create a new "struct extmnttab" and make sure that its fields point
 * to malloc'ed memory
 */
static struct extmnttab *
mntdup(struct extmnttab *old)
{
	struct extmnttab *new = NEW(struct extmnttab);

	new->mnt_special = new_string(old->mnt_special);
	new->mnt_mountp  = new_string(old->mnt_mountp);
	new->mnt_fstype  = new_string(old->mnt_fstype);
	new->mnt_mntopts = new_string(old->mnt_mntopts);
	new->mnt_time    = new_string(old->mnt_time);
	new->mnt_major   = old->mnt_major;
	new->mnt_minor   = old->mnt_minor;
	return (new);
}


static void
mtab_error(char *mtab_file, int status)
{
	if (status == MNT_TOOLONG)
		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
		    mtab_file, MNT_LINE_MAX);
	else if (status == MNT_TOOMANY)
		errmsg(ERR_NOFLAGS,
		    "a line in %s has too many fields", mtab_file);
	else if (status == MNT_TOOFEW)
		errmsg(ERR_NOFLAGS,
		    "a line in %s has too few fields", mtab_file);
	else
		errmsg(ERR_NOFLAGS,
		    "error while reading %s: %d", mtab_file, status);
	exit(1);
	/* NOTREACHED */
}


/*
 * Read the mount table from the specified file.
 * We keep the table in memory for faster lookups.
 */
static void
mtab_read_file(void)
{
	char		*mtab_file = MOUNT_TAB;
	FILE		*fp;
	struct extmnttab	mtab;
	int		status;

	fp = xfopen(mtab_file);

	resetmnttab(fp);
	mount_table_allocated_entries = MOUNT_TABLE_ENTRIES;
	mount_table_entries = 0;
	mount_table = xmalloc(
	    mount_table_allocated_entries * sizeof (struct mtab_entry));

	while ((status = getextmntent(fp, &mtab, sizeof (struct extmnttab)))
	    == 0) {
		struct mtab_entry *mtep;

		if (mount_table_entries == mount_table_allocated_entries) {
			mount_table_allocated_entries += MOUNT_TABLE_ENTRIES;
			mount_table = xrealloc(mount_table,
			    mount_table_allocated_entries *
			    sizeof (struct mtab_entry));
		}
		mtep = &mount_table[mount_table_entries++];
		mtep->mte_mount = mntdup(&mtab);
		mtep->mte_dev_is_valid = FALSE;
		mtep->mte_ignore = (hasmntopt((struct mnttab *)&mtab,
		    MNTOPT_IGNORE) != NULL);
	}

	(void) fclose(fp);

	if (status == -1)			/* reached EOF */
		return;
	mtab_error(mtab_file, status);
	/* NOTREACHED */
}


/*
 * We use this macro when we want to record the option for the purpose of
 * passing it to the FS-specific df
 */
#define	SET_OPTION(opt)		opt##_option = TRUE, \
				df_options[df_options_len++] = arg

static void
parse_options(int argc, char *argv[])
{
	int arg;

	opterr = 0;	/* getopt shouldn't complain about unknown options */

	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlmPvZ")) != EOF) {
		if (arg == 'F') {
			if (F_option)
				errmsg(ERR_FATAL + ERR_USAGE,
				    "more than one FSType specified");
			F_option = 1;
			FSType = optarg;
		} else if (arg == 'V' && ! V_option) {
			V_option = TRUE;
		} else if (arg == 'v' && ! v_option) {
			v_option = TRUE;
		} else if (arg == 'P' && ! P_option) {
			SET_OPTION(P);
		} else if (arg == 'a' && ! a_option) {
			SET_OPTION(a);
		} else if (arg == 'b' && ! b_option) {
			SET_OPTION(b);
		} else if (arg == 'e' && ! e_option) {
			SET_OPTION(e);
		} else if (arg == 'g' && ! g_option) {
			SET_OPTION(g);
		} else if (arg == 'h') {
			use_scaling = TRUE;
		} else if (arg == 'k' && ! k_option) {
			SET_OPTION(k);
		} else if (arg == 'l' && ! l_option) {
			SET_OPTION(l);
		} else if (arg == 'm' && ! m_option) {
			SET_OPTION(m);
		} else if (arg == 'n' && ! n_option) {
			SET_OPTION(n);
		} else if (arg == 't' && ! t_option) {
			SET_OPTION(t);
		} else if (arg == 'o') {
			if (o_option)
				errmsg(ERR_FATAL + ERR_USAGE,
				"the -o option can only be specified once");
			o_option = TRUE;
			o_option_arg = optarg;
		} else if (arg == 'Z') {
			SET_OPTION(Z);
		} else if (arg == '?') {
			errmsg(ERR_USAGE, "unknown option: %c", optopt);
		}
	}

	/*
	 * Option sanity checks
	 */
	if (g_option && o_option)
		errmsg(ERR_FATAL, "-o and -g options are incompatible");
	if (l_option && o_option)
		errmsg(ERR_FATAL, "-o and -l options are incompatible");
	if (n_option && o_option)
		errmsg(ERR_FATAL, "-o and -n options are incompatible");
	if (use_scaling && o_option)
		errmsg(ERR_FATAL, "-o and -h options are incompatible");
}



/*
 * Check if the user-specified argument is a resource name.
 * A resource name is whatever is placed in the mnt_special field of
 * struct mnttab. In the case of NFS, a resource name has the form
 * hostname:pathname
 * We try to find an exact match between the user-specified argument
 * and the mnt_special field of a mount table entry.
 * We also use the heuristic of removing the basename from the user-specified
 * argument and repeating the test until we get a match. This works
 * fine for NFS but may fail for other remote file system types. However,
 * it is guaranteed that the function will not fail if the user specifies
 * the exact resource name.
 * If successful, this function sets the 'dfr_mte' field of '*dfrp'
 */
static void
resource_mount_entry(struct df_request *dfrp)
{
	char *name;

	/*
	 * We need our own copy since we will modify the string
	 */
	name = new_string(dfrp->dfr_cmd_arg);

	for (;;) {
		char *p;
		int i;

		/*
		 * Compare against all known mount points.
		 * We start from the most recent mount, which is at the
		 * end of the array.
		 */
		for (i = mount_table_entries - 1; i >= 0; i--) {
			struct mtab_entry *mtep = &mount_table[i];

			if (EQ(name, mtep->mte_mount->mnt_special)) {
				dfrp->dfr_mte = mtep;
				break;
			}
		}

		/*
		 * Remove the last component of the pathname.
		 * If there is no such component, this is not a resource name.
		 */
		p = strrchr(name, '/');
		if (p == NULL)
			break;
		*p = NUL;
	}
}



/*
 * Try to match the command line argument which is a block special device
 * with the special device of one of the mounted file systems.
 * If one is found, set the appropriate field of 'dfrp' to the mount
 * table entry.
 */
static void
bdev_mount_entry(struct df_request *dfrp)
{
	int i;
	char *special = dfrp->dfr_cmd_arg;

	/*
	 * Compare against all known mount points.
	 * We start from the most recent mount, which is at the
	 * end of the array.
	 */
	for (i = mount_table_entries - 1; i >= 0; i--) {
		struct mtab_entry *mtep = &mount_table[i];

		if (EQ(special, mtep->mte_mount->mnt_special)) {
			dfrp->dfr_mte = mtep;
			break;
		}
	}
}

static struct mtab_entry *
devid_matches(int i, dev_t devno)
{
	struct mtab_entry	*mtep = &mount_table[i];
	struct extmnttab	*mtp = mtep->mte_mount;
	/* int	len = strlen(mtp->mnt_mountp); */

	if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
		return (NULL);
	/*
	 * check if device numbers match. If there is a cached device number
	 * in the mtab_entry, use it, otherwise get the device number
	 * either from the mnttab entry or by stat'ing the mount point.
	 */
	if (! mtep->mte_dev_is_valid) {
		struct stat64 st;
		dev_t dev = NODEV;

		dev = makedev(mtp->mnt_major, mtp->mnt_minor);
		if (dev == 0)
			dev = NODEV;
		if (dev == NODEV) {
			if (stat64(mtp->mnt_mountp, &st) == -1) {
				return (NULL);
			} else {
				dev = st.st_dev;
			}
		}
		mtep->mte_dev = dev;
		mtep->mte_dev_is_valid = TRUE;
	}
	if (mtep->mte_dev == devno) {
		return (mtep);
	}
	return (NULL);
}

/*
 * Find the mount point under which the user-specified path resides
 * and set the 'dfr_mte' field of '*dfrp' to point to the mount table entry.
 */
static void
path_mount_entry(struct df_request *dfrp, dev_t devno)
{
	char			dirpath[MAXPATHLEN];
	char			*dir = dfrp->dfr_cmd_arg;
	struct mtab_entry	*match, *tmatch;
	int i;

	/*
	 * Expand the given path to get a canonical version (i.e. an absolute
	 * path without symbolic links).
	 */
	if (realpath(dir, dirpath) == NULL) {
		errmsg(ERR_PERROR, "cannot canonicalize %s:", dir);
		return;
	}
	/*
	 * If the mnt point is lofs, search from the top of entries from
	 * /etc/mnttab and return the entry that best matches the pathname.
	 * For non-lofs mount points, return the first entry from the bottom
	 * of the entries in /etc/mnttab that matches on the devid field
	 */
	match = NULL;
	if (dfrp->dfr_fstype && EQ(dfrp->dfr_fstype, MNTTYPE_LOFS)) {
		struct extmnttab *entryp;
		char *path, *mountp;
		char p, m;
		int score;
		int best_score = 0;
		int best_index = -1;

		for (i = 0; i < mount_table_entries; i++) {
			entryp = mount_table[i].mte_mount;

			if (!EQ(entryp->mnt_fstype, MNTTYPE_LOFS))
				continue;

			path = dirpath;
			mountp = entryp->mnt_mountp;
			score = 0;
			/*
			 * Count the number of matching characters
			 * until either path or mountpoint is exhausted
			 */
			while ((p = *path++) == (m = *mountp++)) {
				score++;

				if (p == '\0' || m == '\0')
					break;
			}

			/* Both exhausted so we have a match */
			if (p == '\0' && m == '\0') {
				best_index = i;
				break;
			}

			/*
			 * We have exhausted the mountpoint and the current
			 * character in the path is a '/' hence the full path
			 * traverses this mountpoint.
			 * Record this as the best candidate so far.
			 */
			if (p == '/' && m == '\0') {
				if (score > best_score) {
					best_index = i;
					best_score = score;
				}
			}
		}

		if (best_index > -1)
			match = &mount_table[best_index];
	} else {
		for (i = mount_table_entries - 1; i >= 0; i--) {
			if (tmatch = devid_matches(i, devno)) {
				/*
				 * If executing in a zone, there might be lofs
				 * mounts for which the real mount point is
				 * invisible; accept the "best fit" for this
				 * devid.
				 */
				match = tmatch;
				if (!EQ(match->mte_mount->mnt_fstype,
				    MNTTYPE_LOFS)) {
					break;
				}
			}
		}
	}
	if (! match) {
		errmsg(ERR_NOFLAGS,
		    "Could not find mount point for %s", dir);
		return;
	}
	dfrp->dfr_mte = match;
}

/*
 * Execute a single FS-specific df command for all given requests
 * Return 0 if successful, 1 otherwise.
 */
static int
run_fs_specific_df(struct df_request request_list[], int entries)
{
	int	i;
	int	argv_index;
	char	**argv;
	size_t	size;
	pid_t	pid;
	int	status;
	char	cmd_path[MAXPATHLEN];
	char	*fstype;

	if (entries == 0)
		return (0);

	fstype = request_list[0].dfr_fstype;

	if (F_option && ! EQ(FSType, fstype))
		return (0);

	(void) sprintf(cmd_path, "%s%s/df", FS_LIBPATH, fstype);
	/*
	 * Argv entries:
	 *		1 for the path
	 *		2 for -o <options>
	 *		1 for the generic options that we propagate
	 *		1 for the terminating NULL pointer
	 *		n for the number of user-specified arguments
	 */
	size = (5 + entries) * sizeof (char *);
	argv = xmalloc(size);
	(void) memset(argv, 0, size);

	argv[0] = cmd_path;
	argv_index = 1;
	if (o_option) {
		argv[argv_index++] = "-o";
		argv[argv_index++] = o_option_arg;
	}

	/*
	 * Check if we need to propagate any generic options
	 */
	if (df_options_len > 1)
		argv[argv_index++] = df_options;

	/*
	 * If there is a user-specified path, we pass that to the
	 * FS-specific df. Otherwise, we are guaranteed to have a mount
	 * point, since a request without a user path implies that
	 * we are reporting only on mounted file systems.
	 */
	for (i = 0; i < entries; i++) {
		struct df_request *dfrp = &request_list[i];

		argv[argv_index++] = (dfrp->dfr_cmd_arg == NULL)
		    ? DFR_MOUNT_POINT(dfrp)
		    : dfrp->dfr_cmd_arg;
	}

	if (V_option) {
		for (i = 0; i < argv_index-1; i++)
			(void) printf("%s ", argv[i]);
		(void) printf("%s\n", argv[i]);
		return (0);
	}

	pid = fork();

	if (pid == -1) {
		errmsg(ERR_PERROR, "cannot fork process:");
		return (1);
	} else if (pid == 0) {
		(void) execv(cmd_path, argv);
		if (errno == ENOENT)
			errmsg(ERR_NOFLAGS,
			    "operation not applicable for FSType %s",
			    fstype);
		else
			errmsg(ERR_PERROR, "cannot execute %s:", cmd_path);
		exit(2);
	}

	/*
	 * Reap the child
	 */
	for (;;) {
		pid_t wpid = waitpid(pid, &status, 0);

		if (wpid == -1)
			if (errno == EINTR)
				continue;
			else {
				errmsg(ERR_PERROR, "waitpid error:");
				return (1);
			}
		else
			break;
	}

	return ((WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : 1);
}



/*
 * Remove from the request list all requests that do not apply.
 * Notice that the subsequent processing of the requests depends on
 * the sanity checking performed by this function.
 */
static int
prune_list(struct df_request request_list[],
    size_t n_requests, size_t *valid_requests)
{
	size_t	i;
	size_t	n_valid = 0;
	int	errors = 0;

	for (i = 0; i < n_requests; i++) {
		struct df_request *dfrp = &request_list[i];

		/*
		 * Skip file systems that are not mounted if either the
		 * -l or -n options were specified. If none of these options
		 * are present, the appropriate FS-specific df will be invoked.
		 */
		if (! DFR_ISMOUNTEDFS(dfrp)) {
			if (l_option || n_option) {
				errmsg(ERR_NOFLAGS,
				    "%s option incompatible with unmounted "
				    "special device (%s)",
				    l_option ? "-l" : "-n", dfrp->dfr_cmd_arg);
				dfrp->dfr_valid = FALSE;
				errors++;
			}
			else
				n_valid++;
			continue;
		}

		/*
		 * Check for inconsistency between the argument of -F and
		 * the actual file system type.
		 * If there is an inconsistency and the user specified a
		 * path, this is an error since we are asked to interpret
		 * the path using the wrong file system type. If there is
		 * no path associated with this request, we quietly ignore it.
		 */
		if (F_option && ! EQ(dfrp->dfr_fstype, FSType)) {
			dfrp->dfr_valid = FALSE;
			if (dfrp->dfr_cmd_arg != NULL) {
				errmsg(ERR_NOFLAGS,
				"Warning: %s mounted as a %s file system",
				    dfrp->dfr_cmd_arg, dfrp->dfr_fstype);
				errors++;
			}
			continue;
		}

		/*
		 * Skip remote file systems if the -l option is present
		 */
		if (l_option && is_remote_fs(dfrp->dfr_fstype)) {
			if (dfrp->dfr_cmd_arg != NULL) {
				errmsg(ERR_NOFLAGS,
				    "Warning: %s is not a local file system",
				    dfrp->dfr_cmd_arg);
				errors++;
			}
			dfrp->dfr_valid = FALSE;
			continue;
		}

		/*
		 * Skip file systems mounted as "ignore" unless the -a option
		 * is present, or the user explicitly specified them on
		 * the command line.
		 */
		if (dfrp->dfr_mte->mte_ignore &&
		    ! (a_option || dfrp->dfr_cmd_arg)) {
			dfrp->dfr_valid = FALSE;
			continue;
		}

		n_valid++;
	}
	*valid_requests = n_valid;
	return (errors);
}


/*
 * Print the appropriate header for the requested output format.
 * Options are checked in order of their precedence.
 */
static void
print_header(void)
{
	if (use_scaling) { /* this comes from the -h option */
		int arg = 'h';

		(void) printf("%-*s %*s %*s %*s %-*s %s\n",
		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    SCALED_WIDTH, TRANSLATE("Size"),
		    SCALED_WIDTH, TRANSLATE("Used"),
		    AVAILABLE_WIDTH, TRANSLATE("Available"),
		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
		    TRANSLATE("Mounted on"));
		SET_OPTION(h);
		return;
	}
	if (k_option) {
		int arg = 'h';

		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    KBYTE_WIDTH, TRANSLATE("1024-blocks"),
		    KBYTE_WIDTH, TRANSLATE("Used"),
		    KBYTE_WIDTH, TRANSLATE("Available"),
		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
		    TRANSLATE("Mounted on"));
		SET_OPTION(h);
		return;
	}
	if (m_option) {
		int arg = 'h';

		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    KBYTE_WIDTH, TRANSLATE("1M-blocks"),
		    KBYTE_WIDTH, TRANSLATE("Used"),
		    KBYTE_WIDTH, TRANSLATE("Available"),
		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
		    TRANSLATE("Mounted on"));
		SET_OPTION(h);
		return;
	}
	/* Added for XCU4 compliance */
	if (P_option) {
		int arg = 'h';

		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    KBYTE_WIDTH, TRANSLATE("512-blocks"),
		    KBYTE_WIDTH, TRANSLATE("Used"),
		    KBYTE_WIDTH, TRANSLATE("Available"),
		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
		    TRANSLATE("Mounted on"));

		SET_OPTION(h);
		return;
	}
	/* End XCU4 */
	if (v_option) {
		(void) printf("%-*s %-*s %*s %*s %*s %-*s\n",
		    IBCS2_MOUNT_POINT_WIDTH, TRANSLATE("Mount Dir"),
		    IBCS2_FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    BLOCK_WIDTH, TRANSLATE("blocks"),
		    BLOCK_WIDTH, TRANSLATE("used"),
		    BLOCK_WIDTH, TRANSLATE("free"),
		    CAPACITY_WIDTH, TRANSLATE(" %used"));
		return;
	}
	if (e_option) {
		(void) printf(gettext("%-*s %*s\n"),
		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    BLOCK_WIDTH, TRANSLATE("ifree"));
		return;
	}
	if (b_option) {
		(void) printf(gettext("%-*s %*s\n"),
		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
		    BLOCK_WIDTH, TRANSLATE("avail"));
		return;
	}
}


/*
 * Convert an unsigned long long to a string representation and place the
 * result in the caller-supplied buffer.
 * The given number is in units of "unit_from" size, but the
 * converted number will be in units of "unit_to" size. The unit sizes
 * must be powers of 2.
 * The value "(unsigned long long)-1" is a special case and is always
 * converted to "-1".
 * Returns a pointer to the caller-supplied buffer.
 */
static char *
number_to_string(
			char *buf,		/* put the result here */
			unsigned long long number, /* convert this number */
			int unit_from,		/* from units of this size */
			int unit_to)		/* to units of this size */
{
	if ((long long)number == (long long)-1) {
		(void) strcpy(buf, "-1");
		return (buf);
	}

	/* don't crash if, i.e. fsp->f_frsize == 0 */
	if (unit_from <= 0)
		unit_from = 1;
	if (unit_to <= 0)
		unit_to = 1;

	if (unit_from == unit_to)
		(void) sprintf(buf, "%llu", number);
	else if (unit_from < unit_to)
		(void) sprintf(buf, "%llu",
		    number / (unsigned long long)(unit_to / unit_from));
	else
		(void) sprintf(buf, "%llu",
		    number * (unsigned long long)(unit_from / unit_to));

	return (buf);
}

/*
 * The statvfs() implementation allows us to return only two values, the total
 * number of blocks and the number of blocks free.  The equation 'used = total -
 * free' will not work for ZFS filesystems, due to the nature of pooled storage.
 * We choose to return values in the statvfs structure that will produce correct
 * results for 'used' and 'available', but not 'total'.  This function will open
 * the underlying ZFS dataset if necessary and get the real value.
 */
static void
adjust_total_blocks(struct df_request *dfrp, fsblkcnt64_t *total,
    uint64_t blocksize)
{
	char *dataset, *slash;
	boolean_t first = TRUE;
	uint64_t quota = 0;

	if (strcmp(DFR_FSTYPE(dfrp), MNTTYPE_ZFS) != 0 || !load_libzfs())
		return;

	/*
	 * We want to get the total size for this filesystem as bounded by any
	 * quotas. In order to do this, we start at the current filesystem and
	 * work upwards looking for the smallest quota.  When we reach the
	 * pool itself, the quota is the amount used plus the amount
	 * available.
	 */
	if ((dataset = strdup(DFR_SPECIAL(dfrp))) == NULL)
		return;

	slash = dataset + strlen(dataset);
	while (slash != NULL) {
		zfs_handle_t *zhp;
		uint64_t this_quota;

		*slash = '\0';

		zhp = _zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET);
		if (zhp == NULL)
			break;

		/* true at first iteration of loop */
		if (first) {
			quota = _zfs_prop_get_int(zhp, ZFS_PROP_REFQUOTA);
			if (quota == 0)
				quota = UINT64_MAX;
			first = FALSE;
		}

		this_quota = _zfs_prop_get_int(zhp, ZFS_PROP_QUOTA);
		if (this_quota && this_quota < quota)
			quota = this_quota;

		/* true at last iteration of loop */
		if ((slash = strrchr(dataset, '/')) == NULL) {
			uint64_t size;

			size = _zfs_prop_get_int(zhp, ZFS_PROP_USED) +
			    _zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
			if (size < quota)
				quota = size;
		}

		_zfs_close(zhp);
	}

	/*
	 * Modify total only if we managed to get some stats from libzfs.
	 */
	if (quota != 0)
		*total = quota / blocksize;
	free(dataset);
}

/*
 * The output will appear properly columnized regardless of the names of
 * the various fields
 */
static void
g_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
	fsblkcnt64_t	total_blocks = fsp->f_blocks;
	numbuf_t	total_blocks_buf;
	numbuf_t	total_files_buf;
	numbuf_t	free_blocks_buf;
	numbuf_t	available_blocks_buf;
	numbuf_t	free_files_buf;
	numbuf_t	fname_buf;
	char		*temp_buf;

#define	DEFINE_STR_LEN(var)			\
	static char *var##_str;			\
	static size_t var##_len

#define	SET_STR_LEN(name, var)\
	if (! var##_str) {\
		var##_str = TRANSLATE(name); \
		var##_len = strlen(var##_str); \
	}

	DEFINE_STR_LEN(block_size);
	DEFINE_STR_LEN(frag_size);
	DEFINE_STR_LEN(total_blocks);
	DEFINE_STR_LEN(free_blocks);
	DEFINE_STR_LEN(available);
	DEFINE_STR_LEN(total_files);
	DEFINE_STR_LEN(free_files);
	DEFINE_STR_LEN(fstype);
	DEFINE_STR_LEN(fsys_id);
	DEFINE_STR_LEN(fname);
	DEFINE_STR_LEN(flag);

	/*
	 * TRANSLATION_NOTE
	 * The first argument of each of the following macro invocations is a
	 * string that needs to be translated.
	 */
	SET_STR_LEN("block size", block_size);
	SET_STR_LEN("frag size", frag_size);
	SET_STR_LEN("total blocks", total_blocks);
	SET_STR_LEN("free blocks", free_blocks);
	SET_STR_LEN("available", available);
	SET_STR_LEN("total files", total_files);
	SET_STR_LEN("free files", free_files);
	SET_STR_LEN("fstype", fstype);
	SET_STR_LEN("filesys id", fsys_id);
	SET_STR_LEN("filename length", fname);
	SET_STR_LEN("flag", flag);

#define	NCOL1_WIDTH	(int)MAX3(BLOCK_WIDTH, NFILES_WIDTH, FSTYPE_WIDTH)
#define	NCOL2_WIDTH	(int)MAX3(BLOCK_WIDTH, FSID_WIDTH, FLAG_WIDTH) + 2
#define	NCOL3_WIDTH	(int)MAX3(BSIZE_WIDTH, BLOCK_WIDTH, NAMELEN_WIDTH)
#define	NCOL4_WIDTH	(int)MAX(FRAGSIZE_WIDTH, NFILES_WIDTH)

#define	SCOL1_WIDTH	(int)MAX3(total_blocks_len, free_files_len, fstype_len)
#define	SCOL2_WIDTH	(int)MAX3(free_blocks_len, fsys_id_len, flag_len)
#define	SCOL3_WIDTH	(int)MAX3(block_size_len, available_len, fname_len)
#define	SCOL4_WIDTH	(int)MAX(frag_size_len, total_files_len)

	temp_buf = xmalloc(
	    MAX(MOUNT_POINT_WIDTH, strlen(DFR_MOUNT_POINT(dfrp)))
	    + MAX(SPECIAL_DEVICE_WIDTH, strlen(DFR_SPECIAL(dfrp)))
	    + 20); /* plus slop - nulls & formatting */
	(void) sprintf(temp_buf, "%-*s(%-*s):",
	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp));

	(void) printf("%-*s %*lu %-*s %*lu %-*s\n",
	    NCOL1_WIDTH + 1 + SCOL1_WIDTH + 1 + NCOL2_WIDTH + 1 +  SCOL2_WIDTH,
	    temp_buf,
	    NCOL3_WIDTH, fsp->f_bsize, SCOL3_WIDTH, block_size_str,
	    NCOL4_WIDTH, fsp->f_frsize, SCOL4_WIDTH, frag_size_str);
	free(temp_buf);

	/*
	 * Adjust available_blocks value -  it can be less than 0 on
	 * a 4.x file system. Reset it to 0 in order to avoid printing
	 * negative numbers.
	 */
	if ((long long)available_blocks < (long long)0)
		available_blocks = (fsblkcnt64_t)0;

	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);

	(void) printf("%*s %-*s %*s %-*s %*s %-*s %*s %-*s\n",
	    NCOL1_WIDTH, number_to_string(total_blocks_buf,
	    total_blocks, fsp->f_frsize, 512),
	    SCOL1_WIDTH, total_blocks_str,
	    NCOL2_WIDTH, number_to_string(free_blocks_buf,
	    fsp->f_bfree, fsp->f_frsize, 512),
	    SCOL2_WIDTH, free_blocks_str,
	    NCOL3_WIDTH, number_to_string(available_blocks_buf,
	    available_blocks, fsp->f_frsize, 512),
	    SCOL3_WIDTH, available_str,
	    NCOL4_WIDTH, number_to_string(total_files_buf,
	    fsp->f_files, 1, 1),
	    SCOL4_WIDTH, total_files_str);

	(void) printf("%*s %-*s %*lu %-*s %s\n",
	    NCOL1_WIDTH, number_to_string(free_files_buf,
	    fsp->f_ffree, 1, 1),
	    SCOL1_WIDTH, free_files_str,
	    NCOL2_WIDTH, fsp->f_fsid, SCOL2_WIDTH, fsys_id_str,
	    fsp->f_fstr);

	(void) printf("%*s %-*s %#*.*lx %-*s %*s %-*s\n\n",
	    NCOL1_WIDTH, fsp->f_basetype, SCOL1_WIDTH, fstype_str,
	    NCOL2_WIDTH, NCOL2_WIDTH-2, fsp->f_flag, SCOL2_WIDTH, flag_str,
	    NCOL3_WIDTH, number_to_string(fname_buf,
	    (unsigned long long)fsp->f_namemax, 1, 1),
	    SCOL3_WIDTH, fname_str);
}


static void
k_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	fsblkcnt64_t total_blocks		= fsp->f_blocks;
	fsblkcnt64_t	free_blocks		= fsp->f_bfree;
	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
	fsblkcnt64_t	used_blocks;
	char		*file_system		= DFR_SPECIAL(dfrp);
	numbuf_t	total_blocks_buf;
	numbuf_t	used_blocks_buf;
	numbuf_t	available_blocks_buf;
	char		capacity_buf[LINEBUF_SIZE];

	/*
	 * If the free block count is -1, don't trust anything but the total
	 * number of blocks.
	 */
	if (free_blocks == (fsblkcnt64_t)-1) {
		used_blocks = (fsblkcnt64_t)-1;
		(void) strcpy(capacity_buf, "  100%");
	} else {
		fsblkcnt64_t reserved_blocks = free_blocks - available_blocks;

		used_blocks	= total_blocks - free_blocks;

		/*
		 * The capacity estimation is bogus when available_blocks is 0
		 * and the super-user has allocated more space. The reason
		 * is that reserved_blocks is inaccurate in that case, because
		 * when the super-user allocates space, free_blocks is updated
		 * but available_blocks is not (since it can't drop below 0).
		 *
		 * XCU4 and POSIX.2 require that any fractional result of the
		 * capacity estimation be rounded to the next highest integer,
		 * hence the addition of 0.5.
		 */
		(void) sprintf(capacity_buf, "%5.0f%%",
		    (total_blocks == 0) ? 0.0 :
		    ((double)used_blocks /
		    (double)(total_blocks - reserved_blocks))
		    * 100.0 + 0.5);
	}

	/*
	 * The available_blocks can be less than 0 on a 4.x file system.
	 * Reset it to 0 in order to avoid printing negative numbers.
	 */
	if ((long long)available_blocks < (long long)0)
		available_blocks = (fsblkcnt64_t)0;
	/*
	 * Print long special device names (usually NFS mounts) in a line
	 * by themselves when the output is directed to a terminal.
	 */
	if (tty_output && strlen(file_system) > (size_t)FILESYSTEM_WIDTH) {
		(void) printf("%s\n", file_system);
		file_system = "";
	}

	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);

	if (use_scaling) { /* comes from the -h option */
		nicenum_scale(total_blocks, fsp->f_frsize,
		    total_blocks_buf, sizeof (total_blocks_buf), 0);
		nicenum_scale(used_blocks, fsp->f_frsize,
		    used_blocks_buf, sizeof (used_blocks_buf), 0);
		nicenum_scale(available_blocks, fsp->f_frsize,
		    available_blocks_buf, sizeof (available_blocks_buf), 0);

		(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
		    FILESYSTEM_WIDTH, file_system,
		    SCALED_WIDTH, total_blocks_buf,
		    SCALED_WIDTH, used_blocks_buf,
		    AVAILABLE_WIDTH, available_blocks_buf,
		    CAPACITY_WIDTH, capacity_buf, DFR_MOUNT_POINT(dfrp));
		return;
	}

	if (v_option) {
	(void) printf("%-*.*s %-*.*s %*lld %*lld %*lld %-.*s\n",
	    IBCS2_MOUNT_POINT_WIDTH, IBCS2_MOUNT_POINT_WIDTH,
	    DFR_MOUNT_POINT(dfrp),
	    IBCS2_FILESYSTEM_WIDTH, IBCS2_FILESYSTEM_WIDTH, file_system,
	    BLOCK_WIDTH, total_blocks,
	    BLOCK_WIDTH, used_blocks,
	    BLOCK_WIDTH, available_blocks,
	    CAPACITY_WIDTH,	capacity_buf);
		return;
	}

	if (P_option && !k_option && !m_option) {
	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
	    FILESYSTEM_WIDTH, file_system,
	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
	    total_blocks, fsp->f_frsize, 512),
	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
	    used_blocks, fsp->f_frsize, 512),
	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
	    available_blocks, fsp->f_frsize, 512),
	    CAPACITY_WIDTH, capacity_buf,
	    DFR_MOUNT_POINT(dfrp));
	} else if (m_option) {
	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
	    FILESYSTEM_WIDTH, file_system,
	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
	    total_blocks, fsp->f_frsize, 1024*1024),
	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
	    used_blocks, fsp->f_frsize, 1024*1024),
	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
	    available_blocks, fsp->f_frsize, 1024*1024),
	    CAPACITY_WIDTH,	capacity_buf,
	    DFR_MOUNT_POINT(dfrp));
	} else {
	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
	    FILESYSTEM_WIDTH, file_system,
	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
	    total_blocks, fsp->f_frsize, 1024),
	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
	    used_blocks, fsp->f_frsize, 1024),
	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
	    available_blocks, fsp->f_frsize, 1024),
	    CAPACITY_WIDTH,	capacity_buf,
	    DFR_MOUNT_POINT(dfrp));
	}
}

/*
 * The following is for internationalization support.
 */
static bool_int strings_initialized;
static char	*files_str;
static char	*blocks_str;
static char	*total_str;
static char	*kilobytes_str;

static void
strings_init(void)
{
	total_str = TRANSLATE("total");
	files_str = TRANSLATE("files");
	blocks_str = TRANSLATE("blocks");
	kilobytes_str = TRANSLATE("kilobytes");
	strings_initialized = TRUE;
}

#define	STRINGS_INIT()		if (!strings_initialized) strings_init()


static void
t_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	fsblkcnt64_t	total_blocks = fsp->f_blocks;
	numbuf_t	total_blocks_buf;
	numbuf_t	total_files_buf;
	numbuf_t	free_blocks_buf;
	numbuf_t	free_files_buf;

	STRINGS_INIT();

	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);

	(void) printf("%-*s(%-*s): %*s %s %*s %s\n",
	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
	    fsp->f_bfree, fsp->f_frsize, 512),
	    blocks_str,
	    NFILES_WIDTH, number_to_string(free_files_buf,
	    fsp->f_ffree, 1, 1),
	    files_str);
	/*
	 * The total column used to use the same space as the mnt pt & special
	 * dev fields. However, this doesn't work with massive special dev
	 * fields * (eg > 500 chars) causing an enormous amount of white space
	 * before the total column (see bug 4100411). So the code was
	 * simplified to set the total column at the usual gap.
	 * This had the side effect of fixing a bug where the previously
	 * used static buffer was overflowed by the same massive special dev.
	 */
	(void) printf("%*s: %*s %s %*s %s\n",
	    MNT_SPEC_WIDTH, total_str,
	    BLOCK_WIDTH, number_to_string(total_blocks_buf,
	    total_blocks, fsp->f_frsize, 512),
	    blocks_str,
	    NFILES_WIDTH, number_to_string(total_files_buf,
	    fsp->f_files, 1, 1),
	    files_str);
}


static void
eb_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	numbuf_t free_files_buf;
	numbuf_t free_kbytes_buf;

	STRINGS_INIT();

	(void) printf("%-*s(%-*s): %*s %s\n",
	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
	    MAX(KBYTE_WIDTH, NFILES_WIDTH),
	    number_to_string(free_kbytes_buf,
	    fsp->f_bfree, fsp->f_frsize, 1024),
	    kilobytes_str);
	(void) printf("%-*s(%-*s): %*s %s\n",
	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
	    MAX(NFILES_WIDTH, NFILES_WIDTH),
	    number_to_string(free_files_buf, fsp->f_ffree, 1, 1),
	    files_str);
}


static void
e_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	numbuf_t free_files_buf;

	(void) printf("%-*s %*s\n",
	    FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
	    NFILES_WIDTH,
	    number_to_string(free_files_buf, fsp->f_ffree, 1, 1));
}


static void
b_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	numbuf_t free_blocks_buf;

	(void) printf("%-*s %*s\n",
	    FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
	    fsp->f_bfree, fsp->f_frsize, 1024));
}


/* ARGSUSED */
static void
n_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	(void) printf("%-*s: %-*s\n",
	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
	    FSTYPE_WIDTH, dfrp->dfr_fstype);
}


static void
default_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	numbuf_t free_blocks_buf;
	numbuf_t free_files_buf;

	STRINGS_INIT();

	(void) printf("%-*s(%-*s):%*s %s %*s %s\n",
	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
	    fsp->f_bfree, fsp->f_frsize, 512),
	    blocks_str,
	    NFILES_WIDTH, number_to_string(free_files_buf,
	    fsp->f_ffree, 1, 1),
	    files_str);
}


/* ARGSUSED */
static void
V_output(struct df_request *dfrp, struct statvfs64 *fsp)
{
	char temp_buf[LINEBUF_SIZE];

	if (df_options_len > 1)
		(void) strcat(strcpy(temp_buf, df_options), " ");
	else
		temp_buf[0] = NUL;

	(void) printf("%s -F %s %s%s\n",
	    program_name, dfrp->dfr_fstype, temp_buf,
	    dfrp->dfr_cmd_arg ? dfrp->dfr_cmd_arg: DFR_SPECIAL(dfrp));
}


/*
 * This function is used to sort the array of df_requests according to fstype
 */
static int
df_reqcomp(const void *p1, const void *p2)
{
	int v = strcmp(DFRP(p1)->dfr_fstype, DFRP(p2)->dfr_fstype);

	if (v != 0)
		return (v);
	else
		return (DFRP(p1)->dfr_index - DFRP(p2)->dfr_index);
}


static void
vfs_error(char *file, int status)
{
	if (status == VFS_TOOLONG)
		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
		    file, MNT_LINE_MAX);
	else if (status == VFS_TOOMANY)
		errmsg(ERR_NOFLAGS, "a line in %s has too many fields", file);
	else if (status == VFS_TOOFEW)
		errmsg(ERR_NOFLAGS, "a line in %s has too few fields", file);
	else
		errmsg(ERR_NOFLAGS, "error while reading %s: %d", file, status);
}


/*
 * Try to determine the fstype for the specified block device.
 * Return in order of decreasing preference:
 *	file system type from vfstab
 *	file system type as specified by -F option
 *	default file system type
 */
static char *
find_fstype(char *special)
{
	struct vfstab	vtab;
	FILE		*fp;
	int		status;
	char		*vfstab_file = VFS_TAB;

	fp = xfopen(vfstab_file);
	status = getvfsspec(fp, &vtab, special);
	(void) fclose(fp);
	if (status > 0)
		vfs_error(vfstab_file, status);

	if (status == 0) {
		if (F_option && ! EQ(FSType, vtab.vfs_fstype))
			errmsg(ERR_NOFLAGS,
			"warning: %s is of type %s", special, vtab.vfs_fstype);
		return (new_string(vtab.vfs_fstype));
	}
	else
		return (F_option ? FSType : default_fstype(special));
}

/*
 * When this function returns, the following fields are filled for all
 * valid entries in the requests[] array:
 *		dfr_mte		(if the file system is mounted)
 *		dfr_fstype
 *		dfr_index
 *
 * The function returns the number of errors that occurred while building
 * the request list.
 */
static int
create_request_list(
			int argc,
			char *argv[],
			struct df_request *requests_p[],
			size_t *request_count)
{
	struct df_request	*requests;
	struct df_request	*dfrp;
	size_t			size;
	size_t			i;
	size_t			request_index = 0;
	size_t			max_requests;
	int			errors = 0;

	/*
	 * If no args, use the mounted file systems, otherwise use the
	 * user-specified arguments.
	 */
	if (argc == 0) {
		mtab_read_file();
		max_requests = mount_table_entries;
	} else
		max_requests = argc;

	size = max_requests * sizeof (struct df_request);
	requests = xmalloc(size);
	(void) memset(requests, 0, size);

	if (argc == 0) {
		/*
		 * If -Z wasn't specified, we skip mounts in other
		 * zones.  This obviously is a noop in a non-global
		 * zone.
		 */
		boolean_t showall = (getzoneid() != GLOBAL_ZONEID) || Z_option;
		struct zone_summary *zsp;

		if (!showall) {
			zsp = fs_get_zone_summaries();
			if (zsp == NULL)
				errmsg(ERR_FATAL,
				    "unable to retrieve list of zones");
		}

		for (i = 0; i < mount_table_entries; i++) {
			struct extmnttab *mtp = mount_table[i].mte_mount;

			if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
				continue;

			if (!showall) {
				if (fs_mount_in_other_zone(zsp,
				    mtp->mnt_mountp))
					continue;
			}
			dfrp = &requests[request_index++];
			dfrp->dfr_mte		= &mount_table[i];
			dfrp->dfr_fstype	= mtp->mnt_fstype;
			dfrp->dfr_index		= i;
			dfrp->dfr_valid		= TRUE;
		}
	} else {
		struct stat64 *arg_stat; /* array of stat structures	*/
		bool_int *valid_stat;	/* which structures are valid	*/

		arg_stat = xmalloc(argc * sizeof (struct stat64));
		valid_stat = xmalloc(argc * sizeof (bool_int));

		/*
		 * Obtain stat64 information for each argument before
		 * constructing the list of mounted file systems. By
		 * touching all these places we force the automounter
		 * to establish any mounts required to access the arguments,
		 * so that the corresponding mount table entries will exist
		 * when we look for them.
		 * It is still possible that the automounter may timeout
		 * mounts between the time we read the mount table and the
		 * time we process the request. Even in that case, when
		 * we issue the statvfs64(2) for the mount point, the file
		 * system will be mounted again. The only problem will
		 * occur if the automounter maps change in the meantime
		 * and the mount point is eliminated.
		 */
		for (i = 0; i < argc; i++)
			valid_stat[i] = (stat64(argv[i], &arg_stat[i]) == 0);

		mtab_read_file();

		for (i = 0; i < argc; i++) {
			char *arg = argv[i];

			dfrp = &requests[request_index];

			dfrp->dfr_index = request_index;
			dfrp->dfr_cmd_arg = arg;

			if (valid_stat[i]) {
				dfrp->dfr_fstype = arg_stat[i].st_fstype;
				if (S_ISBLK(arg_stat[i].st_mode)) {
					bdev_mount_entry(dfrp);
					dfrp->dfr_valid = TRUE;
				} else if (S_ISDIR(arg_stat[i].st_mode) ||
				    S_ISREG(arg_stat[i].st_mode) ||
				    S_ISFIFO(arg_stat[i].st_mode)) {
					path_mount_entry(dfrp,
					    arg_stat[i].st_dev);
					if (! DFR_ISMOUNTEDFS(dfrp)) {
						errors++;
						continue;
					}
					dfrp->dfr_valid = TRUE;
				}
			} else {
				resource_mount_entry(dfrp);
				dfrp->dfr_valid = DFR_ISMOUNTEDFS(dfrp);
			}

			/*
			 * If we haven't managed to verify that the request
			 * is valid, we must have gotten a bad argument.
			 */
			if (!dfrp->dfr_valid) {
				errmsg(ERR_NOFLAGS,
				    "(%-10s) not a block device, directory or "
				    "mounted resource", arg);
				errors++;
				continue;
			}

			/*
			 * Determine the file system type.
			 */
			if (DFR_ISMOUNTEDFS(dfrp))
				dfrp->dfr_fstype =
				    dfrp->dfr_mte->mte_mount->mnt_fstype;
			else
				dfrp->dfr_fstype =
				    find_fstype(dfrp->dfr_cmd_arg);

			request_index++;
		}
	}
	*requests_p = requests;
	*request_count = request_index;
	return (errors);
}


/*
 * Select the appropriate function and flags to use for output.
 * Notice that using both -e and -b options produces a different form of
 * output than either of those two options alone; this is the behavior of
 * the SVR4 df.
 */
static struct df_output *
select_output(void)
{
	static struct df_output dfo;

	/*
	 * The order of checking options follows the option precedence
	 * rules as they are listed in the man page.
	 */
	if (use_scaling) { /* comes from the -h option */
		dfo.dfo_func = k_output;
		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
	} else if (V_option) {
		dfo.dfo_func = V_output;
		dfo.dfo_flags = DFO_NOFLAGS;
	} else if (g_option) {
		dfo.dfo_func = g_output;
		dfo.dfo_flags = DFO_STATVFS;
	} else if (k_option || m_option || P_option || v_option) {
		dfo.dfo_func = k_output;
		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
	} else if (t_option) {
		dfo.dfo_func = t_output;
		dfo.dfo_flags = DFO_STATVFS;
	} else if (b_option && e_option) {
		dfo.dfo_func = eb_output;
		dfo.dfo_flags = DFO_STATVFS;
	} else if (b_option) {
		dfo.dfo_func = b_output;
		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
	} else if (e_option) {
		dfo.dfo_func = e_output;
		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
	} else if (n_option) {
		dfo.dfo_func = n_output;
		dfo.dfo_flags = DFO_NOFLAGS;
	} else {
		dfo.dfo_func = default_output;
		dfo.dfo_flags = DFO_STATVFS;
	}
	return (&dfo);
}


/*
 * The (argc,argv) pair contains all the non-option arguments
 */
static void
do_df(int argc, char *argv[])
{
	size_t			i;
	struct df_request	*requests;		/* array of requests */
	size_t			n_requests;
	struct df_request	*dfrp;
	int			errors;

	errors = create_request_list(argc, argv, &requests, &n_requests);

	if (n_requests == 0)
		exit(errors);

	/*
	 * If we are going to run the FSType-specific df command,
	 * rearrange the requests so that we can issue a single command
	 * per file system type.
	 */
	if (o_option) {
		size_t j;

		/*
		 * qsort is not a stable sorting method (i.e. requests of
		 * the same file system type may be swapped, and hence appear
		 * in the output in a different order from the one in which
		 * they were listed in the command line). In order to force
		 * stability, we use the dfr_index field which is unique
		 * for each request.
		 */
		qsort(requests,
		    n_requests, sizeof (struct df_request), df_reqcomp);
		for (i = 0; i < n_requests; i = j) {
			char *fstype = requests[i].dfr_fstype;

			for (j = i+1; j < n_requests; j++)
				if (! EQ(fstype, requests[j].dfr_fstype))
					break;

			/*
			 * At this point, requests in the range [i,j) are
			 * of the same type.
			 *
			 * If the -F option was used, and the user specified
			 * arguments, the filesystem types must match
			 *
			 * XXX: the alternative of doing this check here is to
			 *	invoke prune_list, but then we have to
			 *	modify this code to ignore invalid requests.
			 */
			if (F_option && ! EQ(fstype, FSType)) {
				size_t k;

				for (k = i; k < j; k++) {
					dfrp = &requests[k];
					if (dfrp->dfr_cmd_arg != NULL) {
						errmsg(ERR_NOFLAGS,
						    "Warning: %s mounted as a "
						    "%s file system",
						    dfrp->dfr_cmd_arg,
						    dfrp->dfr_fstype);
						errors++;
					}
				}
			} else
				errors += run_fs_specific_df(&requests[i], j-i);
		}
	} else {
		size_t valid_requests;

		/*
		 * We have to prune the request list to avoid printing a header
		 * if there are no valid requests
		 */
		errors += prune_list(requests, n_requests, &valid_requests);

		if (valid_requests) {
			struct df_output *dfop = select_output();

			/* indicates if we already printed out a header line */
			int printed_header = 0;

			for (i = 0; i < n_requests; i++) {
				dfrp = &requests[i];
				if (! dfrp->dfr_valid)
					continue;

				/*
				 * If we don't have a mount point,
				 * this must be a block device.
				 */
				if (DFR_ISMOUNTEDFS(dfrp)) {
					struct statvfs64 stvfs;

					if ((dfop->dfo_flags & DFO_STATVFS) &&
					    statvfs64(DFR_MOUNT_POINT(dfrp),
					    &stvfs) == -1) {
						errmsg(ERR_PERROR,
						    "cannot statvfs %s:",
						    DFR_MOUNT_POINT(dfrp));
						errors++;
						continue;
					}
					if ((!printed_header) &&
					    (dfop->dfo_flags & DFO_HEADER)) {
						print_header();
						printed_header = 1;
					}

					(*dfop->dfo_func)(dfrp, &stvfs);
				} else {
					/*
					 *  -h option only works for
					 *  mounted filesystems
					 */
					if (use_scaling) {
						errmsg(ERR_NOFLAGS,
		"-h option incompatible with unmounted special device (%s)",
						    dfrp->dfr_cmd_arg);
						errors++;
						continue;
					}
					errors += run_fs_specific_df(dfrp, 1);
				}
			}
		}
	}
	exit(errors);
}


/*
 * The rest of this file implements the devnm command
 */

static char *
find_dev_name(char *file, dev_t dev)
{
	struct df_request dfreq;

	dfreq.dfr_cmd_arg = file;
	dfreq.dfr_fstype = 0;
	dfreq.dfr_mte = NULL;
	path_mount_entry(&dfreq, dev);
	return (DFR_ISMOUNTEDFS(&dfreq) ? DFR_SPECIAL(&dfreq) : NULL);
}


static void
do_devnm(int argc, char *argv[])
{
	int arg;
	int errors = 0;
	char *dev_name;

	if (argc == 1)
		errmsg(ERR_NONAME, "Usage: %s name ...", DEVNM_CMD);

	mtab_read_file();

	for (arg = 1; arg < argc; arg++) {
		char *file = argv[arg];
		struct stat64 st;

		if (stat64(file, &st) == -1) {
			errmsg(ERR_PERROR, "%s: ", file);
			errors++;
			continue;
		}

		if (! is_remote_fs(st.st_fstype) &&
		    ! EQ(st.st_fstype, MNTTYPE_TMPFS) &&
		    (dev_name = find_dev_name(file, st.st_dev)))
			(void) printf("%s %s\n", dev_name, file);
		else
			errmsg(ERR_NOFLAGS,
			    "%s not found", file);
	}
	exit(errors);
	/* NOTREACHED */
}