2 * Copyright (c) 1980, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
51 #include <sys/mount.h>
52 #include <sys/sysctl.h>
53 #ifdef MOUNT_CHAR_DEVS
54 #include <ufs/ufs/ufsmount.h>
59 #ifdef MOUNT_CHAR_DEVS
74 /* Maximum widths of various fields. */
85 static void addstat(struct statfs *, struct statfs *);
86 static char *getmntpt(const char *);
87 static int int64width(int64_t);
88 static char *makenetvfslist(void);
89 static void prthuman(const struct statfs *, int64_t);
90 static void prthumanval(int64_t);
91 static intmax_t fsbtoblk(int64_t, uint64_t, u_long);
92 static void prtstat(struct statfs *, struct maxwidths *);
93 static size_t regetmntinfo(struct statfs **, long, const char **);
94 static void update_maxwidths(struct maxwidths *, const struct statfs *);
95 static void usage(void);
100 return (a > b ? a : b);
103 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
104 static int thousands;
105 #ifdef MOUNT_CHAR_DEVS
106 static struct ufs_args mdev;
110 main(int argc, char *argv[])
113 struct statfs statfsbuf, totalbuf;
114 struct maxwidths maxwidths;
115 struct statfs *mntbuf;
116 #ifdef MOUNT_CHAR_DEVS
117 struct iovec *iov = NULL;
120 #ifdef MOUNT_CHAR_DEVS
122 char errmsg[255] = {0};
125 const char **vfslist;
128 #ifdef MOUNT_CHAR_DEVS
133 (void)setlocale(LC_ALL, "");
134 memset(&maxwidths, 0, sizeof(maxwidths));
135 memset(&totalbuf, 0, sizeof(totalbuf));
136 totalbuf.f_bsize = DEV_BSIZE;
137 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
139 while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T,")) != -1)
148 * POSIX specifically discusses the behavior of
149 * both -k and -P. It states that the blocksize should
150 * be set to 1024. Thus, if this occurs, simply break
151 * rather than clobbering the old blocksize.
155 setenv("BLOCKSIZE", "512", 1);
162 setenv("BLOCKSIZE", "1g", 1);
176 setenv("BLOCKSIZE", "1024", 1);
181 errx(1, "-l and -t are mutually exclusive.");
182 vfslist = makevfslist(makenetvfslist());
186 setenv("BLOCKSIZE", "1m", 1);
194 errx(1, "-l and -t are mutually exclusive.");
196 errx(1, "only one -t option may be specified");
198 vfslist = makevfslist(optarg);
215 /* everything (modulo -t) */
216 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
217 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
219 /* just the filesystems specified on the command line */
220 mntbuf = malloc(argc * sizeof(*mntbuf));
224 /* continued in for loop below */
227 /* iterate through specified filesystems */
228 for (; *argv; argv++) {
229 if (stat(*argv, &stbuf) < 0) {
230 if ((mntpt = getmntpt(*argv)) == NULL) {
235 #ifdef MOUNT_CHAR_DEVS
236 } else if (S_ISCHR(stbuf.st_mode)) {
237 if ((mntpt = getmntpt(*argv)) == NULL) {
239 mntpath = strdup("/tmp/df.XXXXXX");
240 if (mntpath == NULL) {
241 warn("strdup failed");
245 mntpt = mkdtemp(mntpath);
247 warn("mkdtemp(\"%s\") failed", mntpath);
253 free_iovec(&iov, &iovlen);
254 build_iovec_argf(&iov, &iovlen, "fstype", "%s",
256 build_iovec_argf(&iov, &iovlen, "fspath", "%s",
258 build_iovec_argf(&iov, &iovlen, "from", "%s",
260 build_iovec(&iov, &iovlen, "errmsg", errmsg,
262 if (nmount(iov, iovlen,
263 MNT_RDONLY|MNT_NOEXEC) < 0) {
265 warn("%s: %s", *argv,
273 } else if (statfs(mntpt, &statfsbuf) == 0) {
274 statfsbuf.f_mntonname[0] = '\0';
275 prtstat(&statfsbuf, &maxwidths);
277 addstat(&totalbuf, &statfsbuf);
282 (void)unmount(mntpt, 0);
292 * Statfs does not take a `wait' flag, so we cannot
293 * implement nflag here.
295 if (statfs(mntpt, &statfsbuf) < 0) {
302 * Check to make sure the arguments we've been given are
303 * satisfied. Return an error if we have been asked to
304 * list a mount point that does not match the other args
305 * we've been given (-l, -t, etc.).
307 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
312 /* the user asked for it, so ignore the ignore flag */
313 statfsbuf.f_flags &= ~MNT_IGNORE;
316 mntbuf[mntsize++] = statfsbuf;
319 memset(&maxwidths, 0, sizeof(maxwidths));
320 for (i = 0; i < mntsize; i++) {
321 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
322 update_maxwidths(&maxwidths, &mntbuf[i]);
324 addstat(&totalbuf, &mntbuf[i]);
327 for (i = 0; i < mntsize; i++)
328 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
329 prtstat(&mntbuf[i], &maxwidths);
331 prtstat(&totalbuf, &maxwidths);
336 getmntpt(const char *name)
339 struct statfs *mntbuf;
341 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
342 for (i = 0; i < mntsize; i++) {
343 if (!strcmp(mntbuf[i].f_mntfromname, name))
344 return (mntbuf[i].f_mntonname);
350 * Make a pass over the file system info in ``mntbuf'' filtering out
351 * file system types not in vfslist and possibly re-stating to get
352 * current (not cached) info. Returns the new count of valid statfs bufs.
355 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
358 struct statfs *mntbuf;
361 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
364 for (j = 0, i = 0; i < mntsize; i++) {
365 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
368 * XXX statfs(2) can fail for various reasons. It may be
369 * possible that the user does not have access to the
370 * pathname, if this happens, we will fall back on
371 * "stale" filesystem statistics.
373 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
374 if (nflag || error < 0)
377 warnx("%s stats possibly stale",
378 mntbuf[i].f_mntonname);
379 mntbuf[j] = mntbuf[i];
387 prthuman(const struct statfs *sfsp, int64_t used)
390 prthumanval(sfsp->f_blocks * sfsp->f_bsize);
391 prthumanval(used * sfsp->f_bsize);
392 prthumanval(sfsp->f_bavail * sfsp->f_bsize);
396 prthumanval(int64_t bytes)
401 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
402 if (hflag == UNITS_SI)
403 flags |= HN_DIVISOR_1000;
405 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
406 bytes, "", HN_AUTOSCALE, flags);
408 (void)printf(" %6s", buf);
412 * Print an inode count in "human-readable" format.
415 prthumanvalinode(int64_t bytes)
420 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
422 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
423 bytes, "", HN_AUTOSCALE, flags);
425 (void)printf(" %5s", buf);
429 * Convert statfs returned file system size into BLOCKSIZE units.
432 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
434 return (num * (intmax_t) fsbs / (int64_t) bs);
438 * Print out status about a file system.
441 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
443 static long blocksize;
444 static int headerlen, timesthrough = 0;
445 static const char *header;
446 int64_t used, availblks, inodes;
449 if (++timesthrough == 1) {
450 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
451 mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
452 if (thousands) { /* make space for commas */
453 mwp->total += (mwp->total - 1) / 3;
454 mwp->used += (mwp->used - 1) / 3;
455 mwp->avail += (mwp->avail - 1) / 3;
456 mwp->iused += (mwp->iused - 1) / 3;
457 mwp->ifree += (mwp->ifree - 1) / 3;
461 mwp->total = mwp->used = mwp->avail =
464 header = getbsize(&headerlen, &blocksize);
465 mwp->total = imax(mwp->total, headerlen);
467 mwp->used = imax(mwp->used, (int)strlen("Used"));
468 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
470 (void)printf("%-*s", mwp->mntfrom, "Filesystem");
472 (void)printf(" %-*s", mwp->fstype, "Type");
473 (void)printf(" %*s %*s %*s Capacity", mwp->total, header,
474 mwp->used, "Used", mwp->avail, "Avail");
476 mwp->iused = imax(hflag ? 0 : mwp->iused,
477 (int)strlen(" iused"));
478 mwp->ifree = imax(hflag ? 0 : mwp->ifree,
479 (int)strlen("ifree"));
480 (void)printf(" %*s %*s %%iused",
481 mwp->iused - 2, "iused", mwp->ifree, "ifree");
483 (void)printf(" Mounted on\n");
485 /* Check for 0 block size. Can this happen? */
486 if (sfsp->f_bsize == 0) {
487 warnx ("File system %s does not have a block size, assuming 512.",
491 (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
493 (void)printf(" %-*s", mwp->fstype, sfsp->f_fstypename);
494 used = sfsp->f_blocks - sfsp->f_bfree;
495 availblks = sfsp->f_bavail + used;
497 prthuman(sfsp, used);
500 format = " %*j'd %*j'd %*j'd";
502 format = " %*jd %*jd %*jd";
504 mwp->total, fsbtoblk(sfsp->f_blocks,
505 sfsp->f_bsize, blocksize),
506 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
507 mwp->avail, fsbtoblk(sfsp->f_bavail,
508 sfsp->f_bsize, blocksize));
510 (void)printf(" %5.0f%%",
511 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
513 inodes = sfsp->f_files;
514 used = inodes - sfsp->f_ffree;
517 prthumanvalinode(used);
518 prthumanvalinode(sfsp->f_ffree);
521 format = " %*j'd %*j'd";
523 format = " %*jd %*jd";
524 (void)printf(format, mwp->iused, (intmax_t)used,
525 mwp->ifree, (intmax_t)sfsp->f_ffree);
527 (void)printf(" %4.0f%% ", inodes == 0 ? 100.0 :
528 (double)used / (double)inodes * 100.0);
531 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
532 (void)printf(" %s", sfsp->f_mntonname);
537 addstat(struct statfs *totalfsp, struct statfs *statfsp)
541 bsize = statfsp->f_bsize / totalfsp->f_bsize;
542 totalfsp->f_blocks += statfsp->f_blocks * bsize;
543 totalfsp->f_bfree += statfsp->f_bfree * bsize;
544 totalfsp->f_bavail += statfsp->f_bavail * bsize;
545 totalfsp->f_files += statfsp->f_files;
546 totalfsp->f_ffree += statfsp->f_ffree;
550 * Update the maximum field-width information in `mwp' based on
551 * the file system specified by `sfsp'.
554 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
556 static long blocksize = 0;
560 getbsize(&dummy, &blocksize);
562 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
563 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
564 mwp->total = imax(mwp->total, int64width(
565 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
566 mwp->used = imax(mwp->used,
567 int64width(fsbtoblk((int64_t)sfsp->f_blocks -
568 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
569 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
570 sfsp->f_bsize, blocksize)));
571 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
573 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
576 /* Return the width in characters of the specified value. */
578 int64width(int64_t val)
583 /* Negative or zero values require one extra digit. */
600 (void)fprintf(stderr,
601 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
602 " [file | filesystem ...]\n");
609 char *str, *strptr, **listptr;
610 struct xvfsconf *xvfsp, *keep_xvfsp;
612 int cnt, i, maxvfsconf;
614 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
615 warn("sysctl(vfs.conflist)");
618 xvfsp = malloc(buflen);
620 warnx("malloc failed");
624 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
625 warn("sysctl(vfs.conflist)");
629 maxvfsconf = buflen / sizeof(struct xvfsconf);
631 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
632 warnx("malloc failed");
637 for (cnt = 0, i = 0; i < maxvfsconf; i++) {
638 if (xvfsp->vfc_flags & VFCF_NETWORK) {
639 listptr[cnt++] = strdup(xvfsp->vfc_name);
640 if (listptr[cnt-1] == NULL) {
641 warnx("malloc failed");
651 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
653 warnx("malloc failed");
659 *str = 'n'; *(str + 1) = 'o';
660 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
661 strlcpy(strptr, listptr[i], 32);
662 strptr += strlen(listptr[i]);