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); 84532aff98SPoul-Henning Kamp static void prtstat(struct statfs *, struct maxwidths *); 85be2c4e54SDavid E. O'Brien static size_t regetmntinfo(struct statfs **, long, const char **); 86b7dbd3e9SMark Murray static void update_maxwidths(struct maxwidths *, const struct statfs *); 87532aff98SPoul-Henning Kamp static void usage(void); 884b88c807SRodney W. Grimes 890bd9f151SIan Dowse static __inline int 900bd9f151SIan Dowse imax(int a, int b) 9162edbd31SIan Dowse { 92b7dbd3e9SMark Murray return (a > b ? a : b); 9362edbd31SIan Dowse } 9462edbd31SIan Dowse 95076419d2SDavid E. O'Brien static int aflag = 0, cflag, hflag, iflag, nflag; 96532aff98SPoul-Henning Kamp static struct ufs_args mdev; 97532aff98SPoul-Henning Kamp 984b88c807SRodney W. Grimes int 99f9bcb0beSWarner Losh main(int argc, char *argv[]) 1004b88c807SRodney W. Grimes { 1014b88c807SRodney W. Grimes struct stat stbuf; 102c6f13844SDavid E. O'Brien struct statfs statfsbuf, totalbuf; 10362edbd31SIan Dowse struct maxwidths maxwidths; 104c6f13844SDavid E. O'Brien struct statfs *mntbuf; 105a95a13bbSKris Kennaway const char *fstype; 106532aff98SPoul-Henning Kamp char *mntpath, *mntpt; 107532aff98SPoul-Henning Kamp const char **vfslist; 108be2c4e54SDavid E. O'Brien size_t i, mntsize; 109be2c4e54SDavid E. O'Brien int ch, rv; 110f3895a82SKris Kennaway 111f3895a82SKris Kennaway fstype = "ufs"; 1124b88c807SRodney W. Grimes 113076419d2SDavid E. O'Brien memset(&totalbuf, 0, sizeof(totalbuf)); 114076419d2SDavid E. O'Brien totalbuf.f_bsize = DEV_BSIZE; 115076419d2SDavid E. O'Brien strncpy(totalbuf.f_mntfromname, "total", MNAMELEN); 116a78192e3SBruce Evans vfslist = NULL; 117076419d2SDavid E. O'Brien while ((ch = getopt(argc, argv, "abcgHhiklmnPt:")) != -1) 1184b88c807SRodney W. Grimes switch (ch) { 1195b42dac8SJulian Elischer case 'a': 1205b42dac8SJulian Elischer aflag = 1; 1215b42dac8SJulian Elischer break; 122dd6d33e8SMichael Haro case 'b': 123dd6d33e8SMichael Haro /* FALLTHROUGH */ 124dd6d33e8SMichael Haro case 'P': 125dd6d33e8SMichael Haro putenv("BLOCKSIZE=512"); 126dd6d33e8SMichael Haro hflag = 0; 127dd6d33e8SMichael Haro break; 128c6f13844SDavid E. O'Brien case 'c': 129c6f13844SDavid E. O'Brien cflag = 1; 130c6f13844SDavid E. O'Brien break; 13193a3fa19SJohn W. De Boskey case 'g': 13293a3fa19SJohn W. De Boskey putenv("BLOCKSIZE=1g"); 13393a3fa19SJohn W. De Boskey hflag = 0; 13493a3fa19SJohn W. De Boskey break; 135dd6d33e8SMichael Haro case 'H': 136dd6d33e8SMichael Haro hflag = UNITS_SI; 137dd6d33e8SMichael Haro break; 138dd6d33e8SMichael Haro case 'h': 139dd6d33e8SMichael Haro hflag = UNITS_2; 140dd6d33e8SMichael Haro break; 1414b88c807SRodney W. Grimes case 'i': 1424b88c807SRodney W. Grimes iflag = 1; 1434b88c807SRodney W. Grimes break; 1447f0eabfdSGarrett Wollman case 'k': 1459d4081eeSDavid Greenman putenv("BLOCKSIZE=1k"); 146dd6d33e8SMichael Haro hflag = 0; 147dd6d33e8SMichael Haro break; 148a25695c3SJim Pirzyk case 'l': 149a25695c3SJim Pirzyk if (vfslist != NULL) 150a25695c3SJim Pirzyk errx(1, "-l and -t are mutually exclusive."); 151a25695c3SJim Pirzyk vfslist = makevfslist(makenetvfslist()); 152a25695c3SJim Pirzyk break; 153dd6d33e8SMichael Haro case 'm': 154dd6d33e8SMichael Haro putenv("BLOCKSIZE=1m"); 155dd6d33e8SMichael Haro hflag = 0; 1567f0eabfdSGarrett Wollman break; 1574b88c807SRodney W. Grimes case 'n': 1584b88c807SRodney W. Grimes nflag = 1; 1594b88c807SRodney W. Grimes break; 1604b88c807SRodney W. Grimes case 't': 161a78192e3SBruce Evans if (vfslist != NULL) 1627b3a12a8SPhilippe Charnier errx(1, "only one -t option may be specified"); 163f3895a82SKris Kennaway fstype = optarg; 164a78192e3SBruce Evans vfslist = makevfslist(optarg); 1654b88c807SRodney W. Grimes break; 1664b88c807SRodney W. Grimes case '?': 1674b88c807SRodney W. Grimes default: 1684b88c807SRodney W. Grimes usage(); 1694b88c807SRodney W. Grimes } 1704b88c807SRodney W. Grimes argc -= optind; 1714b88c807SRodney W. Grimes argv += optind; 1724b88c807SRodney W. Grimes 1734b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 17462edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 17562edbd31SIan Dowse for (i = 0; i < mntsize; i++) 17662edbd31SIan Dowse update_maxwidths(&maxwidths, &mntbuf[i]); 1774b88c807SRodney W. Grimes 178b8904f2aSJoerg Wunsch rv = 0; 1794b88c807SRodney W. Grimes if (!*argv) { 180a78192e3SBruce Evans mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 18162edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 1825b42dac8SJulian Elischer for (i = 0; i < mntsize; i++) { 183076419d2SDavid E. O'Brien if (cflag) 184076419d2SDavid E. O'Brien addstat(&totalbuf, &mntbuf[i]); 185076419d2SDavid E. O'Brien update_maxwidths(&maxwidths, &mntbuf[i]); 186076419d2SDavid E. O'Brien } 187076419d2SDavid E. O'Brien if (cflag) 188076419d2SDavid E. O'Brien update_maxwidths(&maxwidths, &totalbuf); 189076419d2SDavid E. O'Brien for (i = 0; i < mntsize; i++) 1905b42dac8SJulian Elischer if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 19162edbd31SIan Dowse prtstat(&mntbuf[i], &maxwidths); 192076419d2SDavid E. O'Brien if (cflag) 193076419d2SDavid E. O'Brien prtstat(&totalbuf, &maxwidths); 194b8904f2aSJoerg Wunsch exit(rv); 1954b88c807SRodney W. Grimes } 1964b88c807SRodney W. Grimes 1974b88c807SRodney W. Grimes for (; *argv; argv++) { 1984b88c807SRodney W. Grimes if (stat(*argv, &stbuf) < 0) { 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 } 204f3895a82SKris Kennaway } else if (S_ISCHR(stbuf.st_mode)) { 205f3895a82SKris Kennaway if ((mntpt = getmntpt(*argv)) == 0) { 206f3895a82SKris Kennaway mdev.fspec = *argv; 207f3895a82SKris Kennaway mntpath = strdup("/tmp/df.XXXXXX"); 208f3895a82SKris Kennaway if (mntpath == NULL) { 209f3895a82SKris Kennaway warn("strdup failed"); 210f3895a82SKris Kennaway rv = 1; 2114b88c807SRodney W. Grimes continue; 212f3895a82SKris Kennaway } 213f3895a82SKris Kennaway mntpt = mkdtemp(mntpath); 214f3895a82SKris Kennaway if (mntpt == NULL) { 215f3895a82SKris Kennaway warn("mkdtemp(\"%s\") failed", mntpath); 216f3895a82SKris Kennaway rv = 1; 217f3895a82SKris Kennaway free(mntpath); 218f3895a82SKris Kennaway continue; 219f3895a82SKris Kennaway } 220f3895a82SKris Kennaway if (mount(fstype, mntpt, MNT_RDONLY, 221f3895a82SKris Kennaway &mdev) != 0) { 222532aff98SPoul-Henning Kamp warn("%s", *argv); 223532aff98SPoul-Henning Kamp rv = 1; 224f3895a82SKris Kennaway (void)rmdir(mntpt); 225f3895a82SKris Kennaway free(mntpath); 226f3895a82SKris Kennaway continue; 227f3895a82SKris Kennaway } else if (statfs(mntpt, &statfsbuf) == 0) { 228f3895a82SKris Kennaway statfsbuf.f_mntonname[0] = '\0'; 22962edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 230076419d2SDavid E. O'Brien if (cflag) 231076419d2SDavid E. O'Brien addstat(&totalbuf, &statfsbuf); 232f3895a82SKris Kennaway } else { 233f3895a82SKris Kennaway warn("%s", *argv); 234f3895a82SKris Kennaway rv = 1; 235f3895a82SKris Kennaway } 236f3895a82SKris Kennaway (void)unmount(mntpt, 0); 237f3895a82SKris Kennaway (void)rmdir(mntpt); 238f3895a82SKris Kennaway free(mntpath); 239f3895a82SKris Kennaway continue; 240f3895a82SKris Kennaway } 2414b88c807SRodney W. Grimes } else 2424b88c807SRodney W. Grimes mntpt = *argv; 2430e7d023fSBruce Evans 2444b88c807SRodney W. Grimes /* 2454b88c807SRodney W. Grimes * Statfs does not take a `wait' flag, so we cannot 2464b88c807SRodney W. Grimes * implement nflag here. 2474b88c807SRodney W. Grimes */ 2484b88c807SRodney W. Grimes if (statfs(mntpt, &statfsbuf) < 0) { 2494b88c807SRodney W. Grimes warn("%s", mntpt); 250b8904f2aSJoerg Wunsch rv = 1; 2514b88c807SRodney W. Grimes continue; 2524b88c807SRodney W. Grimes } 2530e7d023fSBruce Evans 2540e7d023fSBruce Evans /* 2550e7d023fSBruce Evans * Check to make sure the arguments we've been given are 2560e7d023fSBruce Evans * satisfied. Return an error if we have been asked to 2570e7d023fSBruce Evans * list a mount point that does not match the other args 2580e7d023fSBruce Evans * we've been given (-l, -t, etc.). 259c22acefbSJordan K. Hubbard */ 260c22acefbSJordan K. Hubbard if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 2610e7d023fSBruce Evans rv = 1; 262c22acefbSJordan K. Hubbard continue; 263c22acefbSJordan K. Hubbard } 2640e7d023fSBruce Evans 26562edbd31SIan Dowse if (argc == 1) { 26662edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 26762edbd31SIan Dowse update_maxwidths(&maxwidths, &statfsbuf); 26862edbd31SIan Dowse } 26962edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 270076419d2SDavid E. O'Brien if (cflag) 271076419d2SDavid E. O'Brien addstat(&totalbuf, &statfsbuf); 2724b88c807SRodney W. Grimes } 273076419d2SDavid E. O'Brien if (cflag) 274076419d2SDavid E. O'Brien prtstat(&totalbuf, &maxwidths); 275b8904f2aSJoerg Wunsch return (rv); 2764b88c807SRodney W. Grimes } 2774b88c807SRodney W. Grimes 278532aff98SPoul-Henning Kamp static char * 279b7dbd3e9SMark Murray getmntpt(const char *name) 2804b88c807SRodney W. Grimes { 281be2c4e54SDavid E. O'Brien size_t mntsize, i; 2824b88c807SRodney W. Grimes struct statfs *mntbuf; 2834b88c807SRodney W. Grimes 2844b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 2854b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 2864b88c807SRodney W. Grimes if (!strcmp(mntbuf[i].f_mntfromname, name)) 2874b88c807SRodney W. Grimes return (mntbuf[i].f_mntonname); 2884b88c807SRodney W. Grimes } 2894b88c807SRodney W. Grimes return (0); 2904b88c807SRodney W. Grimes } 2914b88c807SRodney W. Grimes 2924b88c807SRodney W. Grimes /* 2934b88c807SRodney W. Grimes * Make a pass over the file system info in ``mntbuf'' filtering out 294a78192e3SBruce Evans * file system types not in vfslist and possibly re-stating to get 2954b88c807SRodney W. Grimes * current (not cached) info. Returns the new count of valid statfs bufs. 2964b88c807SRodney W. Grimes */ 297be2c4e54SDavid E. O'Brien static size_t 298532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 2994b88c807SRodney W. Grimes { 3004b88c807SRodney W. Grimes int i, j; 3014b88c807SRodney W. Grimes struct statfs *mntbuf; 3024b88c807SRodney W. Grimes 303a78192e3SBruce Evans if (vfslist == NULL) 3044b88c807SRodney W. Grimes return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 3054b88c807SRodney W. Grimes 3064b88c807SRodney W. Grimes mntbuf = *mntbufp; 307a78192e3SBruce Evans for (j = 0, i = 0; i < mntsize; i++) { 308a78192e3SBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 309a78192e3SBruce Evans continue; 3104b88c807SRodney W. Grimes if (!nflag) 3114b88c807SRodney W. Grimes (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 3124b88c807SRodney W. Grimes else if (i != j) 3134b88c807SRodney W. Grimes mntbuf[j] = mntbuf[i]; 3144b88c807SRodney W. Grimes j++; 3154b88c807SRodney W. Grimes } 3164b88c807SRodney W. Grimes return (j); 3174b88c807SRodney W. Grimes } 3184b88c807SRodney W. Grimes 319532aff98SPoul-Henning Kamp static void 320fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used) 321dd6d33e8SMichael Haro { 322dd6d33e8SMichael Haro 3237d3940bbSPawel Jakub Dawidek prthumanval(sfsp->f_blocks * sfsp->f_bsize); 3247d3940bbSPawel Jakub Dawidek prthumanval(used * sfsp->f_bsize); 3257d3940bbSPawel Jakub Dawidek prthumanval(sfsp->f_bavail * sfsp->f_bsize); 326dd6d33e8SMichael Haro } 327dd6d33e8SMichael Haro 328532aff98SPoul-Henning Kamp static void 3297d3940bbSPawel Jakub Dawidek prthumanval(int64_t bytes) 330dd6d33e8SMichael Haro { 3317d3940bbSPawel Jakub Dawidek char buf[6]; 3327d3940bbSPawel Jakub Dawidek int flags; 333dd6d33e8SMichael Haro 3347d3940bbSPawel Jakub Dawidek flags = HN_B | HN_NOSPACE | HN_DECIMAL; 3357d3940bbSPawel Jakub Dawidek if (hflag == UNITS_SI) 3367d3940bbSPawel Jakub Dawidek flags |= HN_DIVISOR_1000; 337dd6d33e8SMichael Haro 3387d3940bbSPawel Jakub Dawidek humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 3397d3940bbSPawel Jakub Dawidek bytes, "", HN_AUTOSCALE, flags); 3407d3940bbSPawel Jakub Dawidek 3417d3940bbSPawel Jakub Dawidek (void)printf(" %6s", buf); 342dd6d33e8SMichael Haro } 343dd6d33e8SMichael Haro 344dd6d33e8SMichael Haro /* 3454b88c807SRodney W. Grimes * Convert statfs returned file system size into BLOCKSIZE units. 3464b88c807SRodney W. Grimes * Attempts to avoid overflow for large file systems. 3474b88c807SRodney W. Grimes */ 3484b88c807SRodney W. Grimes #define fsbtoblk(num, fsbs, bs) \ 3494b88c807SRodney W. Grimes (((fsbs) != 0 && (fsbs) < (bs)) ? \ 350ae21f5adSLukas Ertl (num) / (intmax_t)((bs) / (fsbs)) : \ 351ae21f5adSLukas Ertl (num) * (intmax_t)((fsbs) / (bs))) 3524b88c807SRodney W. Grimes 3534b88c807SRodney W. Grimes /* 3544b88c807SRodney W. Grimes * Print out status about a file system. 3554b88c807SRodney W. Grimes */ 356532aff98SPoul-Henning Kamp static void 35762edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp) 3584b88c807SRodney W. Grimes { 359fde81c7dSKirk McKusick static u_long blocksize; 360b7dbd3e9SMark Murray static int headerlen, timesthrough = 0; 361a95a13bbSKris Kennaway static const char *header; 362fde81c7dSKirk McKusick int64_t used, availblks, inodes; 3634b88c807SRodney W. Grimes 3644b88c807SRodney W. Grimes if (++timesthrough == 1) { 3650bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 366dd6d33e8SMichael Haro if (hflag) { 367dd6d33e8SMichael Haro header = " Size"; 3680bd9f151SIan Dowse mwp->total = mwp->used = mwp->avail = 3690bd9f151SIan Dowse (int)strlen(header); 370dd6d33e8SMichael Haro } else { 371dd6d33e8SMichael Haro header = getbsize(&headerlen, &blocksize); 3720bd9f151SIan Dowse mwp->total = imax(mwp->total, headerlen); 373dd6d33e8SMichael Haro } 3740bd9f151SIan Dowse mwp->used = imax(mwp->used, (int)strlen("Used")); 3750bd9f151SIan Dowse mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 37662edbd31SIan Dowse 377f694b8adSMark Murray (void)printf("%-*s %-*s %*s %*s Capacity", 3780bd9f151SIan Dowse mwp->mntfrom, "Filesystem", mwp->total, header, 3790bd9f151SIan Dowse mwp->used, "Used", mwp->avail, "Avail"); 38062edbd31SIan Dowse if (iflag) { 3810bd9f151SIan Dowse mwp->iused = imax(mwp->iused, (int)strlen(" iused")); 3820bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, (int)strlen("ifree")); 383f694b8adSMark Murray (void)printf(" %*s %*s %%iused", 3840bd9f151SIan Dowse mwp->iused - 2, "iused", mwp->ifree, "ifree"); 38562edbd31SIan Dowse } 3864b88c807SRodney W. Grimes (void)printf(" Mounted on\n"); 3874b88c807SRodney W. Grimes } 3880bd9f151SIan Dowse (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname); 3894b88c807SRodney W. Grimes used = sfsp->f_blocks - sfsp->f_bfree; 3904b88c807SRodney W. Grimes availblks = sfsp->f_bavail + used; 391dd6d33e8SMichael Haro if (hflag) { 392dd6d33e8SMichael Haro prthuman(sfsp, used); 393dd6d33e8SMichael Haro } else { 394ea64ad77SKris Kennaway (void)printf(" %*jd %*jd %*jd", 3950bd9f151SIan Dowse mwp->total, (intmax_t)fsbtoblk(sfsp->f_blocks, 3960bd9f151SIan Dowse sfsp->f_bsize, blocksize), 3970bd9f151SIan Dowse mwp->used, (intmax_t)fsbtoblk(used, sfsp->f_bsize, 3980bd9f151SIan Dowse blocksize), 3990bd9f151SIan Dowse mwp->avail, (intmax_t)fsbtoblk(sfsp->f_bavail, 4000bd9f151SIan Dowse sfsp->f_bsize, blocksize)); 401dd6d33e8SMichael Haro } 4024b88c807SRodney W. Grimes (void)printf(" %5.0f%%", 4034b88c807SRodney W. Grimes availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 4044b88c807SRodney W. Grimes if (iflag) { 4054b88c807SRodney W. Grimes inodes = sfsp->f_files; 4064b88c807SRodney W. Grimes used = inodes - sfsp->f_ffree; 4070bd9f151SIan Dowse (void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used, 4080bd9f151SIan Dowse mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 : 4090bd9f151SIan Dowse (double)used / (double)inodes * 100.0); 410cca108e6SDavid E. O'Brien } else 411cca108e6SDavid E. O'Brien (void)printf(" "); 412c6f13844SDavid E. O'Brien if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0) 413076419d2SDavid E. O'Brien (void)printf(" %s", sfsp->f_mntonname); 414076419d2SDavid E. O'Brien (void)printf("\n"); 415076419d2SDavid E. O'Brien } 416076419d2SDavid E. O'Brien 417076419d2SDavid E. O'Brien void 418076419d2SDavid E. O'Brien addstat(struct statfs *totalfsp, struct statfs *statfsp) 419076419d2SDavid E. O'Brien { 420c6f13844SDavid E. O'Brien uint64_t bsize; 421076419d2SDavid E. O'Brien 422c6f13844SDavid E. O'Brien bsize = statfsp->f_bsize / totalfsp->f_bsize; 423076419d2SDavid E. O'Brien totalfsp->f_blocks += statfsp->f_blocks * bsize; 424076419d2SDavid E. O'Brien totalfsp->f_bfree += statfsp->f_bfree * bsize; 425076419d2SDavid E. O'Brien totalfsp->f_bavail += statfsp->f_bavail * bsize; 426076419d2SDavid E. O'Brien totalfsp->f_files += statfsp->f_files; 427076419d2SDavid E. O'Brien totalfsp->f_ffree += statfsp->f_ffree; 4284b88c807SRodney W. Grimes } 4294b88c807SRodney W. Grimes 4304b88c807SRodney W. Grimes /* 43162edbd31SIan Dowse * Update the maximum field-width information in `mwp' based on 43262edbd31SIan Dowse * the file system specified by `sfsp'. 43362edbd31SIan Dowse */ 434532aff98SPoul-Henning Kamp static void 435b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 43662edbd31SIan Dowse { 437fde81c7dSKirk McKusick static u_long blocksize = 0; 438dc474219SMike Barcroft int dummy; 43962edbd31SIan Dowse 44062edbd31SIan Dowse if (blocksize == 0) 44162edbd31SIan Dowse getbsize(&dummy, &blocksize); 44262edbd31SIan Dowse 4430bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 4440bd9f151SIan Dowse mwp->total = imax(mwp->total, int64width( 445fde81c7dSKirk McKusick fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 4460bd9f151SIan Dowse mwp->used = imax(mwp->used, 4470bd9f151SIan Dowse int64width(fsbtoblk((int64_t)sfsp->f_blocks - 448fde81c7dSKirk McKusick (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 4490bd9f151SIan Dowse mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 45062edbd31SIan Dowse sfsp->f_bsize, blocksize))); 4510bd9f151SIan Dowse mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 45262edbd31SIan Dowse sfsp->f_ffree)); 4530bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 45462edbd31SIan Dowse } 45562edbd31SIan Dowse 4560bd9f151SIan Dowse /* Return the width in characters of the specified value. */ 4570bd9f151SIan Dowse static int 458fde81c7dSKirk McKusick int64width(int64_t val) 45962edbd31SIan Dowse { 4600bd9f151SIan Dowse int len; 46162edbd31SIan Dowse 46262edbd31SIan Dowse len = 0; 46362edbd31SIan Dowse /* Negative or zero values require one extra digit. */ 46462edbd31SIan Dowse if (val <= 0) { 46562edbd31SIan Dowse val = -val; 46662edbd31SIan Dowse len++; 46762edbd31SIan Dowse } 46862edbd31SIan Dowse while (val > 0) { 46962edbd31SIan Dowse len++; 47062edbd31SIan Dowse val /= 10; 47162edbd31SIan Dowse } 47262edbd31SIan Dowse 47362edbd31SIan Dowse return (len); 47462edbd31SIan Dowse } 47562edbd31SIan Dowse 476532aff98SPoul-Henning Kamp static void 477f9bcb0beSWarner Losh usage(void) 4784b88c807SRodney W. Grimes { 479dd6d33e8SMichael Haro 480a78192e3SBruce Evans (void)fprintf(stderr, 481c6f13844SDavid E. O'Brien "usage: df [-b | -g | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...]\n"); 482dd6d33e8SMichael Haro exit(EX_USAGE); 4834b88c807SRodney W. Grimes } 484a25695c3SJim Pirzyk 485532aff98SPoul-Henning Kamp static char * 486f9bcb0beSWarner Losh makenetvfslist(void) 487a25695c3SJim Pirzyk { 488a25695c3SJim Pirzyk char *str, *strptr, **listptr; 489b7dbd3e9SMark Murray struct xvfsconf *xvfsp, *keep_xvfsp; 4905965373eSMaxime Henrion size_t buflen; 4915965373eSMaxime Henrion int cnt, i, maxvfsconf; 492a25695c3SJim Pirzyk 4935965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 4945965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 495a25695c3SJim Pirzyk return (NULL); 496a25695c3SJim Pirzyk } 4975965373eSMaxime Henrion xvfsp = malloc(buflen); 4985965373eSMaxime Henrion if (xvfsp == NULL) { 4995965373eSMaxime Henrion warnx("malloc failed"); 5005965373eSMaxime Henrion return (NULL); 5015965373eSMaxime Henrion } 502b7dbd3e9SMark Murray keep_xvfsp = xvfsp; 5035965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 5045965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 505b7dbd3e9SMark Murray free(keep_xvfsp); 5065965373eSMaxime Henrion return (NULL); 5075965373eSMaxime Henrion } 5085965373eSMaxime Henrion maxvfsconf = buflen / sizeof(struct xvfsconf); 509a25695c3SJim Pirzyk 510a25695c3SJim Pirzyk if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 511a25695c3SJim Pirzyk warnx("malloc failed"); 512b7dbd3e9SMark Murray free(keep_xvfsp); 513a25695c3SJim Pirzyk return (NULL); 514a25695c3SJim Pirzyk } 515a25695c3SJim Pirzyk 5165965373eSMaxime Henrion for (cnt = 0, i = 0; i < maxvfsconf; i++) { 5175965373eSMaxime Henrion if (xvfsp->vfc_flags & VFCF_NETWORK) { 5185965373eSMaxime Henrion listptr[cnt++] = strdup(xvfsp->vfc_name); 519a067aeceSGarrett Wollman if (listptr[cnt-1] == NULL) { 520a25695c3SJim Pirzyk warnx("malloc failed"); 521b7dbd3e9SMark Murray free(listptr); 522b7dbd3e9SMark Murray free(keep_xvfsp); 523a25695c3SJim Pirzyk return (NULL); 524a25695c3SJim Pirzyk } 525a25695c3SJim Pirzyk } 5265965373eSMaxime Henrion xvfsp++; 5275965373eSMaxime Henrion } 528a25695c3SJim Pirzyk 529cf5b29e1SRuslan Ermilov if (cnt == 0 || 530cf5b29e1SRuslan Ermilov (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 531cf5b29e1SRuslan Ermilov if (cnt > 0) 532a25695c3SJim Pirzyk warnx("malloc failed"); 533a25695c3SJim Pirzyk free(listptr); 534b7dbd3e9SMark Murray free(keep_xvfsp); 535a25695c3SJim Pirzyk return (NULL); 536a25695c3SJim Pirzyk } 537a25695c3SJim Pirzyk 538a25695c3SJim Pirzyk *str = 'n'; *(str + 1) = 'o'; 539a25695c3SJim Pirzyk for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 540a25695c3SJim Pirzyk strncpy(strptr, listptr[i], 32); 541a25695c3SJim Pirzyk strptr += strlen(listptr[i]); 542a25695c3SJim Pirzyk *strptr = ','; 543a25695c3SJim Pirzyk free(listptr[i]); 544a25695c3SJim Pirzyk } 54516fc3635SMark Murray *(--strptr) = '\0'; 546a25695c3SJim Pirzyk 547b7dbd3e9SMark Murray free(keep_xvfsp); 548a25695c3SJim Pirzyk free(listptr); 549a25695c3SJim Pirzyk return (str); 550a25695c3SJim Pirzyk } 551