summaryrefslogtreecommitdiffstats
path: root/lib/libutil
diff options
context:
space:
mode:
authorantoine <antoine@FreeBSD.org>2008-03-08 21:55:59 +0000
committerantoine <antoine@FreeBSD.org>2008-03-08 21:55:59 +0000
commitea3f3b4bc096e3278852eb787d17cac0580d5e55 (patch)
tree72a949be8ae5338fbe6c75b6fd95face7fe8a518 /lib/libutil
parent45b0cd410fe1f0bddf8f5492ec17efe57f6cdc94 (diff)
downloadFreeBSD-src-ea3f3b4bc096e3278852eb787d17cac0580d5e55.zip
FreeBSD-src-ea3f3b4bc096e3278852eb787d17cac0580d5e55.tar.gz
Merge changes from NetBSD on humanize_number.c, 1.8 -> 1.13
Significant changes: - rev. 1.11: Use PRId64 instead of a cast to long long and %lld to print an int64_t. - rev. 1.12: Fix a bug that humanize_number() produces "1000" where it should be "1.0G" or "1.0M". The bug reported by Greg Troxel. PR: 118461 PR: 102694 Approved by: rwatson (mentor) Obtained from: NetBSD MFC after: 1 month
Diffstat (limited to 'lib/libutil')
-rw-r--r--lib/libutil/humanize_number.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/libutil/humanize_number.c b/lib/libutil/humanize_number.c
index 21d5e39..f4c3316 100644
--- a/lib/libutil/humanize_number.c
+++ b/lib/libutil/humanize_number.c
@@ -1,4 +1,4 @@
-/* $NetBSD: humanize_number.c,v 1.8 2004/07/27 01:56:24 enami Exp $ */
+/* $NetBSD: humanize_number.c,v 1.13 2007/12/14 17:26:19 christos Exp $ */
/*
* Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
@@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <assert.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -118,7 +119,12 @@ humanize_number(char *buf, size_t len, int64_t bytes,
for (max = 100, i = len - baselen; i-- > 0;)
max *= 10;
- for (i = 0; bytes >= max && i < maxscale; i++)
+ /*
+ * Divide the number until it fits the given column.
+ * If there will be an overflow by the rounding below,
+ * divide once more.
+ */
+ for (i = 0; bytes >= max - 50 && i < maxscale; i++)
bytes /= divisor;
if (scale & HN_GETSCALE)
@@ -139,9 +145,8 @@ humanize_number(char *buf, size_t len, int64_t bytes,
sign * s1, localeconv()->decimal_point, s2,
sep, SCALE2PREFIX(i), suffix);
} else
- r = snprintf(buf, len, "%lld%s%s%s",
- /* LONGLONG */
- (long long)(sign * ((bytes + 50) / 100)),
+ r = snprintf(buf, len, "%" PRId64 "%s%s%s",
+ sign * ((bytes + 50) / 100),
sep, SCALE2PREFIX(i), suffix);
return (r);
OpenPOWER on IntegriCloud