1 0e1eff5b 2021-06-23 op /* $OpenBSD: fmt_scaled.c,v 1.19 2020/10/12 22:08:34 deraadt 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 0e1eff5b 2021-06-23 op /* Format the given "number" into human-readable form in "result".
69 0e1eff5b 2021-06-23 op * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
70 0e1eff5b 2021-06-23 op * Return 0 on success, -1 and errno set if error.
73 0e1eff5b 2021-06-23 op fmt_scaled(long long number, char *result)
75 0e1eff5b 2021-06-23 op long long abval, fract = 0;
76 0e1eff5b 2021-06-23 op unsigned int i;
77 0e1eff5b 2021-06-23 op unit_type unit = NONE;
79 0e1eff5b 2021-06-23 op /* Not every negative long long has a positive representation. */
80 0e1eff5b 2021-06-23 op if (number == LLONG_MIN) {
81 0e1eff5b 2021-06-23 op errno = ERANGE;
85 0e1eff5b 2021-06-23 op abval = llabs(number);
87 0e1eff5b 2021-06-23 op /* Also check for numbers that are just too darned big to format. */
88 0e1eff5b 2021-06-23 op if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
89 0e1eff5b 2021-06-23 op errno = ERANGE;
93 0e1eff5b 2021-06-23 op /* scale whole part; get unscaled fraction */
94 0e1eff5b 2021-06-23 op for (i = 0; i < SCALE_LENGTH; i++) {
95 0e1eff5b 2021-06-23 op if (abval/1024 < scale_factors[i]) {
96 0e1eff5b 2021-06-23 op unit = units[i];
97 0e1eff5b 2021-06-23 op fract = (i == 0) ? 0 : abval % scale_factors[i];
98 0e1eff5b 2021-06-23 op number /= scale_factors[i];
100 0e1eff5b 2021-06-23 op fract /= scale_factors[i - 1];
105 0e1eff5b 2021-06-23 op fract = (10 * fract + 512) / 1024;
106 0e1eff5b 2021-06-23 op /* if the result would be >= 10, round main number */
107 0e1eff5b 2021-06-23 op if (fract >= 10) {
108 0e1eff5b 2021-06-23 op if (number >= 0)
113 0e1eff5b 2021-06-23 op } else if (fract < 0) {
114 0e1eff5b 2021-06-23 op /* shouldn't happen */
118 0e1eff5b 2021-06-23 op if (number == 0)
119 0e1eff5b 2021-06-23 op strlcpy(result, "0B", FMT_SCALED_STRSIZE);
120 0e1eff5b 2021-06-23 op else if (unit == NONE || number >= 100 || number <= -100) {
121 0e1eff5b 2021-06-23 op if (fract >= 5) {
122 0e1eff5b 2021-06-23 op if (number >= 0)
127 0e1eff5b 2021-06-23 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
128 0e1eff5b 2021-06-23 op number, scale_chars[unit]);
130 0e1eff5b 2021-06-23 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
131 0e1eff5b 2021-06-23 op number, fract, scale_chars[unit]);
138 0e1eff5b 2021-06-23 op * This is the original version of the program in the man page.
139 0e1eff5b 2021-06-23 op * Copy-and-paste whatever you need from it.
142 0e1eff5b 2021-06-23 op main(int argc, char **argv)
144 0e1eff5b 2021-06-23 op char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE];
145 0e1eff5b 2021-06-23 op long long ninput = 10483892, result;
147 0e1eff5b 2021-06-23 op if (scan_scaled(cinput, &result) == 0)
148 0e1eff5b 2021-06-23 op printf("\"%s\" -> %lld\n", cinput, result);
150 0e1eff5b 2021-06-23 op perror(cinput);
152 0e1eff5b 2021-06-23 op if (fmt_scaled(ninput, buf) == 0)
153 0e1eff5b 2021-06-23 op printf("%lld -> \"%s\"\n", ninput, buf);
155 0e1eff5b 2021-06-23 op fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));