aboutsummaryrefslogtreecommitdiff
path: root/toys/posix
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2021-02-14 13:09:30 -0600
committerRob Landley <rob@landley.net>2021-02-14 13:14:19 -0600
commit5f5f97f215bb5d398bb7b0027d8e9ab84668e07a (patch)
treed1d97e524afe7fd99fb0baebe4edd7d1d64c6333 /toys/posix
parent0678d2c1e6561960704817722f41d33ee851730f (diff)
downloadtoybox-5f5f97f215bb5d398bb7b0027d8e9ab84668e07a.tar.gz
Redo df plumbing a bit.
Diffstat (limited to 'toys/posix')
-rw-r--r--toys/posix/df.c182
1 files changed, 80 insertions, 102 deletions
diff --git a/toys/posix/df.c b/toys/posix/df.c
index 134184e5..d8ba2986 100644
--- a/toys/posix/df.c
+++ b/toys/posix/df.c
@@ -24,7 +24,7 @@ config DF
-i Show inodes instead of blocks
-t type Display only filesystems of this type
- Pedantic provides a slightly less useful output format dictated by Posix,
+ Pedantic provides a slightly less useful output format dictated by POSIX,
and sets the units to 512 bytes instead of the default 1024 bytes.
*/
@@ -34,62 +34,51 @@ config DF
GLOBALS(
struct arg_list *t;
- long units;
- int width[5], header_shown;
+ int units, width[6];
)
-static void measure_column(int col, const char *s)
+static void measure_columns(char *s[])
{
- TT.width[col] = maxof(TT.width[col], strlen(s));
+ int i;
+
+ for (i = 0; i<5; i++) TT.width[i] = maxof(TT.width[i], strlen(s[i]));
}
-static void measure_numeric_column(int col, long long n)
+static void print_columns(char **dsuapm)
{
- TT.width[col] = maxof(TT.width[col], snprintf(0, 0, "%llu", n));
+ int i;
+
+ for (i = 0; i<6; i++) printf(!i ? "%-*s" : " %*s", TT.width[i], dsuapm[i]);
+ xputc('\n');
}
-static void show_header()
+static void print_header()
{
- TT.header_shown = 1;
+ char *dsuapm[] = {"Filesystem", "Size", "Used", "Avail", "Use%","Mounted on"};
// The filesystem column is always at least this wide.
TT.width[0] = maxof(TT.width[0], 14+(FLAG(H)||FLAG(h)));
- if (FLAG(H)||FLAG(h))
- xprintf(FLAG(i) ? "%-*sInodes IUsed IFree IUse%% Mounted on\n" :
- "%-*s Size Used Avail Use%% Mounted on\n", TT.width[0], "Filesystem");
+ if (FLAG(i)) memcpy(dsuapm+1, (char *[]){"Inodes", "IUsed", "IFree", "IUse%"},
+ sizeof(char *)*4);
else {
- const char *item_label, *used_label, *free_label, *use_label;
-
- if (FLAG(i)) {
- item_label = "Inodes";
- used_label = "IUsed";
- free_label = "IFree";
- use_label = "IUse%";
- } else {
- item_label = TT.units == 512 ? "512-blocks" : "1K-blocks";
- used_label = "Used";
- free_label = "Available";
- use_label = FLAG(P) ? "Capacity" : "Use%";
+ if (!(FLAG(H)||FLAG(h))) {
+ dsuapm[1] = TT.units == 512 ? "512-blocks" : "1K-blocks";
+ dsuapm[3] = "Available";
+ if (FLAG(P)) dsuapm[4] = "Capacity";
}
-
- measure_column(1, item_label);
- measure_column(2, used_label);
- measure_column(3, free_label);
- measure_column(4, use_label);
- xprintf("%-*s %*s %*s %*s %*s Mounted on\n", TT.width[0], "Filesystem",
- TT.width[1], item_label, TT.width[2], used_label, TT.width[3], free_label,
- TT.width[4], use_label);
-
- // For the "Use%" column, the trailing % should be inside the column.
- TT.width[4]--;
}
+
+ measure_columns(dsuapm);
+ TT.width[5] = -1;
+ print_columns(dsuapm);
}
static void show_mt(struct mtab_list *mt, int measuring)
{
- unsigned long long size, used, avail, percent, block;
- char *device;
+ unsigned long long suap[4], block = 1, ll;
+ char *dsuapm[6]; // device, size, used, avail, percent, mount
+ int i;
// Return if it wasn't found (should never happen, but with /etc/mtab...)
if (!mt) return;
@@ -98,8 +87,7 @@ static void show_mt(struct mtab_list *mt, int measuring)
if (TT.t) {
struct arg_list *al;
- for (al = TT.t; al; al = al->next)
- if (!strcmp(mt->type, al->arg)) break;
+ for (al = TT.t; al; al = al->next) if (!strcmp(mt->type, al->arg)) break;
if (!al) return;
}
@@ -107,50 +95,46 @@ static void show_mt(struct mtab_list *mt, int measuring)
// If we don't have -a, skip synthetic filesystems
if (!FLAG(a) && !mt->statvfs.f_blocks) return;
- // Figure out how much total/used/free space this filesystem has
- if (FLAG(i)) {
- size = mt->statvfs.f_files;
- used = mt->statvfs.f_files - mt->statvfs.f_ffree;
- avail = getuid() ? mt->statvfs.f_favail : mt->statvfs.f_ffree;
- } else {
- block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1;
- size = (block * mt->statvfs.f_blocks) / TT.units;
- used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units;
- avail= (block*(getuid()?mt->statvfs.f_bavail:mt->statvfs.f_bfree))/TT.units;
- }
- if (!(used+avail)) percent = 0;
+ // Prepare filesystem display fields
+ *dsuapm = *mt->device == '/' ? xabspath(mt->device, 0) : 0;
+ if (!*dsuapm) *dsuapm = mt->device;
+ if (!mt->stat.st_dev) for (i = 1; i<5; i++) dsuapm[i] = "-";
else {
- percent = (used*100)/(used+avail);
- if (used*100 != percent*(used+avail)) percent++;
- }
+ if (FLAG(i)) {
+ suap[0] = mt->statvfs.f_files;
+ suap[1] = mt->statvfs.f_files - mt->statvfs.f_ffree;
+ suap[2] = getuid() ? mt->statvfs.f_favail : mt->statvfs.f_ffree;
+ } else {
+ block = maxof(mt->statvfs.f_bsize, 1);
+ suap[0] = mt->statvfs.f_blocks;
+ suap[1] = mt->statvfs.f_blocks - mt->statvfs.f_bfree;
+ suap[2] = getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree;
+ }
- device = *mt->device == '/' ? xabspath(mt->device, 0) : 0;
- if (!device) device = mt->device;
+ // Scale and convert to strings
+ dsuapm[1] = toybuf;
+ for (i = 0; i<3; i++) {
+ suap[i] = (block*suap[i])/TT.units;
- if (measuring) {
- measure_column(0, device);
- measure_numeric_column(1, size);
- measure_numeric_column(2, used);
- measure_numeric_column(3, avail);
- } else {
- if (!TT.header_shown) show_header();
-
- if (FLAG(H)||FLAG(h)) {
- char *size_str = toybuf, *used_str = toybuf+64, *avail_str = toybuf+128;
- int hr_flags = FLAG(H) ? HR_1000 : 0;
- int w = 4 + !!FLAG(i);
-
- human_readable(size_str, size, hr_flags);
- human_readable(used_str, used, hr_flags);
- human_readable(avail_str, avail, hr_flags);
- xprintf("%-*s %*s %*s %*s %*llu%% %s\n", TT.width[0], device,
- w, size_str, w, used_str, w, avail_str, w-1, percent, mt->dir);
- } else xprintf("%-*s %*llu %*llu %*llu %*llu%% %s\n",
- TT.width[0], device, TT.width[1], size, TT.width[2], used,
- TT.width[3], avail, TT.width[4], percent, mt->dir);
+ if (FLAG(H)||FLAG(h))
+ human_readable(dsuapm[i+1], suap[i], FLAG(H) ? HR_1000 : 0);
+ else sprintf(dsuapm[i+1], "%llu", suap[i]);
+ dsuapm[i+2] = strchr(dsuapm[i+1], 0)+1;
+ }
+
+ // percent
+ if ((suap[3] = ll = suap[1]+suap[2])) {
+ suap[3] = (block = suap[1]*100)/ll;
+ if (block != suap[3]*ll) suap[3]++;
+ }
+ sprintf(dsuapm[4], "%llu%%", suap[3]);
}
+ dsuapm[5] = mt->dir;
- if (device != mt->device) free(device);
+ if (measuring) measure_columns(dsuapm);
+ else print_columns(dsuapm);
+
+ if (*dsuapm != mt->device) free(*dsuapm);
}
void df_main(void)
@@ -160,7 +144,7 @@ void df_main(void)
char **next;
// Units are 512 bytes if you select "pedantic" without "kilobytes".
- if (FLAG(H)||FLAG(h)) TT.units = 1;
+ if (FLAG(H)||FLAG(h)||FLAG(i)) TT.units = 1;
else TT.units = FLAG(P) ? 512 : 1024;
if (!(mtstart = xgetmountlist(0))) return;
@@ -169,38 +153,29 @@ void df_main(void)
// If we have a list of filesystems on the command line, loop through them.
if (*toys.optargs) {
// Measure the names then output the table.
- for (measuring = 1; measuring >= 0; --measuring) {
+ for (measuring = 1;;) {
for (next = toys.optargs; *next; next++) {
struct stat st;
// Stat it (complain if we can't).
- if (stat(*next, &st)) {
- perror_msg("'%s'", *next);
- continue;
- }
-
- // Find and display this filesystem. Use _last_ hit in case of
- // overmounts (which is first hit in the reversed list).
- for (mt = mtend; mt; mt = mt->prev) {
- if (st.st_dev == mt->stat.st_dev
- || (st.st_rdev && (st.st_rdev == mt->stat.st_dev)))
- {
- show_mt(mt, measuring);
- break;
- }
+ if (stat(*next, &st)) perror_msg("'%s'", *next);
+ else {
+ // Find and display this filesystem. Use _last_ hit in case of
+ // overmounts (which is first hit in the reversed list).
+ for (mt = mtend; mt; mt = mt->prev)
+ if (st.st_dev == mt->stat.st_dev
+ || (st.st_rdev && (st.st_rdev == mt->stat.st_dev)))
+ break;
+ show_mt(mt, measuring);
}
}
+ if (!measuring--) break;
+ print_header();
}
} else {
// Loop through mount list to filter out overmounts.
for (mt = mtend; mt; mt = mt->prev) {
-
- // 0:0 is LANANA null device
- if (!mt->stat.st_dev) continue;
-
- // Filter out overmounts.
- mt3 = mt;
- for (mt2 = mt->prev; mt2; mt2 = mt2->prev) {
+ for (mt3 = mt, mt2 = mt->prev; mt2; mt2 = mt2->prev) {
if (mt->stat.st_dev == mt2->stat.st_dev) {
// For --bind mounts, show earliest mount
if (!strcmp(mt->device, mt2->device)) {
@@ -212,9 +187,12 @@ void df_main(void)
}
// Measure the names then output the table (in filesystem creation order).
- for (measuring = 1; measuring >= 0; --measuring)
+ for (measuring = 1;;) {
for (mt = mtstart; mt; mt = mt->next)
if (mt->stat.st_dev) show_mt(mt, measuring);
+ if (!measuring--) break;
+ print_header();
+ }
}
if (CFG_TOYBOX_FREE) llist_traverse(mtstart, free);