1 /* df.c - report free disk space.
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/df.html
6
7 USE_DF(NEWTOY(df, "HPkht*a[-HPkh]", TOYFLAG_SBIN))
8
9 config DF
10 bool "df"
11 default y
12 help
13 usage: df [-HPkh] [-t type] [FILESYSTEM ...]
14
15 The "disk free" command shows total/used/available disk space for
16 each filesystem listed on the command line, or all currently mounted
17 filesystems.
18
19 -a Show all (including /proc and friends)
20 -P The SUSv3 "Pedantic" option
21 -k Sets units back to 1024 bytes (the default without -P)
22 -h Human readable output (K=1024)
23 -H Human readable output (k=1000)
24 -t type Display only filesystems of this type.
25
26 Pedantic provides a slightly less useful output format dictated by Posix,
27 and sets the units to 512 bytes instead of the default 1024 bytes.
28 */
29
30 #define FOR_df
31 #include "toys.h"
32
GLOBALS(struct arg_list * fstype;long units;int column_widths[5];int header_shown;)33 GLOBALS(
34 struct arg_list *fstype;
35
36 long units;
37 int column_widths[5];
38 int header_shown;
39 )
40
41 static void measure_column(int col, const char *s)
42 {
43 size_t len = strlen(s);
44
45 if (TT.column_widths[col] < len) TT.column_widths[col] = len;
46 }
47
measure_numeric_column(int col,long long n)48 static void measure_numeric_column(int col, long long n)
49 {
50 snprintf(toybuf, sizeof(toybuf), "%lld", n);
51 return measure_column(col, toybuf);
52 }
53
show_header()54 static void show_header()
55 {
56 TT.header_shown = 1;
57
58 // The filesystem column is always at least this wide.
59 if (TT.column_widths[0] < 14) TT.column_widths[0] = 14;
60
61 if (toys.optflags & (FLAG_H|FLAG_h)) {
62 xprintf("%-*s Size Used Avail Use%% Mounted on\n",
63 TT.column_widths[0], "Filesystem");
64 } else {
65 const char *blocks_label = TT.units == 512 ? "512-blocks" : "1K-blocks";
66 const char *use_label = toys.optflags & FLAG_P ? "Capacity" : "Use%";
67
68 measure_column(1, blocks_label);
69 measure_column(2, "Used");
70 measure_column(3, "Available");
71 measure_column(4, use_label);
72 xprintf("%-*s %*s %*s %*s %*s Mounted on\n",
73 TT.column_widths[0], "Filesystem",
74 TT.column_widths[1], blocks_label,
75 TT.column_widths[2], "Used",
76 TT.column_widths[3], "Available",
77 TT.column_widths[4], use_label);
78
79 // For the "Use%" column, the trailing % should be inside the column.
80 TT.column_widths[4]--;
81 }
82 }
83
show_mt(struct mtab_list * mt,int measuring)84 static void show_mt(struct mtab_list *mt, int measuring)
85 {
86 long long size, used, avail, percent, block;
87 char *device;
88
89 // Return if it wasn't found (should never happen, but with /etc/mtab...)
90 if (!mt) return;
91
92 // If we have -t, skip other filesystem types
93 if (TT.fstype) {
94 struct arg_list *al;
95
96 for (al = TT.fstype; al; al = al->next)
97 if (!strcmp(mt->type, al->arg)) break;
98
99 if (!al) return;
100 }
101
102 // If we don't have -a, skip synthetic filesystems
103 if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return;
104
105 // Figure out how much total/used/free space this filesystem has,
106 // forcing 64-bit math because filesystems are big now.
107 block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1;
108 size = (block * mt->statvfs.f_blocks) / TT.units;
109 used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units;
110 avail = (block*(getuid()?mt->statvfs.f_bavail:mt->statvfs.f_bfree))/TT.units;
111 if (!(used+avail)) percent = 0;
112 else {
113 percent = (used*100)/(used+avail);
114 if (used*100 != percent*(used+avail)) percent++;
115 }
116
117 device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL;
118 if (!device) device = mt->device;
119
120 if (measuring) {
121 measure_column(0, device);
122 measure_numeric_column(1, size);
123 measure_numeric_column(2, used);
124 measure_numeric_column(3, avail);
125 } else {
126 if (!TT.header_shown) show_header();
127
128 if (toys.optflags & (FLAG_H|FLAG_h)) {
129 char *size_str = toybuf, *used_str = toybuf+64, *avail_str = toybuf+128;
130 int hr_flags = (toys.optflags & FLAG_H) ? HR_1000 : 0;
131
132 human_readable(size_str, size, hr_flags);
133 human_readable(used_str, used, hr_flags);
134 human_readable(avail_str, avail, hr_flags);
135 xprintf("%-*s %4s %4s %4s % 3lld%% %s\n",
136 TT.column_widths[0], device,
137 size_str, used_str, avail_str, percent, mt->dir);
138 } else xprintf("%-*s %*lld %*lld %*lld %*lld%% %s\n",
139 TT.column_widths[0], device,
140 TT.column_widths[1], size,
141 TT.column_widths[2], used,
142 TT.column_widths[3], avail,
143 TT.column_widths[4], percent,
144 mt->dir);
145 }
146
147 if (device != mt->device) free(device);
148 }
149
df_main(void)150 void df_main(void)
151 {
152 struct mtab_list *mt, *mtstart, *mtend;
153 int measuring;
154
155 if (toys.optflags & (FLAG_H|FLAG_h)) {
156 TT.units = 1;
157 } else {
158 // Units are 512 bytes if you select "pedantic" without "kilobytes".
159 TT.units = toys.optflags & FLAG_P ? 512 : 1024;
160 }
161
162 if (!(mtstart = xgetmountlist(0))) return;
163 mtend = dlist_terminate(mtstart);
164
165 // If we have a list of filesystems on the command line, loop through them.
166 if (*toys.optargs) {
167 // Measure the names then output the table.
168 for (measuring = 1; measuring >= 0; --measuring) {
169 char **next;
170
171 for (next = toys.optargs; *next; next++) {
172 struct stat st;
173
174 // Stat it (complain if we can't).
175 if (stat(*next, &st)) {
176 perror_msg("'%s'", *next);
177 continue;
178 }
179
180 // Find and display this filesystem. Use _last_ hit in case of
181 // overmounts (which is first hit in the reversed list).
182 for (mt = mtend; mt; mt = mt->prev) {
183 if (st.st_dev == mt->stat.st_dev
184 || (st.st_rdev && (st.st_rdev == mt->stat.st_dev)))
185 {
186 show_mt(mt, measuring);
187 break;
188 }
189 }
190 }
191 }
192 } else {
193 // Loop through mount list to filter out overmounts.
194 for (mt = mtend; mt; mt = mt->prev) {
195 struct mtab_list *mt2, *mt3;
196
197 // 0:0 is LANANA null device
198 if (!mt->stat.st_dev) continue;
199
200 // Filter out overmounts.
201 mt3 = mt;
202 for (mt2 = mt->prev; mt2; mt2 = mt2->prev) {
203 if (mt->stat.st_dev == mt2->stat.st_dev) {
204 // For --bind mounts, show earliest mount
205 if (!strcmp(mt->device, mt2->device)) {
206 if (!(toys.optflags & FLAG_a)) mt3->stat.st_dev = 0;
207 mt3 = mt2;
208 } else mt2->stat.st_dev = 0;
209 }
210 }
211 }
212
213 // Measure the names then output the table.
214 for (measuring = 1; measuring >= 0; --measuring) {
215 // Cosmetic: show filesystems in creation order.
216 for (mt = mtstart; mt; mt = mt->next) {
217 if (mt->stat.st_dev) show_mt(mt, measuring);
218 }
219 }
220 }
221
222 if (CFG_TOYBOX_FREE) llist_traverse(mtstart, free);
223 }
224