humanize_number.c revision 135792
1/*	$NetBSD: humanize_number.c,v 1.8 2004/07/27 01:56:24 enami Exp $	*/
2
3/*
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *      This product includes software developed by the NetBSD
22 *      Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libutil/humanize_number.c 135792 2004-09-25 14:11:34Z pjd $");
42
43#include <sys/types.h>
44#include <assert.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <locale.h>
49#include <libutil.h>
50
51int
52humanize_number(char *buf, size_t len, int64_t bytes,
53    const char *suffix, int scale, int flags)
54{
55	const char *prefixes, *sep;
56	int	b, i, r, maxscale, s1, s2, sign;
57	int64_t	divisor, max;
58	size_t	baselen;
59
60	assert(buf != NULL);
61	assert(suffix != NULL);
62	assert(scale >= 0);
63
64	if (flags & HN_DIVISOR_1000) {
65		/* SI for decimal multiplies */
66		divisor = 1000;
67		if (flags & HN_B)
68			prefixes = "B\0k\0M\0G\0T\0P\0E";
69		else
70			prefixes = "\0\0k\0M\0G\0T\0P\0E";
71	} else {
72		/*
73		 * binary multiplies
74		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
75		 */
76		divisor = 1024;
77		if (flags & HN_B)
78			prefixes = "B\0K\0M\0G\0T\0P\0E";
79		else
80			prefixes = "\0\0K\0M\0G\0T\0P\0E";
81	}
82
83#define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
84	maxscale = 7;
85
86	if (scale >= maxscale &&
87	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
88		return (-1);
89
90	if (buf == NULL || suffix == NULL)
91		return (-1);
92
93	if (len > 0)
94		buf[0] = '\0';
95	if (bytes < 0) {
96		sign = -1;
97		bytes *= -100;
98		baselen = 3;		/* sign, digit, prefix */
99	} else {
100		sign = 1;
101		bytes *= 100;
102		baselen = 2;		/* digit, prefix */
103	}
104	if (flags & HN_NOSPACE)
105		sep = "";
106	else {
107		sep = " ";
108		baselen++;
109	}
110	baselen += strlen(suffix);
111
112	/* Check if enough room for `x y' + suffix + `\0' */
113	if (len < baselen + 1)
114		return (-1);
115
116	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
117		/* See if there is additional columns can be used. */
118		for (max = 100, i = len - baselen; i-- > 0;)
119			max *= 10;
120
121		for (i = 0; bytes >= max && i < maxscale; i++)
122			bytes /= divisor;
123
124		if (scale & HN_GETSCALE)
125			return (i);
126	} else
127		for (i = 0; i < scale && i < maxscale; i++)
128			bytes /= divisor;
129
130	/* If a value <= 9.9 after rounding and ... */
131	if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
132		/* baselen + \0 + .N */
133		if (len < baselen + 1 + 2)
134			return (-1);
135		b = ((int)bytes + 5) / 10;
136		s1 = b / 10;
137		s2 = b % 10;
138		r = snprintf(buf, len, "%d%s%d%s%s%s",
139		    sign * s1, localeconv()->decimal_point, s2,
140		    sep, SCALE2PREFIX(i), suffix);
141	} else
142		r = snprintf(buf, len, "%lld%s%s%s",
143		    /* LONGLONG */
144		    (long long)(sign * ((bytes + 50) / 100)),
145		    sep, SCALE2PREFIX(i), suffix);
146
147	return (r);
148}
149