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 #include <ufs/ufs/ufsmount.h>
68 /* Maximum widths of various fields. */
79 static void addstat(struct statfs *, struct statfs *);
80 static char *getmntpt(const char *);
81 static int int64width(int64_t);
82 static char *makenetvfslist(void);
83 static void prthuman(const struct statfs *, int64_t);
84 static void prthumanval(int64_t);
85 static intmax_t fsbtoblk(int64_t, uint64_t, u_long);
86 static void prtstat(struct statfs *, struct maxwidths *);
87 static size_t regetmntinfo(struct statfs **, long, const char **);
88 static void update_maxwidths(struct maxwidths *, const struct statfs *);
89 static void usage(void);
94 return (a > b ? a : b);
97 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
98 static struct ufs_args mdev;
101 main(int argc, char *argv[])
104 struct statfs statfsbuf, totalbuf;
105 struct maxwidths maxwidths;
106 struct statfs *mntbuf;
108 char *mntpath, *mntpt;
109 const char **vfslist;
115 memset(&totalbuf, 0, sizeof(totalbuf));
116 totalbuf.f_bsize = DEV_BSIZE;
117 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
119 while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T")) != -1)
128 * POSIX specifically discusses the the behavior of
129 * both -k and -P. It states that the blocksize should
130 * be set to 1024. Thus, if this occurs, simply break
131 * rather than clobbering the old blocksize.
135 setenv("BLOCKSIZE", "512", 1);
142 setenv("BLOCKSIZE", "1g", 1);
156 setenv("BLOCKSIZE", "1024", 1);
161 errx(1, "-l and -t are mutually exclusive.");
162 vfslist = makevfslist(makenetvfslist());
166 setenv("BLOCKSIZE", "1m", 1);
174 errx(1, "-l and -t are mutually exclusive.");
176 errx(1, "only one -t option may be specified");
178 vfslist = makevfslist(optarg);
190 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
191 bzero(&maxwidths, sizeof(maxwidths));
192 for (i = 0; i < mntsize; i++)
193 update_maxwidths(&maxwidths, &mntbuf[i]);
197 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
198 bzero(&maxwidths, sizeof(maxwidths));
199 for (i = 0; i < mntsize; i++) {
201 addstat(&totalbuf, &mntbuf[i]);
202 update_maxwidths(&maxwidths, &mntbuf[i]);
205 update_maxwidths(&maxwidths, &totalbuf);
206 for (i = 0; i < mntsize; i++)
207 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
208 prtstat(&mntbuf[i], &maxwidths);
210 prtstat(&totalbuf, &maxwidths);
214 for (; *argv; argv++) {
215 if (stat(*argv, &stbuf) < 0) {
216 if ((mntpt = getmntpt(*argv)) == 0) {
221 } else if (S_ISCHR(stbuf.st_mode)) {
222 if ((mntpt = getmntpt(*argv)) == 0) {
224 mntpath = strdup("/tmp/df.XXXXXX");
225 if (mntpath == NULL) {
226 warn("strdup failed");
230 mntpt = mkdtemp(mntpath);
232 warn("mkdtemp(\"%s\") failed", mntpath);
237 if (mount(fstype, mntpt, MNT_RDONLY,
244 } else if (statfs(mntpt, &statfsbuf) == 0) {
245 statfsbuf.f_mntonname[0] = '\0';
246 prtstat(&statfsbuf, &maxwidths);
248 addstat(&totalbuf, &statfsbuf);
253 (void)unmount(mntpt, 0);
262 * Statfs does not take a `wait' flag, so we cannot
263 * implement nflag here.
265 if (statfs(mntpt, &statfsbuf) < 0) {
272 * Check to make sure the arguments we've been given are
273 * satisfied. Return an error if we have been asked to
274 * list a mount point that does not match the other args
275 * we've been given (-l, -t, etc.).
277 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
283 bzero(&maxwidths, sizeof(maxwidths));
284 update_maxwidths(&maxwidths, &statfsbuf);
286 prtstat(&statfsbuf, &maxwidths);
288 addstat(&totalbuf, &statfsbuf);
291 prtstat(&totalbuf, &maxwidths);
296 getmntpt(const char *name)
299 struct statfs *mntbuf;
301 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
302 for (i = 0; i < mntsize; i++) {
303 if (!strcmp(mntbuf[i].f_mntfromname, name))
304 return (mntbuf[i].f_mntonname);
310 * Make a pass over the file system info in ``mntbuf'' filtering out
311 * file system types not in vfslist and possibly re-stating to get
312 * current (not cached) info. Returns the new count of valid statfs bufs.
315 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
318 struct statfs *mntbuf;
321 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
324 for (j = 0, i = 0; i < mntsize; i++) {
325 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
328 * XXX statfs(2) can fail for various reasons. It may be
329 * possible that the user does not have access to the
330 * pathname, if this happens, we will fall back on
331 * "stale" filesystem statistics.
333 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
334 if (nflag || error < 0)
337 warnx("%s stats possibly stale",
338 mntbuf[i].f_mntonname);
339 mntbuf[j] = mntbuf[i];
347 prthuman(const struct statfs *sfsp, int64_t used)
350 prthumanval(sfsp->f_blocks * sfsp->f_bsize);
351 prthumanval(used * sfsp->f_bsize);
352 prthumanval(sfsp->f_bavail * sfsp->f_bsize);
356 prthumanval(int64_t bytes)
361 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
362 if (hflag == UNITS_SI)
363 flags |= HN_DIVISOR_1000;
365 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
366 bytes, "", HN_AUTOSCALE, flags);
368 (void)printf(" %6s", buf);
372 * Print an inode count in "human-readable" format.
375 prthumanvalinode(int64_t bytes)
380 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
382 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
383 bytes, "", HN_AUTOSCALE, flags);
385 (void)printf(" %5s", buf);
389 * Convert statfs returned file system size into BLOCKSIZE units.
390 * Attempts to avoid overflow for large file systems.
393 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
396 if (fsbs != 0 && fsbs < bs)
397 return (num / (intmax_t)(bs / fsbs));
399 return (num * (intmax_t)(fsbs / bs));
403 * Print out status about a file system.
406 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
408 static long blocksize;
409 static int headerlen, timesthrough = 0;
410 static const char *header;
411 int64_t used, availblks, inodes;
413 if (++timesthrough == 1) {
414 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
415 mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
418 mwp->total = mwp->used = mwp->avail =
421 header = getbsize(&headerlen, &blocksize);
422 mwp->total = imax(mwp->total, headerlen);
424 mwp->used = imax(mwp->used, (int)strlen("Used"));
425 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
427 (void)printf("%-*s", mwp->mntfrom, "Filesystem");
429 (void)printf(" %-*s", mwp->fstype, "Type");
430 (void)printf(" %-*s %*s %*s Capacity", mwp->total, header,
431 mwp->used, "Used", mwp->avail, "Avail");
433 mwp->iused = imax(hflag ? 0 : mwp->iused,
434 (int)strlen(" iused"));
435 mwp->ifree = imax(hflag ? 0 : mwp->ifree,
436 (int)strlen("ifree"));
437 (void)printf(" %*s %*s %%iused",
438 mwp->iused - 2, "iused", mwp->ifree, "ifree");
440 (void)printf(" Mounted on\n");
442 (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
444 (void)printf(" %-*s", mwp->fstype, sfsp->f_fstypename);
445 used = sfsp->f_blocks - sfsp->f_bfree;
446 availblks = sfsp->f_bavail + used;
448 prthuman(sfsp, used);
450 (void)printf(" %*jd %*jd %*jd",
451 mwp->total, fsbtoblk(sfsp->f_blocks,
452 sfsp->f_bsize, blocksize),
453 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
454 mwp->avail, fsbtoblk(sfsp->f_bavail,
455 sfsp->f_bsize, blocksize));
457 (void)printf(" %5.0f%%",
458 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
460 inodes = sfsp->f_files;
461 used = inodes - sfsp->f_ffree;
464 prthumanvalinode(used);
465 prthumanvalinode(sfsp->f_ffree);
467 (void)printf(" %*jd %*jd", mwp->iused, (intmax_t)used,
468 mwp->ifree, (intmax_t)sfsp->f_ffree);
470 (void)printf(" %4.0f%% ", inodes == 0 ? 100.0 :
471 (double)used / (double)inodes * 100.0);
474 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
475 (void)printf(" %s", sfsp->f_mntonname);
480 addstat(struct statfs *totalfsp, struct statfs *statfsp)
484 bsize = statfsp->f_bsize / totalfsp->f_bsize;
485 totalfsp->f_blocks += statfsp->f_blocks * bsize;
486 totalfsp->f_bfree += statfsp->f_bfree * bsize;
487 totalfsp->f_bavail += statfsp->f_bavail * bsize;
488 totalfsp->f_files += statfsp->f_files;
489 totalfsp->f_ffree += statfsp->f_ffree;
493 * Update the maximum field-width information in `mwp' based on
494 * the file system specified by `sfsp'.
497 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
499 static long blocksize = 0;
503 getbsize(&dummy, &blocksize);
505 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
506 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
507 mwp->total = imax(mwp->total, int64width(
508 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
509 mwp->used = imax(mwp->used,
510 int64width(fsbtoblk((int64_t)sfsp->f_blocks -
511 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
512 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
513 sfsp->f_bsize, blocksize)));
514 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
516 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
519 /* Return the width in characters of the specified value. */
521 int64width(int64_t val)
526 /* Negative or zero values require one extra digit. */
543 (void)fprintf(stderr,
544 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [file | filesystem ...]\n");
551 char *str, *strptr, **listptr;
552 struct xvfsconf *xvfsp, *keep_xvfsp;
554 int cnt, i, maxvfsconf;
556 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
557 warn("sysctl(vfs.conflist)");
560 xvfsp = malloc(buflen);
562 warnx("malloc failed");
566 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
567 warn("sysctl(vfs.conflist)");
571 maxvfsconf = buflen / sizeof(struct xvfsconf);
573 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
574 warnx("malloc failed");
579 for (cnt = 0, i = 0; i < maxvfsconf; i++) {
580 if (xvfsp->vfc_flags & VFCF_NETWORK) {
581 listptr[cnt++] = strdup(xvfsp->vfc_name);
582 if (listptr[cnt-1] == NULL) {
583 warnx("malloc failed");
593 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
595 warnx("malloc failed");
601 *str = 'n'; *(str + 1) = 'o';
602 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
603 strlcpy(strptr, listptr[i], 32);
604 strptr += strlen(listptr[i]);