xref: /netbsd-src/lib/libc/gen/humanize_number.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: humanize_number.c,v 1.10 2005/09/13 01:44:09 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  * 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 #if defined(LIBC_SCCS) && !defined(lint)
42 __RCSID("$NetBSD: humanize_number.c,v 1.10 2005/09/13 01:44:09 christos Exp $");
43 #endif /* LIBC_SCCS and not lint */
44 
45 #include "namespace.h"
46 #include <assert.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <locale.h>
51 #include <util.h>
52 
53 int
54 humanize_number(char *buf, size_t len, int64_t bytes,
55     const char *suffix, int scale, int flags)
56 {
57 	const char *prefixes, *sep;
58 	int	b, i, r, maxscale, s1, s2, sign;
59 	int64_t	divisor, max;
60 	size_t	baselen;
61 
62 	_DIAGASSERT(buf != NULL);
63 	_DIAGASSERT(suffix != NULL);
64 	_DIAGASSERT(scale >= 0);
65 
66 	if (flags & HN_DIVISOR_1000) {
67 		/* SI for decimal multiplies */
68 		divisor = 1000;
69 		if (flags & HN_B)
70 			prefixes = "B\0k\0M\0G\0T\0P\0E";
71 		else
72 			prefixes = "\0\0k\0M\0G\0T\0P\0E";
73 	} else {
74 		/*
75 		 * binary multiplies
76 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
77 		 */
78 		divisor = 1024;
79 		if (flags & HN_B)
80 			prefixes = "B\0K\0M\0G\0T\0P\0E";
81 		else
82 			prefixes = "\0\0K\0M\0G\0T\0P\0E";
83 	}
84 
85 #define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
86 	maxscale = 7;
87 
88 	if (scale >= maxscale &&
89 	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
90 		return (-1);
91 
92 	if (buf == NULL || suffix == NULL)
93 		return (-1);
94 
95 	if (len > 0)
96 		buf[0] = '\0';
97 	if (bytes < 0) {
98 		sign = -1;
99 		bytes *= -100;
100 		baselen = 3;		/* sign, digit, prefix */
101 	} else {
102 		sign = 1;
103 		bytes *= 100;
104 		baselen = 2;		/* digit, prefix */
105 	}
106 	if (flags & HN_NOSPACE)
107 		sep = "";
108 	else {
109 		sep = " ";
110 		baselen++;
111 	}
112 	baselen += strlen(suffix);
113 
114 	/* Check if enough room for `x y' + suffix + `\0' */
115 	if (len < baselen + 1)
116 		return (-1);
117 
118 	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
119 		/* See if there is additional columns can be used. */
120 		for (max = 100, i = len - baselen; i-- > 0;)
121 			max *= 10;
122 
123 		for (i = 0; bytes >= max && i < maxscale; i++)
124 			bytes /= divisor;
125 
126 		if (scale & HN_GETSCALE)
127 			return (i);
128 	} else
129 		for (i = 0; i < scale && i < maxscale; i++)
130 			bytes /= divisor;
131 
132 	/* If a value <= 9.9 after rounding and ... */
133 	if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
134 		/* baselen + \0 + .N */
135 		if (len < baselen + 1 + 2)
136 			return (-1);
137 		b = ((int)bytes + 5) / 10;
138 		s1 = b / 10;
139 		s2 = b % 10;
140 		r = snprintf(buf, len, "%d%s%d%s%s%s",
141 		    sign * s1, localeconv()->decimal_point, s2,
142 		    sep, SCALE2PREFIX(i), suffix);
143 	} else
144 		r = snprintf(buf, len, "%lld%s%s%s",
145 		    /* LONGLONG */
146 		    (long long)(sign * ((bytes + 50) / 100)),
147 		    sep, SCALE2PREFIX(i), suffix);
148 
149 	return (r);
150 }
151