From 6044f112a622d3a8933dcebf0ec664fcadc87ce6 Mon Sep 17 00:00:00 2001 From: Antoine Brodin Date: Sat, 8 Mar 2008 21:55:59 +0000 Subject: [PATCH] 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 --- lib/libutil/humanize_number.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/libutil/humanize_number.c b/lib/libutil/humanize_number.c index 21d5e3962c4..f4c3316a577 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 #include +#include #include #include #include @@ -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);