Merge from vendor branch OPENSSH:
[dragonfly.git] / lib / libutil / humanize_number.c
1 /*      $NetBSD: humanize_number.c,v 1.8 2004/07/27 01:56:24 enami Exp $        */
2 /* $DragonFly: src/lib/libutil/humanize_number.c,v 1.2 2005/03/04 04:31:11 cpressey Exp $ */
3
4 /*
5  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the NetBSD
23  *      Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * $FreeBSD: /repoman/r/ncvs/src/lib/libutil/humanize_number.c,v 1.1.2.1 2004/09/28 18:24:10 pjd Exp $
41  */
42
43 #include <sys/cdefs.h>
44 #include <sys/types.h>
45
46 #include <assert.h>
47 #include <locale.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "libutil.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, *sep;
59         int     b, i, r, maxscale, s1, s2, sign;
60         int64_t divisor, max;
61         size_t  baselen;
62
63         assert(buf != NULL);
64         assert(suffix != NULL);
65         assert(scale >= 0);
66
67         if (flags & HN_DIVISOR_1000) {
68                 /* SI for decimal multiplies */
69                 divisor = 1000;
70                 if (flags & HN_B)
71                         prefixes = "B\0k\0M\0G\0T\0P\0E";
72                 else
73                         prefixes = "\0\0k\0M\0G\0T\0P\0E";
74         } else {
75                 /*
76                  * binary multiplies
77                  * XXX IEC 60027-2 recommends Ki, Mi, Gi...
78                  */
79                 divisor = 1024;
80                 if (flags & HN_B)
81                         prefixes = "B\0K\0M\0G\0T\0P\0E";
82                 else
83                         prefixes = "\0\0K\0M\0G\0T\0P\0E";
84         }
85
86 #define SCALE2PREFIX(scale)     (&prefixes[(scale) << 1])
87         maxscale = 7;
88
89         if (scale >= maxscale &&
90             (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
91                 return (-1);
92
93         if (buf == NULL || suffix == NULL)
94                 return (-1);
95
96         if (len > 0)
97                 buf[0] = '\0';
98         if (bytes < 0) {
99                 sign = -1;
100                 bytes *= -100;
101                 baselen = 3;            /* sign, digit, prefix */
102         } else {
103                 sign = 1;
104                 bytes *= 100;
105                 baselen = 2;            /* digit, prefix */
106         }
107         if (flags & HN_NOSPACE)
108                 sep = "";
109         else {
110                 sep = " ";
111                 baselen++;
112         }
113         baselen += strlen(suffix);
114
115         /* Check if enough room for `x y' + suffix + `\0' */
116         if (len < baselen + 1)
117                 return (-1);
118
119         if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
120                 /* See if there is additional columns can be used. */
121                 for (max = 100, i = len - baselen; i-- > 0;)
122                         max *= 10;
123
124                 for (i = 0; bytes >= max && i < maxscale; i++)
125                         bytes /= divisor;
126
127                 if (scale & HN_GETSCALE)
128                         return (i);
129         } else
130                 for (i = 0; i < scale && i < maxscale; i++)
131                         bytes /= divisor;
132
133         /* If a value <= 9.9 after rounding and ... */
134         if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
135                 /* baselen + \0 + .N */
136                 if (len < baselen + 1 + 2)
137                         return (-1);
138                 b = ((int)bytes + 5) / 10;
139                 s1 = b / 10;
140                 s2 = b % 10;
141                 r = snprintf(buf, len, "%d%s%d%s%s%s",
142                     sign * s1, localeconv()->decimal_point, s2,
143                     sep, SCALE2PREFIX(i), suffix);
144         } else
145                 r = snprintf(buf, len, "%lld%s%s%s",
146                     /* LONGLONG */
147                     (long long)(sign * ((bytes + 50) / 100)),
148                     sep, SCALE2PREFIX(i), suffix);
149
150         return (r);
151 }