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> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 6 | #include <fstab.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 8 | static const char df_usage[] = "df [filesystem ...]\n" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | "\n" |
| 10 | "\tPrint the filesystem space used and space available.\n"; |
| 11 | |
| 12 | |
| 13 | static int |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 14 | df(char* device, const char * mountPoint) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | { |
| 16 | struct statfs s; |
| 17 | long blocks_used; |
| 18 | long blocks_percent_used; |
| 19 | |
| 20 | if ( statfs(mountPoint, &s) != 0 ) { |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 21 | perror(mountPoint); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | return 1; |
| 23 | } |
| 24 | |
| 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); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 29 | if ( strcmp(device, "/dev/root")==0) |
| 30 | device=(getfsfile ("/"))->fs_spec; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | printf( |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 33 | "%-20s %9ld %9ld %9ld %3ld%% %s\n", |
| 34 | device, |
| 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)), |
| 38 | blocks_percent_used, |
| 39 | mountPoint); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 40 | |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Given a block device, find the mount table entry if that block device |
| 48 | * is mounted. |
| 49 | * |
| 50 | * Given any other file (or directory), find the mount table entry for its |
| 51 | * filesystem. |
| 52 | */ |
| 53 | extern struct mntent * |
| 54 | findMountPoint(const char* name, const char* table) |
| 55 | { |
| 56 | struct stat s; |
| 57 | dev_t mountDevice; |
| 58 | FILE * mountTable; |
| 59 | struct mntent * mountEntry; |
| 60 | |
| 61 | if ( stat(name, &s) != 0 ) |
| 62 | return 0; |
| 63 | |
| 64 | if ( (s.st_mode & S_IFMT) == S_IFBLK ) |
| 65 | mountDevice = s.st_rdev; |
| 66 | else |
| 67 | mountDevice = s.st_dev; |
| 68 | |
| 69 | |
| 70 | if ( (mountTable = setmntent(table, "r")) == 0 ) |
| 71 | return 0; |
| 72 | |
| 73 | while ( (mountEntry = getmntent(mountTable)) != 0 ) { |
| 74 | if ( strcmp(name, mountEntry->mnt_dir) == 0 |
| 75 | || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */ |
| 76 | break; |
| 77 | if ( stat(mountEntry->mnt_fsname, &s) == 0 |
| 78 | && s.st_rdev == mountDevice ) /* Match the device. */ |
| 79 | break; |
| 80 | if ( stat(mountEntry->mnt_dir, &s) == 0 |
| 81 | && s.st_dev == mountDevice ) /* Match the directory's mount point. */ |
| 82 | break; |
| 83 | } |
| 84 | endmntent(mountTable); |
| 85 | return mountEntry; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | extern int |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 91 | df_main(int argc, char * * argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | { |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 93 | printf("%-20s %-14s %s %s %s %s\n", "Filesystem", |
| 94 | "1k-blocks", "Used", "Available", "Use%", "Mounted on"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | |
| 96 | if ( argc > 1 ) { |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 97 | struct mntent* mountEntry; |
| 98 | int status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | |
| 100 | while ( argc > 1 ) { |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 101 | if ( (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 ) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | { |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 103 | fprintf(stderr, "%s: can't find mount point.\n" ,argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | return 1; |
| 105 | } |
| 106 | status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir); |
| 107 | if ( status != 0 ) |
| 108 | return status; |
| 109 | argc--; |
| 110 | argv++; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | else { |
| 115 | FILE * mountTable; |
| 116 | struct mntent * mountEntry; |
| 117 | |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 118 | mountTable = setmntent("/proc/mounts", "r"); |
| 119 | if ( mountTable == 0) { |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 120 | perror("/proc/mounts"); |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 121 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 124 | while ( (mountEntry = getmntent (mountTable))) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 125 | int status=df(mountEntry->mnt_fsname, mountEntry->mnt_dir); |
Eric Andersen | 5c3199e | 1999-10-06 22:06:29 +0000 | [diff] [blame] | 126 | if (status) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | return status; |
| 128 | } |
| 129 | endmntent(mountTable); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | |