From e184a883567ee3fd735644416e4bd683f1894ac5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 12 Dec 2016 19:56:31 +0100 Subject: [PATCH] df: implement -B n and -B formats of -B option Signed-off-by: Denys Vlasenko --- coreutils/df.c | 20 ++++++++++++++++++-- include/libbb.h | 1 + libbb/xatonum.c | 19 +++++++++++++++++++ util-linux/fstrim.c | 25 +++---------------------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/coreutils/df.c b/coreutils/df.c index fdcdae675..79e4c4670 100644 --- a/coreutils/df.c +++ b/coreutils/df.c @@ -129,8 +129,19 @@ int df_main(int argc UNUSED_PARAM, char **argv) if (opt & OPT_MEGA) df_disp_hr = 1024*1024; - if (opt & OPT_BSIZE) - df_disp_hr = xatoul_range(chp, 1, ULONG_MAX); /* disallow 0 */ + if (opt & OPT_BSIZE) { + /* GNU coreutils 8.25 accepts "-BMiB" form too */ + int i; + for (i = 0; kmg_i_suffixes[i].suffix[0]; i++) { + if (strcmp(kmg_i_suffixes[i].suffix, chp) == 0) { + df_disp_hr = kmg_i_suffixes[i].mult; + goto got_it; + } + } + /* Range used to disallow 0 */ + df_disp_hr = xatoul_range_sfx(chp, 1, ULONG_MAX, kmg_i_suffixes); + got_it: ; + } /* From the manpage of df from coreutils-6.10: * Disk space is shown in 1K blocks by default, unless the environment @@ -203,6 +214,11 @@ int df_main(int argc UNUSED_PARAM, char **argv) bb_simple_perror_msg(mount_point); goto set_error; } + /* Some uclibc versions were seen to lose f_frsize + * (kernel does return it, but then uclibc does not copy it) + */ + if (s.f_frsize == 0) + s.f_frsize = s.f_bsize; if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) { if (opt & OPT_INODE) { diff --git a/include/libbb.h b/include/libbb.h index b1fec0157..abdc8c2b8 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -923,6 +923,7 @@ extern const struct suffix_mult bkm_suffixes[]; #define km_suffixes (bkm_suffixes + 1) extern const struct suffix_mult cwbkMG_suffixes[]; #define kMG_suffixes (cwbkMG_suffixes + 3) +extern const struct suffix_mult kmg_i_suffixes[]; #include "xatonum.h" /* Specialized: */ diff --git a/libbb/xatonum.c b/libbb/xatonum.c index 9dd5c3e7e..b63b7f54d 100644 --- a/libbb/xatonum.c +++ b/libbb/xatonum.c @@ -96,3 +96,22 @@ const struct suffix_mult cwbkMG_suffixes[] = { /* coreutils also understands TPEZY suffixes for tera- and so on, with B suffix for decimal */ { "", 0 } }; + +const struct suffix_mult kmg_i_suffixes[] = { + { "KiB", 1024 }, + { "kiB", 1024 }, + { "K", 1024 }, + { "k", 1024 }, + { "MiB", 1048576 }, + { "miB", 1048576 }, + { "M", 1048576 }, + { "m", 1048576 }, + { "GiB", 1073741824 }, + { "giB", 1073741824 }, + { "G", 1073741824 }, + { "g", 1073741824 }, + { "KB", 1000 }, + { "MB", 1000000 }, + { "GB", 1000000000 }, + { "", 0 } +}; diff --git a/util-linux/fstrim.c b/util-linux/fstrim.c index 51400ef0b..fc51878b6 100644 --- a/util-linux/fstrim.c +++ b/util-linux/fstrim.c @@ -47,25 +47,6 @@ struct fstrim_range { #define FITRIM _IOWR('X', 121, struct fstrim_range) #endif -static const struct suffix_mult fstrim_sfx[] = { - { "KiB", 1024 }, - { "kiB", 1024 }, - { "K", 1024 }, - { "k", 1024 }, - { "MiB", 1048576 }, - { "miB", 1048576 }, - { "M", 1048576 }, - { "m", 1048576 }, - { "GiB", 1073741824 }, - { "giB", 1073741824 }, - { "G", 1073741824 }, - { "g", 1073741824 }, - { "KB", 1000 }, - { "MB", 1000000 }, - { "GB", 1000000000 }, - { "", 0 } -}; - int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int fstrim_main(int argc UNUSED_PARAM, char **argv) { @@ -98,11 +79,11 @@ int fstrim_main(int argc UNUSED_PARAM, char **argv) range.len = ULLONG_MAX; if (opts & OPT_o) - range.start = xatoull_sfx(arg_o, fstrim_sfx); + range.start = xatoull_sfx(arg_o, kmg_i_suffixes); if (opts & OPT_l) - range.len = xatoull_sfx(arg_l, fstrim_sfx); + range.len = xatoull_sfx(arg_l, kmg_i_suffixes); if (opts & OPT_m) - range.minlen = xatoull_sfx(arg_m, fstrim_sfx); + range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes); mp = argv[optind]; if (find_block_device(mp)) { -- 2.25.1