blob: 354b2a7cad8daa9faecbb47852a4511022b856d5 [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>
6
7const char df_usage[] = "df [filesystem ...]\n"
8"\n"
9"\tPrint the filesystem space used and space available.\n";
10
11
12static int
13df(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 ) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000020 perror(mountPoint);
Eric Andersencc8ed391999-10-05 16:24:54 +000021 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
Eric Andersencc8ed391999-10-05 16:24:54 +000029 printf(
Eric Andersen5c3199e1999-10-06 22:06:29 +000030 "%-20s %9ld %9ld %9ld %3ld%% %s\n",
31 device,
32 (long)(s.f_blocks * (s.f_bsize / 1024.0)),
33 (long)((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)),
34 (long)(s.f_bavail * (s.f_bsize / 1024.0)),
35 blocks_percent_used,
36 mountPoint);
Eric Andersencc8ed391999-10-05 16:24:54 +000037
38 }
39
40 return 0;
41}
42
43extern int
Eric Andersen17d49ef1999-10-06 20:25:32 +000044df_main(int argc, char * * argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000045{
Eric Andersen5c3199e1999-10-06 22:06:29 +000046 printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
47 "1k-blocks", "Used", "Available", "Use%", "Mounted on");
Eric Andersencc8ed391999-10-05 16:24:54 +000048
49 if ( argc > 1 ) {
Eric Andersen5c3199e1999-10-06 22:06:29 +000050 struct mntent* mountEntry;
51 int status;
Eric Andersencc8ed391999-10-05 16:24:54 +000052
53 while ( argc > 1 ) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000054 if ( (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 )
Eric Andersencc8ed391999-10-05 16:24:54 +000055 {
Eric Andersen17d49ef1999-10-06 20:25:32 +000056 fprintf(stderr, "%s: can't find mount point.\n" ,argv[1]);
Eric Andersencc8ed391999-10-05 16:24:54 +000057 return 1;
58 }
59 status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
60 if ( status != 0 )
61 return status;
62 argc--;
63 argv++;
64 }
65 return 0;
66 }
67 else {
68 FILE * mountTable;
69 struct mntent * mountEntry;
70
Eric Andersen5c3199e1999-10-06 22:06:29 +000071 mountTable = setmntent("/proc/mounts", "r");
72 if ( mountTable == 0) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000073 perror("/proc/mounts");
Eric Andersen5c3199e1999-10-06 22:06:29 +000074 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000075 }
76
Eric Andersen5c3199e1999-10-06 22:06:29 +000077 while ( (mountEntry = getmntent (mountTable))) {
78 int status=df(mountEntry->mnt_fsname ,mountEntry->mnt_dir);
79 if (status)
Eric Andersencc8ed391999-10-05 16:24:54 +000080 return status;
81 }
82 endmntent(mountTable);
83 }
84
85 return 0;
86}
Eric Andersen17d49ef1999-10-06 20:25:32 +000087
88
89
90
91/*
92 * Given a block device, find the mount table entry if that block device
93 * is mounted.
94 *
95 * Given any other file (or directory), find the mount table entry for its
96 * filesystem.
97 */
98extern struct mntent *
Eric Andersen5c3199e1999-10-06 22:06:29 +000099findMountPoint(const char* name, const char* table)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000100{
101 struct stat s;
102 dev_t mountDevice;
103 FILE * mountTable;
104 struct mntent * mountEntry;
105
106 if ( stat(name, &s) != 0 )
107 return 0;
108
109 if ( (s.st_mode & S_IFMT) == S_IFBLK )
110 mountDevice = s.st_rdev;
111 else
112 mountDevice = s.st_dev;
113
114
115 if ( (mountTable = setmntent(table, "r")) == 0 )
116 return 0;
117
118 while ( (mountEntry = getmntent(mountTable)) != 0 ) {
119 if ( strcmp(name, mountEntry->mnt_dir) == 0
120 || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */
121 break;
122 if ( stat(mountEntry->mnt_fsname, &s) == 0
123 && s.st_rdev == mountDevice ) /* Match the device. */
124 break;
125 if ( stat(mountEntry->mnt_dir, &s) == 0
126 && s.st_dev == mountDevice ) /* Match the directory's mount point. */
127 break;
128 }
129 endmntent(mountTable);
130 return mountEntry;
131}