* Copyright (c) 1980 The Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)df.c 5.16 (Berkeley) %G%";
extern int errno
, optind
;
long width
, maxwidth
, mntsize
, getmntinfo();
struct statfs statfsbuf
, *mntbuf
;
while ((ch
= getopt(argc
, argv
, "ikon")) != EOF
)
"usage: df [-ikn] [file | file_system ...]\n");
mntsize
= getmntinfo(&mntbuf
, MNT_NOWAIT
);
for (i
= 0; i
< mntsize
; i
++) {
width
= strlen(mntbuf
[i
].f_mntfromname
);
mntsize
= getmntinfo(&mntbuf
, (nflag
? MNT_NOWAIT
: MNT_WAIT
));
for (i
= 0; i
< mntsize
; i
++)
prtstat(&mntbuf
[i
], maxwidth
);
if (stat(*argv
, &stbuf
) < 0) {
if ((mntpt
= getmntpt(*argv
)) == 0) {
fprintf(stderr
, "df: %s: %s\n", *argv
,
} else if ((stbuf
.st_mode
& S_IFMT
) == S_IFBLK
) {
if ((mntpt
= getmntpt(*argv
)) == 0)
mntpt
= mktemp("/tmp/df.XXXXXX");
!mount(MOUNT_UFS
, mntpt
, M_RDONLY
, &mdev
) &&
!statfs(mntpt
, &statfsbuf
)) {
statfsbuf
.f_mntonname
[0] = '\0';
prtstat(&statfsbuf
, maxwidth
);
fprintf(stderr
, "df: %s: %s\n",
(void)unmount(mntpt
, MNT_NOFORCE
);
* Statfs does not take a `wait' flag, so we cannot
if (statfs(mntpt
, &statfsbuf
) < 0) {
"df: %s: %s\n", mntpt
, strerror(errno
));
maxwidth
= strlen(statfsbuf
.f_mntfromname
) + 1;
prtstat(&statfsbuf
, maxwidth
);
mntsize
= getmntinfo(&mntbuf
, (nflag
? MNT_NOWAIT
: MNT_WAIT
));
for (i
= 0; i
< mntsize
; i
++) {
if (!strcmp(mntbuf
[i
].f_mntfromname
, name
))
return (mntbuf
[i
].f_mntonname
);
* Print out status about a filesystem.
register struct statfs
*sfsp
;
long used
, availblks
, inodes
;
if (++timesthrough
== 1) {
printf("%-*.*s%s used avail capacity",
maxwidth
, maxwidth
, "Filesystem",
kflag
? " kbytes" : "512-blks");
printf(" iused ifree %%iused");
printf("%-*.*s", maxwidth
, maxwidth
, sfsp
->f_mntfromname
);
used
= sfsp
->f_blocks
- sfsp
->f_bfree
;
availblks
= sfsp
->f_bavail
+ used
;
sfsp
->f_blocks
* sfsp
->f_fsize
/ (kflag
? 1024 : 512),
used
* sfsp
->f_fsize
/ (kflag
? 1024 : 512),
sfsp
->f_bavail
* sfsp
->f_fsize
/ (kflag
? 1024 : 512));
availblks
== 0 ? 100.0 : (double)used
/ (double)availblks
* 100.0);
used
= inodes
- sfsp
->f_ffree
;
printf("%8ld%8ld%6.0f%% ", used
, sfsp
->f_ffree
,
inodes
== 0 ? 100.0 : (double)used
/ (double)inodes
* 100.0);
printf(" %s\n", sfsp
->f_mntonname
);
* This code constitutes the old df code for extracting
* information from filesystem superblocks.
perror(_PATH_FSTAB
), exit(1);
while (fsp
= getfsent()) {
if (strcmp(fsp
->fs_type
, FSTAB_RW
) &&
strcmp(fsp
->fs_type
, FSTAB_RO
) &&
strcmp(fsp
->fs_type
, FSTAB_RQ
))
(void) strcpy(root
, fsp
->fs_spec
);
dfree(fsp
->fs_spec
, 1, maxwidth
);
dfree(*argv
++, 0, maxwidth
);
dfree(file
, infsent
, maxwidth
)
register struct statfs
*sfsp
;
if (stat(file
, &stbuf
) == 0 &&
(stbuf
.st_mode
&S_IFMT
) != S_IFCHR
&&
(stbuf
.st_mode
&S_IFMT
) != S_IFBLK
) {
fprintf(stderr
, "df: %s: screwy fstab entry\n", file
);
while (fsp
= getfsent()) {
if (stat(fsp
->fs_spec
, &stb
) == 0 &&
stb
.st_rdev
== stbuf
.st_dev
) {
fprintf(stderr
, "df: %s: mounted on unknown device\n", file
);
if ((fi
= open(file
, O_RDONLY
)) < 0) {
fprintf(stderr
, "df: %s: %s\n", file
, strerror(errno
));
if (bread((long)SBOFF
, (char *)&sblock
, SBSIZE
) == 0) {
sfsp
->f_type
= MOUNT_UFS
;
sfsp
->f_fsize
= sblock
.fs_fsize
;
sfsp
->f_bsize
= sblock
.fs_bsize
;
sfsp
->f_blocks
= sblock
.fs_dsize
;
sfsp
->f_bfree
= sblock
.fs_cstotal
.cs_nbfree
* sblock
.fs_frag
+
sblock
.fs_cstotal
.cs_nffree
;
sfsp
->f_bavail
= (sblock
.fs_dsize
* (100 - sblock
.fs_minfree
) / 100) -
(sblock
.fs_dsize
- sfsp
->f_bfree
);
sfsp
->f_files
= sblock
.fs_ncg
* sblock
.fs_ipg
;
sfsp
->f_ffree
= sblock
.fs_cstotal
.cs_nifree
;
if ((mntpt
= getmntpt(file
)) == 0)
bcopy((caddr_t
)mntpt
, (caddr_t
)&sfsp
->f_mntonname
[0], MNAMELEN
);
bcopy((caddr_t
)file
, (caddr_t
)&sfsp
->f_mntfromname
[0], MNAMELEN
);
(void) lseek(fi
, off
, SEEK_SET
);
if ((n
=read(fi
, buf
, cnt
)) != cnt
) {
/* probably a dismounted disk if errno == EIO */
printf("\nread error off = %ld\n", off
);
printf("count = %d; errno = %d\n", n
, errno
);