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> 57a25695c3SJim Pirzyk #include <sys/sysctl.h> 58a78192e3SBruce Evans #include <ufs/ufs/ufsmount.h> 594b88c807SRodney W. Grimes 604b88c807SRodney W. Grimes #include <err.h> 614b88c807SRodney W. Grimes #include <errno.h> 624b88c807SRodney W. Grimes #include <fcntl.h> 63dd6d33e8SMichael Haro #include <math.h> 644b88c807SRodney W. Grimes #include <stdio.h> 654b88c807SRodney W. Grimes #include <stdlib.h> 664b88c807SRodney W. Grimes #include <string.h> 67dd6d33e8SMichael Haro #include <sysexits.h> 684b88c807SRodney W. Grimes #include <unistd.h> 694b88c807SRodney W. Grimes 70dd6d33e8SMichael Haro #define UNITS_SI 1 71dd6d33e8SMichael Haro #define UNITS_2 2 72dd6d33e8SMichael Haro 73dd6d33e8SMichael Haro #define KILO_SZ(n) (n) 74dd6d33e8SMichael Haro #define MEGA_SZ(n) ((n) * (n)) 75dd6d33e8SMichael Haro #define GIGA_SZ(n) ((n) * (n) * (n)) 76dd6d33e8SMichael Haro #define TERA_SZ(n) ((n) * (n) * (n) * (n)) 77dd6d33e8SMichael Haro #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n)) 78dd6d33e8SMichael Haro 79dd6d33e8SMichael Haro #define KILO_2_SZ (KILO_SZ(1024ULL)) 80dd6d33e8SMichael Haro #define MEGA_2_SZ (MEGA_SZ(1024ULL)) 81dd6d33e8SMichael Haro #define GIGA_2_SZ (GIGA_SZ(1024ULL)) 82dd6d33e8SMichael Haro #define TERA_2_SZ (TERA_SZ(1024ULL)) 83dd6d33e8SMichael Haro #define PETA_2_SZ (PETA_SZ(1024ULL)) 84dd6d33e8SMichael Haro 85dd6d33e8SMichael Haro #define KILO_SI_SZ (KILO_SZ(1000ULL)) 86dd6d33e8SMichael Haro #define MEGA_SI_SZ (MEGA_SZ(1000ULL)) 87dd6d33e8SMichael Haro #define GIGA_SI_SZ (GIGA_SZ(1000ULL)) 88dd6d33e8SMichael Haro #define TERA_SI_SZ (TERA_SZ(1000ULL)) 89dd6d33e8SMichael Haro #define PETA_SI_SZ (PETA_SZ(1000ULL)) 90dd6d33e8SMichael Haro 91dd6d33e8SMichael Haro unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ}; 92dd6d33e8SMichael Haro unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ}; 93dd6d33e8SMichael Haro unsigned long long *valp; 94dd6d33e8SMichael Haro 95dd6d33e8SMichael Haro typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t; 96dd6d33e8SMichael Haro 97dd6d33e8SMichael Haro int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA }; 98dd6d33e8SMichael Haro 994b88c807SRodney W. Grimes int bread __P((off_t, void *, int)); 100a95a13bbSKris Kennaway int checkvfsname __P((const char *, char **)); 1014b88c807SRodney W. Grimes char *getmntpt __P((char *)); 102a95a13bbSKris Kennaway int main __P((int, char *[])); 103a25695c3SJim Pirzyk char *makenetvfslist __P((void)); 104a95a13bbSKris Kennaway char **makevfslist __P((char *)); 105dd6d33e8SMichael Haro void prthuman __P((struct statfs *, long)); 106dd6d33e8SMichael Haro void prthumanval __P((double)); 1074b88c807SRodney W. Grimes void prtstat __P((struct statfs *, int)); 108a95a13bbSKris Kennaway long regetmntinfo __P((struct statfs **, long, char **)); 109b8904f2aSJoerg Wunsch int ufs_df __P((char *, int)); 110dd6d33e8SMichael Haro unit_t unit_adjust __P((double *)); 1114b88c807SRodney W. Grimes void usage __P((void)); 1124b88c807SRodney W. Grimes 113dd6d33e8SMichael Haro int aflag = 0, hflag, iflag, nflag; 1144b88c807SRodney W. Grimes struct ufs_args mdev; 1154b88c807SRodney W. Grimes 1164b88c807SRodney W. Grimes int 1174b88c807SRodney W. Grimes main(argc, argv) 1184b88c807SRodney W. Grimes int argc; 1194b88c807SRodney W. Grimes char *argv[]; 1204b88c807SRodney W. Grimes { 1214b88c807SRodney W. Grimes struct stat stbuf; 1224b88c807SRodney W. Grimes struct statfs statfsbuf, *mntbuf; 123a95a13bbSKris Kennaway const char *fstype; 124a95a13bbSKris Kennaway char *mntpath, *mntpt, **vfslist; 125a78192e3SBruce Evans long mntsize; 126a95a13bbSKris Kennaway int ch, i, maxwidth, rv, width; 127f3895a82SKris Kennaway 128f3895a82SKris Kennaway fstype = "ufs"; 1294b88c807SRodney W. Grimes 130a78192e3SBruce Evans vfslist = NULL; 131a25695c3SJim Pirzyk while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1) 1324b88c807SRodney W. Grimes switch (ch) { 1335b42dac8SJulian Elischer case 'a': 1345b42dac8SJulian Elischer aflag = 1; 1355b42dac8SJulian Elischer break; 136dd6d33e8SMichael Haro case 'b': 137dd6d33e8SMichael Haro /* FALLTHROUGH */ 138dd6d33e8SMichael Haro case 'P': 139dd6d33e8SMichael Haro putenv("BLOCKSIZE=512"); 140dd6d33e8SMichael Haro hflag = 0; 141dd6d33e8SMichael Haro break; 14293a3fa19SJohn W. De Boskey case 'g': 14393a3fa19SJohn W. De Boskey putenv("BLOCKSIZE=1g"); 14493a3fa19SJohn W. De Boskey hflag = 0; 14593a3fa19SJohn W. De Boskey break; 146dd6d33e8SMichael Haro case 'H': 147dd6d33e8SMichael Haro hflag = UNITS_SI; 148dd6d33e8SMichael Haro valp = vals_si; 149dd6d33e8SMichael Haro break; 150dd6d33e8SMichael Haro case 'h': 151dd6d33e8SMichael Haro hflag = UNITS_2; 152dd6d33e8SMichael Haro valp = vals_base2; 153dd6d33e8SMichael Haro break; 1544b88c807SRodney W. Grimes case 'i': 1554b88c807SRodney W. Grimes iflag = 1; 1564b88c807SRodney W. Grimes break; 1577f0eabfdSGarrett Wollman case 'k': 1589d4081eeSDavid Greenman putenv("BLOCKSIZE=1k"); 159dd6d33e8SMichael Haro hflag = 0; 160dd6d33e8SMichael Haro break; 161a25695c3SJim Pirzyk case 'l': 162a25695c3SJim Pirzyk if (vfslist != NULL) 163a25695c3SJim Pirzyk errx(1, "-l and -t are mutually exclusive."); 164a25695c3SJim Pirzyk vfslist = makevfslist(makenetvfslist()); 165a25695c3SJim Pirzyk break; 166dd6d33e8SMichael Haro case 'm': 167dd6d33e8SMichael Haro putenv("BLOCKSIZE=1m"); 168dd6d33e8SMichael Haro hflag = 0; 1697f0eabfdSGarrett Wollman break; 1704b88c807SRodney W. Grimes case 'n': 1714b88c807SRodney W. Grimes nflag = 1; 1724b88c807SRodney W. Grimes break; 1734b88c807SRodney W. Grimes case 't': 174a78192e3SBruce Evans if (vfslist != NULL) 175a78192e3SBruce Evans errx(1, "only one -t option may be specified."); 176f3895a82SKris Kennaway fstype = optarg; 177a78192e3SBruce Evans vfslist = makevfslist(optarg); 1784b88c807SRodney W. Grimes break; 1794b88c807SRodney W. Grimes case '?': 1804b88c807SRodney W. Grimes default: 1814b88c807SRodney W. Grimes usage(); 1824b88c807SRodney W. Grimes } 1834b88c807SRodney W. Grimes argc -= optind; 1844b88c807SRodney W. Grimes argv += optind; 1854b88c807SRodney W. Grimes 1864b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 1874b88c807SRodney W. Grimes maxwidth = 0; 1884b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 1894b88c807SRodney W. Grimes width = strlen(mntbuf[i].f_mntfromname); 1904b88c807SRodney W. Grimes if (width > maxwidth) 1914b88c807SRodney W. Grimes maxwidth = width; 1924b88c807SRodney W. Grimes } 1934b88c807SRodney W. Grimes 194b8904f2aSJoerg Wunsch rv = 0; 1954b88c807SRodney W. Grimes if (!*argv) { 196a78192e3SBruce Evans mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 197a78192e3SBruce Evans if (vfslist != NULL) { 1984b88c807SRodney W. Grimes maxwidth = 0; 1994b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 2004b88c807SRodney W. Grimes width = strlen(mntbuf[i].f_mntfromname); 2014b88c807SRodney W. Grimes if (width > maxwidth) 2024b88c807SRodney W. Grimes maxwidth = width; 2034b88c807SRodney W. Grimes } 2044b88c807SRodney W. Grimes } 2055b42dac8SJulian Elischer for (i = 0; i < mntsize; i++) { 2065b42dac8SJulian Elischer if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 2074b88c807SRodney W. Grimes prtstat(&mntbuf[i], maxwidth); 2085b42dac8SJulian Elischer } 209b8904f2aSJoerg Wunsch exit(rv); 2104b88c807SRodney W. Grimes } 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes for (; *argv; argv++) { 2134b88c807SRodney W. Grimes if (stat(*argv, &stbuf) < 0) { 2144b88c807SRodney W. Grimes if ((mntpt = getmntpt(*argv)) == 0) { 2154b88c807SRodney W. Grimes warn("%s", *argv); 216b8904f2aSJoerg Wunsch rv = 1; 2174b88c807SRodney W. Grimes continue; 2184b88c807SRodney W. Grimes } 219f3895a82SKris Kennaway } else if (S_ISCHR(stbuf.st_mode)) { 220f3895a82SKris Kennaway if ((mntpt = getmntpt(*argv)) == 0) { 221f3895a82SKris Kennaway mdev.fspec = *argv; 222f3895a82SKris Kennaway mntpath = strdup("/tmp/df.XXXXXX"); 223f3895a82SKris Kennaway if (mntpath == NULL) { 224f3895a82SKris Kennaway warn("strdup failed"); 225f3895a82SKris Kennaway rv = 1; 2264b88c807SRodney W. Grimes continue; 227f3895a82SKris Kennaway } 228f3895a82SKris Kennaway mntpt = mkdtemp(mntpath); 229f3895a82SKris Kennaway if (mntpt == NULL) { 230f3895a82SKris Kennaway warn("mkdtemp(\"%s\") failed", mntpath); 231f3895a82SKris Kennaway rv = 1; 232f3895a82SKris Kennaway free(mntpath); 233f3895a82SKris Kennaway continue; 234f3895a82SKris Kennaway } 235f3895a82SKris Kennaway if (mount(fstype, mntpt, MNT_RDONLY, 236f3895a82SKris Kennaway &mdev) != 0) { 237f3895a82SKris Kennaway rv = ufs_df(*argv, maxwidth) || rv; 238f3895a82SKris Kennaway (void)rmdir(mntpt); 239f3895a82SKris Kennaway free(mntpath); 240f3895a82SKris Kennaway continue; 241f3895a82SKris Kennaway } else if (statfs(mntpt, &statfsbuf) == 0) { 242f3895a82SKris Kennaway statfsbuf.f_mntonname[0] = '\0'; 243f3895a82SKris Kennaway prtstat(&statfsbuf, maxwidth); 244f3895a82SKris Kennaway } else { 245f3895a82SKris Kennaway warn("%s", *argv); 246f3895a82SKris Kennaway rv = 1; 247f3895a82SKris Kennaway } 248f3895a82SKris Kennaway (void)unmount(mntpt, 0); 249f3895a82SKris Kennaway (void)rmdir(mntpt); 250f3895a82SKris Kennaway free(mntpath); 251f3895a82SKris Kennaway continue; 252f3895a82SKris Kennaway } 2534b88c807SRodney W. Grimes } else 2544b88c807SRodney W. Grimes mntpt = *argv; 2554b88c807SRodney W. Grimes /* 2564b88c807SRodney W. Grimes * Statfs does not take a `wait' flag, so we cannot 2574b88c807SRodney W. Grimes * implement nflag here. 2584b88c807SRodney W. Grimes */ 2594b88c807SRodney W. Grimes if (statfs(mntpt, &statfsbuf) < 0) { 2604b88c807SRodney W. Grimes warn("%s", mntpt); 261b8904f2aSJoerg Wunsch rv = 1; 2624b88c807SRodney W. Grimes continue; 2634b88c807SRodney W. Grimes } 2644b88c807SRodney W. Grimes if (argc == 1) 2654b88c807SRodney W. Grimes maxwidth = strlen(statfsbuf.f_mntfromname) + 1; 2664b88c807SRodney W. Grimes prtstat(&statfsbuf, maxwidth); 2674b88c807SRodney W. Grimes } 268b8904f2aSJoerg Wunsch return (rv); 2694b88c807SRodney W. Grimes } 2704b88c807SRodney W. Grimes 2714b88c807SRodney W. Grimes char * 2724b88c807SRodney W. Grimes getmntpt(name) 2734b88c807SRodney W. Grimes char *name; 2744b88c807SRodney W. Grimes { 2754b88c807SRodney W. Grimes long mntsize, i; 2764b88c807SRodney W. Grimes struct statfs *mntbuf; 2774b88c807SRodney W. Grimes 2784b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 2794b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 2804b88c807SRodney W. Grimes if (!strcmp(mntbuf[i].f_mntfromname, name)) 2814b88c807SRodney W. Grimes return (mntbuf[i].f_mntonname); 2824b88c807SRodney W. Grimes } 2834b88c807SRodney W. Grimes return (0); 2844b88c807SRodney W. Grimes } 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes /* 2874b88c807SRodney W. Grimes * Make a pass over the filesystem info in ``mntbuf'' filtering out 288a78192e3SBruce Evans * filesystem types not in vfslist and possibly re-stating to get 2894b88c807SRodney W. Grimes * current (not cached) info. Returns the new count of valid statfs bufs. 2904b88c807SRodney W. Grimes */ 2914b88c807SRodney W. Grimes long 292a78192e3SBruce Evans regetmntinfo(mntbufp, mntsize, vfslist) 2934b88c807SRodney W. Grimes struct statfs **mntbufp; 294a78192e3SBruce Evans long mntsize; 295a78192e3SBruce Evans char **vfslist; 2964b88c807SRodney W. Grimes { 2974b88c807SRodney W. Grimes int i, j; 2984b88c807SRodney W. Grimes struct statfs *mntbuf; 2994b88c807SRodney W. Grimes 300a78192e3SBruce Evans if (vfslist == NULL) 3014b88c807SRodney W. Grimes return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 3024b88c807SRodney W. Grimes 3034b88c807SRodney W. Grimes mntbuf = *mntbufp; 304a78192e3SBruce Evans for (j = 0, i = 0; i < mntsize; i++) { 305a78192e3SBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 306a78192e3SBruce Evans continue; 3074b88c807SRodney W. Grimes if (!nflag) 3084b88c807SRodney W. Grimes (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 3094b88c807SRodney W. Grimes else if (i != j) 3104b88c807SRodney W. Grimes mntbuf[j] = mntbuf[i]; 3114b88c807SRodney W. Grimes j++; 3124b88c807SRodney W. Grimes } 3134b88c807SRodney W. Grimes return (j); 3144b88c807SRodney W. Grimes } 3154b88c807SRodney W. Grimes 3164b88c807SRodney W. Grimes /* 317dd6d33e8SMichael Haro * Output in "human-readable" format. Uses 3 digits max and puts 318dd6d33e8SMichael Haro * unit suffixes at the end. Makes output compact and easy to read, 319dd6d33e8SMichael Haro * especially on huge disks. 320dd6d33e8SMichael Haro * 321dd6d33e8SMichael Haro */ 322dd6d33e8SMichael Haro unit_t 323dd6d33e8SMichael Haro unit_adjust(val) 324dd6d33e8SMichael Haro double *val; 325dd6d33e8SMichael Haro { 326dd6d33e8SMichael Haro double abval; 327dd6d33e8SMichael Haro unit_t unit; 328dd6d33e8SMichael Haro unsigned int unit_sz; 329dd6d33e8SMichael Haro 330dd6d33e8SMichael Haro abval = fabs(*val); 331dd6d33e8SMichael Haro 332dd6d33e8SMichael Haro unit_sz = abval ? ilogb(abval) / 10 : 0; 333dd6d33e8SMichael Haro 334dd6d33e8SMichael Haro if (unit_sz >= UNIT_MAX) { 335dd6d33e8SMichael Haro unit = NONE; 336dd6d33e8SMichael Haro } else { 337dd6d33e8SMichael Haro unit = unitp[unit_sz]; 338dd6d33e8SMichael Haro *val /= (double)valp[unit_sz]; 339dd6d33e8SMichael Haro } 340dd6d33e8SMichael Haro 341dd6d33e8SMichael Haro return (unit); 342dd6d33e8SMichael Haro } 343dd6d33e8SMichael Haro 344dd6d33e8SMichael Haro void 345dd6d33e8SMichael Haro prthuman(sfsp, used) 346dd6d33e8SMichael Haro struct statfs *sfsp; 347dd6d33e8SMichael Haro long used; 348dd6d33e8SMichael Haro { 349dd6d33e8SMichael Haro 350dd6d33e8SMichael Haro prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize); 351dd6d33e8SMichael Haro prthumanval((double)used * (double)sfsp->f_bsize); 352dd6d33e8SMichael Haro prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize); 353dd6d33e8SMichael Haro } 354dd6d33e8SMichael Haro 355dd6d33e8SMichael Haro void 356dd6d33e8SMichael Haro prthumanval(bytes) 357dd6d33e8SMichael Haro double bytes; 358dd6d33e8SMichael Haro { 359dd6d33e8SMichael Haro 360dd6d33e8SMichael Haro unit_t unit; 361dd6d33e8SMichael Haro unit = unit_adjust(&bytes); 362dd6d33e8SMichael Haro 363dd6d33e8SMichael Haro if (bytes == 0) 364dd6d33e8SMichael Haro (void)printf(" 0B"); 365dd6d33e8SMichael Haro else if (bytes > 10) 366dd6d33e8SMichael Haro (void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]); 367dd6d33e8SMichael Haro else 368dd6d33e8SMichael Haro (void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]); 369dd6d33e8SMichael Haro } 370dd6d33e8SMichael Haro 371dd6d33e8SMichael Haro /* 3724b88c807SRodney W. Grimes * Convert statfs returned filesystem size into BLOCKSIZE units. 3734b88c807SRodney W. Grimes * Attempts to avoid overflow for large filesystems. 3744b88c807SRodney W. Grimes */ 3754b88c807SRodney W. Grimes #define fsbtoblk(num, fsbs, bs) \ 3764b88c807SRodney W. Grimes (((fsbs) != 0 && (fsbs) < (bs)) ? \ 3774b88c807SRodney W. Grimes (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs))) 3784b88c807SRodney W. Grimes 3794b88c807SRodney W. Grimes /* 3804b88c807SRodney W. Grimes * Print out status about a filesystem. 3814b88c807SRodney W. Grimes */ 3824b88c807SRodney W. Grimes void 3834b88c807SRodney W. Grimes prtstat(sfsp, maxwidth) 3844b88c807SRodney W. Grimes struct statfs *sfsp; 3854b88c807SRodney W. Grimes int maxwidth; 3864b88c807SRodney W. Grimes { 3874b88c807SRodney W. Grimes static long blocksize; 3884b88c807SRodney W. Grimes static int headerlen, timesthrough; 389a95a13bbSKris Kennaway static const char *header; 3904b88c807SRodney W. Grimes long used, availblks, inodes; 3914b88c807SRodney W. Grimes 3924b88c807SRodney W. Grimes if (maxwidth < 11) 3934b88c807SRodney W. Grimes maxwidth = 11; 3944b88c807SRodney W. Grimes if (++timesthrough == 1) { 395dd6d33e8SMichael Haro if (hflag) { 396dd6d33e8SMichael Haro header = " Size"; 397dd6d33e8SMichael Haro headerlen = strlen(header); 398dd6d33e8SMichael Haro (void)printf("%-*.*s %-s Used Avail Capacity", 3994b88c807SRodney W. Grimes maxwidth, maxwidth, "Filesystem", header); 400dd6d33e8SMichael Haro } else { 401dd6d33e8SMichael Haro header = getbsize(&headerlen, &blocksize); 402dd6d33e8SMichael Haro (void)printf("%-*.*s %-s Used Avail Capacity", 403dd6d33e8SMichael Haro maxwidth, maxwidth, "Filesystem", header); 404dd6d33e8SMichael Haro } 4054b88c807SRodney W. Grimes if (iflag) 4064b88c807SRodney W. Grimes (void)printf(" iused ifree %%iused"); 4074b88c807SRodney W. Grimes (void)printf(" Mounted on\n"); 4084b88c807SRodney W. Grimes } 4094b88c807SRodney W. Grimes (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname); 4104b88c807SRodney W. Grimes used = sfsp->f_blocks - sfsp->f_bfree; 4114b88c807SRodney W. Grimes availblks = sfsp->f_bavail + used; 412dd6d33e8SMichael Haro if (hflag) { 413dd6d33e8SMichael Haro prthuman(sfsp, used); 414dd6d33e8SMichael Haro } else { 4154b88c807SRodney W. Grimes (void)printf(" %*ld %8ld %8ld", headerlen, 4164b88c807SRodney W. Grimes fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize), 4174b88c807SRodney W. Grimes fsbtoblk(used, sfsp->f_bsize, blocksize), 4184b88c807SRodney W. Grimes fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize)); 419dd6d33e8SMichael Haro } 4204b88c807SRodney W. Grimes (void)printf(" %5.0f%%", 4214b88c807SRodney W. Grimes availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 4224b88c807SRodney W. Grimes if (iflag) { 4234b88c807SRodney W. Grimes inodes = sfsp->f_files; 4244b88c807SRodney W. Grimes used = inodes - sfsp->f_ffree; 4254b88c807SRodney W. Grimes (void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree, 4264b88c807SRodney W. Grimes inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0); 4274b88c807SRodney W. Grimes } else 4284b88c807SRodney W. Grimes (void)printf(" "); 4294b88c807SRodney W. Grimes (void)printf(" %s\n", sfsp->f_mntonname); 4304b88c807SRodney W. Grimes } 4314b88c807SRodney W. Grimes 4324b88c807SRodney W. Grimes /* 4334b88c807SRodney W. Grimes * This code constitutes the pre-system call Berkeley df code for extracting 4344b88c807SRodney W. Grimes * information from filesystem superblocks. 4354b88c807SRodney W. Grimes */ 436a78192e3SBruce Evans #include <ufs/ufs/dinode.h> 4374b88c807SRodney W. Grimes #include <ufs/ffs/fs.h> 4384b88c807SRodney W. Grimes #include <errno.h> 4394b88c807SRodney W. Grimes #include <fstab.h> 4404b88c807SRodney W. Grimes 4414b88c807SRodney W. Grimes union { 4424b88c807SRodney W. Grimes struct fs iu_fs; 4434b88c807SRodney W. Grimes char dummy[SBSIZE]; 4444b88c807SRodney W. Grimes } sb; 4454b88c807SRodney W. Grimes #define sblock sb.iu_fs 4464b88c807SRodney W. Grimes 4474b88c807SRodney W. Grimes int rfd; 4484b88c807SRodney W. Grimes 449b8904f2aSJoerg Wunsch int 4504b88c807SRodney W. Grimes ufs_df(file, maxwidth) 4514b88c807SRodney W. Grimes char *file; 4524b88c807SRodney W. Grimes int maxwidth; 4534b88c807SRodney W. Grimes { 4544b88c807SRodney W. Grimes struct statfs statfsbuf; 4554b88c807SRodney W. Grimes struct statfs *sfsp; 456a95a13bbSKris Kennaway const char *mntpt; 4574b88c807SRodney W. Grimes static int synced; 4584b88c807SRodney W. Grimes 4594b88c807SRodney W. Grimes if (synced++ == 0) 4604b88c807SRodney W. Grimes sync(); 4614b88c807SRodney W. Grimes 4624b88c807SRodney W. Grimes if ((rfd = open(file, O_RDONLY)) < 0) { 4634b88c807SRodney W. Grimes warn("%s", file); 46480214f04SJoerg Wunsch return (1); 4654b88c807SRodney W. Grimes } 4664b88c807SRodney W. Grimes if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) { 4674b88c807SRodney W. Grimes (void)close(rfd); 46880214f04SJoerg Wunsch return (1); 4694b88c807SRodney W. Grimes } 4704b88c807SRodney W. Grimes sfsp = &statfsbuf; 471a78192e3SBruce Evans sfsp->f_type = 1; 472a78192e3SBruce Evans strcpy(sfsp->f_fstypename, "ufs"); 4734b88c807SRodney W. Grimes sfsp->f_flags = 0; 4744b88c807SRodney W. Grimes sfsp->f_bsize = sblock.fs_fsize; 4754b88c807SRodney W. Grimes sfsp->f_iosize = sblock.fs_bsize; 4764b88c807SRodney W. Grimes sfsp->f_blocks = sblock.fs_dsize; 4774b88c807SRodney W. Grimes sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + 4784b88c807SRodney W. Grimes sblock.fs_cstotal.cs_nffree; 47951ea8b57SBruce Evans sfsp->f_bavail = freespace(&sblock, sblock.fs_minfree); 4804b88c807SRodney W. Grimes sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg; 4814b88c807SRodney W. Grimes sfsp->f_ffree = sblock.fs_cstotal.cs_nifree; 4824b88c807SRodney W. Grimes sfsp->f_fsid.val[0] = 0; 4834b88c807SRodney W. Grimes sfsp->f_fsid.val[1] = 0; 4844b88c807SRodney W. Grimes if ((mntpt = getmntpt(file)) == 0) 4854b88c807SRodney W. Grimes mntpt = ""; 486a95a13bbSKris Kennaway memmove(&sfsp->f_mntonname[0], mntpt, (size_t)MNAMELEN); 487a95a13bbSKris Kennaway memmove(&sfsp->f_mntfromname[0], file, (size_t)MNAMELEN); 4884b88c807SRodney W. Grimes prtstat(sfsp, maxwidth); 4894b88c807SRodney W. Grimes (void)close(rfd); 49080214f04SJoerg Wunsch return (0); 4914b88c807SRodney W. Grimes } 4924b88c807SRodney W. Grimes 4934b88c807SRodney W. Grimes int 4944b88c807SRodney W. Grimes bread(off, buf, cnt) 4954b88c807SRodney W. Grimes off_t off; 4964b88c807SRodney W. Grimes void *buf; 4974b88c807SRodney W. Grimes int cnt; 4984b88c807SRodney W. Grimes { 499a95a13bbSKris Kennaway ssize_t nr; 5004b88c807SRodney W. Grimes 5014b88c807SRodney W. Grimes (void)lseek(rfd, off, SEEK_SET); 502a95a13bbSKris Kennaway if ((nr = read(rfd, buf, (size_t)cnt)) != (ssize_t)cnt) { 5034b88c807SRodney W. Grimes /* Probably a dismounted disk if errno == EIO. */ 5044b88c807SRodney W. Grimes if (errno != EIO) 505a95a13bbSKris Kennaway (void)fprintf(stderr, "\ndf: %lld: %s\n", 506a95a13bbSKris Kennaway (long long)off, strerror(nr > 0 ? EIO : errno)); 5074b88c807SRodney W. Grimes return (0); 5084b88c807SRodney W. Grimes } 5094b88c807SRodney W. Grimes return (1); 5104b88c807SRodney W. Grimes } 5114b88c807SRodney W. Grimes 5124b88c807SRodney W. Grimes void 5134b88c807SRodney W. Grimes usage() 5144b88c807SRodney W. Grimes { 515dd6d33e8SMichael Haro 516a78192e3SBruce Evans (void)fprintf(stderr, 517a25695c3SJim Pirzyk "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n"); 518dd6d33e8SMichael Haro exit(EX_USAGE); 5194b88c807SRodney W. Grimes } 520a25695c3SJim Pirzyk 521a067aeceSGarrett Wollman char * 522a067aeceSGarrett Wollman makenetvfslist() 523a25695c3SJim Pirzyk { 524a25695c3SJim Pirzyk char *str, *strptr, **listptr; 525b3e8643fSMatt Jacob int mib[3], maxvfsconf, cnt=0, i; 526b3e8643fSMatt Jacob size_t miblen; 527a25695c3SJim Pirzyk struct ovfsconf *ptr; 528a25695c3SJim Pirzyk 529a25695c3SJim Pirzyk mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM; 530a25695c3SJim Pirzyk miblen=sizeof(maxvfsconf); 531cc66540eSJacques Vidrine if (sysctl(mib, (unsigned int)(sizeof(mib) / sizeof(mib[0])), 532cc66540eSJacques Vidrine &maxvfsconf, &miblen, NULL, 0)) { 533a25695c3SJim Pirzyk warnx("sysctl failed"); 534a25695c3SJim Pirzyk return (NULL); 535a25695c3SJim Pirzyk } 536a25695c3SJim Pirzyk 537a25695c3SJim Pirzyk if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 538a25695c3SJim Pirzyk warnx("malloc failed"); 539a25695c3SJim Pirzyk return (NULL); 540a25695c3SJim Pirzyk } 541a25695c3SJim Pirzyk 542a25695c3SJim Pirzyk for (ptr = getvfsent(); ptr; ptr = getvfsent()) 543a25695c3SJim Pirzyk if (ptr->vfc_flags & VFCF_NETWORK) { 544a25695c3SJim Pirzyk listptr[cnt++] = strdup(ptr->vfc_name); 545a067aeceSGarrett Wollman if (listptr[cnt-1] == NULL) { 546a25695c3SJim Pirzyk warnx("malloc failed"); 547a25695c3SJim Pirzyk return (NULL); 548a25695c3SJim Pirzyk } 549a25695c3SJim Pirzyk } 550a25695c3SJim Pirzyk 551a25695c3SJim Pirzyk if ((str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 552a25695c3SJim Pirzyk warnx("malloc failed"); 553a25695c3SJim Pirzyk free(listptr); 554a25695c3SJim Pirzyk return (NULL); 555a25695c3SJim Pirzyk } 556a25695c3SJim Pirzyk 557a25695c3SJim Pirzyk *str = 'n'; *(str + 1) = 'o'; 558a25695c3SJim Pirzyk for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 559a25695c3SJim Pirzyk strncpy(strptr, listptr[i], 32); 560a25695c3SJim Pirzyk strptr += strlen(listptr[i]); 561a25695c3SJim Pirzyk *strptr = ','; 562a25695c3SJim Pirzyk free(listptr[i]); 563a25695c3SJim Pirzyk } 564a25695c3SJim Pirzyk *(--strptr) = NULL; 565a25695c3SJim Pirzyk 566a25695c3SJim Pirzyk free(listptr); 567a25695c3SJim Pirzyk return (str); 568a25695c3SJim Pirzyk } 569