blob: 8cc93814bd46f0b21edd47fbc6160816ca426d3f [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001#include "internal.h"
2#include <stdio.h>
3#include <mntent.h>
4#include <sys/stat.h>
5#include <sys/vfs.h>
Eric Andersen596e5461999-10-07 08:30:23 +00006#include <fstab.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00007
8const char df_usage[] = "df [filesystem ...]\n"
9"\n"
10"\tPrint the filesystem space used and space available.\n";
11
12
13static int
Eric Andersen596e5461999-10-07 08:30:23 +000014df(char* device, const char * mountPoint)
Eric Andersencc8ed391999-10-05 16:24:54 +000015{
16 struct statfs s;
17 long blocks_used;
18 long blocks_percent_used;
19
20 if ( statfs(mountPoint, &s) != 0 ) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000021 perror(mountPoint);
Eric Andersencc8ed391999-10-05 16:24:54 +000022 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 Andersen596e5461999-10-07 08:30:23 +000029 if ( strcmp(device, "/dev/root")==0)
30 device=(getfsfile ("/"))->fs_spec;
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Eric Andersencc8ed391999-10-05 16:24:54 +000032 printf(
Eric Andersen5c3199e1999-10-06 22:06:29 +000033 "%-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 Andersencc8ed391999-10-05 16:24:54 +000040
41 }
42
43 return 0;
44}
45
46extern int
Eric Andersen17d49ef1999-10-06 20:25:32 +000047df_main(int argc, char * * argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000048{
Eric Andersen5c3199e1999-10-06 22:06:29 +000049 printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
50 "1k-blocks", "Used", "Available", "Use%", "Mounted on");
Eric Andersencc8ed391999-10-05 16:24:54 +000051
52 if ( argc > 1 ) {
Eric Andersen5c3199e1999-10-06 22:06:29 +000053 struct mntent* mountEntry;
54 int status;
Eric Andersencc8ed391999-10-05 16:24:54 +000055
56 while ( argc > 1 ) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000057 if ( (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 )
Eric Andersencc8ed391999-10-05 16:24:54 +000058 {
Eric Andersen17d49ef1999-10-06 20:25:32 +000059 fprintf(stderr, "%s: can't find mount point.\n" ,argv[1]);
Eric Andersencc8ed391999-10-05 16:24:54 +000060 return 1;
61 }
62 status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
63 if ( status != 0 )
64 return status;
65 argc--;
66 argv++;
67 }
68 return 0;
69 }
70 else {
71 FILE * mountTable;
72 struct mntent * mountEntry;
73
Eric Andersen5c3199e1999-10-06 22:06:29 +000074 mountTable = setmntent("/proc/mounts", "r");
75 if ( mountTable == 0) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000076 perror("/proc/mounts");
Eric Andersen5c3199e1999-10-06 22:06:29 +000077 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000078 }
79
Eric Andersen5c3199e1999-10-06 22:06:29 +000080 while ( (mountEntry = getmntent (mountTable))) {
Eric Andersen596e5461999-10-07 08:30:23 +000081 int status=df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
Eric Andersen5c3199e1999-10-06 22:06:29 +000082 if (status)
Eric Andersencc8ed391999-10-05 16:24:54 +000083 return status;
84 }
85 endmntent(mountTable);
86 }
87
88 return 0;
89}
Eric Andersen17d49ef1999-10-06 20:25:32 +000090
91
92
93
94/*
95 * Given a block device, find the mount table entry if that block device
96 * is mounted.
97 *
98 * Given any other file (or directory), find the mount table entry for its
99 * filesystem.
100 */
101extern struct mntent *
Eric Andersen5c3199e1999-10-06 22:06:29 +0000102findMountPoint(const char* name, const char* table)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000103{
104 struct stat s;
105 dev_t mountDevice;
106 FILE * mountTable;
107 struct mntent * mountEntry;
108
109 if ( stat(name, &s) != 0 )
110 return 0;
111
112 if ( (s.st_mode & S_IFMT) == S_IFBLK )
113 mountDevice = s.st_rdev;
114 else
115 mountDevice = s.st_dev;
116
117
118 if ( (mountTable = setmntent(table, "r")) == 0 )
119 return 0;
120
121 while ( (mountEntry = getmntent(mountTable)) != 0 ) {
122 if ( strcmp(name, mountEntry->mnt_dir) == 0
123 || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */
124 break;
125 if ( stat(mountEntry->mnt_fsname, &s) == 0
126 && s.st_rdev == mountDevice ) /* Match the device. */
127 break;
128 if ( stat(mountEntry->mnt_dir, &s) == 0
129 && s.st_dev == mountDevice ) /* Match the directory's mount point. */
130 break;
131 }
132 endmntent(mountTable);
133 return mountEntry;
134}
Eric Andersen596e5461999-10-07 08:30:23 +0000135
136