Home | History | Annotate | Line # | Download | only in df
df.c revision 1.75
      1  1.75  christos /*	$NetBSD: df.c,v 1.75 2007/07/16 14:39:53 christos 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.15   mycroft "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
     41  1.24   thorpej 	The Regents of the University of California.  All rights reserved.\n");
     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.75  christos __RCSID("$NetBSD: df.c,v 1.75 2007/07/16 14:39:53 christos 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.12       cgd #include <fcntl.h>
     60  1.44    provos #include <util.h>
     61  1.12       cgd #include <stdio.h>
     62  1.12       cgd #include <stdlib.h>
     63  1.12       cgd #include <string.h>
     64  1.12       cgd #include <unistd.h>
     65  1.12       cgd 
     66  1.41     enami extern char *strpct(u_long, u_long, u_int);
     67  1.26      fair 
     68  1.41     enami int	 main(int, char *[]);
     69  1.41     enami int	 bread(off_t, void *, int);
     70  1.41     enami char	*getmntpt(char *);
     71  1.62  christos void	 prtstat(struct statvfs *, int);
     72  1.41     enami int	 selected(const char *);
     73  1.41     enami void	 maketypelist(char *);
     74  1.62  christos long	 regetmntinfo(struct statvfs **, long);
     75  1.41     enami void	 usage(void);
     76  1.68  christos void	 prthumanval(int64_t, const char *);
     77  1.62  christos void	 prthuman(struct statvfs *, int64_t, int64_t);
     78  1.66     enami const char *
     79  1.66     enami 	strpct64(uint64_t, uint64_t, u_int);
     80  1.12       cgd 
     81  1.73  christos int	aflag, gflag, hflag, iflag, lflag, nflag, Pflag;
     82  1.73  christos long	usize = 0;
     83  1.17   mycroft char	**typelist = NULL;
     84  1.12       cgd 
     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.17   mycroft 	long mntsize;
     91  1.17   mycroft 	int ch, i, maxwidth, width;
     92  1.12       cgd 	char *mntpt;
     93  1.12       cgd 
     94  1.73  christos 	while ((ch = getopt(argc, argv, "aGghiklmnPt:")) != -1)
     95  1.15   mycroft 		switch (ch) {
     96  1.34  christos 		case 'a':
     97  1.34  christos 			aflag = 1;
     98  1.34  christos 			break;
     99  1.73  christos 		case 'G':
    100  1.71  christos 			hflag = 0;
    101  1.71  christos 			usize = 1024 * 1024 * 1024;
    102  1.46     grant 			break;
    103  1.73  christos 		case 'g':
    104  1.73  christos 			gflag = 1;
    105  1.73  christos 			break;
    106  1.44    provos 		case 'h':
    107  1.44    provos 			hflag = 1;
    108  1.71  christos 			usize = 0;
    109  1.44    provos 			break;
    110  1.12       cgd 		case 'i':
    111  1.12       cgd 			iflag = 1;
    112  1.12       cgd 			break;
    113  1.12       cgd 		case 'k':
    114  1.71  christos 			hflag = 0;
    115  1.71  christos 			usize = 1024;
    116  1.12       cgd 			break;
    117  1.12       cgd 		case 'l':
    118  1.17   mycroft 			lflag = 1;
    119  1.12       cgd 			break;
    120  1.36   hubertf 		case 'm':
    121  1.71  christos 			hflag = 0;
    122  1.71  christos 			usize = 1024 * 1024;
    123  1.36   hubertf 			break;
    124  1.12       cgd 		case 'n':
    125  1.12       cgd 			nflag = 1;
    126  1.12       cgd 			break;
    127  1.35    kleink 		case 'P':
    128  1.35    kleink 			Pflag = 1;
    129  1.35    kleink 			break;
    130  1.16   mycroft 		case 't':
    131  1.17   mycroft 			if (typelist != NULL)
    132  1.17   mycroft 				errx(1, "only one -t option may be specified.");
    133  1.17   mycroft 			maketypelist(optarg);
    134  1.16   mycroft 			break;
    135  1.12       cgd 		case '?':
    136  1.12       cgd 		default:
    137  1.12       cgd 			usage();
    138  1.12       cgd 		}
    139  1.73  christos 
    140  1.73  christos 	if (gflag && (Pflag || iflag))
    141  1.73  christos 		errx(1, "only one of -g and -P or -i may be specified");
    142  1.73  christos 	if (Pflag && iflag)
    143  1.73  christos 		errx(1, "only one of -P and -i may be specified");
    144  1.75  christos 	if (Pflag && (hflag || (usize != 1024 && usize != 512)))
    145  1.75  christos 		errx(1, "non-standard block size incompatible with -P");
    146  1.73  christos 
    147  1.12       cgd 	argc -= optind;
    148  1.12       cgd 	argv += optind;
    149  1.12       cgd 
    150  1.17   mycroft 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    151  1.12       cgd 	if (mntsize == 0)
    152  1.41     enami 		err(1, "retrieving information on mounted file systems");
    153  1.15   mycroft 
    154  1.41     enami 	if (*argv == NULL) {
    155  1.17   mycroft 		mntsize = regetmntinfo(&mntbuf, mntsize);
    156  1.17   mycroft 	} else {
    157  1.70    rumble 		if ((mntbuf = malloc(argc * sizeof(*mntbuf))) == NULL)
    158  1.70    rumble 			err(1, "can't allocate statvfs array");
    159  1.17   mycroft 		mntsize = 0;
    160  1.41     enami 		for (; *argv != NULL; argv++) {
    161  1.17   mycroft 			if (stat(*argv, &stbuf) < 0) {
    162  1.17   mycroft 				if ((mntpt = getmntpt(*argv)) == 0) {
    163  1.17   mycroft 					warn("%s", *argv);
    164  1.12       cgd 					continue;
    165  1.12       cgd 				}
    166  1.18   mycroft 			} else if (S_ISBLK(stbuf.st_mode)) {
    167  1.43     soren 				if ((mntpt = getmntpt(*argv)) == 0)
    168  1.43     soren 					mntpt = *argv;
    169  1.17   mycroft 			} else
    170  1.17   mycroft 				mntpt = *argv;
    171  1.17   mycroft 			/*
    172  1.17   mycroft 			 * Statfs does not take a `wait' flag, so we cannot
    173  1.17   mycroft 			 * implement nflag here.
    174  1.17   mycroft 			 */
    175  1.62  christos 			if (!statvfs(mntpt, &mntbuf[mntsize]))
    176  1.23   thorpej 				if (lflag &&
    177  1.62  christos 				    (mntbuf[mntsize].f_flag & MNT_LOCAL) == 0)
    178  1.23   thorpej 					warnx("Warning: %s is not a local %s",
    179  1.23   thorpej 					    *argv, "file system");
    180  1.23   thorpej 				else if
    181  1.23   thorpej 				    (!selected(mntbuf[mntsize].f_fstypename))
    182  1.23   thorpej 					warnx("Warning: %s mounted as a %s %s",
    183  1.23   thorpej 					    *argv,
    184  1.23   thorpej 					    mntbuf[mntsize].f_fstypename,
    185  1.23   thorpej 					    "file system");
    186  1.23   thorpej 				else
    187  1.23   thorpej 					++mntsize;
    188  1.17   mycroft 			else
    189  1.17   mycroft 				warn("%s", *argv);
    190  1.12       cgd 		}
    191  1.12       cgd 	}
    192  1.17   mycroft 
    193  1.17   mycroft 	maxwidth = 0;
    194  1.17   mycroft 	for (i = 0; i < mntsize; i++) {
    195  1.17   mycroft 		width = strlen(mntbuf[i].f_mntfromname);
    196  1.17   mycroft 		if (width > maxwidth)
    197  1.17   mycroft 			maxwidth = width;
    198  1.17   mycroft 	}
    199  1.17   mycroft 	for (i = 0; i < mntsize; i++)
    200  1.17   mycroft 		prtstat(&mntbuf[i], maxwidth);
    201  1.17   mycroft 	exit(0);
    202  1.29   mycroft 	/* NOTREACHED */
    203  1.12       cgd }
    204  1.12       cgd 
    205  1.12       cgd char *
    206  1.41     enami getmntpt(char *name)
    207  1.12       cgd {
    208  1.17   mycroft 	long mntsize, i;
    209  1.62  christos 	struct statvfs *mntbuf;
    210  1.12       cgd 
    211  1.17   mycroft 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    212  1.12       cgd 	for (i = 0; i < mntsize; i++) {
    213  1.12       cgd 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    214  1.12       cgd 			return (mntbuf[i].f_mntonname);
    215  1.12       cgd 	}
    216  1.12       cgd 	return (0);
    217  1.12       cgd }
    218  1.12       cgd 
    219  1.17   mycroft static enum { IN_LIST, NOT_IN_LIST } which;
    220  1.17   mycroft 
    221  1.17   mycroft int
    222  1.41     enami selected(const char *type)
    223  1.17   mycroft {
    224  1.17   mycroft 	char **av;
    225  1.17   mycroft 
    226  1.17   mycroft 	/* If no type specified, it's always selected. */
    227  1.17   mycroft 	if (typelist == NULL)
    228  1.17   mycroft 		return (1);
    229  1.17   mycroft 	for (av = typelist; *av != NULL; ++av)
    230  1.20       cgd 		if (!strncmp(type, *av, MFSNAMELEN))
    231  1.17   mycroft 			return (which == IN_LIST ? 1 : 0);
    232  1.17   mycroft 	return (which == IN_LIST ? 0 : 1);
    233  1.17   mycroft }
    234  1.16   mycroft 
    235  1.16   mycroft void
    236  1.41     enami maketypelist(char *fslist)
    237  1.16   mycroft {
    238  1.17   mycroft 	int i;
    239  1.17   mycroft 	char *nextcp, **av;
    240  1.16   mycroft 
    241  1.17   mycroft 	if ((fslist == NULL) || (fslist[0] == '\0'))
    242  1.17   mycroft 		errx(1, "empty type list");
    243  1.16   mycroft 
    244  1.17   mycroft 	/*
    245  1.17   mycroft 	 * XXX
    246  1.17   mycroft 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    247  1.17   mycroft 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    248  1.17   mycroft 	 */
    249  1.17   mycroft 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    250  1.17   mycroft 		fslist += 2;
    251  1.17   mycroft 		which = NOT_IN_LIST;
    252  1.17   mycroft 	} else
    253  1.17   mycroft 		which = IN_LIST;
    254  1.17   mycroft 
    255  1.17   mycroft 	/* Count the number of types. */
    256  1.24   thorpej 	for (i = 1, nextcp = fslist;
    257  1.24   thorpej 	    (nextcp = strchr(nextcp, ',')) != NULL; i++)
    258  1.17   mycroft 		++nextcp;
    259  1.17   mycroft 
    260  1.17   mycroft 	/* Build an array of that many types. */
    261  1.17   mycroft 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    262  1.24   thorpej 		err(1, "can't allocate type array");
    263  1.17   mycroft 	av[0] = fslist;
    264  1.24   thorpej 	for (i = 1, nextcp = fslist;
    265  1.24   thorpej 	    (nextcp = strchr(nextcp, ',')) != NULL; i++) {
    266  1.17   mycroft 		*nextcp = '\0';
    267  1.17   mycroft 		av[i] = ++nextcp;
    268  1.16   mycroft 	}
    269  1.17   mycroft 	/* Terminate the array. */
    270  1.17   mycroft 	av[i] = NULL;
    271  1.17   mycroft }
    272  1.16   mycroft 
    273  1.17   mycroft /*
    274  1.17   mycroft  * Make a pass over the filesystem info in ``mntbuf'' filtering out
    275  1.17   mycroft  * filesystem types not in ``fsmask'' and possibly re-stating to get
    276  1.62  christos  * current (not cached) info.  Returns the new count of valid statvfs bufs.
    277  1.17   mycroft  */
    278  1.17   mycroft long
    279  1.62  christos regetmntinfo(struct statvfs **mntbufp, long mntsize)
    280  1.17   mycroft {
    281  1.17   mycroft 	int i, j;
    282  1.62  christos 	struct statvfs *mntbuf;
    283  1.16   mycroft 
    284  1.42  christos 	if (!lflag && typelist == NULL && aflag)
    285  1.17   mycroft 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
    286  1.16   mycroft 
    287  1.17   mycroft 	mntbuf = *mntbufp;
    288  1.17   mycroft 	j = 0;
    289  1.17   mycroft 	for (i = 0; i < mntsize; i++) {
    290  1.62  christos 		if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0)
    291  1.34  christos 			continue;
    292  1.62  christos 		if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0)
    293  1.17   mycroft 			continue;
    294  1.17   mycroft 		if (!selected(mntbuf[i].f_fstypename))
    295  1.17   mycroft 			continue;
    296  1.17   mycroft 		if (nflag)
    297  1.17   mycroft 			mntbuf[j] = mntbuf[i];
    298  1.32  sommerfe 		else {
    299  1.62  christos 			struct statvfs layerbuf = mntbuf[i];
    300  1.62  christos 			(void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]);
    301  1.32  sommerfe 			/*
    302  1.32  sommerfe 			 * If the FS name changed, then new data is for
    303  1.32  sommerfe 			 * a different layer and we don't want it.
    304  1.32  sommerfe 			 */
    305  1.41     enami 			if (memcmp(layerbuf.f_mntfromname,
    306  1.41     enami 			    mntbuf[j].f_mntfromname, MNAMELEN))
    307  1.41     enami 				mntbuf[j] = layerbuf;
    308  1.32  sommerfe 		}
    309  1.17   mycroft 		j++;
    310  1.16   mycroft 	}
    311  1.17   mycroft 	return (j);
    312  1.16   mycroft }
    313  1.16   mycroft 
    314  1.44    provos void
    315  1.68  christos prthumanval(int64_t bytes, const char *pad)
    316  1.44    provos {
    317  1.59     enami 	char buf[6];
    318  1.44    provos 
    319  1.59     enami 	humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
    320  1.59     enami 	    bytes, "", HN_AUTOSCALE,
    321  1.44    provos 	    HN_B | HN_NOSPACE | HN_DECIMAL);
    322  1.44    provos 
    323  1.44    provos 	(void)printf("%s %6s", pad, buf);
    324  1.44    provos }
    325  1.44    provos 
    326  1.44    provos void
    327  1.62  christos prthuman(struct statvfs *sfsp, int64_t used, int64_t bavail)
    328  1.44    provos {
    329  1.60     enami 
    330  1.74  christos 	prthumanval(sfsp->f_blocks * sfsp->f_frsize, "   ");
    331  1.74  christos 	prthumanval(used * sfsp->f_frsize, "    ");
    332  1.74  christos 	prthumanval(bavail * sfsp->f_frsize, "    ");
    333  1.44    provos }
    334  1.44    provos 
    335  1.12       cgd /*
    336  1.62  christos  * Convert statvfs returned filesystem size into BLOCKSIZE units.
    337  1.15   mycroft  * Attempts to avoid overflow for large filesystems.
    338  1.15   mycroft  */
    339  1.41     enami #define fsbtoblk(num, fsbs, bs)					\
    340  1.41     enami 	(((fsbs) != 0 && (fsbs) < (bs)) ?			\
    341  1.63    martin 	    (int64_t)(num) / (int64_t)((bs) / (fsbs)) :		\
    342  1.56     enami 	    (int64_t)(num) * ((fsbs) / (bs)))
    343  1.15   mycroft 
    344  1.15   mycroft /*
    345  1.12       cgd  * Print out status about a filesystem.
    346  1.12       cgd  */
    347  1.12       cgd void
    348  1.62  christos prtstat(struct statvfs *sfsp, int maxwidth)
    349  1.12       cgd {
    350  1.17   mycroft 	static long blocksize;
    351  1.12       cgd 	static int headerlen, timesthrough;
    352  1.68  christos 	static const char *header;
    353  1.58     enami 	static const char full[] = "100%";
    354  1.58     enami 	static const char empty[] = "  0%";
    355  1.62  christos 	int64_t used, availblks, inodes;
    356  1.60     enami 	int64_t bavail;
    357  1.12       cgd 
    358  1.73  christos 	if (gflag) {
    359  1.73  christos 		/*
    360  1.73  christos 		 * From SunOS-5.6:
    361  1.73  christos 		 *
    362  1.75  christos 		 * /var               (/dev/dsk/c0t0d0s3 ):         8192 block size          1024 frag size
    363  1.75  christos 		 *   984242 total blocks     860692 free blocks   859708 available         249984 total files
    364  1.75  christos 		 *   248691 free files      8388611 filesys id
    365  1.75  christos 		 *      ufs fstype       0x00000004 flag             255 filename length
    366  1.73  christos 		 *
    367  1.73  christos 		 */
    368  1.73  christos 		(void)printf("%10s (%-12s): %7ld block size %12ld frag size\n",
    369  1.73  christos 		    sfsp->f_mntonname, sfsp->f_mntfromname,
    370  1.73  christos 		    sfsp->f_iosize,	/* On UFS/FFS systems this is
    371  1.73  christos 					 * also called the "optimal
    372  1.73  christos 					 * transfer block size" but it
    373  1.73  christos 					 * is of course the file
    374  1.73  christos 					 * system's block size too.
    375  1.73  christos 					 */
    376  1.73  christos 		    sfsp->f_bsize);	/* not so surprisingly the
    377  1.73  christos 					 * "fundamental file system
    378  1.73  christos 					 * block size" is the frag
    379  1.73  christos 					 * size.
    380  1.73  christos 					 */
    381  1.73  christos 		(void)printf("%10" PRId64 " total blocks %10" PRId64
    382  1.73  christos 		    " free blocks  %10" PRId64 " available\n",
    383  1.73  christos 		    (uint64_t)sfsp->f_blocks, (uint64_t)sfsp->f_bfree,
    384  1.73  christos 		    (uint64_t)sfsp->f_bavail);
    385  1.75  christos 		(void)printf("%10" PRId64 " total files  %10" PRId64
    386  1.73  christos 		    " free files %12lx filesys id\n",
    387  1.73  christos 		    (uint64_t)sfsp->f_ffree, (uint64_t)sfsp->f_files,
    388  1.73  christos 		    sfsp->f_fsid);
    389  1.75  christos 		(void)printf("%10s fstype  %#15lx flag  %17ld filename "
    390  1.73  christos 		    "length\n", sfsp->f_fstypename, sfsp->f_flag,
    391  1.73  christos 		    sfsp->f_namemax);
    392  1.73  christos 		(void)printf("%10lu owner %17" PRId64 " syncwrites %12" PRId64
    393  1.73  christos 		    " asyncwrites\n\n", (unsigned long)sfsp->f_owner,
    394  1.73  christos 		    sfsp->f_syncwrites, sfsp->f_asyncwrites);
    395  1.73  christos 
    396  1.73  christos 		/*
    397  1.73  christos 		 * a concession by the structured programming police to the
    398  1.73  christos 		 * indentation police....
    399  1.73  christos 		 */
    400  1.73  christos 		return;
    401  1.73  christos 	}
    402  1.73  christos 	if (maxwidth < 12)
    403  1.73  christos 		maxwidth = 12;
    404  1.12       cgd 	if (++timesthrough == 1) {
    405  1.71  christos 		switch (blocksize = usize) {
    406  1.71  christos 		case 1024:
    407  1.35    kleink 			header = Pflag ? "1024-blocks" : "1K-blocks";
    408  1.12       cgd 			headerlen = strlen(header);
    409  1.71  christos 			break;
    410  1.71  christos 		case 1024 * 1024:
    411  1.75  christos 			header = "1M-blocks";
    412  1.36   hubertf 			headerlen = strlen(header);
    413  1.71  christos 			break;
    414  1.71  christos 		case 1024 * 1024 * 1024:
    415  1.75  christos 			header = "1G-blocks";
    416  1.46     grant 			headerlen = strlen(header);
    417  1.71  christos 			break;
    418  1.71  christos 		default:
    419  1.71  christos 			if (hflag) {
    420  1.75  christos 				header = "Size";
    421  1.71  christos 				headerlen = strlen(header);
    422  1.71  christos 			} else
    423  1.71  christos 				header = getbsize(&headerlen, &blocksize);
    424  1.71  christos 			break;
    425  1.71  christos 		}
    426  1.73  christos 		if (Pflag) {
    427  1.73  christos 			/*
    428  1.75  christos 			 * either:
    429  1.75  christos 			 *  "Filesystem 1024-blocks Used Available Capacity Mounted on\n"
    430  1.75  christos 			 * or:
    431  1.75  christos 			 *  "Filesystem 512-blocks Used Available Capacity Mounted on\n"
    432  1.73  christos 			 */
    433  1.73  christos 			(void)printf("Filesystem %s Used Available Capacity "
    434  1.73  christos 			    "Mounted on\n", header);
    435  1.73  christos 		} else {
    436  1.74  christos 			(void)printf("%-*.*s %s       Used      Avail %%Cap",
    437  1.73  christos 			    maxwidth - (headerlen - 9),
    438  1.73  christos 			    maxwidth - (headerlen - 9),
    439  1.73  christos 			    "Filesystem", header);
    440  1.73  christos 			if (iflag)
    441  1.74  christos 				(void)printf("    iUsed   iAvail %%iCap");
    442  1.73  christos 			(void)printf(" Mounted on\n");
    443  1.73  christos 		}
    444  1.12       cgd 	}
    445  1.12       cgd 	used = sfsp->f_blocks - sfsp->f_bfree;
    446  1.62  christos 	bavail = sfsp->f_bfree - sfsp->f_bresvd;
    447  1.62  christos 	availblks = bavail + used;
    448  1.73  christos 	if (Pflag) {
    449  1.75  christos 		assert(hflag == 0);
    450  1.75  christos 		assert(blocksize > 0);
    451  1.73  christos 		/*
    452  1.73  christos 		 * "%s %d %d %d %s %s\n", <file system name>, <total space>,
    453  1.73  christos 		 * <space used>, <space free>, <percentage used>,
    454  1.73  christos 		 * <file system root>
    455  1.73  christos 		 */
    456  1.73  christos 		(void)printf("%s %" PRId64 " %" PRId64 " %" PRId64 " %s %s\n",
    457  1.73  christos 		    sfsp->f_mntfromname,
    458  1.73  christos 		    fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
    459  1.73  christos 		    fsbtoblk(used, sfsp->f_bsize, blocksize),
    460  1.73  christos 		    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize),
    461  1.73  christos 		    availblks == 0 ? full : strpct64((uint64_t) used,
    462  1.73  christos 		    (uint64_t) availblks, 0), sfsp->f_mntonname);
    463  1.73  christos 		/*
    464  1.73  christos 		 * another concession by the structured programming police to
    465  1.73  christos 		 * the indentation police....
    466  1.73  christos 		 *
    467  1.73  christos 		 * Note iflag cannot be set when Pflag is set.
    468  1.73  christos 		 */
    469  1.73  christos 		return;
    470  1.73  christos 	}
    471  1.73  christos 
    472  1.73  christos 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
    473  1.73  christos 
    474  1.44    provos 	if (hflag)
    475  1.65     enami 		prthuman(sfsp, used, bavail);
    476  1.61     enami 	else
    477  1.73  christos 		(void)printf("%10" PRId64 " %10" PRId64 " %10" PRId64,
    478  1.62  christos 		    fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
    479  1.62  christos 		    fsbtoblk(used, sfsp->f_frsize, blocksize),
    480  1.62  christos 		    fsbtoblk(bavail, sfsp->f_frsize, blocksize));
    481  1.73  christos 	(void)printf(" %4s",
    482  1.66     enami 	    availblks == 0 ? full :
    483  1.66     enami 	    /* We know that these values are never negative */
    484  1.66     enami 	    strpct64((uint64_t)used, (uint64_t)availblks, 0));
    485  1.12       cgd 	if (iflag) {
    486  1.12       cgd 		inodes = sfsp->f_files;
    487  1.12       cgd 		used = inodes - sfsp->f_ffree;
    488  1.73  christos 		(void)printf(" %8ld %8ld %4s",
    489  1.64     enami 		    (u_long)used, (u_long)sfsp->f_ffree,
    490  1.57     enami 		    inodes == 0 ? (used == 0 ? empty : full) :
    491  1.66     enami 		    strpct64((uint64_t)used, (uint64_t)inodes, 0));
    492  1.73  christos 	}
    493  1.73  christos 	(void)printf(" %s\n", sfsp->f_mntonname);
    494  1.12       cgd }
    495  1.12       cgd 
    496  1.12       cgd void
    497  1.41     enami usage(void)
    498  1.12       cgd {
    499  1.41     enami 
    500  1.34  christos 	(void)fprintf(stderr,
    501  1.75  christos 	    "Usage: %s [-aGgln] [-hkm|-ihkm|-Pk] [-t type] [file | "
    502  1.73  christos 	    "file_system ...]\n",
    503  1.40       cgd 	    getprogname());
    504  1.12       cgd 	exit(1);
    505  1.30   mycroft 	/* NOTREACHED */
    506  1.12       cgd }
    507  1.66     enami 
    508  1.66     enami const char *
    509  1.66     enami strpct64(uint64_t numerator, uint64_t denominator, u_int digits)
    510  1.66     enami {
    511  1.66     enami 
    512  1.66     enami 	while (denominator > ULONG_MAX) {
    513  1.66     enami 		numerator >>= 1;
    514  1.66     enami 		denominator >>= 1;
    515  1.66     enami 	}
    516  1.66     enami 	return (strpct((u_long)numerator, (u_long)denominator, digits));
    517  1.66     enami }
    518