xref: /netbsd-src/lib/libc/gen/humanize_number.c (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: humanize_number.c,v 1.3 2002/09/27 18:37:45 drochner 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.3 2002/09/27 18:37:45 drochner 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 
66 	if (flags & HN_DIVISOR_1000) {
67 		/* SI for decimal multiplies */
68 		divisor = 1000;
69 		prefixes = " kMGTPE";
70 	} else {
71 		/*
72 		 * binary multiplies
73 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
74 		 */
75 		divisor = 1024;
76 		prefixes = " KMGTPE";
77 	}
78 
79 	if (scale >= strlen(prefixes) && scale != HN_AUTOSCALE &&
80 	    scale != HN_GETSCALE)
81 		return (-1);
82 
83 	if (buf == NULL || suffix == NULL)
84 		return (-1);
85 
86 	if (len > 0)
87 		buf[0] = '\0';
88 	if (bytes < 0) {
89 		sign = -1;
90 		bytes *= -100;
91 		baselen = 4;
92 	} else {
93 		sign = 1;
94 		bytes *= 100;
95 		baselen = 3;
96 	}
97 
98 	suffixlen = strlen(suffix);
99 
100 	/* check if enough room for `x y' + suffix + `\0' */
101 	if (len < baselen + suffixlen + 1)
102 		return (-1);
103 
104 	if (flags & HN_DIVISOR_1000)
105 		divisor = 1000;
106 	else
107 		divisor = 1024;
108 
109 	max = 100;
110 	for (i = 0; i < len - suffixlen - baselen + ((flags & HN_NOSPACE) ?
111 	    1 : 0); i++)
112 		max *= 10;
113 
114 	if ((scale & HN_AUTOSCALE) || (scale & HN_GETSCALE)) {
115 		for (i = 0; bytes >= max && prefixes[i + 1]; i++)
116 			bytes /= divisor;
117 	} else {
118 		for (i = 0; i < scale && prefixes[i + 1]; i++)
119 			bytes /= divisor;
120 	}
121 
122 	if (scale & HN_GETSCALE)
123 		return (i);
124 
125 	if (bytes < 1000 && flags & HN_DECIMAL) {
126 		if (len < (baselen + 2 + ((flags & HN_NOSPACE) || (i == 0 &&
127 		    !(flags & HN_B)) ? 0 : 1)))
128 			return (-1);
129 		s1 = bytes / 100;
130 		if ((s2 = (((bytes % 100) + 5) / 10)) == 10) {
131 			s1++;
132 			s2 = 0;
133 		}
134 		r = snprintf(buf, len, "%lld%s%lld%s%c%s",
135 		    /* LONGLONG */
136 		    (long long)(sign * s1),
137 		    localeconv()->decimal_point,
138 		    /* LONGLONG */
139 		    (long long)s2,
140 		    (i == 0 && !(flags & HN_B)) || flags & HN_NOSPACE ?
141 		    "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
142 		    prefixes[i], suffix);
143 
144 	} else
145 		r = snprintf(buf, len, "%lld%s%c%s",
146 		    /* LONGLONG */
147 		    (long long)(sign * ((bytes + 50) / 100)),
148 		    i == 0 || flags & HN_NOSPACE ? "" : " ", (i == 0 &&
149 		    (flags & HN_B)) ? 'B' : prefixes[i], suffix);
150 
151 	return (r);
152 }
153