1 0677399e 2022-03-19 op /* $OpenBSD: fmt_scaled.c,v 1.22 2022/03/11 09:04:59 dtucker Exp $ */
4 0e1eff5b 2021-06-23 op * Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
6 0e1eff5b 2021-06-23 op * Redistribution and use in source and binary forms, with or without
7 0e1eff5b 2021-06-23 op * modification, are permitted provided that the following conditions
9 0e1eff5b 2021-06-23 op * 1. Redistributions of source code must retain the above copyright
10 0e1eff5b 2021-06-23 op * notice, this list of conditions and the following disclaimer.
11 0e1eff5b 2021-06-23 op * 2. Redistributions in binary form must reproduce the above copyright
12 0e1eff5b 2021-06-23 op * notice, this list of conditions and the following disclaimer in the
13 0e1eff5b 2021-06-23 op * documentation and/or other materials provided with the distribution.
14 0e1eff5b 2021-06-23 op * 3. The name of the author may not be used to endorse or promote products
15 0e1eff5b 2021-06-23 op * derived from this software without specific prior written permission.
17 0e1eff5b 2021-06-23 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 0e1eff5b 2021-06-23 op * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 0e1eff5b 2021-06-23 op * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 0e1eff5b 2021-06-23 op * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 0e1eff5b 2021-06-23 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 0e1eff5b 2021-06-23 op * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 0e1eff5b 2021-06-23 op * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 0e1eff5b 2021-06-23 op * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 0e1eff5b 2021-06-23 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 0e1eff5b 2021-06-23 op * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 0e1eff5b 2021-06-23 op * fmt_scaled: Format numbers scaled for human comprehension
31 0e1eff5b 2021-06-23 op * scan_scaled: Scan numbers in this format.
33 0e1eff5b 2021-06-23 op * "Human-readable" output uses 4 digits max, and puts a unit suffix at
34 0e1eff5b 2021-06-23 op * the end. Makes output compact and easy-to-read esp. on huge disks.
35 0e1eff5b 2021-06-23 op * Formatting code was originally in OpenBSD "df", converted to library routine.
36 0e1eff5b 2021-06-23 op * Scanning code written for OpenBSD libutil.
39 0e1eff5b 2021-06-23 op #include "compat.h"
41 0e1eff5b 2021-06-23 op #include <stdio.h>
42 0e1eff5b 2021-06-23 op #include <stdlib.h>
43 0e1eff5b 2021-06-23 op #include <errno.h>
44 0e1eff5b 2021-06-23 op #include <string.h>
45 0e1eff5b 2021-06-23 op #include <ctype.h>
46 0e1eff5b 2021-06-23 op #include <limits.h>
48 0e1eff5b 2021-06-23 op typedef enum {
49 0e1eff5b 2021-06-23 op NONE = 0, KILO = 1, MEGA = 2, GIGA = 3, TERA = 4, PETA = 5, EXA = 6
52 0e1eff5b 2021-06-23 op /* These three arrays MUST be in sync! XXX make a struct */
53 0e1eff5b 2021-06-23 op static const unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
54 0e1eff5b 2021-06-23 op static const char scale_chars[] = "BKMGTPE";
55 0e1eff5b 2021-06-23 op static const long long scale_factors[] = {
59 0e1eff5b 2021-06-23 op 1024LL*1024*1024,
60 0e1eff5b 2021-06-23 op 1024LL*1024*1024*1024,
61 0e1eff5b 2021-06-23 op 1024LL*1024*1024*1024*1024,
62 0e1eff5b 2021-06-23 op 1024LL*1024*1024*1024*1024*1024,
64 0e1eff5b 2021-06-23 op #define SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
66 0e1eff5b 2021-06-23 op #define MAX_DIGITS (SCALE_LENGTH * 3) /* XXX strlen(sprintf("%lld", -1)? */
68 0677399e 2022-03-19 op /* Convert the given input string "scaled" into numeric in "result".
69 0677399e 2022-03-19 op * Return 0 on success, -1 and errno set on error.
72 0677399e 2022-03-19 op scan_scaled(char *scaled, long long *result)
74 0677399e 2022-03-19 op char *p = scaled;
76 0677399e 2022-03-19 op unsigned int i, ndigits = 0, fract_digits = 0;
77 0677399e 2022-03-19 op long long scale_fact = 1, whole = 0, fpart = 0;
79 0677399e 2022-03-19 op /* Skip leading whitespace */
80 0677399e 2022-03-19 op while (isascii((unsigned char)*p) && isspace((unsigned char)*p))
83 0677399e 2022-03-19 op /* Then at most one leading + or - */
84 0677399e 2022-03-19 op while (*p == '-' || *p == '+') {
85 0677399e 2022-03-19 op if (*p == '-') {
87 0677399e 2022-03-19 op errno = EINVAL;
92 0677399e 2022-03-19 op } else if (*p == '+') {
94 0677399e 2022-03-19 op errno = EINVAL;
102 0677399e 2022-03-19 op /* Main loop: Scan digits, find decimal point, if present.
103 0677399e 2022-03-19 op * We don't allow exponentials, so no scientific notation
104 0677399e 2022-03-19 op * (but note that E for Exa might look like e to some!).
105 0677399e 2022-03-19 op * Advance 'p' to end, to get scale factor.
107 0677399e 2022-03-19 op for (; isascii((unsigned char)*p) &&
108 0677399e 2022-03-19 op (isdigit((unsigned char)*p) || *p=='.'); ++p) {
109 0677399e 2022-03-19 op if (*p == '.') {
110 0677399e 2022-03-19 op if (fract_digits > 0) { /* oops, more than one '.' */
111 0677399e 2022-03-19 op errno = EINVAL;
114 0677399e 2022-03-19 op fract_digits = 1;
118 0677399e 2022-03-19 op i = (*p) - '0'; /* whew! finally a digit we can use */
119 0677399e 2022-03-19 op if (fract_digits > 0) {
120 0677399e 2022-03-19 op if (fract_digits >= MAX_DIGITS-1)
121 0677399e 2022-03-19 op /* ignore extra fractional digits */
123 0677399e 2022-03-19 op fract_digits++; /* for later scaling */
124 0677399e 2022-03-19 op if (fpart > LLONG_MAX / 10) {
125 0677399e 2022-03-19 op errno = ERANGE;
129 0677399e 2022-03-19 op if (i > LLONG_MAX - fpart) {
130 0677399e 2022-03-19 op errno = ERANGE;
134 0677399e 2022-03-19 op } else { /* normal digit */
135 0677399e 2022-03-19 op if (++ndigits >= MAX_DIGITS) {
136 0677399e 2022-03-19 op errno = ERANGE;
139 0677399e 2022-03-19 op if (whole > LLONG_MAX / 10) {
140 0677399e 2022-03-19 op errno = ERANGE;
144 0677399e 2022-03-19 op if (i > LLONG_MAX - whole) {
145 0677399e 2022-03-19 op errno = ERANGE;
153 0677399e 2022-03-19 op whole *= sign;
155 0677399e 2022-03-19 op /* If no scale factor given, we're done. fraction is discarded. */
157 0677399e 2022-03-19 op *result = whole;
161 0677399e 2022-03-19 op /* Validate scale factor, and scale whole and fraction by it. */
162 0677399e 2022-03-19 op for (i = 0; i < SCALE_LENGTH; i++) {
164 0677399e 2022-03-19 op /* Are we there yet? */
165 0677399e 2022-03-19 op if (*p == scale_chars[i] ||
166 0677399e 2022-03-19 op *p == tolower((unsigned char)scale_chars[i])) {
168 0677399e 2022-03-19 op /* If it ends with alphanumerics after the scale char, bad. */
169 0677399e 2022-03-19 op if (isalnum((unsigned char)*(p+1))) {
170 0677399e 2022-03-19 op errno = EINVAL;
173 0677399e 2022-03-19 op scale_fact = scale_factors[i];
175 0677399e 2022-03-19 op /* check for overflow and underflow after scaling */
176 0677399e 2022-03-19 op if (whole > LLONG_MAX / scale_fact ||
177 0677399e 2022-03-19 op whole < LLONG_MIN / scale_fact) {
178 0677399e 2022-03-19 op errno = ERANGE;
182 0677399e 2022-03-19 op /* scale whole part */
183 0677399e 2022-03-19 op whole *= scale_fact;
185 0677399e 2022-03-19 op /* truncate fpart so it does't overflow.
186 0677399e 2022-03-19 op * then scale fractional part.
188 0677399e 2022-03-19 op while (fpart >= LLONG_MAX / scale_fact) {
190 0677399e 2022-03-19 op fract_digits--;
192 0677399e 2022-03-19 op fpart *= scale_fact;
193 0677399e 2022-03-19 op if (fract_digits > 0) {
194 0677399e 2022-03-19 op for (i = 0; i < fract_digits -1; i++)
197 0677399e 2022-03-19 op if (sign == -1)
198 0677399e 2022-03-19 op whole -= fpart;
200 0677399e 2022-03-19 op whole += fpart;
201 0677399e 2022-03-19 op *result = whole;
206 0677399e 2022-03-19 op /* Invalid unit or character */
207 0677399e 2022-03-19 op errno = EINVAL;
211 0e1eff5b 2021-06-23 op /* Format the given "number" into human-readable form in "result".
212 0e1eff5b 2021-06-23 op * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
213 0e1eff5b 2021-06-23 op * Return 0 on success, -1 and errno set if error.
216 0e1eff5b 2021-06-23 op fmt_scaled(long long number, char *result)
218 0e1eff5b 2021-06-23 op long long abval, fract = 0;
219 0e1eff5b 2021-06-23 op unsigned int i;
220 0e1eff5b 2021-06-23 op unit_type unit = NONE;
222 0e1eff5b 2021-06-23 op /* Not every negative long long has a positive representation. */
223 0e1eff5b 2021-06-23 op if (number == LLONG_MIN) {
224 0e1eff5b 2021-06-23 op errno = ERANGE;
228 0e1eff5b 2021-06-23 op abval = llabs(number);
230 0e1eff5b 2021-06-23 op /* Also check for numbers that are just too darned big to format. */
231 0e1eff5b 2021-06-23 op if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
232 0e1eff5b 2021-06-23 op errno = ERANGE;
236 0e1eff5b 2021-06-23 op /* scale whole part; get unscaled fraction */
237 0e1eff5b 2021-06-23 op for (i = 0; i < SCALE_LENGTH; i++) {
238 0e1eff5b 2021-06-23 op if (abval/1024 < scale_factors[i]) {
239 0e1eff5b 2021-06-23 op unit = units[i];
240 0e1eff5b 2021-06-23 op fract = (i == 0) ? 0 : abval % scale_factors[i];
241 0e1eff5b 2021-06-23 op number /= scale_factors[i];
243 0e1eff5b 2021-06-23 op fract /= scale_factors[i - 1];
248 0e1eff5b 2021-06-23 op fract = (10 * fract + 512) / 1024;
249 0e1eff5b 2021-06-23 op /* if the result would be >= 10, round main number */
250 0e1eff5b 2021-06-23 op if (fract >= 10) {
251 0e1eff5b 2021-06-23 op if (number >= 0)
256 0e1eff5b 2021-06-23 op } else if (fract < 0) {
257 0e1eff5b 2021-06-23 op /* shouldn't happen */
261 0e1eff5b 2021-06-23 op if (number == 0)
262 0e1eff5b 2021-06-23 op strlcpy(result, "0B", FMT_SCALED_STRSIZE);
263 0e1eff5b 2021-06-23 op else if (unit == NONE || number >= 100 || number <= -100) {
264 0e1eff5b 2021-06-23 op if (fract >= 5) {
265 0e1eff5b 2021-06-23 op if (number >= 0)
270 0e1eff5b 2021-06-23 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
271 0e1eff5b 2021-06-23 op number, scale_chars[unit]);
273 0e1eff5b 2021-06-23 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
274 0e1eff5b 2021-06-23 op number, fract, scale_chars[unit]);
281 0e1eff5b 2021-06-23 op * This is the original version of the program in the man page.
282 0e1eff5b 2021-06-23 op * Copy-and-paste whatever you need from it.
285 0e1eff5b 2021-06-23 op main(int argc, char **argv)
287 0e1eff5b 2021-06-23 op char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE];
288 0e1eff5b 2021-06-23 op long long ninput = 10483892, result;
290 0e1eff5b 2021-06-23 op if (scan_scaled(cinput, &result) == 0)
291 0e1eff5b 2021-06-23 op printf("\"%s\" -> %lld\n", cinput, result);
293 0e1eff5b 2021-06-23 op perror(cinput);
295 0e1eff5b 2021-06-23 op if (fmt_scaled(ninput, buf) == 0)
296 0e1eff5b 2021-06-23 op printf("%lld -> \"%s\"\n", ninput, buf);
298 0e1eff5b 2021-06-23 op fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));