df: implement -B n<suff> and -B <suff> formats of -B option

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2016-12-12 19:56:31 +01:00
parent 76de3257f7
commit e184a88356
4 changed files with 41 additions and 24 deletions

View File

@ -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) {

View File

@ -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: */

View File

@ -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 }
};

View File

@ -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)) {