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