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. */
78 static void addstat(struct statfs *, struct statfs *);
79 static char *getmntpt(const char *);
80 static int int64width(int64_t);
81 static char *makenetvfslist(void);
82 static void prthuman(const struct statfs *, int64_t);
83 static void prthumanval(int64_t);
84 static intmax_t fsbtoblk(int64_t, uint64_t, u_long);
85 static void prtstat(struct statfs *, struct maxwidths *);
86 static size_t regetmntinfo(struct statfs **, long, const char **);
87 static void update_maxwidths(struct maxwidths *, const struct statfs *);
88 static void usage(void);
93 return (a > b ? a : b);
96 static int aflag = 0, cflag, hflag, iflag, nflag;
97 static struct ufs_args mdev;
100 main(int argc, char *argv[])
103 struct statfs statfsbuf, totalbuf;
104 struct maxwidths maxwidths;
105 struct statfs *mntbuf;
107 char *mntpath, *mntpt;
108 const char **vfslist;
114 memset(&totalbuf, 0, sizeof(totalbuf));
115 totalbuf.f_bsize = DEV_BSIZE;
116 strncpy(totalbuf.f_mntfromname, "total", MNAMELEN);
118 while ((ch = getopt(argc, argv, "abcgHhiklmnPt:")) != -1)
126 putenv("BLOCKSIZE=512");
133 putenv("BLOCKSIZE=1g");
146 putenv("BLOCKSIZE=1k");
151 errx(1, "-l and -t are mutually exclusive.");
152 vfslist = makevfslist(makenetvfslist());
155 putenv("BLOCKSIZE=1m");
163 errx(1, "only one -t option may be specified");
165 vfslist = makevfslist(optarg);
174 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
175 bzero(&maxwidths, sizeof(maxwidths));
176 for (i = 0; i < mntsize; i++)
177 update_maxwidths(&maxwidths, &mntbuf[i]);
181 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
182 bzero(&maxwidths, sizeof(maxwidths));
183 for (i = 0; i < mntsize; i++) {
185 addstat(&totalbuf, &mntbuf[i]);
186 update_maxwidths(&maxwidths, &mntbuf[i]);
189 update_maxwidths(&maxwidths, &totalbuf);
190 for (i = 0; i < mntsize; i++)
191 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
192 prtstat(&mntbuf[i], &maxwidths);
194 prtstat(&totalbuf, &maxwidths);
198 for (; *argv; argv++) {
199 if (stat(*argv, &stbuf) < 0) {
200 if ((mntpt = getmntpt(*argv)) == 0) {
205 } else if (S_ISCHR(stbuf.st_mode)) {
206 if ((mntpt = getmntpt(*argv)) == 0) {
208 mntpath = strdup("/tmp/df.XXXXXX");
209 if (mntpath == NULL) {
210 warn("strdup failed");
214 mntpt = mkdtemp(mntpath);
216 warn("mkdtemp(\"%s\") failed", mntpath);
221 if (mount(fstype, mntpt, MNT_RDONLY,
228 } else if (statfs(mntpt, &statfsbuf) == 0) {
229 statfsbuf.f_mntonname[0] = '\0';
230 prtstat(&statfsbuf, &maxwidths);
232 addstat(&totalbuf, &statfsbuf);
237 (void)unmount(mntpt, 0);
246 * Statfs does not take a `wait' flag, so we cannot
247 * implement nflag here.
249 if (statfs(mntpt, &statfsbuf) < 0) {
256 * Check to make sure the arguments we've been given are
257 * satisfied. Return an error if we have been asked to
258 * list a mount point that does not match the other args
259 * we've been given (-l, -t, etc.).
261 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
267 bzero(&maxwidths, sizeof(maxwidths));
268 update_maxwidths(&maxwidths, &statfsbuf);
270 prtstat(&statfsbuf, &maxwidths);
272 addstat(&totalbuf, &statfsbuf);
275 prtstat(&totalbuf, &maxwidths);
280 getmntpt(const char *name)
283 struct statfs *mntbuf;
285 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
286 for (i = 0; i < mntsize; i++) {
287 if (!strcmp(mntbuf[i].f_mntfromname, name))
288 return (mntbuf[i].f_mntonname);
294 * Make a pass over the file system info in ``mntbuf'' filtering out
295 * file system types not in vfslist and possibly re-stating to get
296 * current (not cached) info. Returns the new count of valid statfs bufs.
299 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
302 struct statfs *mntbuf;
305 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
308 for (j = 0, i = 0; i < mntsize; i++) {
309 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
312 * XXX statfs(2) can fail for various reasons. It may be
313 * possible that the user does not have access to the
314 * pathname, if this happens, we will fall back on
315 * "stale" filesystem statistics.
317 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
318 if (nflag || error < 0)
321 warnx("%s stats possibly stale",
322 mntbuf[i].f_mntonname);
323 mntbuf[j] = mntbuf[i];
331 prthuman(const struct statfs *sfsp, int64_t used)
334 prthumanval(sfsp->f_blocks * sfsp->f_bsize);
335 prthumanval(used * sfsp->f_bsize);
336 prthumanval(sfsp->f_bavail * sfsp->f_bsize);
340 prthumanval(int64_t bytes)
345 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
346 if (hflag == UNITS_SI)
347 flags |= HN_DIVISOR_1000;
349 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
350 bytes, "", HN_AUTOSCALE, flags);
352 (void)printf(" %6s", buf);
356 * Convert statfs returned file system size into BLOCKSIZE units.
357 * Attempts to avoid overflow for large file systems.
360 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
363 if (fsbs != 0 && fsbs < bs)
364 return (num / (intmax_t)(bs / fsbs));
366 return (num * (intmax_t)(fsbs / bs));
370 * Print out status about a file system.
373 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
375 static u_long blocksize;
376 static int headerlen, timesthrough = 0;
377 static const char *header;
378 int64_t used, availblks, inodes;
380 if (++timesthrough == 1) {
381 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
384 mwp->total = mwp->used = mwp->avail =
387 header = getbsize(&headerlen, &blocksize);
388 mwp->total = imax(mwp->total, headerlen);
390 mwp->used = imax(mwp->used, (int)strlen("Used"));
391 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
393 (void)printf("%-*s %-*s %*s %*s Capacity",
394 mwp->mntfrom, "Filesystem", mwp->total, header,
395 mwp->used, "Used", mwp->avail, "Avail");
397 mwp->iused = imax(mwp->iused, (int)strlen(" iused"));
398 mwp->ifree = imax(mwp->ifree, (int)strlen("ifree"));
399 (void)printf(" %*s %*s %%iused",
400 mwp->iused - 2, "iused", mwp->ifree, "ifree");
402 (void)printf(" Mounted on\n");
404 (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
405 used = sfsp->f_blocks - sfsp->f_bfree;
406 availblks = sfsp->f_bavail + used;
408 prthuman(sfsp, used);
410 (void)printf(" %*jd %*jd %*jd",
411 mwp->total, fsbtoblk(sfsp->f_blocks,
412 sfsp->f_bsize, blocksize),
413 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
414 mwp->avail, fsbtoblk(sfsp->f_bavail,
415 sfsp->f_bsize, blocksize));
417 (void)printf(" %5.0f%%",
418 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
420 inodes = sfsp->f_files;
421 used = inodes - sfsp->f_ffree;
422 (void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used,
423 mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 :
424 (double)used / (double)inodes * 100.0);
427 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
428 (void)printf(" %s", sfsp->f_mntonname);
433 addstat(struct statfs *totalfsp, struct statfs *statfsp)
437 bsize = statfsp->f_bsize / totalfsp->f_bsize;
438 totalfsp->f_blocks += statfsp->f_blocks * bsize;
439 totalfsp->f_bfree += statfsp->f_bfree * bsize;
440 totalfsp->f_bavail += statfsp->f_bavail * bsize;
441 totalfsp->f_files += statfsp->f_files;
442 totalfsp->f_ffree += statfsp->f_ffree;
446 * Update the maximum field-width information in `mwp' based on
447 * the file system specified by `sfsp'.
450 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
452 static u_long blocksize = 0;
456 getbsize(&dummy, &blocksize);
458 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
459 mwp->total = imax(mwp->total, int64width(
460 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
461 mwp->used = imax(mwp->used,
462 int64width(fsbtoblk((int64_t)sfsp->f_blocks -
463 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
464 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
465 sfsp->f_bsize, blocksize)));
466 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
468 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
471 /* Return the width in characters of the specified value. */
473 int64width(int64_t val)
478 /* Negative or zero values require one extra digit. */
495 (void)fprintf(stderr,
496 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...]\n");
503 char *str, *strptr, **listptr;
504 struct xvfsconf *xvfsp, *keep_xvfsp;
506 int cnt, i, maxvfsconf;
508 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
509 warn("sysctl(vfs.conflist)");
512 xvfsp = malloc(buflen);
514 warnx("malloc failed");
518 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
519 warn("sysctl(vfs.conflist)");
523 maxvfsconf = buflen / sizeof(struct xvfsconf);
525 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
526 warnx("malloc failed");
531 for (cnt = 0, i = 0; i < maxvfsconf; i++) {
532 if (xvfsp->vfc_flags & VFCF_NETWORK) {
533 listptr[cnt++] = strdup(xvfsp->vfc_name);
534 if (listptr[cnt-1] == NULL) {
535 warnx("malloc failed");
545 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
547 warnx("malloc failed");
553 *str = 'n'; *(str + 1) = 'o';
554 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
555 strncpy(strptr, listptr[i], 32);
556 strptr += strlen(listptr[i]);