aboutsummaryrefslogtreecommitdiff
path: root/lib/libutil
diff options
context:
space:
mode:
authorAntoine Brodin <antoine@FreeBSD.org>2008-03-08 21:55:59 +0000
committerAntoine Brodin <antoine@FreeBSD.org>2008-03-08 21:55:59 +0000
commit6044f112a622d3a8933dcebf0ec664fcadc87ce6 (patch)
tree72a949be8ae5338fbe6c75b6fd95face7fe8a518 /lib/libutil
parent0a3374af71f9fca3b1460d330593bb17e8425f6c (diff)
downloadsrc-6044f112a622d3a8933dcebf0ec664fcadc87ce6.tar.gz
src-6044f112a622d3a8933dcebf0ec664fcadc87ce6.zip
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
Notes
Notes: svn path=/head/; revision=176954
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 21d5e3962c45..f4c3316a5778 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);