8 const char df_usage[] = "df [filesystem ...]\n"
10 "\tPrint the filesystem space used and space available.\n";
14 df(char* device, const char * mountPoint)
18 long blocks_percent_used;
20 if ( statfs(mountPoint, &s) != 0 ) {
25 if ( s.f_blocks > 0 ) {
26 blocks_used = s.f_blocks - s.f_bfree;
27 blocks_percent_used = (long)
28 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
29 if ( strcmp(device, "/dev/root")==0)
30 device=(getfsfile ("/"))->fs_spec;
33 "%-20s %9ld %9ld %9ld %3ld%% %s\n",
35 (long)(s.f_blocks * (s.f_bsize / 1024.0)),
36 (long)((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)),
37 (long)(s.f_bavail * (s.f_bsize / 1024.0)),
47 df_main(int argc, char * * argv)
49 printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
50 "1k-blocks", "Used", "Available", "Use%", "Mounted on");
53 struct mntent* mountEntry;
57 if ( (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 )
59 fprintf(stderr, "%s: can't find mount point.\n" ,argv[1]);
62 status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
72 struct mntent * mountEntry;
74 mountTable = setmntent("/proc/mounts", "r");
75 if ( mountTable == 0) {
76 perror("/proc/mounts");
80 while ( (mountEntry = getmntent (mountTable))) {
81 int status=df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
85 endmntent(mountTable);
95 * Given a block device, find the mount table entry if that block device
98 * Given any other file (or directory), find the mount table entry for its
101 extern struct mntent *
102 findMountPoint(const char* name, const char* table)
107 struct mntent * mountEntry;
109 if ( stat(name, &s) != 0 )
112 if ( (s.st_mode & S_IFMT) == S_IFBLK )
113 mountDevice = s.st_rdev;
115 mountDevice = s.st_dev;
118 if ( (mountTable = setmntent(table, "r")) == 0 )
121 while ( (mountEntry = getmntent(mountTable)) != 0 ) {
122 if ( strcmp(name, mountEntry->mnt_dir) == 0
123 || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */
125 if ( stat(mountEntry->mnt_fsname, &s) == 0
126 && s.st_rdev == mountDevice ) /* Match the device. */
128 if ( stat(mountEntry->mnt_dir, &s) == 0
129 && s.st_dev == mountDevice ) /* Match the directory's mount point. */
132 endmntent(mountTable);