7 const char df_usage[] = "df [filesystem ...]\n"
9 "\tPrint the filesystem space used and space available.\n";
13 df(const char * device, const char * mountPoint)
17 long blocks_percent_used;
19 if ( statfs(mountPoint, &s) != 0 ) {
24 if ( s.f_blocks > 0 ) {
25 blocks_used = s.f_blocks - s.f_bfree;
26 blocks_percent_used = (long)
27 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
31 "%-20s %7ld %7ld %7ld %5ld%% %s\n"
34 ,s.f_blocks - s.f_bfree
41 "%-20s %7.0f %7.0f %7.0f %5ld%% %s\n"
43 ,s.f_blocks * (s.f_bsize / 1024.0)
44 ,(s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)
45 ,s.f_bavail * (s.f_bsize / 1024.0)
55 df_main(int argc, char * * argv)
57 static const char header[] =
58 "Filesystem 1024-blocks Used Available Capacity Mounted on\n";
62 struct mntent * mountEntry;
66 if ( (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 )
68 fprintf(stderr, "%s: can't find mount point.\n" ,argv[1]);
71 status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
81 struct mntent * mountEntry;
83 if ( (mountTable = setmntent("/proc/mounts", "r")) == 0) {
84 perror("/proc/mounts");
88 while ( (mountEntry = getmntent(mountTable)) != 0 ) {
90 mountEntry->mnt_fsname
91 ,mountEntry->mnt_dir);
95 endmntent(mountTable);
105 * Given a block device, find the mount table entry if that block device
108 * Given any other file (or directory), find the mount table entry for its
111 extern struct mntent *
112 findMountPoint(const char * name, const char * table)
117 struct mntent * mountEntry;
119 if ( stat(name, &s) != 0 )
122 if ( (s.st_mode & S_IFMT) == S_IFBLK )
123 mountDevice = s.st_rdev;
125 mountDevice = s.st_dev;
128 if ( (mountTable = setmntent(table, "r")) == 0 )
131 while ( (mountEntry = getmntent(mountTable)) != 0 ) {
132 if ( strcmp(name, mountEntry->mnt_dir) == 0
133 || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */
135 if ( stat(mountEntry->mnt_fsname, &s) == 0
136 && s.st_rdev == mountDevice ) /* Match the device. */
138 if ( stat(mountEntry->mnt_dir, &s) == 0
139 && s.st_dev == mountDevice ) /* Match the directory's mount point. */
142 endmntent(mountTable);