1 /* df.c - report free disk space.
3 * Copyright 2006 Rob Landley <rob@landley.net>
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/df.html
7 USE_DF(NEWTOY(df, "HPkht*a[-HPkh]", TOYFLAG_SBIN))
13 usage: df [-HPkh] [-t type] [FILESYSTEM ...]
15 The "disk free" command shows total/used/available disk space for
16 each filesystem listed on the command line, or all currently mounted
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.
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.
34 struct arg_list *fstype;
41 static void measure_column(int col, const char *s)
43 size_t len = strlen(s);
45 if (TT.column_widths[col] < len) TT.column_widths[col] = len;
48 static void measure_numeric_column(int col, long long n)
50 snprintf(toybuf, sizeof(toybuf), "%lld", n);
51 return measure_column(col, toybuf);
54 static void show_header()
58 // The filesystem column is always at least this wide.
59 if (TT.column_widths[0] < 14) TT.column_widths[0] = 14;
61 if (toys.optflags & (FLAG_H|FLAG_h)) {
62 xprintf("%-*s Size Used Avail Use%% Mounted on\n",
63 TT.column_widths[0], "Filesystem");
65 const char *blocks_label = TT.units == 512 ? "512-blocks" : "1K-blocks";
66 const char *use_label = toys.optflags & FLAG_P ? "Capacity" : "Use%";
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);
79 // For the "Use%" column, the trailing % should be inside the column.
80 TT.column_widths[4]--;
84 static void show_mt(struct mtab_list *mt, int measuring)
86 long long size, used, avail, percent, block;
89 // Return if it wasn't found (should never happen, but with /etc/mtab...)
92 // If we have -t, skip other filesystem types
96 for (al = TT.fstype; al; al = al->next)
97 if (!strcmp(mt->type, al->arg)) break;
102 // If we don't have -a, skip synthetic filesystems
103 if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return;
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;
113 percent = (used*100)/(used+avail);
114 if (used*100 != percent*(used+avail)) percent++;
117 device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL;
118 if (!device) device = mt->device;
121 measure_column(0, device);
122 measure_numeric_column(1, size);
123 measure_numeric_column(2, used);
124 measure_numeric_column(3, avail);
126 if (!TT.header_shown) show_header();
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;
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,
147 if (device != mt->device) free(device);
152 struct mtab_list *mt, *mtstart, *mtend;
155 if (toys.optflags & (FLAG_H|FLAG_h)) {
158 // Units are 512 bytes if you select "pedantic" without "kilobytes".
159 TT.units = toys.optflags & FLAG_P ? 512 : 1024;
162 if (!(mtstart = xgetmountlist(0))) return;
163 mtend = dlist_terminate(mtstart);
165 // If we have a list of filesystems on the command line, loop through them.
167 // Measure the names then output the table.
168 for (measuring = 1; measuring >= 0; --measuring) {
171 for (next = toys.optargs; *next; next++) {
174 // Stat it (complain if we can't).
175 if (stat(*next, &st)) {
176 perror_msg("'%s'", *next);
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)))
186 show_mt(mt, measuring);
193 // Loop through mount list to filter out overmounts.
194 for (mt = mtend; mt; mt = mt->prev) {
195 struct mtab_list *mt2, *mt3;
197 // 0:0 is LANANA null device
198 if (!mt->stat.st_dev) continue;
200 // Filter out overmounts.
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;
208 } else mt2->stat.st_dev = 0;
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);
222 if (CFG_TOYBOX_FREE) llist_traverse(mtstart, free);