xref: /minix/lib/libc/gen/humanize_number.c (revision ebfedea0)
1 /*	$NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $");
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include "namespace.h"
39 #include <assert.h>
40 #include <inttypes.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <locale.h>
45 
46 int
47 humanize_number(char *buf, size_t len, int64_t bytes,
48     const char *suffix, int scale, int flags)
49 {
50 	const char *prefixes, *sep;
51 	int	b, r, s1, s2, sign;
52 	int64_t	divisor, max, post = 1;
53 	size_t	i, baselen, maxscale;
54 
55 	_DIAGASSERT(buf != NULL);
56 	_DIAGASSERT(suffix != NULL);
57 	_DIAGASSERT(scale >= 0);
58 
59 	if (flags & HN_DIVISOR_1000) {
60 		/* SI for decimal multiplies */
61 		divisor = 1000;
62 		if (flags & HN_B)
63 			prefixes = "B\0k\0M\0G\0T\0P\0E";
64 		else
65 			prefixes = "\0\0k\0M\0G\0T\0P\0E";
66 	} else {
67 		/*
68 		 * binary multiplies
69 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
70 		 */
71 		divisor = 1024;
72 		if (flags & HN_B)
73 			prefixes = "B\0K\0M\0G\0T\0P\0E";
74 		else
75 			prefixes = "\0\0K\0M\0G\0T\0P\0E";
76 	}
77 
78 #define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
79 	maxscale = 7;
80 
81 	if ((size_t)scale >= maxscale &&
82 	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
83 		return (-1);
84 
85 	if (buf == NULL || suffix == NULL)
86 		return (-1);
87 
88 	if (len > 0)
89 		buf[0] = '\0';
90 	if (bytes < 0) {
91 		sign = -1;
92 		baselen = 3;		/* sign, digit, prefix */
93 		if (-bytes < INT64_MAX / 100)
94 			bytes *= -100;
95 		else {
96 			bytes = -bytes;
97 			post = 100;
98 			baselen += 2;
99 		}
100 	} else {
101 		sign = 1;
102 		baselen = 2;		/* digit, prefix */
103 		if (bytes < INT64_MAX / 100)
104 			bytes *= 100;
105 		else {
106 			post = 100;
107 			baselen += 2;
108 		}
109 	}
110 	if (flags & HN_NOSPACE)
111 		sep = "";
112 	else {
113 		sep = " ";
114 		baselen++;
115 	}
116 	baselen += strlen(suffix);
117 
118 	/* Check if enough room for `x y' + suffix + `\0' */
119 	if (len < baselen + 1)
120 		return (-1);
121 
122 	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
123 		/* See if there is additional columns can be used. */
124 		for (max = 100, i = len - baselen; i-- > 0;)
125 			max *= 10;
126 
127 		/*
128 		 * Divide the number until it fits the given column.
129 		 * If there will be an overflow by the rounding below,
130 		 * divide once more.
131 		 */
132 		for (i = 0; bytes >= max - 50 && i < maxscale; i++)
133 			bytes /= divisor;
134 
135 		if (scale & HN_GETSCALE) {
136 			_DIAGASSERT(__type_fit(int, i));
137 			return (int)i;
138 		}
139 	} else
140 		for (i = 0; i < (size_t)scale && i < maxscale; i++)
141 			bytes /= divisor;
142 	bytes *= post;
143 
144 	/* If a value <= 9.9 after rounding and ... */
145 	if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
146 		/* baselen + \0 + .N */
147 		if (len < baselen + 1 + 2)
148 			return (-1);
149 		b = ((int)bytes + 5) / 10;
150 		s1 = b / 10;
151 		s2 = b % 10;
152 		r = snprintf(buf, len, "%d%s%d%s%s%s",
153 		    sign * s1, localeconv()->decimal_point, s2,
154 		    sep, SCALE2PREFIX(i), suffix);
155 	} else
156 		r = snprintf(buf, len, "%" PRId64 "%s%s%s",
157 		    sign * ((bytes + 50) / 100),
158 		    sep, SCALE2PREFIX(i), suffix);
159 
160 	return (r);
161 }
162