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
This commit is contained in:
Antoine Brodin 2008-03-08 21:55:59 +00:00
parent 0a3374af71
commit 6044f112a6
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=176954

View File

@ -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);