diff options
author | Pádraig Brady <P@draigBrady.com> | 2015-06-15 03:55:46 +0100 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2015-06-19 14:50:34 +0100 |
commit | 6fadd46acd5b813b545b58014ddd591ffdb5a41c (patch) | |
tree | 2b9982ef2fed456428e9a64e9a4270ee88985b00 /src | |
parent | a262318f833c0d929da4bbd1c3002f891fa9bfa1 (diff) | |
download | coreutils-6fadd46acd5b813b545b58014ddd591ffdb5a41c.tar.xz |
numfmt: handle suffixes consistently with --{from,to}-unit
* src/numfmt.c (unit_to_umax): Support SI (power of 10) suffixes
with the --from-unit and --to-unit options. Treat suffixes like
is done with --from=auto, which for example will change the meaning
of --to-unit=G to that of --to-unit=Gi. The suffix support was
previously undocumented and it's better to avoid the traditional
coreutils suffix handling in numfmt by default.
* doc/coreutils.texi: Document the new behavior. Also fix a typo
mentioning {from,to}=units=.
* tests/misc/numfmt.pl: Adjust accordingly.
* NEWS: Mention the change in behavior.
Diffstat (limited to 'src')
-rw-r--r-- | src/numfmt.c | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/src/numfmt.c b/src/numfmt.c index 9cbcb2753..58520c26e 100644 --- a/src/numfmt.c +++ b/src/numfmt.c @@ -776,19 +776,48 @@ double_to_human (long double val, int precision, } /* Convert a string of decimal digits, N_STRING, with an optional suffix - to an integral value. Upon successful conversion, return that value. + to an integral value. Suffixes are handled as with --from=auto. + Upon successful conversion, return that value. If it cannot be converted, give a diagnostic and exit. */ static uintmax_t unit_to_umax (const char *n_string) { strtol_error s_err; + const char *c_string = n_string; + char *t_string = NULL; + size_t n_len = strlen (n_string); char *end = NULL; uintmax_t n; + const char *suffixes = "KMGTPEZY"; - s_err = xstrtoumax (n_string, &end, 10, &n, "KMGTPEZY"); + /* Adjust suffixes so K=1000, Ki=1024, KiB=invalid. */ + if (n_len && ! c_isdigit (n_string[n_len - 1])) + { + t_string = xmalloc (n_len + 2); + end = t_string + n_len - 1; + memcpy (t_string, n_string, n_len); + + if (*end == 'i' && 2 <= n_len && ! c_isdigit (*(end - 1))) + *end = '\0'; + else + { + *++end = 'B'; + *++end = '\0'; + suffixes = "KMGTPEZY0"; + } + + c_string = t_string; + } + + s_err = xstrtoumax (c_string, &end, 10, &n, suffixes); if (s_err != LONGINT_OK || *end || n == 0) - error (EXIT_FAILURE, 0, _("invalid unit size: %s"), quote (n_string)); + { + free (t_string); + error (EXIT_FAILURE, 0, _("invalid unit size: %s"), quote (n_string)); + } + + free (t_string); return n; } |