xref: /freebsd/bin/df/df.c (revision 841fe8e83f969d10f48b95a007577fde34fd8532)
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>
557d3940bbSPawel Jakub Dawidek #include <libutil.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 
6862edbd31SIan Dowse /* Maximum widths of various fields. */
6962edbd31SIan Dowse struct maxwidths {
700bd9f151SIan Dowse 	int	mntfrom;
710bd9f151SIan Dowse 	int	total;
720bd9f151SIan Dowse 	int	used;
730bd9f151SIan Dowse 	int	avail;
740bd9f151SIan Dowse 	int	iused;
750bd9f151SIan Dowse 	int	ifree;
7662edbd31SIan Dowse };
7762edbd31SIan Dowse 
78c6f13844SDavid E. O'Brien static void	  addstat(struct statfs *, struct statfs *);
79b7dbd3e9SMark Murray static char	 *getmntpt(const char *);
800bd9f151SIan Dowse static int	  int64width(int64_t);
81532aff98SPoul-Henning Kamp static char	 *makenetvfslist(void);
82fde81c7dSKirk McKusick static void	  prthuman(const struct statfs *, int64_t);
837d3940bbSPawel Jakub Dawidek static void	  prthumanval(int64_t);
84841fe8e8SDavid Schultz static intmax_t	  fsbtoblk(int64_t, uint64_t, u_long);
85532aff98SPoul-Henning Kamp static void	  prtstat(struct statfs *, struct maxwidths *);
86be2c4e54SDavid E. O'Brien static size_t	  regetmntinfo(struct statfs **, long, const char **);
87b7dbd3e9SMark Murray static void	  update_maxwidths(struct maxwidths *, const struct statfs *);
88532aff98SPoul-Henning Kamp static void	  usage(void);
894b88c807SRodney W. Grimes 
900bd9f151SIan Dowse static __inline int
910bd9f151SIan Dowse imax(int a, int b)
9262edbd31SIan Dowse {
93b7dbd3e9SMark Murray 	return (a > b ? a : b);
9462edbd31SIan Dowse }
9562edbd31SIan Dowse 
96076419d2SDavid E. O'Brien static int	aflag = 0, cflag, hflag, iflag, nflag;
97532aff98SPoul-Henning Kamp static struct	ufs_args mdev;
98532aff98SPoul-Henning Kamp 
994b88c807SRodney W. Grimes int
100f9bcb0beSWarner Losh main(int argc, char *argv[])
1014b88c807SRodney W. Grimes {
1024b88c807SRodney W. Grimes 	struct stat stbuf;
103c6f13844SDavid E. O'Brien 	struct statfs statfsbuf, totalbuf;
10462edbd31SIan Dowse 	struct maxwidths maxwidths;
105c6f13844SDavid E. O'Brien 	struct statfs *mntbuf;
106a95a13bbSKris Kennaway 	const char *fstype;
107532aff98SPoul-Henning Kamp 	char *mntpath, *mntpt;
108532aff98SPoul-Henning Kamp 	const char **vfslist;
109be2c4e54SDavid E. O'Brien 	size_t i, mntsize;
110be2c4e54SDavid E. O'Brien 	int ch, rv;
111f3895a82SKris Kennaway 
112f3895a82SKris Kennaway 	fstype = "ufs";
1134b88c807SRodney W. Grimes 
114076419d2SDavid E. O'Brien 	memset(&totalbuf, 0, sizeof(totalbuf));
115076419d2SDavid E. O'Brien 	totalbuf.f_bsize = DEV_BSIZE;
116076419d2SDavid E. O'Brien 	strncpy(totalbuf.f_mntfromname, "total", MNAMELEN);
117a78192e3SBruce Evans 	vfslist = NULL;
118076419d2SDavid E. O'Brien 	while ((ch = getopt(argc, argv, "abcgHhiklmnPt:")) != -1)
1194b88c807SRodney W. Grimes 		switch (ch) {
1205b42dac8SJulian Elischer 		case 'a':
1215b42dac8SJulian Elischer 			aflag = 1;
1225b42dac8SJulian Elischer 			break;
123dd6d33e8SMichael Haro 		case 'b':
124dd6d33e8SMichael Haro 				/* FALLTHROUGH */
125dd6d33e8SMichael Haro 		case 'P':
126dd6d33e8SMichael Haro 			putenv("BLOCKSIZE=512");
127dd6d33e8SMichael Haro 			hflag = 0;
128dd6d33e8SMichael Haro 			break;
129c6f13844SDavid E. O'Brien 		case 'c':
130c6f13844SDavid E. O'Brien 			cflag = 1;
131c6f13844SDavid E. O'Brien 			break;
13293a3fa19SJohn W. De Boskey 		case 'g':
13393a3fa19SJohn W. De Boskey 			putenv("BLOCKSIZE=1g");
13493a3fa19SJohn W. De Boskey 			hflag = 0;
13593a3fa19SJohn W. De Boskey 			break;
136dd6d33e8SMichael Haro 		case 'H':
137dd6d33e8SMichael Haro 			hflag = UNITS_SI;
138dd6d33e8SMichael Haro 			break;
139dd6d33e8SMichael Haro 		case 'h':
140dd6d33e8SMichael Haro 			hflag = UNITS_2;
141dd6d33e8SMichael Haro 			break;
1424b88c807SRodney W. Grimes 		case 'i':
1434b88c807SRodney W. Grimes 			iflag = 1;
1444b88c807SRodney W. Grimes 			break;
1457f0eabfdSGarrett Wollman 		case 'k':
1469d4081eeSDavid Greenman 			putenv("BLOCKSIZE=1k");
147dd6d33e8SMichael Haro 			hflag = 0;
148dd6d33e8SMichael Haro 			break;
149a25695c3SJim Pirzyk 		case 'l':
150a25695c3SJim Pirzyk 			if (vfslist != NULL)
151a25695c3SJim Pirzyk 				errx(1, "-l and -t are mutually exclusive.");
152a25695c3SJim Pirzyk 			vfslist = makevfslist(makenetvfslist());
153a25695c3SJim Pirzyk 			break;
154dd6d33e8SMichael Haro 		case 'm':
155dd6d33e8SMichael Haro 			putenv("BLOCKSIZE=1m");
156dd6d33e8SMichael Haro 			hflag = 0;
1577f0eabfdSGarrett Wollman 			break;
1584b88c807SRodney W. Grimes 		case 'n':
1594b88c807SRodney W. Grimes 			nflag = 1;
1604b88c807SRodney W. Grimes 			break;
1614b88c807SRodney W. Grimes 		case 't':
162a78192e3SBruce Evans 			if (vfslist != NULL)
1637b3a12a8SPhilippe Charnier 				errx(1, "only one -t option may be specified");
164f3895a82SKris Kennaway 			fstype = optarg;
165a78192e3SBruce Evans 			vfslist = makevfslist(optarg);
1664b88c807SRodney W. Grimes 			break;
1674b88c807SRodney W. Grimes 		case '?':
1684b88c807SRodney W. Grimes 		default:
1694b88c807SRodney W. Grimes 			usage();
1704b88c807SRodney W. Grimes 		}
1714b88c807SRodney W. Grimes 	argc -= optind;
1724b88c807SRodney W. Grimes 	argv += optind;
1734b88c807SRodney W. Grimes 
1744b88c807SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
17562edbd31SIan Dowse 	bzero(&maxwidths, sizeof(maxwidths));
17662edbd31SIan Dowse 	for (i = 0; i < mntsize; i++)
17762edbd31SIan Dowse 		update_maxwidths(&maxwidths, &mntbuf[i]);
1784b88c807SRodney W. Grimes 
179b8904f2aSJoerg Wunsch 	rv = 0;
1804b88c807SRodney W. Grimes 	if (!*argv) {
181a78192e3SBruce Evans 		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
18262edbd31SIan Dowse 		bzero(&maxwidths, sizeof(maxwidths));
1835b42dac8SJulian Elischer 		for (i = 0; i < mntsize; i++) {
184076419d2SDavid E. O'Brien 			if (cflag)
185076419d2SDavid E. O'Brien 				addstat(&totalbuf, &mntbuf[i]);
186076419d2SDavid E. O'Brien 			update_maxwidths(&maxwidths, &mntbuf[i]);
187076419d2SDavid E. O'Brien 		}
188076419d2SDavid E. O'Brien 		if (cflag)
189076419d2SDavid E. O'Brien 			update_maxwidths(&maxwidths, &totalbuf);
190076419d2SDavid E. O'Brien 		for (i = 0; i < mntsize; i++)
1915b42dac8SJulian Elischer 			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
19262edbd31SIan Dowse 				prtstat(&mntbuf[i], &maxwidths);
193076419d2SDavid E. O'Brien 		if (cflag)
194076419d2SDavid E. O'Brien 			prtstat(&totalbuf, &maxwidths);
195b8904f2aSJoerg Wunsch 		exit(rv);
1964b88c807SRodney W. Grimes 	}
1974b88c807SRodney W. Grimes 
1984b88c807SRodney W. Grimes 	for (; *argv; argv++) {
1994b88c807SRodney W. Grimes 		if (stat(*argv, &stbuf) < 0) {
2004b88c807SRodney W. Grimes 			if ((mntpt = getmntpt(*argv)) == 0) {
2014b88c807SRodney W. Grimes 				warn("%s", *argv);
202b8904f2aSJoerg Wunsch 				rv = 1;
2034b88c807SRodney W. Grimes 				continue;
2044b88c807SRodney W. Grimes 			}
205f3895a82SKris Kennaway 		} else if (S_ISCHR(stbuf.st_mode)) {
206f3895a82SKris Kennaway 			if ((mntpt = getmntpt(*argv)) == 0) {
207f3895a82SKris Kennaway 				mdev.fspec = *argv;
208f3895a82SKris Kennaway 				mntpath = strdup("/tmp/df.XXXXXX");
209f3895a82SKris Kennaway 				if (mntpath == NULL) {
210f3895a82SKris Kennaway 					warn("strdup failed");
211f3895a82SKris Kennaway 					rv = 1;
2124b88c807SRodney W. Grimes 					continue;
213f3895a82SKris Kennaway 				}
214f3895a82SKris Kennaway 				mntpt = mkdtemp(mntpath);
215f3895a82SKris Kennaway 				if (mntpt == NULL) {
216f3895a82SKris Kennaway 					warn("mkdtemp(\"%s\") failed", mntpath);
217f3895a82SKris Kennaway 					rv = 1;
218f3895a82SKris Kennaway 					free(mntpath);
219f3895a82SKris Kennaway 					continue;
220f3895a82SKris Kennaway 				}
221f3895a82SKris Kennaway 				if (mount(fstype, mntpt, MNT_RDONLY,
222f3895a82SKris Kennaway 				    &mdev) != 0) {
223532aff98SPoul-Henning Kamp 					warn("%s", *argv);
224532aff98SPoul-Henning Kamp 					rv = 1;
225f3895a82SKris Kennaway 					(void)rmdir(mntpt);
226f3895a82SKris Kennaway 					free(mntpath);
227f3895a82SKris Kennaway 					continue;
228f3895a82SKris Kennaway 				} else if (statfs(mntpt, &statfsbuf) == 0) {
229f3895a82SKris Kennaway 					statfsbuf.f_mntonname[0] = '\0';
23062edbd31SIan Dowse 					prtstat(&statfsbuf, &maxwidths);
231076419d2SDavid E. O'Brien 					if (cflag)
232076419d2SDavid E. O'Brien 						addstat(&totalbuf, &statfsbuf);
233f3895a82SKris Kennaway 				} else {
234f3895a82SKris Kennaway 					warn("%s", *argv);
235f3895a82SKris Kennaway 					rv = 1;
236f3895a82SKris Kennaway 				}
237f3895a82SKris Kennaway 				(void)unmount(mntpt, 0);
238f3895a82SKris Kennaway 				(void)rmdir(mntpt);
239f3895a82SKris Kennaway 				free(mntpath);
240f3895a82SKris Kennaway 				continue;
241f3895a82SKris Kennaway 			}
2424b88c807SRodney W. Grimes 		} else
2434b88c807SRodney W. Grimes 			mntpt = *argv;
2440e7d023fSBruce Evans 
2454b88c807SRodney W. Grimes 		/*
2464b88c807SRodney W. Grimes 		 * Statfs does not take a `wait' flag, so we cannot
2474b88c807SRodney W. Grimes 		 * implement nflag here.
2484b88c807SRodney W. Grimes 		 */
2494b88c807SRodney W. Grimes 		if (statfs(mntpt, &statfsbuf) < 0) {
2504b88c807SRodney W. Grimes 			warn("%s", mntpt);
251b8904f2aSJoerg Wunsch 			rv = 1;
2524b88c807SRodney W. Grimes 			continue;
2534b88c807SRodney W. Grimes 		}
2540e7d023fSBruce Evans 
2550e7d023fSBruce Evans 		/*
2560e7d023fSBruce Evans 		 * Check to make sure the arguments we've been given are
2570e7d023fSBruce Evans 		 * satisfied.  Return an error if we have been asked to
2580e7d023fSBruce Evans 		 * list a mount point that does not match the other args
2590e7d023fSBruce Evans 		 * we've been given (-l, -t, etc.).
260c22acefbSJordan K. Hubbard 		 */
261c22acefbSJordan K. Hubbard 		if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
2620e7d023fSBruce Evans 			rv = 1;
263c22acefbSJordan K. Hubbard 			continue;
264c22acefbSJordan K. Hubbard 		}
2650e7d023fSBruce Evans 
26662edbd31SIan Dowse 		if (argc == 1) {
26762edbd31SIan Dowse 			bzero(&maxwidths, sizeof(maxwidths));
26862edbd31SIan Dowse 			update_maxwidths(&maxwidths, &statfsbuf);
26962edbd31SIan Dowse 		}
27062edbd31SIan Dowse 		prtstat(&statfsbuf, &maxwidths);
271076419d2SDavid E. O'Brien 		if (cflag)
272076419d2SDavid E. O'Brien 			addstat(&totalbuf, &statfsbuf);
2734b88c807SRodney W. Grimes 	}
274076419d2SDavid E. O'Brien 	if (cflag)
275076419d2SDavid E. O'Brien 		prtstat(&totalbuf, &maxwidths);
276b8904f2aSJoerg Wunsch 	return (rv);
2774b88c807SRodney W. Grimes }
2784b88c807SRodney W. Grimes 
279532aff98SPoul-Henning Kamp static char *
280b7dbd3e9SMark Murray getmntpt(const char *name)
2814b88c807SRodney W. Grimes {
282be2c4e54SDavid E. O'Brien 	size_t mntsize, i;
2834b88c807SRodney W. Grimes 	struct statfs *mntbuf;
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
2864b88c807SRodney W. Grimes 	for (i = 0; i < mntsize; i++) {
2874b88c807SRodney W. Grimes 		if (!strcmp(mntbuf[i].f_mntfromname, name))
2884b88c807SRodney W. Grimes 			return (mntbuf[i].f_mntonname);
2894b88c807SRodney W. Grimes 	}
2904b88c807SRodney W. Grimes 	return (0);
2914b88c807SRodney W. Grimes }
2924b88c807SRodney W. Grimes 
2934b88c807SRodney W. Grimes /*
2944b88c807SRodney W. Grimes  * Make a pass over the file system info in ``mntbuf'' filtering out
295a78192e3SBruce Evans  * file system types not in vfslist and possibly re-stating to get
2964b88c807SRodney W. Grimes  * current (not cached) info.  Returns the new count of valid statfs bufs.
2974b88c807SRodney W. Grimes  */
298be2c4e54SDavid E. O'Brien static size_t
299532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
3004b88c807SRodney W. Grimes {
3014b88c807SRodney W. Grimes 	int i, j;
3024b88c807SRodney W. Grimes 	struct statfs *mntbuf;
3034b88c807SRodney W. Grimes 
304a78192e3SBruce Evans 	if (vfslist == NULL)
3054b88c807SRodney W. Grimes 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 	mntbuf = *mntbufp;
308a78192e3SBruce Evans 	for (j = 0, i = 0; i < mntsize; i++) {
309a78192e3SBruce Evans 		if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
310a78192e3SBruce Evans 			continue;
3114b88c807SRodney W. Grimes 		if (!nflag)
3124b88c807SRodney W. Grimes 			(void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
3134b88c807SRodney W. Grimes 		else if (i != j)
3144b88c807SRodney W. Grimes 			mntbuf[j] = mntbuf[i];
3154b88c807SRodney W. Grimes 		j++;
3164b88c807SRodney W. Grimes 	}
3174b88c807SRodney W. Grimes 	return (j);
3184b88c807SRodney W. Grimes }
3194b88c807SRodney W. Grimes 
320532aff98SPoul-Henning Kamp static void
321fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used)
322dd6d33e8SMichael Haro {
323dd6d33e8SMichael Haro 
3247d3940bbSPawel Jakub Dawidek 	prthumanval(sfsp->f_blocks * sfsp->f_bsize);
3257d3940bbSPawel Jakub Dawidek 	prthumanval(used * sfsp->f_bsize);
3267d3940bbSPawel Jakub Dawidek 	prthumanval(sfsp->f_bavail * sfsp->f_bsize);
327dd6d33e8SMichael Haro }
328dd6d33e8SMichael Haro 
329532aff98SPoul-Henning Kamp static void
3307d3940bbSPawel Jakub Dawidek prthumanval(int64_t bytes)
331dd6d33e8SMichael Haro {
3327d3940bbSPawel Jakub Dawidek 	char buf[6];
3337d3940bbSPawel Jakub Dawidek 	int flags;
334dd6d33e8SMichael Haro 
3357d3940bbSPawel Jakub Dawidek 	flags = HN_B | HN_NOSPACE | HN_DECIMAL;
3367d3940bbSPawel Jakub Dawidek 	if (hflag == UNITS_SI)
3377d3940bbSPawel Jakub Dawidek 		flags |= HN_DIVISOR_1000;
338dd6d33e8SMichael Haro 
3397d3940bbSPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
3407d3940bbSPawel Jakub Dawidek 	    bytes, "", HN_AUTOSCALE, flags);
3417d3940bbSPawel Jakub Dawidek 
3427d3940bbSPawel Jakub Dawidek 	(void)printf("  %6s", buf);
343dd6d33e8SMichael Haro }
344dd6d33e8SMichael Haro 
345dd6d33e8SMichael Haro /*
3464b88c807SRodney W. Grimes  * Convert statfs returned file system size into BLOCKSIZE units.
3474b88c807SRodney W. Grimes  * Attempts to avoid overflow for large file systems.
3484b88c807SRodney W. Grimes  */
349841fe8e8SDavid Schultz static intmax_t
350841fe8e8SDavid Schultz fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
351841fe8e8SDavid Schultz {
352841fe8e8SDavid Schultz 
353841fe8e8SDavid Schultz 	if (fsbs != 0 && fsbs < bs)
354841fe8e8SDavid Schultz 		return (num / (intmax_t)(bs / fsbs));
355841fe8e8SDavid Schultz 	else
356841fe8e8SDavid Schultz 		return (num * (intmax_t)(fsbs / bs));
357841fe8e8SDavid Schultz }
3584b88c807SRodney W. Grimes 
3594b88c807SRodney W. Grimes /*
3604b88c807SRodney W. Grimes  * Print out status about a file system.
3614b88c807SRodney W. Grimes  */
362532aff98SPoul-Henning Kamp static void
36362edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp)
3644b88c807SRodney W. Grimes {
365fde81c7dSKirk McKusick 	static u_long blocksize;
366b7dbd3e9SMark Murray 	static int headerlen, timesthrough = 0;
367a95a13bbSKris Kennaway 	static const char *header;
368fde81c7dSKirk McKusick 	int64_t used, availblks, inodes;
3694b88c807SRodney W. Grimes 
3704b88c807SRodney W. Grimes 	if (++timesthrough == 1) {
3710bd9f151SIan Dowse 		mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
372dd6d33e8SMichael Haro 		if (hflag) {
373dd6d33e8SMichael Haro 			header = "   Size";
3740bd9f151SIan Dowse 			mwp->total = mwp->used = mwp->avail =
3750bd9f151SIan Dowse 			    (int)strlen(header);
376dd6d33e8SMichael Haro 		} else {
377dd6d33e8SMichael Haro 			header = getbsize(&headerlen, &blocksize);
3780bd9f151SIan Dowse 			mwp->total = imax(mwp->total, headerlen);
379dd6d33e8SMichael Haro 		}
3800bd9f151SIan Dowse 		mwp->used = imax(mwp->used, (int)strlen("Used"));
3810bd9f151SIan Dowse 		mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
38262edbd31SIan Dowse 
383f694b8adSMark Murray 		(void)printf("%-*s %-*s %*s %*s Capacity",
3840bd9f151SIan Dowse 		    mwp->mntfrom, "Filesystem", mwp->total, header,
3850bd9f151SIan Dowse 		    mwp->used, "Used", mwp->avail, "Avail");
38662edbd31SIan Dowse 		if (iflag) {
3870bd9f151SIan Dowse 			mwp->iused = imax(mwp->iused, (int)strlen("  iused"));
3880bd9f151SIan Dowse 			mwp->ifree = imax(mwp->ifree, (int)strlen("ifree"));
389f694b8adSMark Murray 			(void)printf(" %*s %*s %%iused",
3900bd9f151SIan Dowse 			    mwp->iused - 2, "iused", mwp->ifree, "ifree");
39162edbd31SIan Dowse 		}
3924b88c807SRodney W. Grimes 		(void)printf("  Mounted on\n");
3934b88c807SRodney W. Grimes 	}
3940bd9f151SIan Dowse 	(void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
3954b88c807SRodney W. Grimes 	used = sfsp->f_blocks - sfsp->f_bfree;
3964b88c807SRodney W. Grimes 	availblks = sfsp->f_bavail + used;
397dd6d33e8SMichael Haro 	if (hflag) {
398dd6d33e8SMichael Haro 		prthuman(sfsp, used);
399dd6d33e8SMichael Haro 	} else {
400ea64ad77SKris Kennaway 		(void)printf(" %*jd %*jd %*jd",
401841fe8e8SDavid Schultz 		    mwp->total, fsbtoblk(sfsp->f_blocks,
4020bd9f151SIan Dowse 		    sfsp->f_bsize, blocksize),
403841fe8e8SDavid Schultz 		    mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
404841fe8e8SDavid Schultz 		    mwp->avail, fsbtoblk(sfsp->f_bavail,
4050bd9f151SIan Dowse 		    sfsp->f_bsize, blocksize));
406dd6d33e8SMichael Haro 	}
4074b88c807SRodney W. Grimes 	(void)printf(" %5.0f%%",
4084b88c807SRodney W. Grimes 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
4094b88c807SRodney W. Grimes 	if (iflag) {
4104b88c807SRodney W. Grimes 		inodes = sfsp->f_files;
4114b88c807SRodney W. Grimes 		used = inodes - sfsp->f_ffree;
4120bd9f151SIan Dowse 		(void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used,
4130bd9f151SIan Dowse 		    mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 :
4140bd9f151SIan Dowse 		    (double)used / (double)inodes * 100.0);
415cca108e6SDavid E. O'Brien 	} else
416cca108e6SDavid E. O'Brien 		(void)printf("  ");
417c6f13844SDavid E. O'Brien 	if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
418076419d2SDavid E. O'Brien 		(void)printf("  %s", sfsp->f_mntonname);
419076419d2SDavid E. O'Brien 	(void)printf("\n");
420076419d2SDavid E. O'Brien }
421076419d2SDavid E. O'Brien 
422076419d2SDavid E. O'Brien void
423076419d2SDavid E. O'Brien addstat(struct statfs *totalfsp, struct statfs *statfsp)
424076419d2SDavid E. O'Brien {
425c6f13844SDavid E. O'Brien 	uint64_t bsize;
426076419d2SDavid E. O'Brien 
427c6f13844SDavid E. O'Brien 	bsize = statfsp->f_bsize / totalfsp->f_bsize;
428076419d2SDavid E. O'Brien 	totalfsp->f_blocks += statfsp->f_blocks * bsize;
429076419d2SDavid E. O'Brien 	totalfsp->f_bfree += statfsp->f_bfree * bsize;
430076419d2SDavid E. O'Brien 	totalfsp->f_bavail += statfsp->f_bavail * bsize;
431076419d2SDavid E. O'Brien 	totalfsp->f_files += statfsp->f_files;
432076419d2SDavid E. O'Brien 	totalfsp->f_ffree += statfsp->f_ffree;
4334b88c807SRodney W. Grimes }
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes /*
43662edbd31SIan Dowse  * Update the maximum field-width information in `mwp' based on
43762edbd31SIan Dowse  * the file system specified by `sfsp'.
43862edbd31SIan Dowse  */
439532aff98SPoul-Henning Kamp static void
440b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
44162edbd31SIan Dowse {
442fde81c7dSKirk McKusick 	static u_long blocksize = 0;
443dc474219SMike Barcroft 	int dummy;
44462edbd31SIan Dowse 
44562edbd31SIan Dowse 	if (blocksize == 0)
44662edbd31SIan Dowse 		getbsize(&dummy, &blocksize);
44762edbd31SIan Dowse 
4480bd9f151SIan Dowse 	mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
4490bd9f151SIan Dowse 	mwp->total = imax(mwp->total, int64width(
450fde81c7dSKirk McKusick 	    fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
4510bd9f151SIan Dowse 	mwp->used = imax(mwp->used,
4520bd9f151SIan Dowse 	    int64width(fsbtoblk((int64_t)sfsp->f_blocks -
453fde81c7dSKirk McKusick 	    (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
4540bd9f151SIan Dowse 	mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
45562edbd31SIan Dowse 	    sfsp->f_bsize, blocksize)));
4560bd9f151SIan Dowse 	mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
45762edbd31SIan Dowse 	    sfsp->f_ffree));
4580bd9f151SIan Dowse 	mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
45962edbd31SIan Dowse }
46062edbd31SIan Dowse 
4610bd9f151SIan Dowse /* Return the width in characters of the specified value. */
4620bd9f151SIan Dowse static int
463fde81c7dSKirk McKusick int64width(int64_t val)
46462edbd31SIan Dowse {
4650bd9f151SIan Dowse 	int len;
46662edbd31SIan Dowse 
46762edbd31SIan Dowse 	len = 0;
46862edbd31SIan Dowse 	/* Negative or zero values require one extra digit. */
46962edbd31SIan Dowse 	if (val <= 0) {
47062edbd31SIan Dowse 		val = -val;
47162edbd31SIan Dowse 		len++;
47262edbd31SIan Dowse 	}
47362edbd31SIan Dowse 	while (val > 0) {
47462edbd31SIan Dowse 		len++;
47562edbd31SIan Dowse 		val /= 10;
47662edbd31SIan Dowse 	}
47762edbd31SIan Dowse 
47862edbd31SIan Dowse 	return (len);
47962edbd31SIan Dowse }
48062edbd31SIan Dowse 
481532aff98SPoul-Henning Kamp static void
482f9bcb0beSWarner Losh usage(void)
4834b88c807SRodney W. Grimes {
484dd6d33e8SMichael Haro 
485a78192e3SBruce Evans 	(void)fprintf(stderr,
486c6f13844SDavid E. O'Brien "usage: df [-b | -g | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...]\n");
487dd6d33e8SMichael Haro 	exit(EX_USAGE);
4884b88c807SRodney W. Grimes }
489a25695c3SJim Pirzyk 
490532aff98SPoul-Henning Kamp static char *
491f9bcb0beSWarner Losh makenetvfslist(void)
492a25695c3SJim Pirzyk {
493a25695c3SJim Pirzyk 	char *str, *strptr, **listptr;
494b7dbd3e9SMark Murray 	struct xvfsconf *xvfsp, *keep_xvfsp;
4955965373eSMaxime Henrion 	size_t buflen;
4965965373eSMaxime Henrion 	int cnt, i, maxvfsconf;
497a25695c3SJim Pirzyk 
4985965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
4995965373eSMaxime Henrion 		warn("sysctl(vfs.conflist)");
500a25695c3SJim Pirzyk 		return (NULL);
501a25695c3SJim Pirzyk 	}
5025965373eSMaxime Henrion 	xvfsp = malloc(buflen);
5035965373eSMaxime Henrion 	if (xvfsp == NULL) {
5045965373eSMaxime Henrion 		warnx("malloc failed");
5055965373eSMaxime Henrion 		return (NULL);
5065965373eSMaxime Henrion 	}
507b7dbd3e9SMark Murray 	keep_xvfsp = xvfsp;
5085965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
5095965373eSMaxime Henrion 		warn("sysctl(vfs.conflist)");
510b7dbd3e9SMark Murray 		free(keep_xvfsp);
5115965373eSMaxime Henrion 		return (NULL);
5125965373eSMaxime Henrion 	}
5135965373eSMaxime Henrion 	maxvfsconf = buflen / sizeof(struct xvfsconf);
514a25695c3SJim Pirzyk 
515a25695c3SJim Pirzyk 	if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
516a25695c3SJim Pirzyk 		warnx("malloc failed");
517b7dbd3e9SMark Murray 		free(keep_xvfsp);
518a25695c3SJim Pirzyk 		return (NULL);
519a25695c3SJim Pirzyk 	}
520a25695c3SJim Pirzyk 
5215965373eSMaxime Henrion 	for (cnt = 0, i = 0; i < maxvfsconf; i++) {
5225965373eSMaxime Henrion 		if (xvfsp->vfc_flags & VFCF_NETWORK) {
5235965373eSMaxime Henrion 			listptr[cnt++] = strdup(xvfsp->vfc_name);
524a067aeceSGarrett Wollman 			if (listptr[cnt-1] == NULL) {
525a25695c3SJim Pirzyk 				warnx("malloc failed");
526b7dbd3e9SMark Murray 				free(listptr);
527b7dbd3e9SMark Murray 				free(keep_xvfsp);
528a25695c3SJim Pirzyk 				return (NULL);
529a25695c3SJim Pirzyk 			}
530a25695c3SJim Pirzyk 		}
5315965373eSMaxime Henrion 		xvfsp++;
5325965373eSMaxime Henrion 	}
533a25695c3SJim Pirzyk 
534cf5b29e1SRuslan Ermilov 	if (cnt == 0 ||
535cf5b29e1SRuslan Ermilov 	    (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
536cf5b29e1SRuslan Ermilov 		if (cnt > 0)
537a25695c3SJim Pirzyk 			warnx("malloc failed");
538a25695c3SJim Pirzyk 		free(listptr);
539b7dbd3e9SMark Murray 		free(keep_xvfsp);
540a25695c3SJim Pirzyk 		return (NULL);
541a25695c3SJim Pirzyk 	}
542a25695c3SJim Pirzyk 
543a25695c3SJim Pirzyk 	*str = 'n'; *(str + 1) = 'o';
544a25695c3SJim Pirzyk 	for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
545a25695c3SJim Pirzyk 		strncpy(strptr, listptr[i], 32);
546a25695c3SJim Pirzyk 		strptr += strlen(listptr[i]);
547a25695c3SJim Pirzyk 		*strptr = ',';
548a25695c3SJim Pirzyk 		free(listptr[i]);
549a25695c3SJim Pirzyk 	}
55016fc3635SMark Murray 	*(--strptr) = '\0';
551a25695c3SJim Pirzyk 
552b7dbd3e9SMark Murray 	free(keep_xvfsp);
553a25695c3SJim Pirzyk 	free(listptr);
554a25695c3SJim Pirzyk 	return (str);
555a25695c3SJim Pirzyk }
556