summaryrefslogtreecommitdiff
path: root/src/pacman/util.c
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-02-06 05:50:48 -0600
committerDan McGee <dan@archlinux.org>2012-02-06 05:50:48 -0600
commitf55be4897799c5a6dcb454455ad9edcfc36f8495 (patch)
treedd87b34de4b85f307d5dd05f6d576aadc3ad3697 /src/pacman/util.c
parentb488f229d2ec4f2e4b9e746d68422460ca664715 (diff)
parentb7c06d6d678ebe6a434b2387c3bda14647113f32 (diff)
Merge branch 'maint'
Conflicts: lib/libalpm/alpm_list.c
Diffstat (limited to 'src/pacman/util.c')
-rw-r--r--src/pacman/util.c35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 49beb83d..1c3699ed 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -841,13 +841,13 @@ static alpm_list_t *create_verbose_row(pm_target_t *target, int dl_size)
/* and size */
size -= target->remove ? alpm_pkg_get_isize(target->remove) : 0;
size += target->install ? alpm_pkg_get_isize(target->install) : 0;
- human_size = humanize_size(size, 'M', &label);
+ human_size = humanize_size(size, 'M', 2, &label);
pm_asprintf(&str, "%.2f %s", human_size, label);
ret = alpm_list_add(ret, str);
if(dl_size) {
size = target->install ? alpm_pkg_download_size(target->install) : 0;
- human_size = humanize_size(size, 'M', &label);
+ human_size = humanize_size(size, 'M', 2, &label);
if(size != 0) {
pm_asprintf(&str, "%.2f %s", human_size, label);
} else {
@@ -931,21 +931,21 @@ static void _display_targets(alpm_list_t *targets, int verbose)
free(str);
if(dlsize > 0 || config->op_s_downloadonly) {
- size = humanize_size(dlsize, 'M', &label);
+ size = humanize_size(dlsize, 'M', 2, &label);
printf(_("Total Download Size: %.2f %s\n"), size, label);
}
if(!config->op_s_downloadonly) {
if(isize > 0) {
- size = humanize_size(isize, 'M', &label);
+ size = humanize_size(isize, 'M', 2, &label);
printf(_("Total Installed Size: %.2f %s\n"), size, label);
}
if(rsize > 0 && isize == 0) {
- size = humanize_size(rsize, 'M', &label);
+ size = humanize_size(rsize, 'M', 2, &label);
printf(_("Total Removed Size: %.2f %s\n"), size, label);
}
/* only show this net value if different from raw installed size */
if(isize > 0 && rsize > 0) {
- size = humanize_size(isize - rsize, 'M', &label);
+ size = humanize_size(isize - rsize, 'M', 2, &label);
printf(_("Net Upgrade Size: %.2f %s\n"), size, label);
}
}
@@ -1038,18 +1038,31 @@ static char *pkg_get_location(alpm_pkg_t *pkg)
}
}
+/* a pow() implementation that is specialized for an integer base and small,
+ * positive-only integer exponents. */
+static double simple_pow(int base, int exp)
+{
+ double result = 1.0;
+ for(; exp > 0; exp--) {
+ result *= base;
+ }
+ return result;
+}
+
/** Converts sizes in bytes into human readable units.
*
* @param bytes the size in bytes
* @param target_unit '\0' or a short label. If equal to one of the short unit
* labels ('B', 'K', ...) bytes is converted to target_unit; if '\0', the first
* unit which will bring the value to below a threshold of 2048 will be chosen.
- * @param long_labels whether to use short ("K") or long ("KiB") unit labels
+ * @param precision number of decimal places, ensures -0.0.0 gets rounded to
+ * 0.00; -1 if no rounding desired
* @param label will be set to the appropriate unit label
*
* @return the size in the appropriate unit
*/
-double humanize_size(off_t bytes, const char target_unit, const char **label)
+double humanize_size(off_t bytes, const char target_unit, int precision,
+ const char **label)
{
static const char *labels[] = {"B", "KiB", "MiB", "GiB",
"TiB", "PiB", "EiB", "ZiB", "YiB"};
@@ -1071,6 +1084,12 @@ double humanize_size(off_t bytes, const char target_unit, const char **label)
*label = labels[index];
}
+ /* fix FS#27924 so that it doesn't display negative zeroes */
+ if(precision >= 0 && val < 0.0 &&
+ val > (-0.5 / simple_pow(10, precision))) {
+ val = 0.0;
+ }
+
return val;
}