Home | History | Annotate | Line # | Download | only in df
df.c revision 1.96
      1  1.96       ryo /*	$NetBSD: df.c,v 1.96 2020/08/21 16:40:02 ryo Exp $ */
      2  1.19       cgd 
      3  1.12       cgd /*
      4  1.15   mycroft  * Copyright (c) 1980, 1990, 1993, 1994
      5  1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.12       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.12       cgd  * All or some portions of this file are derived from material licensed
      8  1.12       cgd  * to the University of California by American Telephone and Telegraph
      9  1.12       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.12       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.12       cgd  *
     12  1.12       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.12       cgd  * modification, are permitted provided that the following conditions
     14  1.12       cgd  * are met:
     15  1.12       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.12       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.12       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.12       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.12       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.49       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.12       cgd  *    may be used to endorse or promote products derived from this software
     22  1.12       cgd  *    without specific prior written permission.
     23  1.12       cgd  *
     24  1.12       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.12       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.12       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.12       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.12       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.12       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.12       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.12       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.12       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.12       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.12       cgd  * SUCH DAMAGE.
     35  1.12       cgd  */
     36  1.12       cgd 
     37  1.24   thorpej #include <sys/cdefs.h>
     38  1.12       cgd #ifndef lint
     39  1.24   thorpej __COPYRIGHT(
     40  1.83     lukem "@(#) Copyright (c) 1980, 1990, 1993, 1994\
     41  1.83     lukem  The Regents of the University of California.  All rights reserved.");
     42  1.12       cgd #endif /* not lint */
     43  1.12       cgd 
     44  1.12       cgd #ifndef lint
     45  1.19       cgd #if 0
     46  1.19       cgd static char sccsid[] = "@(#)df.c	8.7 (Berkeley) 4/2/94";
     47  1.19       cgd #else
     48  1.96       ryo __RCSID("$NetBSD: df.c,v 1.96 2020/08/21 16:40:02 ryo Exp $");
     49  1.19       cgd #endif
     50  1.12       cgd #endif /* not lint */
     51  1.12       cgd 
     52  1.12       cgd #include <sys/param.h>
     53  1.12       cgd #include <sys/stat.h>
     54  1.12       cgd #include <sys/mount.h>
     55  1.27       mrg 
     56  1.75  christos #include <assert.h>
     57  1.15   mycroft #include <err.h>
     58  1.12       cgd #include <errno.h>
     59  1.94  christos #include <stdbool.h>
     60  1.12       cgd #include <fcntl.h>
     61  1.82  christos #include <locale.h>
     62  1.44    provos #include <util.h>
     63  1.12       cgd #include <stdio.h>
     64  1.12       cgd #include <stdlib.h>
     65  1.12       cgd #include <string.h>
     66  1.12       cgd #include <unistd.h>
     67  1.87  christos #include <util.h>
     68  1.12       cgd 
     69  1.87  christos static char	*getmntpt(const char *);
     70  1.95  christos static void	 prtstat(const struct statvfs *, int);
     71  1.87  christos static int	 selected(const char *, size_t);
     72  1.87  christos static void	 maketypelist(char *);
     73  1.87  christos static size_t	 regetmntinfo(struct statvfs **, size_t);
     74  1.87  christos __dead static void usage(void);
     75  1.96       ryo static void	 prthumanval(int64_t, int);
     76  1.94  christos static void	 prthuman(const struct statvfs *, int64_t, int64_t);
     77  1.87  christos 
     78  1.94  christos static int	 aflag, gflag, hflag, iflag, lflag, nflag, Pflag, Wflag;
     79  1.87  christos static long	 usize;
     80  1.87  christos static char	**typelist;
     81  1.12       cgd 
     82  1.96       ryo #define WIDTH_INODE	8
     83  1.96       ryo #define WIDTH_BLKSIZE	10
     84  1.96       ryo 
     85  1.12       cgd int
     86  1.41     enami main(int argc, char *argv[])
     87  1.12       cgd {
     88  1.12       cgd 	struct stat stbuf;
     89  1.62  christos 	struct statvfs *mntbuf;
     90  1.94  christos 	int ch, maxwidth, width;
     91  1.94  christos 	size_t i, mntcount;
     92  1.12       cgd 	char *mntpt;
     93  1.12       cgd 
     94  1.82  christos 	setprogname(argv[0]);
     95  1.82  christos 	(void)setlocale(LC_ALL, "");
     96  1.82  christos 
     97  1.94  christos 	while ((ch = getopt(argc, argv, "aGghiklmnPt:W")) != -1)
     98  1.15   mycroft 		switch (ch) {
     99  1.34  christos 		case 'a':
    100  1.34  christos 			aflag = 1;
    101  1.34  christos 			break;
    102  1.81  christos 		case 'g':
    103  1.71  christos 			hflag = 0;
    104  1.71  christos 			usize = 1024 * 1024 * 1024;
    105  1.46     grant 			break;
    106  1.81  christos 		case 'G':
    107  1.73  christos 			gflag = 1;
    108  1.73  christos 			break;
    109  1.44    provos 		case 'h':
    110  1.44    provos 			hflag = 1;
    111  1.71  christos 			usize = 0;
    112  1.44    provos 			break;
    113  1.12       cgd 		case 'i':
    114  1.12       cgd 			iflag = 1;
    115  1.12       cgd 			break;
    116  1.12       cgd 		case 'k':
    117  1.71  christos 			hflag = 0;
    118  1.71  christos 			usize = 1024;
    119  1.12       cgd 			break;
    120  1.12       cgd 		case 'l':
    121  1.17   mycroft 			lflag = 1;
    122  1.12       cgd 			break;
    123  1.36   hubertf 		case 'm':
    124  1.71  christos 			hflag = 0;
    125  1.71  christos 			usize = 1024 * 1024;
    126  1.36   hubertf 			break;
    127  1.12       cgd 		case 'n':
    128  1.12       cgd 			nflag = 1;
    129  1.12       cgd 			break;
    130  1.35    kleink 		case 'P':
    131  1.35    kleink 			Pflag = 1;
    132  1.35    kleink 			break;
    133  1.94  christos 		case 'W':
    134  1.94  christos 			Wflag = 1;
    135  1.94  christos 			break;
    136  1.16   mycroft 		case 't':
    137  1.17   mycroft 			if (typelist != NULL)
    138  1.80  christos 				errx(EXIT_FAILURE,
    139  1.80  christos 				    "only one -t option may be specified.");
    140  1.17   mycroft 			maketypelist(optarg);
    141  1.16   mycroft 			break;
    142  1.12       cgd 		case '?':
    143  1.12       cgd 		default:
    144  1.12       cgd 			usage();
    145  1.12       cgd 		}
    146  1.73  christos 
    147  1.73  christos 	if (gflag && (Pflag || iflag))
    148  1.80  christos 		errx(EXIT_FAILURE,
    149  1.81  christos 		    "only one of -G and -P or -i may be specified");
    150  1.73  christos 	if (Pflag && iflag)
    151  1.80  christos 		errx(EXIT_FAILURE,
    152  1.80  christos 		    "only one of -P and -i may be specified");
    153  1.80  christos #if 0
    154  1.80  christos 	/*
    155  1.80  christos 	 * The block size cannot be checked until after getbsize() is called.
    156  1.80  christos 	 */
    157  1.75  christos 	if (Pflag && (hflag || (usize != 1024 && usize != 512)))
    158  1.80  christos 		errx(EXIT_FAILURE,
    159  1.80  christos 		    "non-standard block size incompatible with -P");
    160  1.80  christos #endif
    161  1.12       cgd 	argc -= optind;
    162  1.12       cgd 	argv += optind;
    163  1.12       cgd 
    164  1.94  christos 	mntcount = getmntinfo(&mntbuf, MNT_NOWAIT);
    165  1.94  christos 	if (mntcount == 0)
    166  1.80  christos 		err(EXIT_FAILURE,
    167  1.80  christos 		    "retrieving information on mounted file systems");
    168  1.15   mycroft 
    169  1.41     enami 	if (*argv == NULL) {
    170  1.94  christos 		mntcount = regetmntinfo(&mntbuf, mntcount);
    171  1.17   mycroft 	} else {
    172  1.94  christos 		if ((mntbuf = calloc(argc, sizeof(*mntbuf))) == NULL)
    173  1.80  christos 			err(EXIT_FAILURE, "can't allocate statvfs array");
    174  1.94  christos 		mntcount = 0;
    175  1.80  christos 		for (/*EMPTY*/; *argv != NULL; argv++) {
    176  1.17   mycroft 			if (stat(*argv, &stbuf) < 0) {
    177  1.17   mycroft 				if ((mntpt = getmntpt(*argv)) == 0) {
    178  1.17   mycroft 					warn("%s", *argv);
    179  1.12       cgd 					continue;
    180  1.12       cgd 				}
    181  1.18   mycroft 			} else if (S_ISBLK(stbuf.st_mode)) {
    182  1.43     soren 				if ((mntpt = getmntpt(*argv)) == 0)
    183  1.43     soren 					mntpt = *argv;
    184  1.17   mycroft 			} else
    185  1.17   mycroft 				mntpt = *argv;
    186  1.17   mycroft 			/*
    187  1.17   mycroft 			 * Statfs does not take a `wait' flag, so we cannot
    188  1.17   mycroft 			 * implement nflag here.
    189  1.17   mycroft 			 */
    190  1.94  christos 			if (!statvfs(mntpt, &mntbuf[mntcount]))
    191  1.23   thorpej 				if (lflag &&
    192  1.94  christos 				    (mntbuf[mntcount].f_flag & MNT_LOCAL) == 0)
    193  1.23   thorpej 					warnx("Warning: %s is not a local %s",
    194  1.23   thorpej 					    *argv, "file system");
    195  1.23   thorpej 				else if
    196  1.94  christos 				    (!selected(mntbuf[mntcount].f_fstypename,
    197  1.94  christos 					sizeof(mntbuf[mntcount].f_fstypename)))
    198  1.23   thorpej 					warnx("Warning: %s mounted as a %s %s",
    199  1.23   thorpej 					    *argv,
    200  1.94  christos 					    mntbuf[mntcount].f_fstypename,
    201  1.23   thorpej 					    "file system");
    202  1.23   thorpej 				else
    203  1.94  christos 					++mntcount;
    204  1.17   mycroft 			else
    205  1.17   mycroft 				warn("%s", *argv);
    206  1.12       cgd 		}
    207  1.12       cgd 	}
    208  1.17   mycroft 
    209  1.17   mycroft 	maxwidth = 0;
    210  1.94  christos 	for (i = 0; i < mntcount; i++) {
    211  1.95  christos 		width = (int)strlen(Wflag && mntbuf[i].f_mntfromlabel[0] ?
    212  1.95  christos 		    mntbuf[i].f_mntfromlabel : mntbuf[i].f_mntfromname);
    213  1.17   mycroft 		if (width > maxwidth)
    214  1.17   mycroft 			maxwidth = width;
    215  1.17   mycroft 	}
    216  1.94  christos 	for (i = 0; i < mntcount; i++)
    217  1.95  christos 		prtstat(&mntbuf[i], maxwidth);
    218  1.87  christos 	return 0;
    219  1.12       cgd }
    220  1.12       cgd 
    221  1.87  christos static char *
    222  1.87  christos getmntpt(const char *name)
    223  1.12       cgd {
    224  1.94  christos 	size_t mntcount, i;
    225  1.62  christos 	struct statvfs *mntbuf;
    226  1.12       cgd 
    227  1.94  christos 	mntcount = getmntinfo(&mntbuf, MNT_NOWAIT);
    228  1.94  christos 	if (mntcount == 0)
    229  1.87  christos 		err(EXIT_FAILURE, "Can't get mount information");
    230  1.94  christos 	for (i = 0; i < mntcount; i++) {
    231  1.12       cgd 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    232  1.87  christos 			return mntbuf[i].f_mntonname;
    233  1.12       cgd 	}
    234  1.87  christos 	return 0;
    235  1.12       cgd }
    236  1.12       cgd 
    237  1.17   mycroft static enum { IN_LIST, NOT_IN_LIST } which;
    238  1.17   mycroft 
    239  1.87  christos static int
    240  1.76  christos selected(const char *type, size_t len)
    241  1.17   mycroft {
    242  1.17   mycroft 	char **av;
    243  1.17   mycroft 
    244  1.17   mycroft 	/* If no type specified, it's always selected. */
    245  1.17   mycroft 	if (typelist == NULL)
    246  1.87  christos 		return 1;
    247  1.17   mycroft 	for (av = typelist; *av != NULL; ++av)
    248  1.76  christos 		if (!strncmp(type, *av, len))
    249  1.87  christos 			return which == IN_LIST ? 1 : 0;
    250  1.87  christos 	return which == IN_LIST ? 0 : 1;
    251  1.17   mycroft }
    252  1.16   mycroft 
    253  1.87  christos static void
    254  1.41     enami maketypelist(char *fslist)
    255  1.16   mycroft {
    256  1.87  christos 	size_t i;
    257  1.17   mycroft 	char *nextcp, **av;
    258  1.16   mycroft 
    259  1.17   mycroft 	if ((fslist == NULL) || (fslist[0] == '\0'))
    260  1.80  christos 		errx(EXIT_FAILURE, "empty type list");
    261  1.16   mycroft 
    262  1.17   mycroft 	/*
    263  1.17   mycroft 	 * XXX
    264  1.17   mycroft 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    265  1.17   mycroft 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    266  1.17   mycroft 	 */
    267  1.17   mycroft 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    268  1.17   mycroft 		fslist += 2;
    269  1.17   mycroft 		which = NOT_IN_LIST;
    270  1.17   mycroft 	} else
    271  1.17   mycroft 		which = IN_LIST;
    272  1.17   mycroft 
    273  1.17   mycroft 	/* Count the number of types. */
    274  1.24   thorpej 	for (i = 1, nextcp = fslist;
    275  1.24   thorpej 	    (nextcp = strchr(nextcp, ',')) != NULL; i++)
    276  1.17   mycroft 		++nextcp;
    277  1.17   mycroft 
    278  1.17   mycroft 	/* Build an array of that many types. */
    279  1.94  christos 	if ((av = typelist = calloc((i + 1), sizeof(*av))) == NULL)
    280  1.80  christos 		err(EXIT_FAILURE, "can't allocate type array");
    281  1.17   mycroft 	av[0] = fslist;
    282  1.24   thorpej 	for (i = 1, nextcp = fslist;
    283  1.24   thorpej 	    (nextcp = strchr(nextcp, ',')) != NULL; i++) {
    284  1.17   mycroft 		*nextcp = '\0';
    285  1.17   mycroft 		av[i] = ++nextcp;
    286  1.16   mycroft 	}
    287  1.17   mycroft 	/* Terminate the array. */
    288  1.17   mycroft 	av[i] = NULL;
    289  1.17   mycroft }
    290  1.16   mycroft 
    291  1.17   mycroft /*
    292  1.17   mycroft  * Make a pass over the filesystem info in ``mntbuf'' filtering out
    293  1.17   mycroft  * filesystem types not in ``fsmask'' and possibly re-stating to get
    294  1.62  christos  * current (not cached) info.  Returns the new count of valid statvfs bufs.
    295  1.17   mycroft  */
    296  1.87  christos static size_t
    297  1.94  christos regetmntinfo(struct statvfs **mntbufp, size_t mntcount)
    298  1.17   mycroft {
    299  1.87  christos 	size_t i, j;
    300  1.62  christos 	struct statvfs *mntbuf;
    301  1.16   mycroft 
    302  1.42  christos 	if (!lflag && typelist == NULL && aflag)
    303  1.94  christos 		return nflag ? mntcount : (size_t)getmntinfo(mntbufp, MNT_WAIT);
    304  1.16   mycroft 
    305  1.17   mycroft 	mntbuf = *mntbufp;
    306  1.17   mycroft 	j = 0;
    307  1.94  christos 	for (i = 0; i < mntcount; i++) {
    308  1.62  christos 		if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0)
    309  1.34  christos 			continue;
    310  1.62  christos 		if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0)
    311  1.17   mycroft 			continue;
    312  1.76  christos 		if (!selected(mntbuf[i].f_fstypename,
    313  1.76  christos 		    sizeof(mntbuf[i].f_fstypename)))
    314  1.17   mycroft 			continue;
    315  1.17   mycroft 		if (nflag)
    316  1.17   mycroft 			mntbuf[j] = mntbuf[i];
    317  1.32  sommerfe 		else {
    318  1.62  christos 			struct statvfs layerbuf = mntbuf[i];
    319  1.62  christos 			(void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]);
    320  1.32  sommerfe 			/*
    321  1.32  sommerfe 			 * If the FS name changed, then new data is for
    322  1.32  sommerfe 			 * a different layer and we don't want it.
    323  1.32  sommerfe 			 */
    324  1.41     enami 			if (memcmp(layerbuf.f_mntfromname,
    325  1.41     enami 			    mntbuf[j].f_mntfromname, MNAMELEN))
    326  1.41     enami 				mntbuf[j] = layerbuf;
    327  1.32  sommerfe 		}
    328  1.17   mycroft 		j++;
    329  1.16   mycroft 	}
    330  1.87  christos 	return j;
    331  1.16   mycroft }
    332  1.16   mycroft 
    333  1.87  christos static void
    334  1.96       ryo prthumanval(int64_t bytes, int width)
    335  1.44    provos {
    336  1.59     enami 	char buf[6];
    337  1.44    provos 
    338  1.80  christos 	(void)humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
    339  1.59     enami 	    bytes, "", HN_AUTOSCALE,
    340  1.44    provos 	    HN_B | HN_NOSPACE | HN_DECIMAL);
    341  1.44    provos 
    342  1.96       ryo 	(void)printf("%*s", width, buf);
    343  1.44    provos }
    344  1.44    provos 
    345  1.87  christos static void
    346  1.94  christos prthuman(const struct statvfs *sfsp, int64_t used, int64_t bavail)
    347  1.44    provos {
    348  1.60     enami 
    349  1.96       ryo 	prthumanval((int64_t)(sfsp->f_blocks * sfsp->f_frsize), WIDTH_BLKSIZE);
    350  1.96       ryo 	prthumanval((int64_t)(used * sfsp->f_frsize), 1 + WIDTH_BLKSIZE);
    351  1.96       ryo 	prthumanval((int64_t)(bavail * sfsp->f_frsize), 1 + WIDTH_BLKSIZE);
    352  1.44    provos }
    353  1.44    provos 
    354  1.12       cgd /*
    355  1.62  christos  * Convert statvfs returned filesystem size into BLOCKSIZE units.
    356  1.15   mycroft  * Attempts to avoid overflow for large filesystems.
    357  1.15   mycroft  */
    358  1.41     enami #define fsbtoblk(num, fsbs, bs)					\
    359  1.84     lukem 	(((fsbs) != 0 && (uint64_t)(fsbs) < (uint64_t)(bs)) ?	\
    360  1.63    martin 	    (int64_t)(num) / (int64_t)((bs) / (fsbs)) :		\
    361  1.85     lukem 	    (int64_t)(num) * (int64_t)((fsbs) / (bs)))
    362  1.15   mycroft 
    363  1.15   mycroft /*
    364  1.12       cgd  * Print out status about a filesystem.
    365  1.12       cgd  */
    366  1.87  christos static void
    367  1.95  christos prtstat(const struct statvfs *sfsp, int maxwidth)
    368  1.12       cgd {
    369  1.17   mycroft 	static long blocksize;
    370  1.12       cgd 	static int headerlen, timesthrough;
    371  1.68  christos 	static const char *header;
    372  1.87  christos 	static const char full[] = "100";
    373  1.87  christos 	static const char empty[] = "  0";
    374  1.62  christos 	int64_t used, availblks, inodes;
    375  1.60     enami 	int64_t bavail;
    376  1.87  christos 	char pb[64];
    377  1.95  christos 	char mntfromname[sizeof(sfsp->f_mntfromname) + 10];
    378  1.94  christos 
    379  1.95  christos 	if (Wflag && sfsp->f_mntfromlabel[0]) {
    380  1.94  christos 		snprintf(mntfromname, sizeof(mntfromname), "NAME=%s",
    381  1.95  christos 		    sfsp->f_mntfromlabel);
    382  1.94  christos 	} else {
    383  1.94  christos 		strlcpy(mntfromname, sfsp->f_mntfromname, sizeof(mntfromname));
    384  1.94  christos 	}
    385  1.12       cgd 
    386  1.73  christos 	if (gflag) {
    387  1.73  christos 		/*
    388  1.73  christos 		 * From SunOS-5.6:
    389  1.73  christos 		 *
    390  1.80  christos 		 * /var               (/dev/dsk/c0t0d0s3 ):         8192 block size          1024 frag size
    391  1.75  christos 		 *   984242 total blocks     860692 free blocks   859708 available         249984 total files
    392  1.80  christos 		 *   248691 free files      8388611 filesys id
    393  1.75  christos 		 *      ufs fstype       0x00000004 flag             255 filename length
    394  1.73  christos 		 *
    395  1.73  christos 		 */
    396  1.73  christos 		(void)printf("%10s (%-12s): %7ld block size %12ld frag size\n",
    397  1.94  christos 		    sfsp->f_mntonname, mntfromname,
    398  1.91  christos 		    sfsp->f_bsize,	/* On UFS/FFS systems this is
    399  1.73  christos 					 * also called the "optimal
    400  1.73  christos 					 * transfer block size" but it
    401  1.73  christos 					 * is of course the file
    402  1.73  christos 					 * system's block size too.
    403  1.73  christos 					 */
    404  1.91  christos 		    sfsp->f_frsize);	/* not so surprisingly the
    405  1.73  christos 					 * "fundamental file system
    406  1.73  christos 					 * block size" is the frag
    407  1.73  christos 					 * size.
    408  1.73  christos 					 */
    409  1.80  christos 		(void)printf("%10" PRId64 " total blocks %10" PRId64
    410  1.73  christos 		    " free blocks  %10" PRId64 " available\n",
    411  1.73  christos 		    (uint64_t)sfsp->f_blocks, (uint64_t)sfsp->f_bfree,
    412  1.73  christos 		    (uint64_t)sfsp->f_bavail);
    413  1.75  christos 		(void)printf("%10" PRId64 " total files  %10" PRId64
    414  1.73  christos 		    " free files %12lx filesys id\n",
    415  1.73  christos 		    (uint64_t)sfsp->f_ffree, (uint64_t)sfsp->f_files,
    416  1.73  christos 		    sfsp->f_fsid);
    417  1.75  christos 		(void)printf("%10s fstype  %#15lx flag  %17ld filename "
    418  1.73  christos 		    "length\n", sfsp->f_fstypename, sfsp->f_flag,
    419  1.73  christos 		    sfsp->f_namemax);
    420  1.73  christos 		(void)printf("%10lu owner %17" PRId64 " syncwrites %12" PRId64
    421  1.73  christos 		    " asyncwrites\n\n", (unsigned long)sfsp->f_owner,
    422  1.73  christos 		    sfsp->f_syncwrites, sfsp->f_asyncwrites);
    423  1.73  christos 
    424  1.73  christos 		/*
    425  1.73  christos 		 * a concession by the structured programming police to the
    426  1.73  christos 		 * indentation police....
    427  1.73  christos 		 */
    428  1.73  christos 		return;
    429  1.73  christos 	}
    430  1.73  christos 	if (maxwidth < 12)
    431  1.73  christos 		maxwidth = 12;
    432  1.12       cgd 	if (++timesthrough == 1) {
    433  1.71  christos 		switch (blocksize = usize) {
    434  1.71  christos 		case 1024:
    435  1.35    kleink 			header = Pflag ? "1024-blocks" : "1K-blocks";
    436  1.80  christos 			headerlen = (int)strlen(header);
    437  1.71  christos 			break;
    438  1.71  christos 		case 1024 * 1024:
    439  1.75  christos 			header = "1M-blocks";
    440  1.80  christos 			headerlen = (int)strlen(header);
    441  1.71  christos 			break;
    442  1.71  christos 		case 1024 * 1024 * 1024:
    443  1.75  christos 			header = "1G-blocks";
    444  1.80  christos 			headerlen = (int)strlen(header);
    445  1.71  christos 			break;
    446  1.71  christos 		default:
    447  1.71  christos 			if (hflag) {
    448  1.75  christos 				header = "Size";
    449  1.80  christos 				headerlen = (int)strlen(header);
    450  1.71  christos 			} else
    451  1.71  christos 				header = getbsize(&headerlen, &blocksize);
    452  1.71  christos 			break;
    453  1.71  christos 		}
    454  1.73  christos 		if (Pflag) {
    455  1.73  christos 			/*
    456  1.75  christos 			 * either:
    457  1.75  christos 			 *  "Filesystem 1024-blocks Used Available Capacity Mounted on\n"
    458  1.75  christos 			 * or:
    459  1.75  christos 			 *  "Filesystem 512-blocks Used Available Capacity Mounted on\n"
    460  1.73  christos 			 */
    461  1.80  christos 			if (blocksize != 1024 && blocksize != 512)
    462  1.80  christos 				errx(EXIT_FAILURE,
    463  1.80  christos 				    "non-standard block size incompatible with -P");
    464  1.73  christos 			(void)printf("Filesystem %s Used Available Capacity "
    465  1.73  christos 			    "Mounted on\n", header);
    466  1.73  christos 		} else {
    467  1.96       ryo 			(void)printf("%-*.*s %*s %*s %*s %%Cap",
    468  1.96       ryo 			    maxwidth, maxwidth, "Filesystem",
    469  1.96       ryo 			    WIDTH_BLKSIZE, header,
    470  1.96       ryo 			    WIDTH_BLKSIZE, "Used",
    471  1.96       ryo 			    WIDTH_BLKSIZE, "Avail");
    472  1.96       ryo 			if (iflag) {
    473  1.96       ryo 				(void)printf(" %*s %*s %%iCap",
    474  1.96       ryo 				    WIDTH_INODE, "iUsed",
    475  1.96       ryo 				    WIDTH_INODE, "iAvail");
    476  1.96       ryo 			}
    477  1.73  christos 			(void)printf(" Mounted on\n");
    478  1.73  christos 		}
    479  1.12       cgd 	}
    480  1.12       cgd 	used = sfsp->f_blocks - sfsp->f_bfree;
    481  1.62  christos 	bavail = sfsp->f_bfree - sfsp->f_bresvd;
    482  1.62  christos 	availblks = bavail + used;
    483  1.73  christos 	if (Pflag) {
    484  1.75  christos 		assert(hflag == 0);
    485  1.75  christos 		assert(blocksize > 0);
    486  1.73  christos 		/*
    487  1.73  christos 		 * "%s %d %d %d %s %s\n", <file system name>, <total space>,
    488  1.73  christos 		 * <space used>, <space free>, <percentage used>,
    489  1.80  christos 		 * <file system root>
    490  1.73  christos 		 */
    491  1.87  christos 		(void)printf("%s %" PRId64 " %" PRId64 " %" PRId64 " %s%% %s\n",
    492  1.94  christos 		    mntfromname,
    493  1.86   mlelstv 		    fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
    494  1.86   mlelstv 		    fsbtoblk(used, sfsp->f_frsize, blocksize),
    495  1.86   mlelstv 		    fsbtoblk(bavail, sfsp->f_frsize, blocksize),
    496  1.90  christos 		    availblks == 0 ? full : strspct(pb, sizeof(pb), used,
    497  1.87  christos 		    availblks, 0), sfsp->f_mntonname);
    498  1.73  christos 		/*
    499  1.73  christos 		 * another concession by the structured programming police to
    500  1.73  christos 		 * the indentation police....
    501  1.73  christos 		 *
    502  1.73  christos 		 * Note iflag cannot be set when Pflag is set.
    503  1.73  christos 		 */
    504  1.73  christos 		return;
    505  1.73  christos 	}
    506  1.73  christos 
    507  1.94  christos 	(void)printf("%-*.*s ", maxwidth, maxwidth, mntfromname);
    508  1.73  christos 
    509  1.44    provos 	if (hflag)
    510  1.65     enami 		prthuman(sfsp, used, bavail);
    511  1.61     enami 	else
    512  1.96       ryo 		(void)printf("%*" PRId64 " %*" PRId64 " %*" PRId64,
    513  1.96       ryo 		    WIDTH_BLKSIZE,
    514  1.62  christos 		    fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
    515  1.96       ryo 		    WIDTH_BLKSIZE, fsbtoblk(used, sfsp->f_frsize, blocksize),
    516  1.96       ryo 		    WIDTH_BLKSIZE, fsbtoblk(bavail, sfsp->f_frsize, blocksize));
    517  1.87  christos 	(void)printf(" %3s%%",
    518  1.66     enami 	    availblks == 0 ? full :
    519  1.90  christos 	    strspct(pb, sizeof(pb), used, availblks, 0));
    520  1.12       cgd 	if (iflag) {
    521  1.12       cgd 		inodes = sfsp->f_files;
    522  1.12       cgd 		used = inodes - sfsp->f_ffree;
    523  1.96       ryo 		(void)printf(" %*jd %*jd %4s%%",
    524  1.96       ryo 		    WIDTH_INODE, (intmax_t)used,
    525  1.96       ryo 		    WIDTH_INODE, (intmax_t)sfsp->f_ffree,
    526  1.57     enami 		    inodes == 0 ? (used == 0 ? empty : full) :
    527  1.90  christos 		    strspct(pb, sizeof(pb), used, inodes, 0));
    528  1.73  christos 	}
    529  1.73  christos 	(void)printf(" %s\n", sfsp->f_mntonname);
    530  1.12       cgd }
    531  1.12       cgd 
    532  1.87  christos static void
    533  1.41     enami usage(void)
    534  1.12       cgd {
    535  1.41     enami 
    536  1.34  christos 	(void)fprintf(stderr,
    537  1.94  christos 	    "Usage: %s [-aglnW] [-Ghkm|-ihkm|-Pk] [-t type] [file | "
    538  1.73  christos 	    "file_system ...]\n",
    539  1.40       cgd 	    getprogname());
    540  1.12       cgd 	exit(1);
    541  1.30   mycroft 	/* NOTREACHED */
    542  1.12       cgd }
    543