diff options
| author | Antoine Brodin <antoine@FreeBSD.org> | 2008-03-08 21:55:59 +0000 |
|---|---|---|
| committer | Antoine Brodin <antoine@FreeBSD.org> | 2008-03-08 21:55:59 +0000 |
| commit | 6044f112a622d3a8933dcebf0ec664fcadc87ce6 (patch) | |
| tree | 72a949be8ae5338fbe6c75b6fd95face7fe8a518 /lib/libutil/humanize_number.c | |
| parent | 0a3374af71f9fca3b1460d330593bb17e8425f6c (diff) | |
Notes
Diffstat (limited to 'lib/libutil/humanize_number.c')
| -rw-r--r-- | lib/libutil/humanize_number.c | 15 |
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); |
