Skip to content

Commit

Permalink
sys/shell/vfs: make output of vfs df human readable
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 4, 2022
1 parent 44440ca commit 1cf3891
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions sys/shell/commands/sc_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ static int _errno_string(int err, char *buf, size_t buflen)
}
#undef _case_snprintf_errno_name

static void _print_size(uint64_t size)
{
unsigned long len;
const char *unit;

if (size == 0) {
len = 0;
unit = NULL;
} else if ((size & (GiB(1) - 1)) == 0) {
len = size / GiB(1);
unit = "G";
}
else if ((size & (MiB(1) - 1)) == 0) {
len = size / MiB(1);
unit = "M";
}
else if ((size & (KiB(1) - 1)) == 0) {
len = size / KiB(1);
unit = "k";
} else {
len = size;
unit = NULL;
}

if (unit) {
printf("%11lu%s ", len, unit);
} else {
printf("%12lu ", len);
}
}

static void _print_df(vfs_DIR *dir)
{
struct statvfs buf;
Expand All @@ -148,9 +179,11 @@ static void _print_df(vfs_DIR *dir)
printf("statvfs failed: %s\n", err);
return;
}
printf("%12lu %12lu %12lu %7lu%%\n", (unsigned long)buf.f_blocks,
(unsigned long)(buf.f_blocks - buf.f_bfree), (unsigned long)buf.f_bavail,
(unsigned long)(((buf.f_blocks - buf.f_bfree) * 100) / buf.f_blocks));

_print_size(buf.f_blocks * buf.f_bsize);
_print_size((buf.f_blocks - buf.f_bfree) * buf.f_bsize);
_print_size(buf.f_bavail * buf.f_bsize);
printf("%7lu%%\n", (unsigned long)(((buf.f_blocks - buf.f_bfree) * 100) / buf.f_blocks));
}

static int _df_handler(int argc, char **argv)
Expand Down

0 comments on commit 1cf3891

Please sign in to comment.