19ddb49cbSWarner Losh /*- 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; 71b56ca465SPawel Jakub Dawidek int fstype; 720bd9f151SIan Dowse int total; 730bd9f151SIan Dowse int used; 740bd9f151SIan Dowse int avail; 750bd9f151SIan Dowse int iused; 760bd9f151SIan Dowse int ifree; 7762edbd31SIan Dowse }; 7862edbd31SIan Dowse 79c6f13844SDavid E. O'Brien static void addstat(struct statfs *, struct statfs *); 80b7dbd3e9SMark Murray static char *getmntpt(const char *); 810bd9f151SIan Dowse static int int64width(int64_t); 82532aff98SPoul-Henning Kamp static char *makenetvfslist(void); 83fde81c7dSKirk McKusick static void prthuman(const struct statfs *, int64_t); 847d3940bbSPawel Jakub Dawidek static void prthumanval(int64_t); 85841fe8e8SDavid Schultz static intmax_t fsbtoblk(int64_t, uint64_t, u_long); 86532aff98SPoul-Henning Kamp static void prtstat(struct statfs *, struct maxwidths *); 87be2c4e54SDavid E. O'Brien static size_t regetmntinfo(struct statfs **, long, const char **); 88b7dbd3e9SMark Murray static void update_maxwidths(struct maxwidths *, const struct statfs *); 89532aff98SPoul-Henning Kamp static void usage(void); 904b88c807SRodney W. Grimes 910bd9f151SIan Dowse static __inline int 920bd9f151SIan Dowse imax(int a, int b) 9362edbd31SIan Dowse { 94b7dbd3e9SMark Murray return (a > b ? a : b); 9562edbd31SIan Dowse } 9662edbd31SIan Dowse 97b56ca465SPawel Jakub Dawidek static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag; 98532aff98SPoul-Henning Kamp static struct ufs_args mdev; 99532aff98SPoul-Henning Kamp 1004b88c807SRodney W. Grimes int 101f9bcb0beSWarner Losh main(int argc, char *argv[]) 1024b88c807SRodney W. Grimes { 1034b88c807SRodney W. Grimes struct stat stbuf; 104c6f13844SDavid E. O'Brien struct statfs statfsbuf, totalbuf; 10562edbd31SIan Dowse struct maxwidths maxwidths; 106c6f13844SDavid E. O'Brien struct statfs *mntbuf; 107a95a13bbSKris Kennaway const char *fstype; 108532aff98SPoul-Henning Kamp char *mntpath, *mntpt; 109532aff98SPoul-Henning Kamp const char **vfslist; 110be2c4e54SDavid E. O'Brien size_t i, mntsize; 111be2c4e54SDavid E. O'Brien int ch, rv; 112f3895a82SKris Kennaway 113f3895a82SKris Kennaway fstype = "ufs"; 1144b88c807SRodney W. Grimes 115076419d2SDavid E. O'Brien memset(&totalbuf, 0, sizeof(totalbuf)); 116076419d2SDavid E. O'Brien totalbuf.f_bsize = DEV_BSIZE; 117d7c881e8SWarner Losh strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN); 118a78192e3SBruce Evans vfslist = NULL; 119b56ca465SPawel Jakub Dawidek while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T")) != -1) 1204b88c807SRodney W. Grimes switch (ch) { 1215b42dac8SJulian Elischer case 'a': 1225b42dac8SJulian Elischer aflag = 1; 1235b42dac8SJulian Elischer break; 124dd6d33e8SMichael Haro case 'b': 125dd6d33e8SMichael Haro /* FALLTHROUGH */ 126dd6d33e8SMichael Haro case 'P': 127df464e43SChristian S.J. Peron /* 128df464e43SChristian S.J. Peron * POSIX specifically discusses the the behavior of 129df464e43SChristian S.J. Peron * both -k and -P. It states that the blocksize should 130df464e43SChristian S.J. Peron * be set to 1024. Thus, if this occurs, simply break 131df464e43SChristian S.J. Peron * rather than clobbering the old blocksize. 132df464e43SChristian S.J. Peron */ 133df464e43SChristian S.J. Peron if (kflag) 134df464e43SChristian S.J. Peron break; 1352966d28cSSean Farley setenv("BLOCKSIZE", "512", 1); 136dd6d33e8SMichael Haro hflag = 0; 137dd6d33e8SMichael Haro break; 138c6f13844SDavid E. O'Brien case 'c': 139c6f13844SDavid E. O'Brien cflag = 1; 140c6f13844SDavid E. O'Brien break; 14193a3fa19SJohn W. De Boskey case 'g': 1422966d28cSSean Farley setenv("BLOCKSIZE", "1g", 1); 14393a3fa19SJohn W. De Boskey hflag = 0; 14493a3fa19SJohn W. De Boskey break; 145dd6d33e8SMichael Haro case 'H': 146dd6d33e8SMichael Haro hflag = UNITS_SI; 147dd6d33e8SMichael Haro break; 148dd6d33e8SMichael Haro case 'h': 149dd6d33e8SMichael Haro hflag = UNITS_2; 150dd6d33e8SMichael Haro break; 1514b88c807SRodney W. Grimes case 'i': 1524b88c807SRodney W. Grimes iflag = 1; 1534b88c807SRodney W. Grimes break; 1547f0eabfdSGarrett Wollman case 'k': 155df464e43SChristian S.J. Peron kflag++; 1562966d28cSSean Farley setenv("BLOCKSIZE", "1024", 1); 157dd6d33e8SMichael Haro hflag = 0; 158dd6d33e8SMichael Haro break; 159a25695c3SJim Pirzyk case 'l': 160a25695c3SJim Pirzyk if (vfslist != NULL) 161a25695c3SJim Pirzyk errx(1, "-l and -t are mutually exclusive."); 162a25695c3SJim Pirzyk vfslist = makevfslist(makenetvfslist()); 16327f82335SWill Andrews lflag = 1; 164a25695c3SJim Pirzyk break; 165dd6d33e8SMichael Haro case 'm': 1662966d28cSSean Farley setenv("BLOCKSIZE", "1m", 1); 167dd6d33e8SMichael Haro hflag = 0; 1687f0eabfdSGarrett Wollman break; 1694b88c807SRodney W. Grimes case 'n': 1704b88c807SRodney W. Grimes nflag = 1; 1714b88c807SRodney W. Grimes break; 1724b88c807SRodney W. Grimes case 't': 17327f82335SWill Andrews if (lflag) 17427f82335SWill Andrews errx(1, "-l and -t are mutually exclusive."); 175a78192e3SBruce Evans if (vfslist != NULL) 1767b3a12a8SPhilippe Charnier errx(1, "only one -t option may be specified"); 177f3895a82SKris Kennaway fstype = optarg; 178a78192e3SBruce Evans vfslist = makevfslist(optarg); 1794b88c807SRodney W. Grimes break; 180b56ca465SPawel Jakub Dawidek case 'T': 181b56ca465SPawel Jakub Dawidek Tflag = 1; 182b56ca465SPawel Jakub Dawidek break; 1834b88c807SRodney W. Grimes case '?': 1844b88c807SRodney W. Grimes default: 1854b88c807SRodney W. Grimes usage(); 1864b88c807SRodney W. Grimes } 1874b88c807SRodney W. Grimes argc -= optind; 1884b88c807SRodney W. Grimes argv += optind; 1894b88c807SRodney W. Grimes 1904b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 19162edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 19262edbd31SIan Dowse for (i = 0; i < mntsize; i++) 19362edbd31SIan Dowse update_maxwidths(&maxwidths, &mntbuf[i]); 1944b88c807SRodney W. Grimes 195b8904f2aSJoerg Wunsch rv = 0; 1964b88c807SRodney W. Grimes if (!*argv) { 197a78192e3SBruce Evans mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 19862edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 1995b42dac8SJulian Elischer for (i = 0; i < mntsize; i++) { 200076419d2SDavid E. O'Brien if (cflag) 201076419d2SDavid E. O'Brien addstat(&totalbuf, &mntbuf[i]); 202076419d2SDavid E. O'Brien update_maxwidths(&maxwidths, &mntbuf[i]); 203076419d2SDavid E. O'Brien } 204076419d2SDavid E. O'Brien if (cflag) 205076419d2SDavid E. O'Brien update_maxwidths(&maxwidths, &totalbuf); 206076419d2SDavid E. O'Brien for (i = 0; i < mntsize; i++) 2075b42dac8SJulian Elischer if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 20862edbd31SIan Dowse prtstat(&mntbuf[i], &maxwidths); 209076419d2SDavid E. O'Brien if (cflag) 210076419d2SDavid E. O'Brien prtstat(&totalbuf, &maxwidths); 211b8904f2aSJoerg Wunsch exit(rv); 2124b88c807SRodney W. Grimes } 2134b88c807SRodney W. Grimes 2144b88c807SRodney W. Grimes for (; *argv; argv++) { 2154b88c807SRodney W. Grimes if (stat(*argv, &stbuf) < 0) { 2164b88c807SRodney W. Grimes if ((mntpt = getmntpt(*argv)) == 0) { 2174b88c807SRodney W. Grimes warn("%s", *argv); 218b8904f2aSJoerg Wunsch rv = 1; 2194b88c807SRodney W. Grimes continue; 2204b88c807SRodney W. Grimes } 221f3895a82SKris Kennaway } else if (S_ISCHR(stbuf.st_mode)) { 222f3895a82SKris Kennaway if ((mntpt = getmntpt(*argv)) == 0) { 223f3895a82SKris Kennaway mdev.fspec = *argv; 224f3895a82SKris Kennaway mntpath = strdup("/tmp/df.XXXXXX"); 225f3895a82SKris Kennaway if (mntpath == NULL) { 226f3895a82SKris Kennaway warn("strdup failed"); 227f3895a82SKris Kennaway rv = 1; 2284b88c807SRodney W. Grimes continue; 229f3895a82SKris Kennaway } 230f3895a82SKris Kennaway mntpt = mkdtemp(mntpath); 231f3895a82SKris Kennaway if (mntpt == NULL) { 232f3895a82SKris Kennaway warn("mkdtemp(\"%s\") failed", mntpath); 233f3895a82SKris Kennaway rv = 1; 234f3895a82SKris Kennaway free(mntpath); 235f3895a82SKris Kennaway continue; 236f3895a82SKris Kennaway } 237f3895a82SKris Kennaway if (mount(fstype, mntpt, MNT_RDONLY, 238f3895a82SKris Kennaway &mdev) != 0) { 239532aff98SPoul-Henning Kamp warn("%s", *argv); 240532aff98SPoul-Henning Kamp rv = 1; 241f3895a82SKris Kennaway (void)rmdir(mntpt); 242f3895a82SKris Kennaway free(mntpath); 243f3895a82SKris Kennaway continue; 244f3895a82SKris Kennaway } else if (statfs(mntpt, &statfsbuf) == 0) { 245f3895a82SKris Kennaway statfsbuf.f_mntonname[0] = '\0'; 24662edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 247076419d2SDavid E. O'Brien if (cflag) 248076419d2SDavid E. O'Brien addstat(&totalbuf, &statfsbuf); 249f3895a82SKris Kennaway } else { 250f3895a82SKris Kennaway warn("%s", *argv); 251f3895a82SKris Kennaway rv = 1; 252f3895a82SKris Kennaway } 253f3895a82SKris Kennaway (void)unmount(mntpt, 0); 254f3895a82SKris Kennaway (void)rmdir(mntpt); 255f3895a82SKris Kennaway free(mntpath); 256f3895a82SKris Kennaway continue; 257f3895a82SKris Kennaway } 2584b88c807SRodney W. Grimes } else 2594b88c807SRodney W. Grimes mntpt = *argv; 2600e7d023fSBruce Evans 2614b88c807SRodney W. Grimes /* 2624b88c807SRodney W. Grimes * Statfs does not take a `wait' flag, so we cannot 2634b88c807SRodney W. Grimes * implement nflag here. 2644b88c807SRodney W. Grimes */ 2654b88c807SRodney W. Grimes if (statfs(mntpt, &statfsbuf) < 0) { 2664b88c807SRodney W. Grimes warn("%s", mntpt); 267b8904f2aSJoerg Wunsch rv = 1; 2684b88c807SRodney W. Grimes continue; 2694b88c807SRodney W. Grimes } 2700e7d023fSBruce Evans 2710e7d023fSBruce Evans /* 2720e7d023fSBruce Evans * Check to make sure the arguments we've been given are 2730e7d023fSBruce Evans * satisfied. Return an error if we have been asked to 2740e7d023fSBruce Evans * list a mount point that does not match the other args 2750e7d023fSBruce Evans * we've been given (-l, -t, etc.). 276c22acefbSJordan K. Hubbard */ 277c22acefbSJordan K. Hubbard if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 2780e7d023fSBruce Evans rv = 1; 279c22acefbSJordan K. Hubbard continue; 280c22acefbSJordan K. Hubbard } 2810e7d023fSBruce Evans 28262edbd31SIan Dowse if (argc == 1) { 28362edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 28462edbd31SIan Dowse update_maxwidths(&maxwidths, &statfsbuf); 28562edbd31SIan Dowse } 28662edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 287076419d2SDavid E. O'Brien if (cflag) 288076419d2SDavid E. O'Brien addstat(&totalbuf, &statfsbuf); 2894b88c807SRodney W. Grimes } 290076419d2SDavid E. O'Brien if (cflag) 291076419d2SDavid E. O'Brien prtstat(&totalbuf, &maxwidths); 292b8904f2aSJoerg Wunsch return (rv); 2934b88c807SRodney W. Grimes } 2944b88c807SRodney W. Grimes 295532aff98SPoul-Henning Kamp static char * 296b7dbd3e9SMark Murray getmntpt(const char *name) 2974b88c807SRodney W. Grimes { 298be2c4e54SDavid E. O'Brien size_t mntsize, i; 2994b88c807SRodney W. Grimes struct statfs *mntbuf; 3004b88c807SRodney W. Grimes 3014b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 3024b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 3034b88c807SRodney W. Grimes if (!strcmp(mntbuf[i].f_mntfromname, name)) 3044b88c807SRodney W. Grimes return (mntbuf[i].f_mntonname); 3054b88c807SRodney W. Grimes } 3064b88c807SRodney W. Grimes return (0); 3074b88c807SRodney W. Grimes } 3084b88c807SRodney W. Grimes 3094b88c807SRodney W. Grimes /* 3104b88c807SRodney W. Grimes * Make a pass over the file system info in ``mntbuf'' filtering out 311a78192e3SBruce Evans * file system types not in vfslist and possibly re-stating to get 3124b88c807SRodney W. Grimes * current (not cached) info. Returns the new count of valid statfs bufs. 3134b88c807SRodney W. Grimes */ 314be2c4e54SDavid E. O'Brien static size_t 315532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 3164b88c807SRodney W. Grimes { 317239c9e60SChristian S.J. Peron int error, i, j; 3184b88c807SRodney W. Grimes struct statfs *mntbuf; 3194b88c807SRodney W. Grimes 320a78192e3SBruce Evans if (vfslist == NULL) 3214b88c807SRodney W. Grimes return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 3224b88c807SRodney W. Grimes 3234b88c807SRodney W. Grimes mntbuf = *mntbufp; 324a78192e3SBruce Evans for (j = 0, i = 0; i < mntsize; i++) { 325a78192e3SBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 326a78192e3SBruce Evans continue; 327239c9e60SChristian S.J. Peron /* 328239c9e60SChristian S.J. Peron * XXX statfs(2) can fail for various reasons. It may be 329239c9e60SChristian S.J. Peron * possible that the user does not have access to the 330239c9e60SChristian S.J. Peron * pathname, if this happens, we will fall back on 331239c9e60SChristian S.J. Peron * "stale" filesystem statistics. 332239c9e60SChristian S.J. Peron */ 333239c9e60SChristian S.J. Peron error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]); 334239c9e60SChristian S.J. Peron if (nflag || error < 0) 335239c9e60SChristian S.J. Peron if (i != j) { 336239c9e60SChristian S.J. Peron if (error < 0) 337239c9e60SChristian S.J. Peron warnx("%s stats possibly stale", 338239c9e60SChristian S.J. Peron mntbuf[i].f_mntonname); 3394b88c807SRodney W. Grimes mntbuf[j] = mntbuf[i]; 340239c9e60SChristian S.J. Peron } 3414b88c807SRodney W. Grimes j++; 3424b88c807SRodney W. Grimes } 3434b88c807SRodney W. Grimes return (j); 3444b88c807SRodney W. Grimes } 3454b88c807SRodney W. Grimes 346532aff98SPoul-Henning Kamp static void 347fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used) 348dd6d33e8SMichael Haro { 349dd6d33e8SMichael Haro 3507d3940bbSPawel Jakub Dawidek prthumanval(sfsp->f_blocks * sfsp->f_bsize); 3517d3940bbSPawel Jakub Dawidek prthumanval(used * sfsp->f_bsize); 3527d3940bbSPawel Jakub Dawidek prthumanval(sfsp->f_bavail * sfsp->f_bsize); 353dd6d33e8SMichael Haro } 354dd6d33e8SMichael Haro 355532aff98SPoul-Henning Kamp static void 3567d3940bbSPawel Jakub Dawidek prthumanval(int64_t bytes) 357dd6d33e8SMichael Haro { 3587d3940bbSPawel Jakub Dawidek char buf[6]; 3597d3940bbSPawel Jakub Dawidek int flags; 360dd6d33e8SMichael Haro 3617d3940bbSPawel Jakub Dawidek flags = HN_B | HN_NOSPACE | HN_DECIMAL; 3627d3940bbSPawel Jakub Dawidek if (hflag == UNITS_SI) 3637d3940bbSPawel Jakub Dawidek flags |= HN_DIVISOR_1000; 364dd6d33e8SMichael Haro 3657d3940bbSPawel Jakub Dawidek humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 3667d3940bbSPawel Jakub Dawidek bytes, "", HN_AUTOSCALE, flags); 3677d3940bbSPawel Jakub Dawidek 3687d3940bbSPawel Jakub Dawidek (void)printf(" %6s", buf); 369dd6d33e8SMichael Haro } 370dd6d33e8SMichael Haro 371dd6d33e8SMichael Haro /* 3724b88c807SRodney W. Grimes * Convert statfs returned file system size into BLOCKSIZE units. 3734b88c807SRodney W. Grimes * Attempts to avoid overflow for large file systems. 3744b88c807SRodney W. Grimes */ 375841fe8e8SDavid Schultz static intmax_t 376841fe8e8SDavid Schultz fsbtoblk(int64_t num, uint64_t fsbs, u_long bs) 377841fe8e8SDavid Schultz { 378841fe8e8SDavid Schultz 379841fe8e8SDavid Schultz if (fsbs != 0 && fsbs < bs) 380841fe8e8SDavid Schultz return (num / (intmax_t)(bs / fsbs)); 381841fe8e8SDavid Schultz else 382841fe8e8SDavid Schultz return (num * (intmax_t)(fsbs / bs)); 383841fe8e8SDavid Schultz } 3844b88c807SRodney W. Grimes 3854b88c807SRodney W. Grimes /* 3864b88c807SRodney W. Grimes * Print out status about a file system. 3874b88c807SRodney W. Grimes */ 388532aff98SPoul-Henning Kamp static void 38962edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp) 3904b88c807SRodney W. Grimes { 3912897dce8SAlexander Kabaev static long blocksize; 392b7dbd3e9SMark Murray static int headerlen, timesthrough = 0; 393a95a13bbSKris Kennaway static const char *header; 394fde81c7dSKirk McKusick int64_t used, availblks, inodes; 3954b88c807SRodney W. Grimes 3964b88c807SRodney W. Grimes if (++timesthrough == 1) { 3970bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 398b56ca465SPawel Jakub Dawidek mwp->fstype = imax(mwp->fstype, (int)strlen("Type")); 399dd6d33e8SMichael Haro if (hflag) { 400dd6d33e8SMichael Haro header = " Size"; 4010bd9f151SIan Dowse mwp->total = mwp->used = mwp->avail = 4020bd9f151SIan Dowse (int)strlen(header); 403dd6d33e8SMichael Haro } else { 404dd6d33e8SMichael Haro header = getbsize(&headerlen, &blocksize); 4050bd9f151SIan Dowse mwp->total = imax(mwp->total, headerlen); 406dd6d33e8SMichael Haro } 4070bd9f151SIan Dowse mwp->used = imax(mwp->used, (int)strlen("Used")); 4080bd9f151SIan Dowse mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 40962edbd31SIan Dowse 410b56ca465SPawel Jakub Dawidek (void)printf("%-*s", mwp->mntfrom, "Filesystem"); 411b56ca465SPawel Jakub Dawidek if (Tflag) 412b56ca465SPawel Jakub Dawidek (void)printf(" %-*s", mwp->fstype, "Type"); 413b56ca465SPawel Jakub Dawidek (void)printf(" %-*s %*s %*s Capacity", mwp->total, header, 4140bd9f151SIan Dowse mwp->used, "Used", mwp->avail, "Avail"); 41562edbd31SIan Dowse if (iflag) { 4160bd9f151SIan Dowse mwp->iused = imax(mwp->iused, (int)strlen(" iused")); 4170bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, (int)strlen("ifree")); 418f694b8adSMark Murray (void)printf(" %*s %*s %%iused", 4190bd9f151SIan Dowse mwp->iused - 2, "iused", mwp->ifree, "ifree"); 42062edbd31SIan Dowse } 4214b88c807SRodney W. Grimes (void)printf(" Mounted on\n"); 4224b88c807SRodney W. Grimes } 4230bd9f151SIan Dowse (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname); 424b56ca465SPawel Jakub Dawidek if (Tflag) 425b56ca465SPawel Jakub Dawidek (void)printf(" %-*s", mwp->fstype, sfsp->f_fstypename); 4264b88c807SRodney W. Grimes used = sfsp->f_blocks - sfsp->f_bfree; 4274b88c807SRodney W. Grimes availblks = sfsp->f_bavail + used; 428dd6d33e8SMichael Haro if (hflag) { 429dd6d33e8SMichael Haro prthuman(sfsp, used); 430dd6d33e8SMichael Haro } else { 431ea64ad77SKris Kennaway (void)printf(" %*jd %*jd %*jd", 432841fe8e8SDavid Schultz mwp->total, fsbtoblk(sfsp->f_blocks, 4330bd9f151SIan Dowse sfsp->f_bsize, blocksize), 434841fe8e8SDavid Schultz mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize), 435841fe8e8SDavid Schultz mwp->avail, fsbtoblk(sfsp->f_bavail, 4360bd9f151SIan Dowse sfsp->f_bsize, blocksize)); 437dd6d33e8SMichael Haro } 4384b88c807SRodney W. Grimes (void)printf(" %5.0f%%", 4394b88c807SRodney W. Grimes availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 4404b88c807SRodney W. Grimes if (iflag) { 4414b88c807SRodney W. Grimes inodes = sfsp->f_files; 4424b88c807SRodney W. Grimes used = inodes - sfsp->f_ffree; 4430bd9f151SIan Dowse (void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used, 4440bd9f151SIan Dowse mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 : 4450bd9f151SIan Dowse (double)used / (double)inodes * 100.0); 446cca108e6SDavid E. O'Brien } else 447cca108e6SDavid E. O'Brien (void)printf(" "); 448c6f13844SDavid E. O'Brien if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0) 449076419d2SDavid E. O'Brien (void)printf(" %s", sfsp->f_mntonname); 450076419d2SDavid E. O'Brien (void)printf("\n"); 451076419d2SDavid E. O'Brien } 452076419d2SDavid E. O'Brien 453076419d2SDavid E. O'Brien void 454076419d2SDavid E. O'Brien addstat(struct statfs *totalfsp, struct statfs *statfsp) 455076419d2SDavid E. O'Brien { 456c6f13844SDavid E. O'Brien uint64_t bsize; 457076419d2SDavid E. O'Brien 458c6f13844SDavid E. O'Brien bsize = statfsp->f_bsize / totalfsp->f_bsize; 459076419d2SDavid E. O'Brien totalfsp->f_blocks += statfsp->f_blocks * bsize; 460076419d2SDavid E. O'Brien totalfsp->f_bfree += statfsp->f_bfree * bsize; 461076419d2SDavid E. O'Brien totalfsp->f_bavail += statfsp->f_bavail * bsize; 462076419d2SDavid E. O'Brien totalfsp->f_files += statfsp->f_files; 463076419d2SDavid E. O'Brien totalfsp->f_ffree += statfsp->f_ffree; 4644b88c807SRodney W. Grimes } 4654b88c807SRodney W. Grimes 4664b88c807SRodney W. Grimes /* 46762edbd31SIan Dowse * Update the maximum field-width information in `mwp' based on 46862edbd31SIan Dowse * the file system specified by `sfsp'. 46962edbd31SIan Dowse */ 470532aff98SPoul-Henning Kamp static void 471b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 47262edbd31SIan Dowse { 4732897dce8SAlexander Kabaev static long blocksize = 0; 474dc474219SMike Barcroft int dummy; 47562edbd31SIan Dowse 47662edbd31SIan Dowse if (blocksize == 0) 47762edbd31SIan Dowse getbsize(&dummy, &blocksize); 47862edbd31SIan Dowse 4790bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 480b56ca465SPawel Jakub Dawidek mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename)); 4810bd9f151SIan Dowse mwp->total = imax(mwp->total, int64width( 482fde81c7dSKirk McKusick fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 4830bd9f151SIan Dowse mwp->used = imax(mwp->used, 4840bd9f151SIan Dowse int64width(fsbtoblk((int64_t)sfsp->f_blocks - 485fde81c7dSKirk McKusick (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 4860bd9f151SIan Dowse mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 48762edbd31SIan Dowse sfsp->f_bsize, blocksize))); 4880bd9f151SIan Dowse mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 48962edbd31SIan Dowse sfsp->f_ffree)); 4900bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 49162edbd31SIan Dowse } 49262edbd31SIan Dowse 4930bd9f151SIan Dowse /* Return the width in characters of the specified value. */ 4940bd9f151SIan Dowse static int 495fde81c7dSKirk McKusick int64width(int64_t val) 49662edbd31SIan Dowse { 4970bd9f151SIan Dowse int len; 49862edbd31SIan Dowse 49962edbd31SIan Dowse len = 0; 50062edbd31SIan Dowse /* Negative or zero values require one extra digit. */ 50162edbd31SIan Dowse if (val <= 0) { 50262edbd31SIan Dowse val = -val; 50362edbd31SIan Dowse len++; 50462edbd31SIan Dowse } 50562edbd31SIan Dowse while (val > 0) { 50662edbd31SIan Dowse len++; 50762edbd31SIan Dowse val /= 10; 50862edbd31SIan Dowse } 50962edbd31SIan Dowse 51062edbd31SIan Dowse return (len); 51162edbd31SIan Dowse } 51262edbd31SIan Dowse 513532aff98SPoul-Henning Kamp static void 514f9bcb0beSWarner Losh usage(void) 5154b88c807SRodney W. Grimes { 516dd6d33e8SMichael Haro 517a78192e3SBruce Evans (void)fprintf(stderr, 518b56ca465SPawel Jakub Dawidek "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [file | filesystem ...]\n"); 519dd6d33e8SMichael Haro exit(EX_USAGE); 5204b88c807SRodney W. Grimes } 521a25695c3SJim Pirzyk 522532aff98SPoul-Henning Kamp static char * 523f9bcb0beSWarner Losh makenetvfslist(void) 524a25695c3SJim Pirzyk { 525a25695c3SJim Pirzyk char *str, *strptr, **listptr; 526b7dbd3e9SMark Murray struct xvfsconf *xvfsp, *keep_xvfsp; 5275965373eSMaxime Henrion size_t buflen; 5285965373eSMaxime Henrion int cnt, i, maxvfsconf; 529a25695c3SJim Pirzyk 5305965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 5315965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 532a25695c3SJim Pirzyk return (NULL); 533a25695c3SJim Pirzyk } 5345965373eSMaxime Henrion xvfsp = malloc(buflen); 5355965373eSMaxime Henrion if (xvfsp == NULL) { 5365965373eSMaxime Henrion warnx("malloc failed"); 5375965373eSMaxime Henrion return (NULL); 5385965373eSMaxime Henrion } 539b7dbd3e9SMark Murray keep_xvfsp = xvfsp; 5405965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 5415965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 542b7dbd3e9SMark Murray free(keep_xvfsp); 5435965373eSMaxime Henrion return (NULL); 5445965373eSMaxime Henrion } 5455965373eSMaxime Henrion maxvfsconf = buflen / sizeof(struct xvfsconf); 546a25695c3SJim Pirzyk 547a25695c3SJim Pirzyk if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 548a25695c3SJim Pirzyk warnx("malloc failed"); 549b7dbd3e9SMark Murray free(keep_xvfsp); 550a25695c3SJim Pirzyk return (NULL); 551a25695c3SJim Pirzyk } 552a25695c3SJim Pirzyk 5535965373eSMaxime Henrion for (cnt = 0, i = 0; i < maxvfsconf; i++) { 5545965373eSMaxime Henrion if (xvfsp->vfc_flags & VFCF_NETWORK) { 5555965373eSMaxime Henrion listptr[cnt++] = strdup(xvfsp->vfc_name); 556a067aeceSGarrett Wollman if (listptr[cnt-1] == NULL) { 557a25695c3SJim Pirzyk warnx("malloc failed"); 558b7dbd3e9SMark Murray free(listptr); 559b7dbd3e9SMark Murray free(keep_xvfsp); 560a25695c3SJim Pirzyk return (NULL); 561a25695c3SJim Pirzyk } 562a25695c3SJim Pirzyk } 5635965373eSMaxime Henrion xvfsp++; 5645965373eSMaxime Henrion } 565a25695c3SJim Pirzyk 566cf5b29e1SRuslan Ermilov if (cnt == 0 || 567cf5b29e1SRuslan Ermilov (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 568cf5b29e1SRuslan Ermilov if (cnt > 0) 569a25695c3SJim Pirzyk warnx("malloc failed"); 570a25695c3SJim Pirzyk free(listptr); 571b7dbd3e9SMark Murray free(keep_xvfsp); 572a25695c3SJim Pirzyk return (NULL); 573a25695c3SJim Pirzyk } 574a25695c3SJim Pirzyk 575a25695c3SJim Pirzyk *str = 'n'; *(str + 1) = 'o'; 576a25695c3SJim Pirzyk for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 577d7c881e8SWarner Losh strlcpy(strptr, listptr[i], 32); 578a25695c3SJim Pirzyk strptr += strlen(listptr[i]); 579a25695c3SJim Pirzyk *strptr = ','; 580a25695c3SJim Pirzyk free(listptr[i]); 581a25695c3SJim Pirzyk } 58216fc3635SMark Murray *(--strptr) = '\0'; 583a25695c3SJim Pirzyk 584b7dbd3e9SMark Murray free(keep_xvfsp); 585a25695c3SJim Pirzyk free(listptr); 586a25695c3SJim Pirzyk return (str); 587a25695c3SJim Pirzyk } 588