xref: /freebsd/bin/df/df.c (revision dd6d33e86ec3275636de2f3a0672b8857347c599)
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  * 3. All advertising materials mentioning features or use of this software
194b88c807SRodney W. Grimes  *    must display the following acknowledgement:
204b88c807SRodney W. Grimes  *	This product includes software developed by the University of
214b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
224b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
234b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
244b88c807SRodney W. Grimes  *    without specific prior written permission.
254b88c807SRodney W. Grimes  *
264b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
274b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
284b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
294b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
304b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
314b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
324b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
334b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
344b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
354b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
364b88c807SRodney W. Grimes  * SUCH DAMAGE.
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
4016cc192aSSteve Price static const char copyright[] =
414b88c807SRodney W. Grimes "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
424b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #ifndef lint
4616cc192aSSteve Price #if 0
4716cc192aSSteve Price static char sccsid[] = "@(#)df.c	8.9 (Berkeley) 5/8/95";
4816cc192aSSteve Price #else
4916cc192aSSteve Price static const char rcsid[] =
502a456239SPeter Wemm   "$FreeBSD$";
5116cc192aSSteve Price #endif
524b88c807SRodney W. Grimes #endif /* not lint */
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes #include <sys/param.h>
554b88c807SRodney W. Grimes #include <sys/stat.h>
564b88c807SRodney W. Grimes #include <sys/mount.h>
57a78192e3SBruce Evans #include <ufs/ufs/ufsmount.h>
584b88c807SRodney W. Grimes 
594b88c807SRodney W. Grimes #include <err.h>
604b88c807SRodney W. Grimes #include <errno.h>
614b88c807SRodney W. Grimes #include <fcntl.h>
62dd6d33e8SMichael Haro #include <math.h>
634b88c807SRodney W. Grimes #include <stdio.h>
644b88c807SRodney W. Grimes #include <stdlib.h>
654b88c807SRodney W. Grimes #include <string.h>
66dd6d33e8SMichael Haro #include <sysexits.h>
674b88c807SRodney W. Grimes #include <unistd.h>
684b88c807SRodney W. Grimes 
69dd6d33e8SMichael Haro #define UNITS_SI 1
70dd6d33e8SMichael Haro #define UNITS_2 2
71dd6d33e8SMichael Haro 
72dd6d33e8SMichael Haro #define KILO_SZ(n) (n)
73dd6d33e8SMichael Haro #define MEGA_SZ(n) ((n) * (n))
74dd6d33e8SMichael Haro #define GIGA_SZ(n) ((n) * (n) * (n))
75dd6d33e8SMichael Haro #define TERA_SZ(n) ((n) * (n) * (n) * (n))
76dd6d33e8SMichael Haro #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
77dd6d33e8SMichael Haro 
78dd6d33e8SMichael Haro #define KILO_2_SZ (KILO_SZ(1024ULL))
79dd6d33e8SMichael Haro #define MEGA_2_SZ (MEGA_SZ(1024ULL))
80dd6d33e8SMichael Haro #define GIGA_2_SZ (GIGA_SZ(1024ULL))
81dd6d33e8SMichael Haro #define TERA_2_SZ (TERA_SZ(1024ULL))
82dd6d33e8SMichael Haro #define PETA_2_SZ (PETA_SZ(1024ULL))
83dd6d33e8SMichael Haro 
84dd6d33e8SMichael Haro #define KILO_SI_SZ (KILO_SZ(1000ULL))
85dd6d33e8SMichael Haro #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
86dd6d33e8SMichael Haro #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
87dd6d33e8SMichael Haro #define TERA_SI_SZ (TERA_SZ(1000ULL))
88dd6d33e8SMichael Haro #define PETA_SI_SZ (PETA_SZ(1000ULL))
89dd6d33e8SMichael Haro 
90dd6d33e8SMichael Haro unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
91dd6d33e8SMichael Haro unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
92dd6d33e8SMichael Haro unsigned long long *valp;
93dd6d33e8SMichael Haro 
94dd6d33e8SMichael Haro typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
95dd6d33e8SMichael Haro 
96dd6d33e8SMichael Haro int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
97dd6d33e8SMichael Haro 
98a78192e3SBruce Evans int	  checkvfsname __P((const char *, char **));
99a78192e3SBruce Evans char	**makevfslist __P((char *));
100a78192e3SBruce Evans long	  regetmntinfo __P((struct statfs **, long, char **));
1014b88c807SRodney W. Grimes int	  bread __P((off_t, void *, int));
1024b88c807SRodney W. Grimes char	 *getmntpt __P((char *));
103dd6d33e8SMichael Haro void	  prthuman __P((struct statfs *, long));
104dd6d33e8SMichael Haro void	  prthumanval __P((double));
1054b88c807SRodney W. Grimes void	  prtstat __P((struct statfs *, int));
106b8904f2aSJoerg Wunsch int	  ufs_df __P((char *, int));
107dd6d33e8SMichael Haro unit_t	  unit_adjust __P((double *));
1084b88c807SRodney W. Grimes void	  usage __P((void));
1094b88c807SRodney W. Grimes 
110dd6d33e8SMichael Haro int	aflag = 0, hflag, iflag, nflag;
1114b88c807SRodney W. Grimes struct	ufs_args mdev;
1124b88c807SRodney W. Grimes 
1134b88c807SRodney W. Grimes int
1144b88c807SRodney W. Grimes main(argc, argv)
1154b88c807SRodney W. Grimes 	int argc;
1164b88c807SRodney W. Grimes 	char *argv[];
1174b88c807SRodney W. Grimes {
1184b88c807SRodney W. Grimes 	struct stat stbuf;
1194b88c807SRodney W. Grimes 	struct statfs statfsbuf, *mntbuf;
120a78192e3SBruce Evans 	long mntsize;
12180214f04SJoerg Wunsch 	int ch, err, i, maxwidth, rv, width;
122dd4cdf58SPeter Wemm 	char *mntpt, *mntpath, **vfslist;
1234b88c807SRodney W. Grimes 
124a78192e3SBruce Evans 	vfslist = NULL;
125dd6d33e8SMichael Haro 	while ((ch = getopt(argc, argv, "abHhikmnPt:")) != -1)
1264b88c807SRodney W. Grimes 		switch (ch) {
1275b42dac8SJulian Elischer 		case 'a':
1285b42dac8SJulian Elischer 			aflag = 1;
1295b42dac8SJulian Elischer 			break;
130dd6d33e8SMichael Haro 		case 'b':
131dd6d33e8SMichael Haro 				/* FALLTHROUGH */
132dd6d33e8SMichael Haro 		case 'P':
133dd6d33e8SMichael Haro 			putenv("BLOCKSIZE=512");
134dd6d33e8SMichael Haro 			hflag = 0;
135dd6d33e8SMichael Haro 			break;
136dd6d33e8SMichael Haro 		case 'H':
137dd6d33e8SMichael Haro 			hflag = UNITS_SI;
138dd6d33e8SMichael Haro 			valp = vals_si;
139dd6d33e8SMichael Haro 			break;
140dd6d33e8SMichael Haro 		case 'h':
141dd6d33e8SMichael Haro 			hflag = UNITS_2;
142dd6d33e8SMichael Haro 			valp = vals_base2;
143dd6d33e8SMichael Haro 			break;
1444b88c807SRodney W. Grimes 		case 'i':
1454b88c807SRodney W. Grimes 			iflag = 1;
1464b88c807SRodney W. Grimes 			break;
1477f0eabfdSGarrett Wollman 		case 'k':
1489d4081eeSDavid Greenman 			putenv("BLOCKSIZE=1k");
149dd6d33e8SMichael Haro 			hflag = 0;
150dd6d33e8SMichael Haro 			break;
151dd6d33e8SMichael Haro 		case 'm':
152dd6d33e8SMichael Haro 			putenv("BLOCKSIZE=1m");
153dd6d33e8SMichael Haro 			hflag = 0;
1547f0eabfdSGarrett Wollman 			break;
1554b88c807SRodney W. Grimes 		case 'n':
1564b88c807SRodney W. Grimes 			nflag = 1;
1574b88c807SRodney W. Grimes 			break;
1584b88c807SRodney W. Grimes 		case 't':
159a78192e3SBruce Evans 			if (vfslist != NULL)
160a78192e3SBruce Evans 				errx(1, "only one -t option may be specified.");
161a78192e3SBruce Evans 			vfslist = makevfslist(optarg);
1624b88c807SRodney W. Grimes 			break;
1634b88c807SRodney W. Grimes 		case '?':
1644b88c807SRodney W. Grimes 		default:
1654b88c807SRodney W. Grimes 			usage();
1664b88c807SRodney W. Grimes 		}
1674b88c807SRodney W. Grimes 	argc -= optind;
1684b88c807SRodney W. Grimes 	argv += optind;
1694b88c807SRodney W. Grimes 
1704b88c807SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
1714b88c807SRodney W. Grimes 	maxwidth = 0;
1724b88c807SRodney W. Grimes 	for (i = 0; i < mntsize; i++) {
1734b88c807SRodney W. Grimes 		width = strlen(mntbuf[i].f_mntfromname);
1744b88c807SRodney W. Grimes 		if (width > maxwidth)
1754b88c807SRodney W. Grimes 			maxwidth = width;
1764b88c807SRodney W. Grimes 	}
1774b88c807SRodney W. Grimes 
178b8904f2aSJoerg Wunsch 	rv = 0;
1794b88c807SRodney W. Grimes 	if (!*argv) {
180a78192e3SBruce Evans 		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
181a78192e3SBruce Evans 		if (vfslist != NULL) {
1824b88c807SRodney W. Grimes 			maxwidth = 0;
1834b88c807SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
1844b88c807SRodney W. Grimes 				width = strlen(mntbuf[i].f_mntfromname);
1854b88c807SRodney W. Grimes 				if (width > maxwidth)
1864b88c807SRodney W. Grimes 					maxwidth = width;
1874b88c807SRodney W. Grimes 			}
1884b88c807SRodney W. Grimes 		}
1895b42dac8SJulian Elischer 		for (i = 0; i < mntsize; i++) {
1905b42dac8SJulian Elischer 			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
1914b88c807SRodney W. Grimes 				prtstat(&mntbuf[i], maxwidth);
1925b42dac8SJulian Elischer 		}
193b8904f2aSJoerg Wunsch 		exit(rv);
1944b88c807SRodney W. Grimes 	}
1954b88c807SRodney W. Grimes 
1964b88c807SRodney W. Grimes 	for (; *argv; argv++) {
1974b88c807SRodney W. Grimes 		if (stat(*argv, &stbuf) < 0) {
1984b88c807SRodney W. Grimes 			err = errno;
1994b88c807SRodney W. Grimes 			if ((mntpt = getmntpt(*argv)) == 0) {
2004b88c807SRodney W. Grimes 				warn("%s", *argv);
201b8904f2aSJoerg Wunsch 				rv = 1;
2024b88c807SRodney W. Grimes 				continue;
2034b88c807SRodney W. Grimes 			}
2044b88c807SRodney W. Grimes 		} else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
205b8904f2aSJoerg Wunsch 			rv = ufs_df(*argv, maxwidth) || rv;
2064b88c807SRodney W. Grimes 			continue;
2074b88c807SRodney W. Grimes 		} else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
2084b88c807SRodney W. Grimes 			if ((mntpt = getmntpt(*argv)) == 0) {
2094b88c807SRodney W. Grimes 				mdev.fspec = *argv;
210dd4cdf58SPeter Wemm 				mntpath = strdup("/tmp/df.XXXXXX");
211dd4cdf58SPeter Wemm 				if (mntpath == NULL) {
212dd4cdf58SPeter Wemm 					warn("strdup failed");
213b8904f2aSJoerg Wunsch 					rv = 1;
2144b88c807SRodney W. Grimes 					continue;
2154b88c807SRodney W. Grimes 				}
216dd4cdf58SPeter Wemm 				mntpt = mkdtemp(mntpath);
217dd4cdf58SPeter Wemm 				if (mntpt == NULL) {
218dd4cdf58SPeter Wemm 					warn("mkdtemp(\"%s\") failed", mntpath);
219dd4cdf58SPeter Wemm 					rv = 1;
220dd4cdf58SPeter Wemm 					free(mntpath);
221dd4cdf58SPeter Wemm 					continue;
222dd4cdf58SPeter Wemm 				}
223a78192e3SBruce Evans 				if (mount("ufs", mntpt, MNT_RDONLY,
2244b88c807SRodney W. Grimes 				    &mdev) != 0) {
225b8904f2aSJoerg Wunsch 					rv = ufs_df(*argv, maxwidth) || rv;
2264b88c807SRodney W. Grimes 					(void)rmdir(mntpt);
227dd4cdf58SPeter Wemm 					free(mntpath);
2284b88c807SRodney W. Grimes 					continue;
2299408216eSGuido van Rooij 				} else if (statfs(mntpt, &statfsbuf) == 0) {
2304b88c807SRodney W. Grimes 					statfsbuf.f_mntonname[0] = '\0';
2314b88c807SRodney W. Grimes 					prtstat(&statfsbuf, maxwidth);
232b8904f2aSJoerg Wunsch 				} else {
2334b88c807SRodney W. Grimes 					warn("%s", *argv);
234b8904f2aSJoerg Wunsch 					rv = 1;
235b8904f2aSJoerg Wunsch 				}
2364b88c807SRodney W. Grimes 				(void)unmount(mntpt, 0);
2374b88c807SRodney W. Grimes 				(void)rmdir(mntpt);
238dd4cdf58SPeter Wemm 				free(mntpath);
2394b88c807SRodney W. Grimes 				continue;
2404b88c807SRodney W. Grimes 			}
2414b88c807SRodney W. Grimes 		} else
2424b88c807SRodney W. Grimes 			mntpt = *argv;
2434b88c807SRodney W. Grimes 		/*
2444b88c807SRodney W. Grimes 		 * Statfs does not take a `wait' flag, so we cannot
2454b88c807SRodney W. Grimes 		 * implement nflag here.
2464b88c807SRodney W. Grimes 		 */
2474b88c807SRodney W. Grimes 		if (statfs(mntpt, &statfsbuf) < 0) {
2484b88c807SRodney W. Grimes 			warn("%s", mntpt);
249b8904f2aSJoerg Wunsch 			rv = 1;
2504b88c807SRodney W. Grimes 			continue;
2514b88c807SRodney W. Grimes 		}
2524b88c807SRodney W. Grimes 		if (argc == 1)
2534b88c807SRodney W. Grimes 			maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
2544b88c807SRodney W. Grimes 		prtstat(&statfsbuf, maxwidth);
2554b88c807SRodney W. Grimes 	}
256b8904f2aSJoerg Wunsch 	return (rv);
2574b88c807SRodney W. Grimes }
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes char *
2604b88c807SRodney W. Grimes getmntpt(name)
2614b88c807SRodney W. Grimes 	char *name;
2624b88c807SRodney W. Grimes {
2634b88c807SRodney W. Grimes 	long mntsize, i;
2644b88c807SRodney W. Grimes 	struct statfs *mntbuf;
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
2674b88c807SRodney W. Grimes 	for (i = 0; i < mntsize; i++) {
2684b88c807SRodney W. Grimes 		if (!strcmp(mntbuf[i].f_mntfromname, name))
2694b88c807SRodney W. Grimes 			return (mntbuf[i].f_mntonname);
2704b88c807SRodney W. Grimes 	}
2714b88c807SRodney W. Grimes 	return (0);
2724b88c807SRodney W. Grimes }
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes /*
2754b88c807SRodney W. Grimes  * Make a pass over the filesystem info in ``mntbuf'' filtering out
276a78192e3SBruce Evans  * filesystem types not in vfslist and possibly re-stating to get
2774b88c807SRodney W. Grimes  * current (not cached) info.  Returns the new count of valid statfs bufs.
2784b88c807SRodney W. Grimes  */
2794b88c807SRodney W. Grimes long
280a78192e3SBruce Evans regetmntinfo(mntbufp, mntsize, vfslist)
2814b88c807SRodney W. Grimes 	struct statfs **mntbufp;
282a78192e3SBruce Evans 	long mntsize;
283a78192e3SBruce Evans 	char **vfslist;
2844b88c807SRodney W. Grimes {
2854b88c807SRodney W. Grimes 	int i, j;
2864b88c807SRodney W. Grimes 	struct statfs *mntbuf;
2874b88c807SRodney W. Grimes 
288a78192e3SBruce Evans 	if (vfslist == NULL)
2894b88c807SRodney W. Grimes 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
2904b88c807SRodney W. Grimes 
2914b88c807SRodney W. Grimes 	mntbuf = *mntbufp;
292a78192e3SBruce Evans 	for (j = 0, i = 0; i < mntsize; i++) {
293a78192e3SBruce Evans 		if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
294a78192e3SBruce Evans 			continue;
2954b88c807SRodney W. Grimes 		if (!nflag)
2964b88c807SRodney W. Grimes 			(void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
2974b88c807SRodney W. Grimes 		else if (i != j)
2984b88c807SRodney W. Grimes 			mntbuf[j] = mntbuf[i];
2994b88c807SRodney W. Grimes 		j++;
3004b88c807SRodney W. Grimes 	}
3014b88c807SRodney W. Grimes 	return (j);
3024b88c807SRodney W. Grimes }
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes /*
305dd6d33e8SMichael Haro  * Output in "human-readable" format.  Uses 3 digits max and puts
306dd6d33e8SMichael Haro  * unit suffixes at the end.  Makes output compact and easy to read,
307dd6d33e8SMichael Haro  * especially on huge disks.
308dd6d33e8SMichael Haro  *
309dd6d33e8SMichael Haro  */
310dd6d33e8SMichael Haro unit_t
311dd6d33e8SMichael Haro unit_adjust(val)
312dd6d33e8SMichael Haro 	double *val;
313dd6d33e8SMichael Haro {
314dd6d33e8SMichael Haro 	double abval;
315dd6d33e8SMichael Haro 	unit_t unit;
316dd6d33e8SMichael Haro 	unsigned int unit_sz;
317dd6d33e8SMichael Haro 
318dd6d33e8SMichael Haro 	abval = fabs(*val);
319dd6d33e8SMichael Haro 
320dd6d33e8SMichael Haro 	unit_sz = abval ? ilogb(abval) / 10 : 0;
321dd6d33e8SMichael Haro 
322dd6d33e8SMichael Haro 	if (unit_sz >= UNIT_MAX) {
323dd6d33e8SMichael Haro 		unit = NONE;
324dd6d33e8SMichael Haro 	} else {
325dd6d33e8SMichael Haro 		unit = unitp[unit_sz];
326dd6d33e8SMichael Haro 		*val /= (double)valp[unit_sz];
327dd6d33e8SMichael Haro 	}
328dd6d33e8SMichael Haro 
329dd6d33e8SMichael Haro 	return (unit);
330dd6d33e8SMichael Haro }
331dd6d33e8SMichael Haro 
332dd6d33e8SMichael Haro void
333dd6d33e8SMichael Haro prthuman(sfsp, used)
334dd6d33e8SMichael Haro 	struct statfs *sfsp;
335dd6d33e8SMichael Haro 	long used;
336dd6d33e8SMichael Haro {
337dd6d33e8SMichael Haro 
338dd6d33e8SMichael Haro 	prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize);
339dd6d33e8SMichael Haro 	prthumanval((double)used * (double)sfsp->f_bsize);
340dd6d33e8SMichael Haro 	prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize);
341dd6d33e8SMichael Haro }
342dd6d33e8SMichael Haro 
343dd6d33e8SMichael Haro void
344dd6d33e8SMichael Haro prthumanval(bytes)
345dd6d33e8SMichael Haro 	double bytes;
346dd6d33e8SMichael Haro {
347dd6d33e8SMichael Haro 
348dd6d33e8SMichael Haro 	unit_t unit;
349dd6d33e8SMichael Haro 	unit = unit_adjust(&bytes);
350dd6d33e8SMichael Haro 
351dd6d33e8SMichael Haro 	if (bytes == 0)
352dd6d33e8SMichael Haro 		(void)printf("     0B");
353dd6d33e8SMichael Haro 	else if (bytes > 10)
354dd6d33e8SMichael Haro 		(void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]);
355dd6d33e8SMichael Haro 	else
356dd6d33e8SMichael Haro 		(void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]);
357dd6d33e8SMichael Haro }
358dd6d33e8SMichael Haro 
359dd6d33e8SMichael Haro /*
3604b88c807SRodney W. Grimes  * Convert statfs returned filesystem size into BLOCKSIZE units.
3614b88c807SRodney W. Grimes  * Attempts to avoid overflow for large filesystems.
3624b88c807SRodney W. Grimes  */
3634b88c807SRodney W. Grimes #define fsbtoblk(num, fsbs, bs) \
3644b88c807SRodney W. Grimes 	(((fsbs) != 0 && (fsbs) < (bs)) ? \
3654b88c807SRodney W. Grimes 		(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes /*
3684b88c807SRodney W. Grimes  * Print out status about a filesystem.
3694b88c807SRodney W. Grimes  */
3704b88c807SRodney W. Grimes void
3714b88c807SRodney W. Grimes prtstat(sfsp, maxwidth)
3724b88c807SRodney W. Grimes 	struct statfs *sfsp;
3734b88c807SRodney W. Grimes 	int maxwidth;
3744b88c807SRodney W. Grimes {
3754b88c807SRodney W. Grimes 	static long blocksize;
3764b88c807SRodney W. Grimes 	static int headerlen, timesthrough;
3774b88c807SRodney W. Grimes 	static char *header;
3784b88c807SRodney W. Grimes 	long used, availblks, inodes;
3794b88c807SRodney W. Grimes 
3804b88c807SRodney W. Grimes 	if (maxwidth < 11)
3814b88c807SRodney W. Grimes 		maxwidth = 11;
3824b88c807SRodney W. Grimes 	if (++timesthrough == 1) {
383dd6d33e8SMichael Haro 		if (hflag) {
384dd6d33e8SMichael Haro 			header = "  Size";
385dd6d33e8SMichael Haro 			headerlen = strlen(header);
386dd6d33e8SMichael Haro 			(void)printf("%-*.*s %-s   Used  Avail Capacity",
3874b88c807SRodney W. Grimes 				maxwidth, maxwidth, "Filesystem", header);
388dd6d33e8SMichael Haro 		} else {
389dd6d33e8SMichael Haro 			header = getbsize(&headerlen, &blocksize);
390dd6d33e8SMichael Haro 			(void)printf("%-*.*s %-s     Used    Avail Capacity",
391dd6d33e8SMichael Haro 				maxwidth, maxwidth, "Filesystem", header);
392dd6d33e8SMichael Haro 		}
3934b88c807SRodney W. Grimes 		if (iflag)
3944b88c807SRodney W. Grimes 			(void)printf(" iused   ifree  %%iused");
3954b88c807SRodney W. Grimes 		(void)printf("  Mounted on\n");
3964b88c807SRodney W. Grimes 	}
3974b88c807SRodney W. Grimes 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
3984b88c807SRodney W. Grimes 	used = sfsp->f_blocks - sfsp->f_bfree;
3994b88c807SRodney W. Grimes 	availblks = sfsp->f_bavail + used;
400dd6d33e8SMichael Haro 	if (hflag) {
401dd6d33e8SMichael Haro 		prthuman(sfsp, used);
402dd6d33e8SMichael Haro 	} else {
4034b88c807SRodney W. Grimes 		(void)printf(" %*ld %8ld %8ld", headerlen,
4044b88c807SRodney W. Grimes 	            fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
4054b88c807SRodney W. Grimes 	            fsbtoblk(used, sfsp->f_bsize, blocksize),
4064b88c807SRodney W. Grimes 	            fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
407dd6d33e8SMichael Haro 	}
4084b88c807SRodney W. Grimes 	(void)printf(" %5.0f%%",
4094b88c807SRodney W. Grimes 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
4104b88c807SRodney W. Grimes 	if (iflag) {
4114b88c807SRodney W. Grimes 		inodes = sfsp->f_files;
4124b88c807SRodney W. Grimes 		used = inodes - sfsp->f_ffree;
4134b88c807SRodney W. Grimes 		(void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
4144b88c807SRodney W. Grimes 		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
4154b88c807SRodney W. Grimes 	} else
4164b88c807SRodney W. Grimes 		(void)printf("  ");
4174b88c807SRodney W. Grimes 	(void)printf("  %s\n", sfsp->f_mntonname);
4184b88c807SRodney W. Grimes }
4194b88c807SRodney W. Grimes 
4204b88c807SRodney W. Grimes /*
4214b88c807SRodney W. Grimes  * This code constitutes the pre-system call Berkeley df code for extracting
4224b88c807SRodney W. Grimes  * information from filesystem superblocks.
4234b88c807SRodney W. Grimes  */
424a78192e3SBruce Evans #include <ufs/ufs/dinode.h>
4254b88c807SRodney W. Grimes #include <ufs/ffs/fs.h>
4264b88c807SRodney W. Grimes #include <errno.h>
4274b88c807SRodney W. Grimes #include <fstab.h>
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes union {
4304b88c807SRodney W. Grimes 	struct fs iu_fs;
4314b88c807SRodney W. Grimes 	char dummy[SBSIZE];
4324b88c807SRodney W. Grimes } sb;
4334b88c807SRodney W. Grimes #define sblock sb.iu_fs
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes int	rfd;
4364b88c807SRodney W. Grimes 
437b8904f2aSJoerg Wunsch int
4384b88c807SRodney W. Grimes ufs_df(file, maxwidth)
4394b88c807SRodney W. Grimes 	char *file;
4404b88c807SRodney W. Grimes 	int maxwidth;
4414b88c807SRodney W. Grimes {
4424b88c807SRodney W. Grimes 	struct statfs statfsbuf;
4434b88c807SRodney W. Grimes 	struct statfs *sfsp;
4444b88c807SRodney W. Grimes 	char *mntpt;
4454b88c807SRodney W. Grimes 	static int synced;
4464b88c807SRodney W. Grimes 
4474b88c807SRodney W. Grimes 	if (synced++ == 0)
4484b88c807SRodney W. Grimes 		sync();
4494b88c807SRodney W. Grimes 
4504b88c807SRodney W. Grimes 	if ((rfd = open(file, O_RDONLY)) < 0) {
4514b88c807SRodney W. Grimes 		warn("%s", file);
45280214f04SJoerg Wunsch 		return (1);
4534b88c807SRodney W. Grimes 	}
4544b88c807SRodney W. Grimes 	if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
4554b88c807SRodney W. Grimes 		(void)close(rfd);
45680214f04SJoerg Wunsch 		return (1);
4574b88c807SRodney W. Grimes 	}
4584b88c807SRodney W. Grimes 	sfsp = &statfsbuf;
459a78192e3SBruce Evans 	sfsp->f_type = 1;
460a78192e3SBruce Evans 	strcpy(sfsp->f_fstypename, "ufs");
4614b88c807SRodney W. Grimes 	sfsp->f_flags = 0;
4624b88c807SRodney W. Grimes 	sfsp->f_bsize = sblock.fs_fsize;
4634b88c807SRodney W. Grimes 	sfsp->f_iosize = sblock.fs_bsize;
4644b88c807SRodney W. Grimes 	sfsp->f_blocks = sblock.fs_dsize;
4654b88c807SRodney W. Grimes 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
4664b88c807SRodney W. Grimes 		sblock.fs_cstotal.cs_nffree;
46751ea8b57SBruce Evans 	sfsp->f_bavail = freespace(&sblock, sblock.fs_minfree);
4684b88c807SRodney W. Grimes 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
4694b88c807SRodney W. Grimes 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
4704b88c807SRodney W. Grimes 	sfsp->f_fsid.val[0] = 0;
4714b88c807SRodney W. Grimes 	sfsp->f_fsid.val[1] = 0;
4724b88c807SRodney W. Grimes 	if ((mntpt = getmntpt(file)) == 0)
4734b88c807SRodney W. Grimes 		mntpt = "";
4744b88c807SRodney W. Grimes 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
4754b88c807SRodney W. Grimes 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
4764b88c807SRodney W. Grimes 	prtstat(sfsp, maxwidth);
4774b88c807SRodney W. Grimes 	(void)close(rfd);
47880214f04SJoerg Wunsch 	return (0);
4794b88c807SRodney W. Grimes }
4804b88c807SRodney W. Grimes 
4814b88c807SRodney W. Grimes int
4824b88c807SRodney W. Grimes bread(off, buf, cnt)
4834b88c807SRodney W. Grimes 	off_t off;
4844b88c807SRodney W. Grimes 	void *buf;
4854b88c807SRodney W. Grimes 	int cnt;
4864b88c807SRodney W. Grimes {
4874b88c807SRodney W. Grimes 	int nr;
4884b88c807SRodney W. Grimes 
4894b88c807SRodney W. Grimes 	(void)lseek(rfd, off, SEEK_SET);
4904b88c807SRodney W. Grimes 	if ((nr = read(rfd, buf, cnt)) != cnt) {
4914b88c807SRodney W. Grimes 		/* Probably a dismounted disk if errno == EIO. */
4924b88c807SRodney W. Grimes 		if (errno != EIO)
4934b88c807SRodney W. Grimes 			(void)fprintf(stderr, "\ndf: %qd: %s\n",
4944b88c807SRodney W. Grimes 			    off, strerror(nr > 0 ? EIO : errno));
4954b88c807SRodney W. Grimes 		return (0);
4964b88c807SRodney W. Grimes 	}
4974b88c807SRodney W. Grimes 	return (1);
4984b88c807SRodney W. Grimes }
4994b88c807SRodney W. Grimes 
5004b88c807SRodney W. Grimes void
5014b88c807SRodney W. Grimes usage()
5024b88c807SRodney W. Grimes {
503dd6d33e8SMichael Haro 
504a78192e3SBruce Evans 	(void)fprintf(stderr,
505dd6d33e8SMichael Haro 	    "usage: df [-b | -H | -h | -k | -m | -P] [-ain] [-t type] [file | filesystem ...]\n");
506dd6d33e8SMichael Haro 	exit(EX_USAGE);
5074b88c807SRodney W. Grimes }
508