Merge commit 'brent/register_phys_mem'
[freebsd-src/fkvm-freebsd.git] / lib / libutil / humanize_number.c
blobf4c3316a57782908b4cd339645bf6818f38bcae1
1 /* $NetBSD: humanize_number.c,v 1.13 2007/12/14 17:26:19 christos Exp $ */
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
44 #include <assert.h>
45 #include <inttypes.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <locale.h>
50 #include <libutil.h>
52 int
53 humanize_number(char *buf, size_t len, int64_t bytes,
54 const char *suffix, int scale, int flags)
56 const char *prefixes, *sep;
57 int b, i, r, maxscale, s1, s2, sign;
58 int64_t divisor, max;
59 size_t baselen;
61 assert(buf != NULL);
62 assert(suffix != NULL);
63 assert(scale >= 0);
65 if (flags & HN_DIVISOR_1000) {
66 /* SI for decimal multiplies */
67 divisor = 1000;
68 if (flags & HN_B)
69 prefixes = "B\0k\0M\0G\0T\0P\0E";
70 else
71 prefixes = "\0\0k\0M\0G\0T\0P\0E";
72 } else {
74 * binary multiplies
75 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
77 divisor = 1024;
78 if (flags & HN_B)
79 prefixes = "B\0K\0M\0G\0T\0P\0E";
80 else
81 prefixes = "\0\0K\0M\0G\0T\0P\0E";
84 #define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
85 maxscale = 7;
87 if (scale >= maxscale &&
88 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
89 return (-1);
91 if (buf == NULL || suffix == NULL)
92 return (-1);
94 if (len > 0)
95 buf[0] = '\0';
96 if (bytes < 0) {
97 sign = -1;
98 bytes *= -100;
99 baselen = 3; /* sign, digit, prefix */
100 } else {
101 sign = 1;
102 bytes *= 100;
103 baselen = 2; /* digit, prefix */
105 if (flags & HN_NOSPACE)
106 sep = "";
107 else {
108 sep = " ";
109 baselen++;
111 baselen += strlen(suffix);
113 /* Check if enough room for `x y' + suffix + `\0' */
114 if (len < baselen + 1)
115 return (-1);
117 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
118 /* See if there is additional columns can be used. */
119 for (max = 100, i = len - baselen; i-- > 0;)
120 max *= 10;
123 * Divide the number until it fits the given column.
124 * If there will be an overflow by the rounding below,
125 * divide once more.
127 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
128 bytes /= divisor;
130 if (scale & HN_GETSCALE)
131 return (i);
132 } else
133 for (i = 0; i < scale && i < maxscale; i++)
134 bytes /= divisor;
136 /* If a value <= 9.9 after rounding and ... */
137 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
138 /* baselen + \0 + .N */
139 if (len < baselen + 1 + 2)
140 return (-1);
141 b = ((int)bytes + 5) / 10;
142 s1 = b / 10;
143 s2 = b % 10;
144 r = snprintf(buf, len, "%d%s%d%s%s%s",
145 sign * s1, localeconv()->decimal_point, s2,
146 sep, SCALE2PREFIX(i), suffix);
147 } else
148 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
149 sign * ((bytes + 50) / 100),
150 sep, SCALE2PREFIX(i), suffix);
152 return (r);