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 3909a80d48SDavid E. O'Brien #if 0 404b88c807SRodney W. Grimes #ifndef lint 4116cc192aSSteve Price static const char copyright[] = 424b88c807SRodney W. Grimes "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 434b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 444b88c807SRodney W. Grimes #endif /* not lint */ 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #ifndef lint 4716cc192aSSteve Price static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; 484b88c807SRodney W. Grimes #endif /* not lint */ 4909a80d48SDavid E. O'Brien #endif 505eb43ac2SDavid E. O'Brien #include <sys/cdefs.h> 515eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 524b88c807SRodney W. Grimes 534b88c807SRodney W. Grimes #include <sys/param.h> 544b88c807SRodney W. Grimes #include <sys/stat.h> 554b88c807SRodney W. Grimes #include <sys/mount.h> 56a25695c3SJim Pirzyk #include <sys/sysctl.h> 57a78192e3SBruce Evans #include <ufs/ufs/ufsmount.h> 584b88c807SRodney W. Grimes #include <err.h> 59dd6d33e8SMichael Haro #include <math.h> 60b7dbd3e9SMark Murray #include <inttypes.h> 614b88c807SRodney W. Grimes #include <stdio.h> 624b88c807SRodney W. Grimes #include <stdlib.h> 634b88c807SRodney W. Grimes #include <string.h> 64dd6d33e8SMichael Haro #include <sysexits.h> 654b88c807SRodney W. Grimes #include <unistd.h> 664b88c807SRodney W. Grimes 67532aff98SPoul-Henning Kamp #include "extern.h" 68532aff98SPoul-Henning Kamp 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 9062edbd31SIan Dowse /* Maximum widths of various fields. */ 9162edbd31SIan Dowse struct maxwidths { 92b7dbd3e9SMark Murray size_t mntfrom; 93b7dbd3e9SMark Murray size_t total; 94b7dbd3e9SMark Murray size_t used; 95b7dbd3e9SMark Murray size_t avail; 96b7dbd3e9SMark Murray size_t iused; 97b7dbd3e9SMark Murray size_t ifree; 9862edbd31SIan Dowse }; 9962edbd31SIan Dowse 100b7dbd3e9SMark Murray static uintmax_t vals_si [] = { 101b7dbd3e9SMark Murray 1, 102b7dbd3e9SMark Murray KILO_SI_SZ, 103b7dbd3e9SMark Murray MEGA_SI_SZ, 104b7dbd3e9SMark Murray GIGA_SI_SZ, 105b7dbd3e9SMark Murray TERA_SI_SZ, 106b7dbd3e9SMark Murray PETA_SI_SZ 107b7dbd3e9SMark Murray }; 108b7dbd3e9SMark Murray static uintmax_t vals_base2[] = { 109b7dbd3e9SMark Murray 1, 110b7dbd3e9SMark Murray KILO_2_SZ, 111b7dbd3e9SMark Murray MEGA_2_SZ, 112b7dbd3e9SMark Murray GIGA_2_SZ, 113b7dbd3e9SMark Murray TERA_2_SZ, 114b7dbd3e9SMark Murray PETA_2_SZ 115b7dbd3e9SMark Murray }; 116b7dbd3e9SMark Murray static uintmax_t *valp; 117dd6d33e8SMichael Haro 118dd6d33e8SMichael Haro typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t; 119dd6d33e8SMichael Haro 120b7dbd3e9SMark Murray static unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA }; 121dd6d33e8SMichael Haro 122b7dbd3e9SMark Murray static char *getmntpt(const char *); 123fde81c7dSKirk McKusick static size_t int64width(int64_t); 124532aff98SPoul-Henning Kamp static char *makenetvfslist(void); 125fde81c7dSKirk McKusick static void prthuman(const struct statfs *, int64_t); 126532aff98SPoul-Henning Kamp static void prthumanval(double); 127532aff98SPoul-Henning Kamp static void prtstat(struct statfs *, struct maxwidths *); 128be2c4e54SDavid E. O'Brien static size_t regetmntinfo(struct statfs **, long, const char **); 129532aff98SPoul-Henning Kamp static unit_t unit_adjust(double *); 130b7dbd3e9SMark Murray static void update_maxwidths(struct maxwidths *, const struct statfs *); 131532aff98SPoul-Henning Kamp static void usage(void); 1324b88c807SRodney W. Grimes 133b7dbd3e9SMark Murray static __inline u_int 134b7dbd3e9SMark Murray max(u_int a, u_int b) 13562edbd31SIan Dowse { 136b7dbd3e9SMark Murray return (a > b ? a : b); 13762edbd31SIan Dowse } 13862edbd31SIan Dowse 139532aff98SPoul-Henning Kamp static int aflag = 0, hflag, iflag, nflag; 140532aff98SPoul-Henning Kamp static struct ufs_args mdev; 141532aff98SPoul-Henning Kamp 1424b88c807SRodney W. Grimes int 143f9bcb0beSWarner Losh main(int argc, char *argv[]) 1444b88c807SRodney W. Grimes { 1454b88c807SRodney W. Grimes struct stat stbuf; 1464b88c807SRodney W. Grimes struct statfs statfsbuf, *mntbuf; 14762edbd31SIan Dowse struct maxwidths maxwidths; 148a95a13bbSKris Kennaway const char *fstype; 149532aff98SPoul-Henning Kamp char *mntpath, *mntpt; 150532aff98SPoul-Henning Kamp const char **vfslist; 151be2c4e54SDavid E. O'Brien size_t i, mntsize; 152be2c4e54SDavid E. O'Brien int ch, rv; 153f3895a82SKris Kennaway 154f3895a82SKris Kennaway fstype = "ufs"; 1554b88c807SRodney W. Grimes 156a78192e3SBruce Evans vfslist = NULL; 157a25695c3SJim Pirzyk while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1) 1584b88c807SRodney W. Grimes switch (ch) { 1595b42dac8SJulian Elischer case 'a': 1605b42dac8SJulian Elischer aflag = 1; 1615b42dac8SJulian Elischer break; 162dd6d33e8SMichael Haro case 'b': 163dd6d33e8SMichael Haro /* FALLTHROUGH */ 164dd6d33e8SMichael Haro case 'P': 165dd6d33e8SMichael Haro putenv("BLOCKSIZE=512"); 166dd6d33e8SMichael Haro hflag = 0; 167dd6d33e8SMichael Haro break; 16893a3fa19SJohn W. De Boskey case 'g': 16993a3fa19SJohn W. De Boskey putenv("BLOCKSIZE=1g"); 17093a3fa19SJohn W. De Boskey hflag = 0; 17193a3fa19SJohn W. De Boskey break; 172dd6d33e8SMichael Haro case 'H': 173dd6d33e8SMichael Haro hflag = UNITS_SI; 174dd6d33e8SMichael Haro valp = vals_si; 175dd6d33e8SMichael Haro break; 176dd6d33e8SMichael Haro case 'h': 177dd6d33e8SMichael Haro hflag = UNITS_2; 178dd6d33e8SMichael Haro valp = vals_base2; 179dd6d33e8SMichael Haro break; 1804b88c807SRodney W. Grimes case 'i': 1814b88c807SRodney W. Grimes iflag = 1; 1824b88c807SRodney W. Grimes break; 1837f0eabfdSGarrett Wollman case 'k': 1849d4081eeSDavid Greenman putenv("BLOCKSIZE=1k"); 185dd6d33e8SMichael Haro hflag = 0; 186dd6d33e8SMichael Haro break; 187a25695c3SJim Pirzyk case 'l': 188a25695c3SJim Pirzyk if (vfslist != NULL) 189a25695c3SJim Pirzyk errx(1, "-l and -t are mutually exclusive."); 190a25695c3SJim Pirzyk vfslist = makevfslist(makenetvfslist()); 191a25695c3SJim Pirzyk break; 192dd6d33e8SMichael Haro case 'm': 193dd6d33e8SMichael Haro putenv("BLOCKSIZE=1m"); 194dd6d33e8SMichael Haro hflag = 0; 1957f0eabfdSGarrett Wollman break; 1964b88c807SRodney W. Grimes case 'n': 1974b88c807SRodney W. Grimes nflag = 1; 1984b88c807SRodney W. Grimes break; 1994b88c807SRodney W. Grimes case 't': 200a78192e3SBruce Evans if (vfslist != NULL) 2017b3a12a8SPhilippe Charnier errx(1, "only one -t option may be specified"); 202f3895a82SKris Kennaway fstype = optarg; 203a78192e3SBruce Evans vfslist = makevfslist(optarg); 2044b88c807SRodney W. Grimes break; 2054b88c807SRodney W. Grimes case '?': 2064b88c807SRodney W. Grimes default: 2074b88c807SRodney W. Grimes usage(); 2084b88c807SRodney W. Grimes } 2094b88c807SRodney W. Grimes argc -= optind; 2104b88c807SRodney W. Grimes argv += optind; 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 21362edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 21462edbd31SIan Dowse for (i = 0; i < mntsize; i++) 21562edbd31SIan Dowse update_maxwidths(&maxwidths, &mntbuf[i]); 2164b88c807SRodney W. Grimes 217b8904f2aSJoerg Wunsch rv = 0; 2184b88c807SRodney W. Grimes if (!*argv) { 219a78192e3SBruce Evans mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 22062edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 22162edbd31SIan Dowse for (i = 0; i < mntsize; i++) 22262edbd31SIan Dowse update_maxwidths(&maxwidths, &mntbuf[i]); 2235b42dac8SJulian Elischer for (i = 0; i < mntsize; i++) { 2245b42dac8SJulian Elischer if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 22562edbd31SIan Dowse prtstat(&mntbuf[i], &maxwidths); 2265b42dac8SJulian Elischer } 227b8904f2aSJoerg Wunsch exit(rv); 2284b88c807SRodney W. Grimes } 2294b88c807SRodney W. Grimes 2304b88c807SRodney W. Grimes for (; *argv; argv++) { 2314b88c807SRodney W. Grimes if (stat(*argv, &stbuf) < 0) { 2324b88c807SRodney W. Grimes if ((mntpt = getmntpt(*argv)) == 0) { 2334b88c807SRodney W. Grimes warn("%s", *argv); 234b8904f2aSJoerg Wunsch rv = 1; 2354b88c807SRodney W. Grimes continue; 2364b88c807SRodney W. Grimes } 237f3895a82SKris Kennaway } else if (S_ISCHR(stbuf.st_mode)) { 238f3895a82SKris Kennaway if ((mntpt = getmntpt(*argv)) == 0) { 239f3895a82SKris Kennaway mdev.fspec = *argv; 240f3895a82SKris Kennaway mntpath = strdup("/tmp/df.XXXXXX"); 241f3895a82SKris Kennaway if (mntpath == NULL) { 242f3895a82SKris Kennaway warn("strdup failed"); 243f3895a82SKris Kennaway rv = 1; 2444b88c807SRodney W. Grimes continue; 245f3895a82SKris Kennaway } 246f3895a82SKris Kennaway mntpt = mkdtemp(mntpath); 247f3895a82SKris Kennaway if (mntpt == NULL) { 248f3895a82SKris Kennaway warn("mkdtemp(\"%s\") failed", mntpath); 249f3895a82SKris Kennaway rv = 1; 250f3895a82SKris Kennaway free(mntpath); 251f3895a82SKris Kennaway continue; 252f3895a82SKris Kennaway } 253f3895a82SKris Kennaway if (mount(fstype, mntpt, MNT_RDONLY, 254f3895a82SKris Kennaway &mdev) != 0) { 255532aff98SPoul-Henning Kamp warn("%s", *argv); 256532aff98SPoul-Henning Kamp rv = 1; 257f3895a82SKris Kennaway (void)rmdir(mntpt); 258f3895a82SKris Kennaway free(mntpath); 259f3895a82SKris Kennaway continue; 260f3895a82SKris Kennaway } else if (statfs(mntpt, &statfsbuf) == 0) { 261f3895a82SKris Kennaway statfsbuf.f_mntonname[0] = '\0'; 26262edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 263f3895a82SKris Kennaway } else { 264f3895a82SKris Kennaway warn("%s", *argv); 265f3895a82SKris Kennaway rv = 1; 266f3895a82SKris Kennaway } 267f3895a82SKris Kennaway (void)unmount(mntpt, 0); 268f3895a82SKris Kennaway (void)rmdir(mntpt); 269f3895a82SKris Kennaway free(mntpath); 270f3895a82SKris Kennaway continue; 271f3895a82SKris Kennaway } 2724b88c807SRodney W. Grimes } else 2734b88c807SRodney W. Grimes mntpt = *argv; 2740e7d023fSBruce Evans 2754b88c807SRodney W. Grimes /* 2764b88c807SRodney W. Grimes * Statfs does not take a `wait' flag, so we cannot 2774b88c807SRodney W. Grimes * implement nflag here. 2784b88c807SRodney W. Grimes */ 2794b88c807SRodney W. Grimes if (statfs(mntpt, &statfsbuf) < 0) { 2804b88c807SRodney W. Grimes warn("%s", mntpt); 281b8904f2aSJoerg Wunsch rv = 1; 2824b88c807SRodney W. Grimes continue; 2834b88c807SRodney W. Grimes } 2840e7d023fSBruce Evans 2850e7d023fSBruce Evans /* 2860e7d023fSBruce Evans * Check to make sure the arguments we've been given are 2870e7d023fSBruce Evans * satisfied. Return an error if we have been asked to 2880e7d023fSBruce Evans * list a mount point that does not match the other args 2890e7d023fSBruce Evans * we've been given (-l, -t, etc.). 290c22acefbSJordan K. Hubbard */ 291c22acefbSJordan K. Hubbard if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 2920e7d023fSBruce Evans rv = 1; 293c22acefbSJordan K. Hubbard continue; 294c22acefbSJordan K. Hubbard } 2950e7d023fSBruce Evans 29662edbd31SIan Dowse if (argc == 1) { 29762edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 29862edbd31SIan Dowse update_maxwidths(&maxwidths, &statfsbuf); 29962edbd31SIan Dowse } 30062edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 3014b88c807SRodney W. Grimes } 302b8904f2aSJoerg Wunsch return (rv); 3034b88c807SRodney W. Grimes } 3044b88c807SRodney W. Grimes 305532aff98SPoul-Henning Kamp static char * 306b7dbd3e9SMark Murray getmntpt(const char *name) 3074b88c807SRodney W. Grimes { 308be2c4e54SDavid E. O'Brien size_t mntsize, i; 3094b88c807SRodney W. Grimes struct statfs *mntbuf; 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 3124b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 3134b88c807SRodney W. Grimes if (!strcmp(mntbuf[i].f_mntfromname, name)) 3144b88c807SRodney W. Grimes return (mntbuf[i].f_mntonname); 3154b88c807SRodney W. Grimes } 3164b88c807SRodney W. Grimes return (0); 3174b88c807SRodney W. Grimes } 3184b88c807SRodney W. Grimes 3194b88c807SRodney W. Grimes /* 3204b88c807SRodney W. Grimes * Make a pass over the file system info in ``mntbuf'' filtering out 321a78192e3SBruce Evans * file system types not in vfslist and possibly re-stating to get 3224b88c807SRodney W. Grimes * current (not cached) info. Returns the new count of valid statfs bufs. 3234b88c807SRodney W. Grimes */ 324be2c4e54SDavid E. O'Brien static size_t 325532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 3264b88c807SRodney W. Grimes { 3274b88c807SRodney W. Grimes int i, j; 3284b88c807SRodney W. Grimes struct statfs *mntbuf; 3294b88c807SRodney W. Grimes 330a78192e3SBruce Evans if (vfslist == NULL) 3314b88c807SRodney W. Grimes return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes mntbuf = *mntbufp; 334a78192e3SBruce Evans for (j = 0, i = 0; i < mntsize; i++) { 335a78192e3SBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 336a78192e3SBruce Evans continue; 3374b88c807SRodney W. Grimes if (!nflag) 3384b88c807SRodney W. Grimes (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 3394b88c807SRodney W. Grimes else if (i != j) 3404b88c807SRodney W. Grimes mntbuf[j] = mntbuf[i]; 3414b88c807SRodney W. Grimes j++; 3424b88c807SRodney W. Grimes } 3434b88c807SRodney W. Grimes return (j); 3444b88c807SRodney W. Grimes } 3454b88c807SRodney W. Grimes 3464b88c807SRodney W. Grimes /* 347dd6d33e8SMichael Haro * Output in "human-readable" format. Uses 3 digits max and puts 348dd6d33e8SMichael Haro * unit suffixes at the end. Makes output compact and easy to read, 349dd6d33e8SMichael Haro * especially on huge disks. 350dd6d33e8SMichael Haro * 351dd6d33e8SMichael Haro */ 352532aff98SPoul-Henning Kamp static unit_t 353f9bcb0beSWarner Losh unit_adjust(double *val) 354dd6d33e8SMichael Haro { 355dd6d33e8SMichael Haro double abval; 356dd6d33e8SMichael Haro unit_t unit; 357b7dbd3e9SMark Murray int unit_sz; 358dd6d33e8SMichael Haro 359dd6d33e8SMichael Haro abval = fabs(*val); 360dd6d33e8SMichael Haro 361dd6d33e8SMichael Haro unit_sz = abval ? ilogb(abval) / 10 : 0; 362dd6d33e8SMichael Haro 363b7dbd3e9SMark Murray if (unit_sz >= (int)UNIT_MAX) { 364dd6d33e8SMichael Haro unit = NONE; 365dd6d33e8SMichael Haro } else { 366dd6d33e8SMichael Haro unit = unitp[unit_sz]; 367dd6d33e8SMichael Haro *val /= (double)valp[unit_sz]; 368dd6d33e8SMichael Haro } 369dd6d33e8SMichael Haro 370dd6d33e8SMichael Haro return (unit); 371dd6d33e8SMichael Haro } 372dd6d33e8SMichael Haro 373532aff98SPoul-Henning Kamp static void 374fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used) 375dd6d33e8SMichael Haro { 376dd6d33e8SMichael Haro 377dd6d33e8SMichael Haro prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize); 378dd6d33e8SMichael Haro prthumanval((double)used * (double)sfsp->f_bsize); 379dd6d33e8SMichael Haro prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize); 380dd6d33e8SMichael Haro } 381dd6d33e8SMichael Haro 382532aff98SPoul-Henning Kamp static void 383f9bcb0beSWarner Losh prthumanval(double bytes) 384dd6d33e8SMichael Haro { 385dd6d33e8SMichael Haro 386dd6d33e8SMichael Haro unit_t unit; 387dd6d33e8SMichael Haro unit = unit_adjust(&bytes); 388dd6d33e8SMichael Haro 389dd6d33e8SMichael Haro if (bytes == 0) 390dd6d33e8SMichael Haro (void)printf(" 0B"); 391dd6d33e8SMichael Haro else if (bytes > 10) 392dd6d33e8SMichael Haro (void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]); 393dd6d33e8SMichael Haro else 394dd6d33e8SMichael Haro (void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]); 395dd6d33e8SMichael Haro } 396dd6d33e8SMichael Haro 397dd6d33e8SMichael Haro /* 3984b88c807SRodney W. Grimes * Convert statfs returned file system size into BLOCKSIZE units. 3994b88c807SRodney W. Grimes * Attempts to avoid overflow for large file systems. 4004b88c807SRodney W. Grimes */ 4014b88c807SRodney W. Grimes #define fsbtoblk(num, fsbs, bs) \ 4024b88c807SRodney W. Grimes (((fsbs) != 0 && (fsbs) < (bs)) ? \ 4034b88c807SRodney W. Grimes (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs))) 4044b88c807SRodney W. Grimes 4054b88c807SRodney W. Grimes /* 4064b88c807SRodney W. Grimes * Print out status about a file system. 4074b88c807SRodney W. Grimes */ 408532aff98SPoul-Henning Kamp static void 40962edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp) 4104b88c807SRodney W. Grimes { 411fde81c7dSKirk McKusick static u_long blocksize; 412b7dbd3e9SMark Murray static int headerlen, timesthrough = 0; 413a95a13bbSKris Kennaway static const char *header; 414fde81c7dSKirk McKusick int64_t used, availblks, inodes; 4154b88c807SRodney W. Grimes 4164b88c807SRodney W. Grimes if (++timesthrough == 1) { 417b7dbd3e9SMark Murray mwp->mntfrom = max(mwp->mntfrom, strlen("Filesystem")); 418dd6d33e8SMichael Haro if (hflag) { 419dd6d33e8SMichael Haro header = " Size"; 42062edbd31SIan Dowse mwp->total = mwp->used = mwp->avail = strlen(header); 421dd6d33e8SMichael Haro } else { 422dd6d33e8SMichael Haro header = getbsize(&headerlen, &blocksize); 423b7dbd3e9SMark Murray mwp->total = max(mwp->total, (u_int)headerlen); 424dd6d33e8SMichael Haro } 425b7dbd3e9SMark Murray mwp->used = max(mwp->used, strlen("Used")); 426b7dbd3e9SMark Murray mwp->avail = max(mwp->avail, strlen("Avail")); 42762edbd31SIan Dowse 428f694b8adSMark Murray (void)printf("%-*s %-*s %*s %*s Capacity", 429f694b8adSMark Murray (u_int)mwp->mntfrom, "Filesystem", 430f694b8adSMark Murray (u_int)mwp->total, header, 431f694b8adSMark Murray (u_int)mwp->used, "Used", 432f694b8adSMark Murray (u_int)mwp->avail, "Avail"); 43362edbd31SIan Dowse if (iflag) { 434b7dbd3e9SMark Murray mwp->iused = max(mwp->iused, strlen(" iused")); 435b7dbd3e9SMark Murray mwp->ifree = max(mwp->ifree, strlen("ifree")); 436f694b8adSMark Murray (void)printf(" %*s %*s %%iused", 437f694b8adSMark Murray (u_int)mwp->iused - 2, "iused", 438f694b8adSMark Murray (u_int)mwp->ifree, "ifree"); 43962edbd31SIan Dowse } 4404b88c807SRodney W. Grimes (void)printf(" Mounted on\n"); 4414b88c807SRodney W. Grimes } 442f694b8adSMark Murray (void)printf("%-*s", (u_int)mwp->mntfrom, sfsp->f_mntfromname); 4434b88c807SRodney W. Grimes used = sfsp->f_blocks - sfsp->f_bfree; 4444b88c807SRodney W. Grimes availblks = sfsp->f_bavail + used; 445dd6d33e8SMichael Haro if (hflag) { 446dd6d33e8SMichael Haro prthuman(sfsp, used); 447dd6d33e8SMichael Haro } else { 448fde81c7dSKirk McKusick (void)printf(" %*qd %*qd %*qd", 449fde81c7dSKirk McKusick (u_int)mwp->total, 450fde81c7dSKirk McKusick (intmax_t)fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize), 451fde81c7dSKirk McKusick (u_int)mwp->used, 452fde81c7dSKirk McKusick (intmax_t)fsbtoblk(used, sfsp->f_bsize, blocksize), 453fde81c7dSKirk McKusick (u_int)mwp->avail, 454fde81c7dSKirk McKusick (intmax_t)fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize)); 455dd6d33e8SMichael Haro } 4564b88c807SRodney W. Grimes (void)printf(" %5.0f%%", 4574b88c807SRodney W. Grimes availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 4584b88c807SRodney W. Grimes if (iflag) { 4594b88c807SRodney W. Grimes inodes = sfsp->f_files; 4604b88c807SRodney W. Grimes used = inodes - sfsp->f_ffree; 461fde81c7dSKirk McKusick (void)printf(" %*qd %*qd %4.0f%% ", 462fde81c7dSKirk McKusick (u_int)mwp->iused, (intmax_t)used, 463fde81c7dSKirk McKusick (u_int)mwp->ifree, (intmax_t)sfsp->f_ffree, 464f694b8adSMark Murray inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0); 4654b88c807SRodney W. Grimes } else 4664b88c807SRodney W. Grimes (void)printf(" "); 4674b88c807SRodney W. Grimes (void)printf(" %s\n", sfsp->f_mntonname); 4684b88c807SRodney W. Grimes } 4694b88c807SRodney W. Grimes 4704b88c807SRodney W. Grimes /* 47162edbd31SIan Dowse * Update the maximum field-width information in `mwp' based on 47262edbd31SIan Dowse * the file system specified by `sfsp'. 47362edbd31SIan Dowse */ 474532aff98SPoul-Henning Kamp static void 475b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 47662edbd31SIan Dowse { 477fde81c7dSKirk McKusick static u_long blocksize = 0; 478dc474219SMike Barcroft int dummy; 47962edbd31SIan Dowse 48062edbd31SIan Dowse if (blocksize == 0) 48162edbd31SIan Dowse getbsize(&dummy, &blocksize); 48262edbd31SIan Dowse 483b7dbd3e9SMark Murray mwp->mntfrom = max(mwp->mntfrom, strlen(sfsp->f_mntfromname)); 484fde81c7dSKirk McKusick mwp->total = max(mwp->total, int64width( 485fde81c7dSKirk McKusick fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 486fde81c7dSKirk McKusick mwp->used = max(mwp->used, int64width(fsbtoblk((int64_t)sfsp->f_blocks - 487fde81c7dSKirk McKusick (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 488fde81c7dSKirk McKusick mwp->avail = max(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 48962edbd31SIan Dowse sfsp->f_bsize, blocksize))); 490fde81c7dSKirk McKusick mwp->iused = max(mwp->iused, int64width((int64_t)sfsp->f_files - 49162edbd31SIan Dowse sfsp->f_ffree)); 492fde81c7dSKirk McKusick mwp->ifree = max(mwp->ifree, int64width(sfsp->f_ffree)); 49362edbd31SIan Dowse } 49462edbd31SIan Dowse 49562edbd31SIan Dowse /* Return the width in characters of the specified long. */ 496b7dbd3e9SMark Murray static size_t 497fde81c7dSKirk McKusick int64width(int64_t val) 49862edbd31SIan Dowse { 499b7dbd3e9SMark Murray size_t len; 50062edbd31SIan Dowse 50162edbd31SIan Dowse len = 0; 50262edbd31SIan Dowse /* Negative or zero values require one extra digit. */ 50362edbd31SIan Dowse if (val <= 0) { 50462edbd31SIan Dowse val = -val; 50562edbd31SIan Dowse len++; 50662edbd31SIan Dowse } 50762edbd31SIan Dowse while (val > 0) { 50862edbd31SIan Dowse len++; 50962edbd31SIan Dowse val /= 10; 51062edbd31SIan Dowse } 51162edbd31SIan Dowse 51262edbd31SIan Dowse return (len); 51362edbd31SIan Dowse } 51462edbd31SIan Dowse 515532aff98SPoul-Henning Kamp static void 516f9bcb0beSWarner Losh usage(void) 5174b88c807SRodney W. Grimes { 518dd6d33e8SMichael Haro 519a78192e3SBruce Evans (void)fprintf(stderr, 520a25695c3SJim Pirzyk "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n"); 521dd6d33e8SMichael Haro exit(EX_USAGE); 5224b88c807SRodney W. Grimes } 523a25695c3SJim Pirzyk 524532aff98SPoul-Henning Kamp static char * 525f9bcb0beSWarner Losh makenetvfslist(void) 526a25695c3SJim Pirzyk { 527a25695c3SJim Pirzyk char *str, *strptr, **listptr; 528b7dbd3e9SMark Murray struct xvfsconf *xvfsp, *keep_xvfsp; 5295965373eSMaxime Henrion size_t buflen; 5305965373eSMaxime Henrion int cnt, i, maxvfsconf; 531a25695c3SJim Pirzyk 5325965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 5335965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 534a25695c3SJim Pirzyk return (NULL); 535a25695c3SJim Pirzyk } 5365965373eSMaxime Henrion xvfsp = malloc(buflen); 5375965373eSMaxime Henrion if (xvfsp == NULL) { 5385965373eSMaxime Henrion warnx("malloc failed"); 5395965373eSMaxime Henrion return (NULL); 5405965373eSMaxime Henrion } 541b7dbd3e9SMark Murray keep_xvfsp = xvfsp; 5425965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 5435965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 544b7dbd3e9SMark Murray free(keep_xvfsp); 5455965373eSMaxime Henrion return (NULL); 5465965373eSMaxime Henrion } 5475965373eSMaxime Henrion maxvfsconf = buflen / sizeof(struct xvfsconf); 548a25695c3SJim Pirzyk 549a25695c3SJim Pirzyk if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 550a25695c3SJim Pirzyk warnx("malloc failed"); 551b7dbd3e9SMark Murray free(keep_xvfsp); 552a25695c3SJim Pirzyk return (NULL); 553a25695c3SJim Pirzyk } 554a25695c3SJim Pirzyk 5555965373eSMaxime Henrion for (cnt = 0, i = 0; i < maxvfsconf; i++) { 5565965373eSMaxime Henrion if (xvfsp->vfc_flags & VFCF_NETWORK) { 5575965373eSMaxime Henrion listptr[cnt++] = strdup(xvfsp->vfc_name); 558a067aeceSGarrett Wollman if (listptr[cnt-1] == NULL) { 559a25695c3SJim Pirzyk warnx("malloc failed"); 560b7dbd3e9SMark Murray free(listptr); 561b7dbd3e9SMark Murray free(keep_xvfsp); 562a25695c3SJim Pirzyk return (NULL); 563a25695c3SJim Pirzyk } 564a25695c3SJim Pirzyk } 5655965373eSMaxime Henrion xvfsp++; 5665965373eSMaxime Henrion } 567a25695c3SJim Pirzyk 568cf5b29e1SRuslan Ermilov if (cnt == 0 || 569cf5b29e1SRuslan Ermilov (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 570cf5b29e1SRuslan Ermilov if (cnt > 0) 571a25695c3SJim Pirzyk warnx("malloc failed"); 572a25695c3SJim Pirzyk free(listptr); 573b7dbd3e9SMark Murray free(keep_xvfsp); 574a25695c3SJim Pirzyk return (NULL); 575a25695c3SJim Pirzyk } 576a25695c3SJim Pirzyk 577a25695c3SJim Pirzyk *str = 'n'; *(str + 1) = 'o'; 578a25695c3SJim Pirzyk for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 579a25695c3SJim Pirzyk strncpy(strptr, listptr[i], 32); 580a25695c3SJim Pirzyk strptr += strlen(listptr[i]); 581a25695c3SJim Pirzyk *strptr = ','; 582a25695c3SJim Pirzyk free(listptr[i]); 583a25695c3SJim Pirzyk } 584a25695c3SJim Pirzyk *(--strptr) = NULL; 585a25695c3SJim Pirzyk 586b7dbd3e9SMark Murray free(keep_xvfsp); 587a25695c3SJim Pirzyk free(listptr); 588a25695c3SJim Pirzyk return (str); 589a25695c3SJim Pirzyk } 590