humanize_number.c revision 1.18 1 1.18 kre /* $NetBSD: humanize_number.c,v 1.18 2019/03/11 15:10:51 kre Exp $ */
2 1.1 abs
3 1.1 abs /*
4 1.1 abs * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 1.1 abs * All rights reserved.
6 1.1 abs *
7 1.1 abs * This code is derived from software contributed to The NetBSD Foundation
8 1.1 abs * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 abs * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
10 1.1 abs *
11 1.1 abs * Redistribution and use in source and binary forms, with or without
12 1.1 abs * modification, are permitted provided that the following conditions
13 1.1 abs * are met:
14 1.1 abs * 1. Redistributions of source code must retain the above copyright
15 1.1 abs * notice, this list of conditions and the following disclaimer.
16 1.1 abs * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 abs * notice, this list of conditions and the following disclaimer in the
18 1.1 abs * documentation and/or other materials provided with the distribution.
19 1.1 abs *
20 1.1 abs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 abs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 abs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 abs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 abs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 abs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 abs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 abs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 abs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 abs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 abs * POSSIBILITY OF SUCH DAMAGE.
31 1.1 abs */
32 1.1 abs
33 1.1 abs #include <sys/cdefs.h>
34 1.10 christos #if defined(LIBC_SCCS) && !defined(lint)
35 1.18 kre __RCSID("$NetBSD: humanize_number.c,v 1.18 2019/03/11 15:10:51 kre Exp $");
36 1.10 christos #endif /* LIBC_SCCS and not lint */
37 1.1 abs
38 1.9 kleink #include "namespace.h"
39 1.1 abs #include <assert.h>
40 1.11 simonb #include <inttypes.h>
41 1.1 abs #include <stdio.h>
42 1.1 abs #include <stdlib.h>
43 1.1 abs #include <string.h>
44 1.1 abs #include <locale.h>
45 1.1 abs
46 1.1 abs int
47 1.1 abs humanize_number(char *buf, size_t len, int64_t bytes,
48 1.1 abs const char *suffix, int scale, int flags)
49 1.1 abs {
50 1.8 enami const char *prefixes, *sep;
51 1.18 kre int b, i, r, s1, s2, sign;
52 1.16 christos int64_t divisor, max, post = 1;
53 1.18 kre size_t baselen;
54 1.18 kre int maxscale;
55 1.1 abs
56 1.1 abs _DIAGASSERT(buf != NULL);
57 1.4 thorpej _DIAGASSERT(scale >= 0);
58 1.1 abs
59 1.18 kre if (suffix == NULL)
60 1.18 kre suffix = "";
61 1.18 kre
62 1.3 drochner if (flags & HN_DIVISOR_1000) {
63 1.3 drochner /* SI for decimal multiplies */
64 1.3 drochner divisor = 1000;
65 1.8 enami if (flags & HN_B)
66 1.8 enami prefixes = "B\0k\0M\0G\0T\0P\0E";
67 1.8 enami else
68 1.8 enami prefixes = "\0\0k\0M\0G\0T\0P\0E";
69 1.3 drochner } else {
70 1.3 drochner /*
71 1.3 drochner * binary multiplies
72 1.3 drochner * XXX IEC 60027-2 recommends Ki, Mi, Gi...
73 1.3 drochner */
74 1.3 drochner divisor = 1024;
75 1.8 enami if (flags & HN_B)
76 1.8 enami prefixes = "B\0K\0M\0G\0T\0P\0E";
77 1.8 enami else
78 1.8 enami prefixes = "\0\0K\0M\0G\0T\0P\0E";
79 1.3 drochner }
80 1.3 drochner
81 1.8 enami #define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
82 1.17 christos maxscale = 6;
83 1.8 enami
84 1.18 kre if (scale < 0 || (scale > maxscale &&
85 1.18 kre (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0))
86 1.1 abs return (-1);
87 1.1 abs
88 1.18 kre if (buf == NULL)
89 1.1 abs return (-1);
90 1.1 abs
91 1.1 abs if (len > 0)
92 1.1 abs buf[0] = '\0';
93 1.18 kre
94 1.1 abs if (bytes < 0) {
95 1.1 abs sign = -1;
96 1.8 enami baselen = 3; /* sign, digit, prefix */
97 1.16 christos if (-bytes < INT64_MAX / 100)
98 1.16 christos bytes *= -100;
99 1.16 christos else {
100 1.16 christos bytes = -bytes;
101 1.16 christos post = 100;
102 1.16 christos baselen += 2;
103 1.16 christos }
104 1.1 abs } else {
105 1.1 abs sign = 1;
106 1.8 enami baselen = 2; /* digit, prefix */
107 1.16 christos if (bytes < INT64_MAX / 100)
108 1.16 christos bytes *= 100;
109 1.16 christos else {
110 1.16 christos post = 100;
111 1.16 christos baselen += 2;
112 1.16 christos }
113 1.1 abs }
114 1.8 enami if (flags & HN_NOSPACE)
115 1.8 enami sep = "";
116 1.8 enami else {
117 1.8 enami sep = " ";
118 1.8 enami baselen++;
119 1.8 enami }
120 1.8 enami baselen += strlen(suffix);
121 1.1 abs
122 1.8 enami /* Check if enough room for `x y' + suffix + `\0' */
123 1.8 enami if (len < baselen + 1)
124 1.1 abs return (-1);
125 1.1 abs
126 1.8 enami if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
127 1.12 enami /*
128 1.18 kre * 19 is number of digits in biggest possible int64_t
129 1.18 kre * If we don't do this, the calc of max just below can
130 1.18 kre * overflow, leading to absurd results. If the buffer
131 1.18 kre * is big enough for the number, simply use it, no scaling.
132 1.12 enami */
133 1.18 kre if (len - baselen > 19)
134 1.18 kre i = 0;
135 1.18 kre else {
136 1.18 kre /* See if there are additional columns to be used. */
137 1.18 kre for (max = 100, i = len - baselen; i-- > 0;)
138 1.18 kre max *= 10;
139 1.18 kre
140 1.18 kre /*
141 1.18 kre * Divide the number until it fits the avail buffer.
142 1.18 kre * If there will be an overflow by the rounding below,
143 1.18 kre * (the "-50") divide once more.
144 1.18 kre */
145 1.18 kre for (i = 0; bytes >= max - 50 && i < maxscale; i++)
146 1.18 kre bytes /= divisor;
147 1.18 kre }
148 1.15 christos if (scale & HN_GETSCALE) {
149 1.15 christos _DIAGASSERT(__type_fit(int, i));
150 1.18 kre return i;
151 1.15 christos }
152 1.18 kre } else {
153 1.18 kre /* XXX
154 1.18 kre * we already know scale <= maxscale, so
155 1.18 kre * i < scale ==> i < maxscale
156 1.18 kre */
157 1.18 kre for (i = 0; i < scale && i < maxscale; i++)
158 1.1 abs bytes /= divisor;
159 1.18 kre }
160 1.18 kre
161 1.18 kre if (i == 0) {
162 1.18 kre /*
163 1.18 kre * Cannot go the bytes *= post route, as
164 1.18 kre * that can cause overflow of bytes
165 1.18 kre *
166 1.18 kre * but if we already scaled up, undo that.
167 1.18 kre */
168 1.18 kre if (post == 1)
169 1.18 kre bytes /= 100;
170 1.1 abs
171 1.11 simonb r = snprintf(buf, len, "%" PRId64 "%s%s%s",
172 1.18 kre sign * bytes, sep, SCALE2PREFIX(0), suffix);
173 1.18 kre } else {
174 1.18 kre /*
175 1.18 kre * Here this is safe, as if i > 0, we have already
176 1.18 kre * divided bytes by at least 1000, post <= 100, so ...
177 1.18 kre */
178 1.18 kre bytes *= post;
179 1.18 kre
180 1.18 kre /* If a value <= 9.9 after rounding and ... */
181 1.18 kre if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
182 1.18 kre /* baselen + \0 + .N */
183 1.18 kre if (len < baselen + 1 + 1 +
184 1.18 kre strlen(localeconv()->decimal_point))
185 1.18 kre return (-1);
186 1.18 kre b = ((int)bytes + 5) / 10;
187 1.18 kre s1 = b / 10;
188 1.18 kre s2 = b % 10;
189 1.18 kre r = snprintf(buf, len, "%d%s%d%s%s%s",
190 1.18 kre sign * s1, localeconv()->decimal_point, s2,
191 1.18 kre sep, SCALE2PREFIX(i), suffix);
192 1.18 kre } else
193 1.18 kre r = snprintf(buf, len, "%" PRId64 "%s%s%s",
194 1.18 kre sign * ((bytes + 50) / 100),
195 1.18 kre sep, SCALE2PREFIX(i), suffix);
196 1.18 kre }
197 1.18 kre
198 1.18 kre if ((size_t)r > len)
199 1.18 kre r = -1;
200 1.1 abs
201 1.1 abs return (r);
202 1.1 abs }
203