Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame^] | 1 | #include "internal.h" |
| 2 | #include <stdio.h> |
| 3 | #include <mntent.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/vfs.h> |
| 6 | |
| 7 | const char df_usage[] = "df [filesystem ...]\n" |
| 8 | "\n" |
| 9 | "\tPrint the filesystem space used and space available.\n"; |
| 10 | |
| 11 | |
| 12 | static int |
| 13 | df(const char * device, const char * mountPoint) |
| 14 | { |
| 15 | struct statfs s; |
| 16 | long blocks_used; |
| 17 | long blocks_percent_used; |
| 18 | |
| 19 | if ( statfs(mountPoint, &s) != 0 ) { |
| 20 | name_and_error(mountPoint); |
| 21 | return 1; |
| 22 | } |
| 23 | |
| 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); |
| 28 | |
| 29 | /* |
| 30 | printf( |
| 31 | "%-20s %7ld %7ld %7ld %5ld%% %s\n" |
| 32 | ,device |
| 33 | ,s.f_blocks |
| 34 | ,s.f_blocks - s.f_bfree |
| 35 | ,s.f_bavail |
| 36 | ,blocks_percent_used |
| 37 | ,mountPoint); |
| 38 | */ |
| 39 | |
| 40 | printf( |
| 41 | "%-20s %7.0f %7.0f %7.0f %5ld%% %s\n" |
| 42 | ,device |
| 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) |
| 46 | ,blocks_percent_used |
| 47 | ,mountPoint); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | extern int |
| 55 | df_main(struct FileInfo * i, int argc, char * * argv) |
| 56 | { |
| 57 | static const char header[] = |
| 58 | "Filesystem 1024-blocks Used Available Capacity Mounted on\n"; |
| 59 | printf(header); |
| 60 | |
| 61 | if ( argc > 1 ) { |
| 62 | struct mntent * mountEntry; |
| 63 | int status; |
| 64 | |
| 65 | while ( argc > 1 ) { |
| 66 | if ( (mountEntry = findMountPoint(argv[1], "/etc/mtab")) == 0 |
| 67 | && (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 ) |
| 68 | { |
| 69 | fprintf(stderr, "%s: can't find mount point.\n" |
| 70 | ,argv[1]); |
| 71 | return 1; |
| 72 | } |
| 73 | status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir); |
| 74 | if ( status != 0 ) |
| 75 | return status; |
| 76 | argc--; |
| 77 | argv++; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | else { |
| 82 | FILE * mountTable; |
| 83 | struct mntent * mountEntry; |
| 84 | |
| 85 | if ( (mountTable = setmntent("/etc/mtab", "r")) == 0 |
| 86 | && (mountTable = setmntent("/proc/mounts", "r")) == 0 |
| 87 | ) { |
| 88 | name_and_error("/etc/mtab"); |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | while ( (mountEntry = getmntent(mountTable)) != 0 ) { |
| 93 | int status = df( |
| 94 | mountEntry->mnt_fsname |
| 95 | ,mountEntry->mnt_dir); |
| 96 | if ( status != 0 ) |
| 97 | return status; |
| 98 | } |
| 99 | endmntent(mountTable); |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |