xref: /freebsd/bin/df/df.c (revision c6f13844f606d75d7d19ba6a901e410f95eee09c)
14b88c807SRodney W. Grimes /*
24b88c807SRodney W. Grimes  * Copyright (c) 1980, 1990, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
54b88c807SRodney W. Grimes  * All or some portions of this file are derived from material licensed
64b88c807SRodney W. Grimes  * to the University of California by American Telephone and Telegraph
74b88c807SRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
84b88c807SRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
184b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
3509a80d48SDavid E. O'Brien #if 0
364b88c807SRodney W. Grimes #ifndef lint
3716cc192aSSteve Price static const char copyright[] =
384b88c807SRodney W. Grimes "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
394b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
404b88c807SRodney W. Grimes #endif /* not lint */
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #ifndef lint
4316cc192aSSteve Price static char sccsid[] = "@(#)df.c	8.9 (Berkeley) 5/8/95";
444b88c807SRodney W. Grimes #endif /* not lint */
4509a80d48SDavid E. O'Brien #endif
465eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
475eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes #include <sys/param.h>
504b88c807SRodney W. Grimes #include <sys/stat.h>
514b88c807SRodney W. Grimes #include <sys/mount.h>
52a25695c3SJim Pirzyk #include <sys/sysctl.h>
53a78192e3SBruce Evans #include <ufs/ufs/ufsmount.h>
544b88c807SRodney W. Grimes #include <err.h>
55dd6d33e8SMichael Haro #include <math.h>
560bd9f151SIan Dowse #include <stdint.h>
574b88c807SRodney W. Grimes #include <stdio.h>
584b88c807SRodney W. Grimes #include <stdlib.h>
594b88c807SRodney W. Grimes #include <string.h>
60dd6d33e8SMichael Haro #include <sysexits.h>
614b88c807SRodney W. Grimes #include <unistd.h>
624b88c807SRodney W. Grimes 
63532aff98SPoul-Henning Kamp #include "extern.h"
64532aff98SPoul-Henning Kamp 
65dd6d33e8SMichael Haro #define UNITS_SI 1
66dd6d33e8SMichael Haro #define UNITS_2 2
67dd6d33e8SMichael Haro 
68dd6d33e8SMichael Haro #define KILO_SZ(n) (n)
69dd6d33e8SMichael Haro #define MEGA_SZ(n) ((n) * (n))
70dd6d33e8SMichael Haro #define GIGA_SZ(n) ((n) * (n) * (n))
71dd6d33e8SMichael Haro #define TERA_SZ(n) ((n) * (n) * (n) * (n))
72dd6d33e8SMichael Haro #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
73dd6d33e8SMichael Haro 
74dd6d33e8SMichael Haro #define KILO_2_SZ (KILO_SZ(1024ULL))
75dd6d33e8SMichael Haro #define MEGA_2_SZ (MEGA_SZ(1024ULL))
76dd6d33e8SMichael Haro #define GIGA_2_SZ (GIGA_SZ(1024ULL))
77dd6d33e8SMichael Haro #define TERA_2_SZ (TERA_SZ(1024ULL))
78dd6d33e8SMichael Haro #define PETA_2_SZ (PETA_SZ(1024ULL))
79dd6d33e8SMichael Haro 
80dd6d33e8SMichael Haro #define KILO_SI_SZ (KILO_SZ(1000ULL))
81dd6d33e8SMichael Haro #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
82dd6d33e8SMichael Haro #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
83dd6d33e8SMichael Haro #define TERA_SI_SZ (TERA_SZ(1000ULL))
84dd6d33e8SMichael Haro #define PETA_SI_SZ (PETA_SZ(1000ULL))
85dd6d33e8SMichael Haro 
8662edbd31SIan Dowse /* Maximum widths of various fields. */
8762edbd31SIan Dowse struct maxwidths {
880bd9f151SIan Dowse 	int	mntfrom;
890bd9f151SIan Dowse 	int	total;
900bd9f151SIan Dowse 	int	used;
910bd9f151SIan Dowse 	int	avail;
920bd9f151SIan Dowse 	int	iused;
930bd9f151SIan Dowse 	int	ifree;
9462edbd31SIan Dowse };
9562edbd31SIan Dowse 
96b7dbd3e9SMark Murray static uintmax_t vals_si [] = {
97b7dbd3e9SMark Murray 	1,
98b7dbd3e9SMark Murray 	KILO_SI_SZ,
99b7dbd3e9SMark Murray 	MEGA_SI_SZ,
100b7dbd3e9SMark Murray 	GIGA_SI_SZ,
101b7dbd3e9SMark Murray 	TERA_SI_SZ,
102b7dbd3e9SMark Murray 	PETA_SI_SZ
103b7dbd3e9SMark Murray };
104b7dbd3e9SMark Murray static uintmax_t vals_base2[] = {
105b7dbd3e9SMark Murray 	1,
106b7dbd3e9SMark Murray 	KILO_2_SZ,
107b7dbd3e9SMark Murray 	MEGA_2_SZ,
108b7dbd3e9SMark Murray 	GIGA_2_SZ,
109b7dbd3e9SMark Murray 	TERA_2_SZ,
110b7dbd3e9SMark Murray 	PETA_2_SZ
111b7dbd3e9SMark Murray };
112b7dbd3e9SMark Murray static uintmax_t *valp;
113dd6d33e8SMichael Haro 
114dd6d33e8SMichael Haro typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
115dd6d33e8SMichael Haro 
116b7dbd3e9SMark Murray static unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
117dd6d33e8SMichael Haro 
118c6f13844SDavid E. O'Brien static void	  addstat(struct statfs *, struct statfs *);
119b7dbd3e9SMark Murray static char	 *getmntpt(const char *);
1200bd9f151SIan Dowse static int	  int64width(int64_t);
121532aff98SPoul-Henning Kamp static char	 *makenetvfslist(void);
122fde81c7dSKirk McKusick static void	  prthuman(const struct statfs *, int64_t);
123532aff98SPoul-Henning Kamp static void	  prthumanval(double);
124532aff98SPoul-Henning Kamp static void	  prtstat(struct statfs *, struct maxwidths *);
125be2c4e54SDavid E. O'Brien static size_t	  regetmntinfo(struct statfs **, long, const char **);
126532aff98SPoul-Henning Kamp static unit_t	  unit_adjust(double *);
127b7dbd3e9SMark Murray static void	  update_maxwidths(struct maxwidths *, const struct statfs *);
128532aff98SPoul-Henning Kamp static void	  usage(void);
1294b88c807SRodney W. Grimes 
1300bd9f151SIan Dowse static __inline int
1310bd9f151SIan Dowse imax(int a, int b)
13262edbd31SIan Dowse {
133b7dbd3e9SMark Murray 	return (a > b ? a : b);
13462edbd31SIan Dowse }
13562edbd31SIan Dowse 
136076419d2SDavid E. O'Brien static int	aflag = 0, cflag, hflag, iflag, nflag;
137532aff98SPoul-Henning Kamp static struct	ufs_args mdev;
138532aff98SPoul-Henning Kamp 
1394b88c807SRodney W. Grimes int
140f9bcb0beSWarner Losh main(int argc, char *argv[])
1414b88c807SRodney W. Grimes {
1424b88c807SRodney W. Grimes 	struct stat stbuf;
143c6f13844SDavid E. O'Brien 	struct statfs statfsbuf, totalbuf;
14462edbd31SIan Dowse 	struct maxwidths maxwidths;
145c6f13844SDavid E. O'Brien 	struct statfs *mntbuf;
146a95a13bbSKris Kennaway 	const char *fstype;
147532aff98SPoul-Henning Kamp 	char *mntpath, *mntpt;
148532aff98SPoul-Henning Kamp 	const char **vfslist;
149be2c4e54SDavid E. O'Brien 	size_t i, mntsize;
150be2c4e54SDavid E. O'Brien 	int ch, rv;
151f3895a82SKris Kennaway 
152f3895a82SKris Kennaway 	fstype = "ufs";
1534b88c807SRodney W. Grimes 
154076419d2SDavid E. O'Brien 	memset(&totalbuf, 0, sizeof(totalbuf));
155076419d2SDavid E. O'Brien 	totalbuf.f_bsize = DEV_BSIZE;
156076419d2SDavid E. O'Brien 	strncpy(totalbuf.f_mntfromname, "total", MNAMELEN);
157a78192e3SBruce Evans 	vfslist = NULL;
158076419d2SDavid E. O'Brien 	while ((ch = getopt(argc, argv, "abcgHhiklmnPt:")) != -1)
1594b88c807SRodney W. Grimes 		switch (ch) {
1605b42dac8SJulian Elischer 		case 'a':
1615b42dac8SJulian Elischer 			aflag = 1;
1625b42dac8SJulian Elischer 			break;
163dd6d33e8SMichael Haro 		case 'b':
164dd6d33e8SMichael Haro 				/* FALLTHROUGH */
165dd6d33e8SMichael Haro 		case 'P':
166dd6d33e8SMichael Haro 			putenv("BLOCKSIZE=512");
167dd6d33e8SMichael Haro 			hflag = 0;
168dd6d33e8SMichael Haro 			break;
169c6f13844SDavid E. O'Brien 		case 'c':
170c6f13844SDavid E. O'Brien 			cflag = 1;
171c6f13844SDavid E. O'Brien 			break;
17293a3fa19SJohn W. De Boskey 		case 'g':
17393a3fa19SJohn W. De Boskey 			putenv("BLOCKSIZE=1g");
17493a3fa19SJohn W. De Boskey 			hflag = 0;
17593a3fa19SJohn W. De Boskey 			break;
176dd6d33e8SMichael Haro 		case 'H':
177dd6d33e8SMichael Haro 			hflag = UNITS_SI;
178dd6d33e8SMichael Haro 			valp = vals_si;
179dd6d33e8SMichael Haro 			break;
180dd6d33e8SMichael Haro 		case 'h':
181dd6d33e8SMichael Haro 			hflag = UNITS_2;
182dd6d33e8SMichael Haro 			valp = vals_base2;
183dd6d33e8SMichael Haro 			break;
1844b88c807SRodney W. Grimes 		case 'i':
1854b88c807SRodney W. Grimes 			iflag = 1;
1864b88c807SRodney W. Grimes 			break;
1877f0eabfdSGarrett Wollman 		case 'k':
1889d4081eeSDavid Greenman 			putenv("BLOCKSIZE=1k");
189dd6d33e8SMichael Haro 			hflag = 0;
190dd6d33e8SMichael Haro 			break;
191a25695c3SJim Pirzyk 		case 'l':
192a25695c3SJim Pirzyk 			if (vfslist != NULL)
193a25695c3SJim Pirzyk 				errx(1, "-l and -t are mutually exclusive.");
194a25695c3SJim Pirzyk 			vfslist = makevfslist(makenetvfslist());
195a25695c3SJim Pirzyk 			break;
196dd6d33e8SMichael Haro 		case 'm':
197dd6d33e8SMichael Haro 			putenv("BLOCKSIZE=1m");
198dd6d33e8SMichael Haro 			hflag = 0;
1997f0eabfdSGarrett Wollman 			break;
2004b88c807SRodney W. Grimes 		case 'n':
2014b88c807SRodney W. Grimes 			nflag = 1;
2024b88c807SRodney W. Grimes 			break;
2034b88c807SRodney W. Grimes 		case 't':
204a78192e3SBruce Evans 			if (vfslist != NULL)
2057b3a12a8SPhilippe Charnier 				errx(1, "only one -t option may be specified");
206f3895a82SKris Kennaway 			fstype = optarg;
207a78192e3SBruce Evans 			vfslist = makevfslist(optarg);
2084b88c807SRodney W. Grimes 			break;
2094b88c807SRodney W. Grimes 		case '?':
2104b88c807SRodney W. Grimes 		default:
2114b88c807SRodney W. Grimes 			usage();
2124b88c807SRodney W. Grimes 		}
2134b88c807SRodney W. Grimes 	argc -= optind;
2144b88c807SRodney W. Grimes 	argv += optind;
2154b88c807SRodney W. Grimes 
2164b88c807SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
21762edbd31SIan Dowse 	bzero(&maxwidths, sizeof(maxwidths));
21862edbd31SIan Dowse 	for (i = 0; i < mntsize; i++)
21962edbd31SIan Dowse 		update_maxwidths(&maxwidths, &mntbuf[i]);
2204b88c807SRodney W. Grimes 
221b8904f2aSJoerg Wunsch 	rv = 0;
2224b88c807SRodney W. Grimes 	if (!*argv) {
223a78192e3SBruce Evans 		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
22462edbd31SIan Dowse 		bzero(&maxwidths, sizeof(maxwidths));
2255b42dac8SJulian Elischer 		for (i = 0; i < mntsize; i++) {
226076419d2SDavid E. O'Brien 			if (cflag)
227076419d2SDavid E. O'Brien 				addstat(&totalbuf, &mntbuf[i]);
228076419d2SDavid E. O'Brien 			update_maxwidths(&maxwidths, &mntbuf[i]);
229076419d2SDavid E. O'Brien 		}
230076419d2SDavid E. O'Brien 		if (cflag)
231076419d2SDavid E. O'Brien 			update_maxwidths(&maxwidths, &totalbuf);
232076419d2SDavid E. O'Brien 		for (i = 0; i < mntsize; i++)
2335b42dac8SJulian Elischer 			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
23462edbd31SIan Dowse 				prtstat(&mntbuf[i], &maxwidths);
235076419d2SDavid E. O'Brien 		if (cflag)
236076419d2SDavid E. O'Brien 			prtstat(&totalbuf, &maxwidths);
237b8904f2aSJoerg Wunsch 		exit(rv);
2384b88c807SRodney W. Grimes 	}
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes 	for (; *argv; argv++) {
2414b88c807SRodney W. Grimes 		if (stat(*argv, &stbuf) < 0) {
2424b88c807SRodney W. Grimes 			if ((mntpt = getmntpt(*argv)) == 0) {
2434b88c807SRodney W. Grimes 				warn("%s", *argv);
244b8904f2aSJoerg Wunsch 				rv = 1;
2454b88c807SRodney W. Grimes 				continue;
2464b88c807SRodney W. Grimes 			}
247f3895a82SKris Kennaway 		} else if (S_ISCHR(stbuf.st_mode)) {
248f3895a82SKris Kennaway 			if ((mntpt = getmntpt(*argv)) == 0) {
249f3895a82SKris Kennaway 				mdev.fspec = *argv;
250f3895a82SKris Kennaway 				mntpath = strdup("/tmp/df.XXXXXX");
251f3895a82SKris Kennaway 				if (mntpath == NULL) {
252f3895a82SKris Kennaway 					warn("strdup failed");
253f3895a82SKris Kennaway 					rv = 1;
2544b88c807SRodney W. Grimes 					continue;
255f3895a82SKris Kennaway 				}
256f3895a82SKris Kennaway 				mntpt = mkdtemp(mntpath);
257f3895a82SKris Kennaway 				if (mntpt == NULL) {
258f3895a82SKris Kennaway 					warn("mkdtemp(\"%s\") failed", mntpath);
259f3895a82SKris Kennaway 					rv = 1;
260f3895a82SKris Kennaway 					free(mntpath);
261f3895a82SKris Kennaway 					continue;
262f3895a82SKris Kennaway 				}
263f3895a82SKris Kennaway 				if (mount(fstype, mntpt, MNT_RDONLY,
264f3895a82SKris Kennaway 				    &mdev) != 0) {
265532aff98SPoul-Henning Kamp 					warn("%s", *argv);
266532aff98SPoul-Henning Kamp 					rv = 1;
267f3895a82SKris Kennaway 					(void)rmdir(mntpt);
268f3895a82SKris Kennaway 					free(mntpath);
269f3895a82SKris Kennaway 					continue;
270f3895a82SKris Kennaway 				} else if (statfs(mntpt, &statfsbuf) == 0) {
271f3895a82SKris Kennaway 					statfsbuf.f_mntonname[0] = '\0';
27262edbd31SIan Dowse 					prtstat(&statfsbuf, &maxwidths);
273076419d2SDavid E. O'Brien 					if (cflag)
274076419d2SDavid E. O'Brien 						addstat(&totalbuf, &statfsbuf);
275f3895a82SKris Kennaway 				} else {
276f3895a82SKris Kennaway 					warn("%s", *argv);
277f3895a82SKris Kennaway 					rv = 1;
278f3895a82SKris Kennaway 				}
279f3895a82SKris Kennaway 				(void)unmount(mntpt, 0);
280f3895a82SKris Kennaway 				(void)rmdir(mntpt);
281f3895a82SKris Kennaway 				free(mntpath);
282f3895a82SKris Kennaway 				continue;
283f3895a82SKris Kennaway 			}
2844b88c807SRodney W. Grimes 		} else
2854b88c807SRodney W. Grimes 			mntpt = *argv;
2860e7d023fSBruce Evans 
2874b88c807SRodney W. Grimes 		/*
2884b88c807SRodney W. Grimes 		 * Statfs does not take a `wait' flag, so we cannot
2894b88c807SRodney W. Grimes 		 * implement nflag here.
2904b88c807SRodney W. Grimes 		 */
2914b88c807SRodney W. Grimes 		if (statfs(mntpt, &statfsbuf) < 0) {
2924b88c807SRodney W. Grimes 			warn("%s", mntpt);
293b8904f2aSJoerg Wunsch 			rv = 1;
2944b88c807SRodney W. Grimes 			continue;
2954b88c807SRodney W. Grimes 		}
2960e7d023fSBruce Evans 
2970e7d023fSBruce Evans 		/*
2980e7d023fSBruce Evans 		 * Check to make sure the arguments we've been given are
2990e7d023fSBruce Evans 		 * satisfied.  Return an error if we have been asked to
3000e7d023fSBruce Evans 		 * list a mount point that does not match the other args
3010e7d023fSBruce Evans 		 * we've been given (-l, -t, etc.).
302c22acefbSJordan K. Hubbard 		 */
303c22acefbSJordan K. Hubbard 		if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
3040e7d023fSBruce Evans 			rv = 1;
305c22acefbSJordan K. Hubbard 			continue;
306c22acefbSJordan K. Hubbard 		}
3070e7d023fSBruce Evans 
30862edbd31SIan Dowse 		if (argc == 1) {
30962edbd31SIan Dowse 			bzero(&maxwidths, sizeof(maxwidths));
31062edbd31SIan Dowse 			update_maxwidths(&maxwidths, &statfsbuf);
31162edbd31SIan Dowse 		}
31262edbd31SIan Dowse 		prtstat(&statfsbuf, &maxwidths);
313076419d2SDavid E. O'Brien 		if (cflag)
314076419d2SDavid E. O'Brien 			addstat(&totalbuf, &statfsbuf);
3154b88c807SRodney W. Grimes 	}
316076419d2SDavid E. O'Brien 	if (cflag)
317076419d2SDavid E. O'Brien 		prtstat(&totalbuf, &maxwidths);
318b8904f2aSJoerg Wunsch 	return (rv);
3194b88c807SRodney W. Grimes }
3204b88c807SRodney W. Grimes 
321532aff98SPoul-Henning Kamp static char *
322b7dbd3e9SMark Murray getmntpt(const char *name)
3234b88c807SRodney W. Grimes {
324be2c4e54SDavid E. O'Brien 	size_t mntsize, i;
3254b88c807SRodney W. Grimes 	struct statfs *mntbuf;
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
3284b88c807SRodney W. Grimes 	for (i = 0; i < mntsize; i++) {
3294b88c807SRodney W. Grimes 		if (!strcmp(mntbuf[i].f_mntfromname, name))
3304b88c807SRodney W. Grimes 			return (mntbuf[i].f_mntonname);
3314b88c807SRodney W. Grimes 	}
3324b88c807SRodney W. Grimes 	return (0);
3334b88c807SRodney W. Grimes }
3344b88c807SRodney W. Grimes 
3354b88c807SRodney W. Grimes /*
3364b88c807SRodney W. Grimes  * Make a pass over the file system info in ``mntbuf'' filtering out
337a78192e3SBruce Evans  * file system types not in vfslist and possibly re-stating to get
3384b88c807SRodney W. Grimes  * current (not cached) info.  Returns the new count of valid statfs bufs.
3394b88c807SRodney W. Grimes  */
340be2c4e54SDavid E. O'Brien static size_t
341532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
3424b88c807SRodney W. Grimes {
3434b88c807SRodney W. Grimes 	int i, j;
3444b88c807SRodney W. Grimes 	struct statfs *mntbuf;
3454b88c807SRodney W. Grimes 
346a78192e3SBruce Evans 	if (vfslist == NULL)
3474b88c807SRodney W. Grimes 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes 	mntbuf = *mntbufp;
350a78192e3SBruce Evans 	for (j = 0, i = 0; i < mntsize; i++) {
351a78192e3SBruce Evans 		if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
352a78192e3SBruce Evans 			continue;
3534b88c807SRodney W. Grimes 		if (!nflag)
3544b88c807SRodney W. Grimes 			(void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
3554b88c807SRodney W. Grimes 		else if (i != j)
3564b88c807SRodney W. Grimes 			mntbuf[j] = mntbuf[i];
3574b88c807SRodney W. Grimes 		j++;
3584b88c807SRodney W. Grimes 	}
3594b88c807SRodney W. Grimes 	return (j);
3604b88c807SRodney W. Grimes }
3614b88c807SRodney W. Grimes 
3624b88c807SRodney W. Grimes /*
363dd6d33e8SMichael Haro  * Output in "human-readable" format.  Uses 3 digits max and puts
364dd6d33e8SMichael Haro  * unit suffixes at the end.  Makes output compact and easy to read,
365dd6d33e8SMichael Haro  * especially on huge disks.
366dd6d33e8SMichael Haro  *
367dd6d33e8SMichael Haro  */
368532aff98SPoul-Henning Kamp static unit_t
369f9bcb0beSWarner Losh unit_adjust(double *val)
370dd6d33e8SMichael Haro {
371dd6d33e8SMichael Haro 	double abval;
372dd6d33e8SMichael Haro 	unit_t unit;
373b7dbd3e9SMark Murray 	int unit_sz;
374dd6d33e8SMichael Haro 
375dd6d33e8SMichael Haro 	abval = fabs(*val);
376dd6d33e8SMichael Haro 
377dd6d33e8SMichael Haro 	unit_sz = abval ? ilogb(abval) / 10 : 0;
378dd6d33e8SMichael Haro 
379b7dbd3e9SMark Murray 	if (unit_sz >= (int)UNIT_MAX) {
380dd6d33e8SMichael Haro 		unit = NONE;
381dd6d33e8SMichael Haro 	} else {
382dd6d33e8SMichael Haro 		unit = unitp[unit_sz];
383dd6d33e8SMichael Haro 		*val /= (double)valp[unit_sz];
384dd6d33e8SMichael Haro 	}
385dd6d33e8SMichael Haro 
386dd6d33e8SMichael Haro 	return (unit);
387dd6d33e8SMichael Haro }
388dd6d33e8SMichael Haro 
389532aff98SPoul-Henning Kamp static void
390fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used)
391dd6d33e8SMichael Haro {
392dd6d33e8SMichael Haro 
393dd6d33e8SMichael Haro 	prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize);
394dd6d33e8SMichael Haro 	prthumanval((double)used * (double)sfsp->f_bsize);
395dd6d33e8SMichael Haro 	prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize);
396dd6d33e8SMichael Haro }
397dd6d33e8SMichael Haro 
398532aff98SPoul-Henning Kamp static void
399f9bcb0beSWarner Losh prthumanval(double bytes)
400dd6d33e8SMichael Haro {
401dd6d33e8SMichael Haro 
402dd6d33e8SMichael Haro 	unit_t unit;
403dd6d33e8SMichael Haro 	unit = unit_adjust(&bytes);
404dd6d33e8SMichael Haro 
405dd6d33e8SMichael Haro 	if (bytes == 0)
406dd6d33e8SMichael Haro 		(void)printf("      0B");
407dd6d33e8SMichael Haro 	else if (bytes > 10)
408076419d2SDavid E. O'Brien 		(void)printf(" % 6.0f%c", bytes, "BKMGTPE"[unit]);
409dd6d33e8SMichael Haro 	else
410076419d2SDavid E. O'Brien 		(void)printf(" % 6.1f%c", bytes, "BKMGTPE"[unit]);
411dd6d33e8SMichael Haro }
412dd6d33e8SMichael Haro 
413dd6d33e8SMichael Haro /*
4144b88c807SRodney W. Grimes  * Convert statfs returned file system size into BLOCKSIZE units.
4154b88c807SRodney W. Grimes  * Attempts to avoid overflow for large file systems.
4164b88c807SRodney W. Grimes  */
4174b88c807SRodney W. Grimes #define fsbtoblk(num, fsbs, bs) \
4184b88c807SRodney W. Grimes 	(((fsbs) != 0 && (fsbs) < (bs)) ? \
419ae21f5adSLukas Ertl 		(num) / (intmax_t)((bs) / (fsbs)) : \
420ae21f5adSLukas Ertl 		(num) * (intmax_t)((fsbs) / (bs)))
4214b88c807SRodney W. Grimes 
4224b88c807SRodney W. Grimes /*
4234b88c807SRodney W. Grimes  * Print out status about a file system.
4244b88c807SRodney W. Grimes  */
425532aff98SPoul-Henning Kamp static void
42662edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp)
4274b88c807SRodney W. Grimes {
428fde81c7dSKirk McKusick 	static u_long blocksize;
429b7dbd3e9SMark Murray 	static int headerlen, timesthrough = 0;
430a95a13bbSKris Kennaway 	static const char *header;
431fde81c7dSKirk McKusick 	int64_t used, availblks, inodes;
4324b88c807SRodney W. Grimes 
4334b88c807SRodney W. Grimes 	if (++timesthrough == 1) {
4340bd9f151SIan Dowse 		mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
435dd6d33e8SMichael Haro 		if (hflag) {
436dd6d33e8SMichael Haro 			header = "   Size";
4370bd9f151SIan Dowse 			mwp->total = mwp->used = mwp->avail =
4380bd9f151SIan Dowse 			    (int)strlen(header);
439dd6d33e8SMichael Haro 		} else {
440dd6d33e8SMichael Haro 			header = getbsize(&headerlen, &blocksize);
4410bd9f151SIan Dowse 			mwp->total = imax(mwp->total, headerlen);
442dd6d33e8SMichael Haro 		}
4430bd9f151SIan Dowse 		mwp->used = imax(mwp->used, (int)strlen("Used"));
4440bd9f151SIan Dowse 		mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
44562edbd31SIan Dowse 
446f694b8adSMark Murray 		(void)printf("%-*s %-*s %*s %*s Capacity",
4470bd9f151SIan Dowse 		    mwp->mntfrom, "Filesystem", mwp->total, header,
4480bd9f151SIan Dowse 		    mwp->used, "Used", mwp->avail, "Avail");
44962edbd31SIan Dowse 		if (iflag) {
4500bd9f151SIan Dowse 			mwp->iused = imax(mwp->iused, (int)strlen("  iused"));
4510bd9f151SIan Dowse 			mwp->ifree = imax(mwp->ifree, (int)strlen("ifree"));
452f694b8adSMark Murray 			(void)printf(" %*s %*s %%iused",
4530bd9f151SIan Dowse 			    mwp->iused - 2, "iused", mwp->ifree, "ifree");
45462edbd31SIan Dowse 		}
4554b88c807SRodney W. Grimes 		(void)printf("  Mounted on\n");
4564b88c807SRodney W. Grimes 	}
4570bd9f151SIan Dowse 	(void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
4584b88c807SRodney W. Grimes 	used = sfsp->f_blocks - sfsp->f_bfree;
4594b88c807SRodney W. Grimes 	availblks = sfsp->f_bavail + used;
460dd6d33e8SMichael Haro 	if (hflag) {
461dd6d33e8SMichael Haro 		prthuman(sfsp, used);
462dd6d33e8SMichael Haro 	} else {
463ea64ad77SKris Kennaway 		(void)printf(" %*jd %*jd %*jd",
4640bd9f151SIan Dowse 		    mwp->total, (intmax_t)fsbtoblk(sfsp->f_blocks,
4650bd9f151SIan Dowse 		    sfsp->f_bsize, blocksize),
4660bd9f151SIan Dowse 		    mwp->used, (intmax_t)fsbtoblk(used, sfsp->f_bsize,
4670bd9f151SIan Dowse 		    blocksize),
4680bd9f151SIan Dowse 		    mwp->avail, (intmax_t)fsbtoblk(sfsp->f_bavail,
4690bd9f151SIan Dowse 		    sfsp->f_bsize, blocksize));
470dd6d33e8SMichael Haro 	}
4714b88c807SRodney W. Grimes 	(void)printf(" %5.0f%%",
4724b88c807SRodney W. Grimes 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
4734b88c807SRodney W. Grimes 	if (iflag) {
4744b88c807SRodney W. Grimes 		inodes = sfsp->f_files;
4754b88c807SRodney W. Grimes 		used = inodes - sfsp->f_ffree;
4760bd9f151SIan Dowse 		(void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used,
4770bd9f151SIan Dowse 		    mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 :
4780bd9f151SIan Dowse 		    (double)used / (double)inodes * 100.0);
479cca108e6SDavid E. O'Brien 	} else
480cca108e6SDavid E. O'Brien 		(void)printf("  ");
481c6f13844SDavid E. O'Brien 	if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
482076419d2SDavid E. O'Brien 		(void)printf("  %s", sfsp->f_mntonname);
483076419d2SDavid E. O'Brien 	(void)printf("\n");
484076419d2SDavid E. O'Brien }
485076419d2SDavid E. O'Brien 
486076419d2SDavid E. O'Brien void
487076419d2SDavid E. O'Brien addstat(struct statfs *totalfsp, struct statfs *statfsp)
488076419d2SDavid E. O'Brien {
489c6f13844SDavid E. O'Brien 	uint64_t bsize;
490076419d2SDavid E. O'Brien 
491c6f13844SDavid E. O'Brien 	bsize = statfsp->f_bsize / totalfsp->f_bsize;
492076419d2SDavid E. O'Brien 	totalfsp->f_blocks += statfsp->f_blocks * bsize;
493076419d2SDavid E. O'Brien 	totalfsp->f_bfree += statfsp->f_bfree * bsize;
494076419d2SDavid E. O'Brien 	totalfsp->f_bavail += statfsp->f_bavail * bsize;
495076419d2SDavid E. O'Brien 	totalfsp->f_files += statfsp->f_files;
496076419d2SDavid E. O'Brien 	totalfsp->f_ffree += statfsp->f_ffree;
4974b88c807SRodney W. Grimes }
4984b88c807SRodney W. Grimes 
4994b88c807SRodney W. Grimes /*
50062edbd31SIan Dowse  * Update the maximum field-width information in `mwp' based on
50162edbd31SIan Dowse  * the file system specified by `sfsp'.
50262edbd31SIan Dowse  */
503532aff98SPoul-Henning Kamp static void
504b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
50562edbd31SIan Dowse {
506fde81c7dSKirk McKusick 	static u_long blocksize = 0;
507dc474219SMike Barcroft 	int dummy;
50862edbd31SIan Dowse 
50962edbd31SIan Dowse 	if (blocksize == 0)
51062edbd31SIan Dowse 		getbsize(&dummy, &blocksize);
51162edbd31SIan Dowse 
5120bd9f151SIan Dowse 	mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
5130bd9f151SIan Dowse 	mwp->total = imax(mwp->total, int64width(
514fde81c7dSKirk McKusick 	    fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
5150bd9f151SIan Dowse 	mwp->used = imax(mwp->used,
5160bd9f151SIan Dowse 	    int64width(fsbtoblk((int64_t)sfsp->f_blocks -
517fde81c7dSKirk McKusick 	    (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
5180bd9f151SIan Dowse 	mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
51962edbd31SIan Dowse 	    sfsp->f_bsize, blocksize)));
5200bd9f151SIan Dowse 	mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
52162edbd31SIan Dowse 	    sfsp->f_ffree));
5220bd9f151SIan Dowse 	mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
52362edbd31SIan Dowse }
52462edbd31SIan Dowse 
5250bd9f151SIan Dowse /* Return the width in characters of the specified value. */
5260bd9f151SIan Dowse static int
527fde81c7dSKirk McKusick int64width(int64_t val)
52862edbd31SIan Dowse {
5290bd9f151SIan Dowse 	int len;
53062edbd31SIan Dowse 
53162edbd31SIan Dowse 	len = 0;
53262edbd31SIan Dowse 	/* Negative or zero values require one extra digit. */
53362edbd31SIan Dowse 	if (val <= 0) {
53462edbd31SIan Dowse 		val = -val;
53562edbd31SIan Dowse 		len++;
53662edbd31SIan Dowse 	}
53762edbd31SIan Dowse 	while (val > 0) {
53862edbd31SIan Dowse 		len++;
53962edbd31SIan Dowse 		val /= 10;
54062edbd31SIan Dowse 	}
54162edbd31SIan Dowse 
54262edbd31SIan Dowse 	return (len);
54362edbd31SIan Dowse }
54462edbd31SIan Dowse 
545532aff98SPoul-Henning Kamp static void
546f9bcb0beSWarner Losh usage(void)
5474b88c807SRodney W. Grimes {
548dd6d33e8SMichael Haro 
549a78192e3SBruce Evans 	(void)fprintf(stderr,
550c6f13844SDavid E. O'Brien "usage: df [-b | -g | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...]\n");
551dd6d33e8SMichael Haro 	exit(EX_USAGE);
5524b88c807SRodney W. Grimes }
553a25695c3SJim Pirzyk 
554532aff98SPoul-Henning Kamp static char *
555f9bcb0beSWarner Losh makenetvfslist(void)
556a25695c3SJim Pirzyk {
557a25695c3SJim Pirzyk 	char *str, *strptr, **listptr;
558b7dbd3e9SMark Murray 	struct xvfsconf *xvfsp, *keep_xvfsp;
5595965373eSMaxime Henrion 	size_t buflen;
5605965373eSMaxime Henrion 	int cnt, i, maxvfsconf;
561a25695c3SJim Pirzyk 
5625965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
5635965373eSMaxime Henrion 		warn("sysctl(vfs.conflist)");
564a25695c3SJim Pirzyk 		return (NULL);
565a25695c3SJim Pirzyk 	}
5665965373eSMaxime Henrion 	xvfsp = malloc(buflen);
5675965373eSMaxime Henrion 	if (xvfsp == NULL) {
5685965373eSMaxime Henrion 		warnx("malloc failed");
5695965373eSMaxime Henrion 		return (NULL);
5705965373eSMaxime Henrion 	}
571b7dbd3e9SMark Murray 	keep_xvfsp = xvfsp;
5725965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
5735965373eSMaxime Henrion 		warn("sysctl(vfs.conflist)");
574b7dbd3e9SMark Murray 		free(keep_xvfsp);
5755965373eSMaxime Henrion 		return (NULL);
5765965373eSMaxime Henrion 	}
5775965373eSMaxime Henrion 	maxvfsconf = buflen / sizeof(struct xvfsconf);
578a25695c3SJim Pirzyk 
579a25695c3SJim Pirzyk 	if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
580a25695c3SJim Pirzyk 		warnx("malloc failed");
581b7dbd3e9SMark Murray 		free(keep_xvfsp);
582a25695c3SJim Pirzyk 		return (NULL);
583a25695c3SJim Pirzyk 	}
584a25695c3SJim Pirzyk 
5855965373eSMaxime Henrion 	for (cnt = 0, i = 0; i < maxvfsconf; i++) {
5865965373eSMaxime Henrion 		if (xvfsp->vfc_flags & VFCF_NETWORK) {
5875965373eSMaxime Henrion 			listptr[cnt++] = strdup(xvfsp->vfc_name);
588a067aeceSGarrett Wollman 			if (listptr[cnt-1] == NULL) {
589a25695c3SJim Pirzyk 				warnx("malloc failed");
590b7dbd3e9SMark Murray 				free(listptr);
591b7dbd3e9SMark Murray 				free(keep_xvfsp);
592a25695c3SJim Pirzyk 				return (NULL);
593a25695c3SJim Pirzyk 			}
594a25695c3SJim Pirzyk 		}
5955965373eSMaxime Henrion 		xvfsp++;
5965965373eSMaxime Henrion 	}
597a25695c3SJim Pirzyk 
598cf5b29e1SRuslan Ermilov 	if (cnt == 0 ||
599cf5b29e1SRuslan Ermilov 	    (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
600cf5b29e1SRuslan Ermilov 		if (cnt > 0)
601a25695c3SJim Pirzyk 			warnx("malloc failed");
602a25695c3SJim Pirzyk 		free(listptr);
603b7dbd3e9SMark Murray 		free(keep_xvfsp);
604a25695c3SJim Pirzyk 		return (NULL);
605a25695c3SJim Pirzyk 	}
606a25695c3SJim Pirzyk 
607a25695c3SJim Pirzyk 	*str = 'n'; *(str + 1) = 'o';
608a25695c3SJim Pirzyk 	for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
609a25695c3SJim Pirzyk 		strncpy(strptr, listptr[i], 32);
610a25695c3SJim Pirzyk 		strptr += strlen(listptr[i]);
611a25695c3SJim Pirzyk 		*strptr = ',';
612a25695c3SJim Pirzyk 		free(listptr[i]);
613a25695c3SJim Pirzyk 	}
61416fc3635SMark Murray 	*(--strptr) = '\0';
615a25695c3SJim Pirzyk 
616b7dbd3e9SMark Murray 	free(keep_xvfsp);
617a25695c3SJim Pirzyk 	free(listptr);
618a25695c3SJim Pirzyk 	return (str);
619a25695c3SJim Pirzyk }
620