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