xref: /netbsd-src/lib/libc/gen/humanize_number.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: humanize_number.c,v 1.5 2003/12/26 11:30:36 simonb 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 #ifndef __lint
42 __COPYRIGHT("@(#) Copyright (c) 2002\n\
43 	The NetBSD Foundation, inc. All rights reserved.\n");
44 __RCSID("$NetBSD: humanize_number.c,v 1.5 2003/12/26 11:30:36 simonb Exp $");
45 #endif /* !__lint */
46 
47 #include <assert.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <locale.h>
52 #include <util.h>
53 
54 int
55 humanize_number(char *buf, size_t len, int64_t bytes,
56     const char *suffix, int scale, int flags)
57 {
58 	const char *prefixes;
59 	int	i, r;
60 	int64_t	divisor, max, s1, s2, sign;
61 	size_t	baselen, suffixlen;
62 
63 	_DIAGASSERT(buf != NULL);
64 	_DIAGASSERT(suffix != NULL);
65 	_DIAGASSERT(scale >= 0);
66 
67 	if (flags & HN_DIVISOR_1000) {
68 		/* SI for decimal multiplies */
69 		divisor = 1000;
70 		prefixes = " kMGTPE";
71 	} else {
72 		/*
73 		 * binary multiplies
74 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
75 		 */
76 		divisor = 1024;
77 		prefixes = " KMGTPE";
78 	}
79 
80 	if ((size_t) scale >= strlen(prefixes) && scale != HN_AUTOSCALE &&
81 	    scale != HN_GETSCALE)
82 		return (-1);
83 
84 	if (buf == NULL || suffix == NULL)
85 		return (-1);
86 
87 	if (len > 0)
88 		buf[0] = '\0';
89 	if (bytes < 0) {
90 		sign = -1;
91 		bytes *= -100;
92 		baselen = 4;
93 	} else {
94 		sign = 1;
95 		bytes *= 100;
96 		baselen = 3;
97 	}
98 
99 	suffixlen = strlen(suffix);
100 
101 	/* check if enough room for `x y' + suffix + `\0' */
102 	if (len < baselen + suffixlen + 1)
103 		return (-1);
104 
105 	if (flags & HN_DIVISOR_1000)
106 		divisor = 1000;
107 	else
108 		divisor = 1024;
109 
110 	max = 100;
111 	for (i = 0;
112 	     (size_t) i < len - suffixlen - baselen + ((flags & HN_NOSPACE) ?
113 	     1 : 0); i++)
114 		max *= 10;
115 
116 	if ((scale & HN_AUTOSCALE) || (scale & HN_GETSCALE)) {
117 		for (i = 0; bytes >= max && prefixes[i + 1]; i++)
118 			bytes /= divisor;
119 	} else {
120 		for (i = 0; i < scale && prefixes[i + 1]; i++)
121 			bytes /= divisor;
122 	}
123 
124 	if (scale & HN_GETSCALE)
125 		return (i);
126 
127 	if (bytes < 1000 && flags & HN_DECIMAL) {
128 		if (len < (baselen + 2 + ((flags & HN_NOSPACE) || (i == 0 &&
129 		    !(flags & HN_B)) ? 0 : 1)))
130 			return (-1);
131 		s1 = bytes / 100;
132 		if ((s2 = (((bytes % 100) + 5) / 10)) == 10) {
133 			s1++;
134 			s2 = 0;
135 		}
136 		if (s1 < 10 && i == 0)
137 			/* Don't ever use .0 for a number less than 10. */
138 			r = snprintf(buf, len, "%lld%s%c%s",
139 			    /* LONGLONG */
140 			    (long long)(sign * s1),
141 			    (i == 0 && !(flags & HN_B)) || flags & HN_NOSPACE ?
142 			    "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
143 			    prefixes[i], suffix);
144 		else
145 			r = snprintf(buf, len, "%lld%s%lld%s%c%s",
146 			    /* LONGLONG */
147 			    (long long)(sign * s1),
148 			    localeconv()->decimal_point,
149 			    /* LONGLONG */
150 			    (long long)s2,
151 			    (i == 0 && !(flags & HN_B)) || flags & HN_NOSPACE ?
152 			    "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
153 			    prefixes[i], suffix);
154 
155 	} else
156 		r = snprintf(buf, len, "%lld%s%c%s",
157 		    /* LONGLONG */
158 		    (long long)(sign * ((bytes + 50) / 100)),
159 		    i == 0 || flags & HN_NOSPACE ? "" : " ", (i == 0 &&
160 		    (flags & HN_B)) ? 'B' : prefixes[i], suffix);
161 
162 	return (r);
163 }
164