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 * 3. 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
34 * @(#) Copyright (c) 1980, 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
35 * @(#)df.c 8.9 (Berkeley) 5/8/95
36 * $FreeBSD: src/bin/df/df.c,v 1.23.2.9 2002/07/01 00:14:24 iedowse Exp $
39 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/sysctl.h>
43 #include <sys/statvfs.h>
45 #include <vfs/ufs/dinode.h>
46 #include <vfs/ufs/ufsmount.h>
63 #define HN_FRACTIONAL HN_DECIMAL
66 /* Maximum widths of various fields. */
78 char **makevfslist(char *);
79 int checkvfsname(const char *, char **);
81 static char *getmntpt(char *);
82 static int quadwidth(int64_t);
83 static char *makenetvfslist(void);
84 static void prthuman(struct statvfs
*, int64_t);
85 static void prthumanval(int64_t);
86 static void prtstat(struct statfs
*, struct statvfs
*, struct maxwidths
*);
87 static long regetmntinfo(struct statfs
**, struct statvfs
**, long, char **);
88 static void update_maxwidths(struct maxwidths
*, struct statfs
*, struct statvfs
*);
89 static void usage(void);
91 static int aflag
= 0, hflag
, iflag
, nflag
, Tflag
;
92 static struct ufs_args mdev
;
97 return (a
> b
? a
: b
);
101 main(int argc
, char **argv
)
104 struct statfs statfsbuf
, *mntbuf
;
105 struct statvfs statvfsbuf
, *mntvbuf
;
106 struct maxwidths maxwidths
;
108 char *mntpath
, *mntpt
, **vfslist
;
115 while ((ch
= getopt(argc
, argv
, "abgHhiklmnPt:T")) != -1)
123 if (setenv("BLOCKSIZE", "512", 1) != 0)
124 warn("setenv: cannot set BLOCKSIZE=512");
128 if (setenv("BLOCKSIZE", "1g", 1) != 0)
129 warn("setenv: cannot set BLOCKSIZE=1g");
142 if (setenv("BLOCKSIZE", "1k", 1) != 0)
143 warn("setenv: cannot set BLOCKSIZE=1k");
148 errx(1, "-l and -t are mutually exclusive.");
149 vfslist
= makevfslist(makenetvfslist());
152 if (setenv("BLOCKSIZE", "1m", 1) != 0)
153 warn("setenv: cannot set BLOCKSIZE=1m");
161 errx(1, "only one -t option may be specified");
163 vfslist
= makevfslist(optarg
);
175 mntsize
= getmntvinfo(&mntbuf
, &mntvbuf
, MNT_NOWAIT
);
176 bzero(&maxwidths
, sizeof(maxwidths
));
177 for (i
= 0; i
< mntsize
; i
++)
178 update_maxwidths(&maxwidths
, &mntbuf
[i
], &mntvbuf
[i
]);
182 mntsize
= regetmntinfo(&mntbuf
, &mntvbuf
, mntsize
, vfslist
);
183 bzero(&maxwidths
, sizeof(maxwidths
));
184 for (i
= 0; i
< mntsize
; i
++)
185 update_maxwidths(&maxwidths
, &mntbuf
[i
], &mntvbuf
[i
]);
186 for (i
= 0; i
< mntsize
; i
++) {
187 if (aflag
|| (mntbuf
[i
].f_flags
& MNT_IGNORE
) == 0)
188 prtstat(&mntbuf
[i
], &mntvbuf
[i
], &maxwidths
);
193 for (; *argv
; argv
++) {
194 if (stat(*argv
, &stbuf
) < 0) {
195 if ((mntpt
= getmntpt(*argv
)) == NULL
) {
200 } else if (S_ISCHR(stbuf
.st_mode
)) {
201 if ((mntpt
= getmntpt(*argv
)) == NULL
) {
203 mntpath
= strdup("/tmp/df.XXXXXX");
204 if (mntpath
== NULL
) {
205 warn("strdup failed");
209 mntpt
= mkdtemp(mntpath
);
211 warn("mkdtemp(\"%s\") failed", mntpath
);
216 if (mount(fstype
, mntpt
, MNT_RDONLY
,
223 } else if (statfs(mntpt
, &statfsbuf
) == 0 &&
224 statvfs(mntpt
, &statvfsbuf
) == 0) {
225 statfsbuf
.f_mntonname
[0] = '\0';
226 prtstat(&statfsbuf
, &statvfsbuf
, &maxwidths
);
239 * Statfs does not take a `wait' flag, so we cannot
240 * implement nflag here.
242 if (statfs(mntpt
, &statfsbuf
) < 0) {
247 if (statvfs(mntpt
, &statvfsbuf
) < 0) {
253 * Check to make sure the arguments we've been given are
254 * satisfied. Return an error if we have been asked to
255 * list a mount point that does not match the other args
256 * we've been given (-l, -t, etc.).
258 if (checkvfsname(statfsbuf
.f_fstypename
, vfslist
)) {
264 bzero(&maxwidths
, sizeof(maxwidths
));
265 update_maxwidths(&maxwidths
, &statfsbuf
, &statvfsbuf
);
267 prtstat(&statfsbuf
, &statvfsbuf
, &maxwidths
);
276 struct statfs
*mntbuf
;
277 struct statvfs
*mntvbuf
;
279 mntsize
= getmntvinfo(&mntbuf
, &mntvbuf
, MNT_NOWAIT
);
280 for (i
= 0; i
< mntsize
; i
++) {
281 if (!strcmp(mntbuf
[i
].f_mntfromname
, name
))
282 return (mntbuf
[i
].f_mntonname
);
288 * Make a pass over the filesystem info in ``mntbuf'' filtering out
289 * filesystem types not in vfslist and possibly re-stating to get
290 * current (not cached) info. Returns the new count of valid statfs bufs.
293 regetmntinfo(struct statfs
**mntbufp
, struct statvfs
**mntvbufp
, long mntsize
, char **vfslist
)
296 struct statfs
*mntbuf
;
297 struct statvfs
*mntvbuf
;
300 return (nflag
? mntsize
: getmntvinfo(mntbufp
, mntvbufp
, MNT_WAIT
));
304 for (j
= 0, i
= 0; i
< mntsize
; i
++) {
305 if (checkvfsname(mntbuf
[i
].f_fstypename
, vfslist
))
308 statfs(mntbuf
[i
].f_mntonname
,&mntbuf
[j
]);
309 statvfs(mntbuf
[i
].f_mntonname
,&mntvbuf
[j
]);
311 mntbuf
[j
] = mntbuf
[i
];
312 mntvbuf
[j
] = mntvbuf
[i
];
320 prthuman(struct statvfs
*vsfsp
, int64_t used
)
322 prthumanval(vsfsp
->f_blocks
* vsfsp
->f_bsize
);
323 prthumanval(used
* vsfsp
->f_bsize
);
324 prthumanval(vsfsp
->f_bavail
* vsfsp
->f_bsize
);
328 prthumanval(int64_t bytes
)
333 flags
= HN_B
| HN_NOSPACE
| HN_FRACTIONAL
;
334 if (hflag
== UNITS_SI
)
335 flags
|= HN_DIVISOR_1000
;
337 humanize_number(buf
, sizeof(buf
) - (bytes
< 0 ? 0 : 1),
338 bytes
, "", HN_AUTOSCALE
, flags
);
344 * Print an inode count in "human-readable" format.
347 prthumanvalinode(int64_t bytes
)
352 flags
= HN_NOSPACE
| HN_FRACTIONAL
| HN_DIVISOR_1000
;
354 humanize_number(buf
, sizeof(buf
) - (bytes
< 0 ? 0 : 1),
355 bytes
, "", HN_AUTOSCALE
, flags
);
361 * Convert statfs returned filesystem size into BLOCKSIZE units.
362 * Attempts to avoid overflow for large filesystems.
365 fsbtoblk(int64_t num
, uint64_t bsize
, u_long reqbsize
)
367 if (bsize
!= 0 && bsize
< reqbsize
)
368 return (num
/ (intmax_t)(reqbsize
/ bsize
));
370 return (num
* (intmax_t)(bsize
/ reqbsize
));
374 * Print out status about a filesystem.
377 prtstat(struct statfs
*sfsp
, struct statvfs
*vsfsp
, struct maxwidths
*mwp
)
379 static long blocksize
;
380 static int headerlen
, timesthrough
;
381 static const char *header
;
382 int64_t used
, availblks
, inodes
;
384 if (++timesthrough
== 1) {
385 mwp
->mntfrom
= imax(mwp
->mntfrom
, strlen("Filesystem"));
386 mwp
->fstype
= imax(mwp
->fstype
, strlen("Type"));
389 mwp
->total
= mwp
->used
= mwp
->avail
= strlen(header
);
391 header
= getbsize(&headerlen
, &blocksize
);
392 mwp
->total
= imax(mwp
->total
, headerlen
);
394 mwp
->used
= imax(mwp
->used
, strlen("Used"));
395 mwp
->avail
= imax(mwp
->avail
, strlen("Avail"));
397 printf("%-*s", mwp
->mntfrom
, "Filesystem");
399 printf(" %-*s", mwp
->fstype
, "Type");
400 printf(" %-*s %*s %*s Capacity", mwp
->total
, header
, mwp
->used
,
401 "Used", mwp
->avail
, "Avail");
403 mwp
->iused
= imax(hflag
? 0 : mwp
->iused
,
404 (int)strlen(" iused"));
405 mwp
->ifree
= imax(hflag
? 0 : mwp
->ifree
,
406 (int)strlen("ifree"));
407 printf(" %*s %*s %%iused", mwp
->iused
- 2,
408 "iused", mwp
->ifree
, "ifree");
410 printf(" Mounted on\n");
412 printf("%-*s", mwp
->mntfrom
, sfsp
->f_mntfromname
);
414 printf(" %-*s", mwp
->fstype
, sfsp
->f_fstypename
);
415 used
= vsfsp
->f_blocks
- vsfsp
->f_bfree
;
416 availblks
= vsfsp
->f_bavail
+ used
;
418 prthuman(vsfsp
, used
);
420 printf(" %*jd %*jd %*jd", mwp
->total
,
421 fsbtoblk(vsfsp
->f_blocks
, vsfsp
->f_bsize
, blocksize
),
422 mwp
->used
, fsbtoblk(used
, vsfsp
->f_bsize
, blocksize
),
423 mwp
->avail
, fsbtoblk(vsfsp
->f_bavail
, vsfsp
->f_bsize
,
427 availblks
== 0 ? 100.0 : (double)used
/ (double)availblks
* 100.0);
429 inodes
= vsfsp
->f_files
;
430 used
= inodes
- vsfsp
->f_ffree
;
433 prthumanvalinode(used
);
434 prthumanvalinode(vsfsp
->f_ffree
);
436 printf(" %*jd %*jd", mwp
->iused
, (intmax_t)used
,
437 mwp
->ifree
, (intmax_t)vsfsp
->f_ffree
);
439 printf(" %4.0f%% ", inodes
== 0 ? 100.0 :
440 (double)used
/ (double)inodes
* 100.0);
443 printf(" %s\n", sfsp
->f_mntonname
);
447 * Update the maximum field-width information in `mwp' based on
448 * the filesystem specified by `sfsp'.
451 update_maxwidths(struct maxwidths
*mwp
, struct statfs
*sfsp
, struct statvfs
*vsfsp
)
453 static long blocksize
;
457 getbsize(&dummy
, &blocksize
);
459 mwp
->mntfrom
= imax(mwp
->mntfrom
, strlen(sfsp
->f_mntfromname
));
460 mwp
->fstype
= imax(mwp
->fstype
, strlen(sfsp
->f_fstypename
));
461 mwp
->total
= imax(mwp
->total
, quadwidth(fsbtoblk(vsfsp
->f_blocks
,
462 vsfsp
->f_bsize
, blocksize
)));
463 mwp
->used
= imax(mwp
->used
, quadwidth(fsbtoblk(vsfsp
->f_blocks
-
464 vsfsp
->f_bfree
, vsfsp
->f_bsize
, blocksize
)));
465 mwp
->avail
= imax(mwp
->avail
, quadwidth(fsbtoblk(vsfsp
->f_bavail
,
466 vsfsp
->f_bsize
, blocksize
)));
467 mwp
->iused
= imax(mwp
->iused
, quadwidth(vsfsp
->f_files
-
469 mwp
->ifree
= imax(mwp
->ifree
, quadwidth(vsfsp
->f_ffree
));
472 /* Return the width in characters of the specified long. */
474 quadwidth(int64_t val
)
479 /* Negative or zero values require one extra digit. */
496 "usage: df [-b | -H | -h | -k | -m | -P] [-ailnT] [-t type] [file | filesystem ...]\n");
503 char *str
, *strptr
, **listptr
;
504 int mib
[3], maxvfsconf
, cnt
=0, i
;
506 struct ovfsconf
*ptr
;
508 mib
[0] = CTL_VFS
; mib
[1] = VFS_GENERIC
; mib
[2] = VFS_MAXTYPENUM
;
509 miblen
=sizeof(maxvfsconf
);
510 if (sysctl(mib
, (unsigned int)(NELEM(mib
)),
511 &maxvfsconf
, &miblen
, NULL
, 0)) {
512 warnx("sysctl failed");
516 if ((listptr
= malloc(sizeof(char*) * maxvfsconf
)) == NULL
) {
517 warnx("malloc failed");
521 for (ptr
= getvfsent(); ptr
; ptr
= getvfsent())
522 if (ptr
->vfc_flags
& VFCF_NETWORK
) {
523 listptr
[cnt
++] = strdup(ptr
->vfc_name
);
524 if (listptr
[cnt
-1] == NULL
) {
525 warnx("malloc failed");
532 (str
= malloc(sizeof(char) * (32 * cnt
+ cnt
+ 2))) == NULL
) {
534 warnx("malloc failed");
539 *str
= 'n'; *(str
+ 1) = 'o';
540 for (i
= 0, strptr
= str
+ 2; i
< cnt
; i
++, strptr
++) {
541 strncpy(strptr
, listptr
[i
], 32);
542 strptr
+= strlen(listptr
[i
]);