1 0d6fb46a 2022-01-09 op /* $OpenBSD: fmt_scaled.c,v 1.19 2020/10/12 22:08:34 deraadt Exp $ */
4 0d6fb46a 2022-01-09 op * Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
6 0d6fb46a 2022-01-09 op * Redistribution and use in source and binary forms, with or without
7 0d6fb46a 2022-01-09 op * modification, are permitted provided that the following conditions
9 0d6fb46a 2022-01-09 op * 1. Redistributions of source code must retain the above copyright
10 0d6fb46a 2022-01-09 op * notice, this list of conditions and the following disclaimer.
11 0d6fb46a 2022-01-09 op * 2. Redistributions in binary form must reproduce the above copyright
12 0d6fb46a 2022-01-09 op * notice, this list of conditions and the following disclaimer in the
13 0d6fb46a 2022-01-09 op * documentation and/or other materials provided with the distribution.
14 0d6fb46a 2022-01-09 op * 3. The name of the author may not be used to endorse or promote products
15 0d6fb46a 2022-01-09 op * derived from this software without specific prior written permission.
17 0d6fb46a 2022-01-09 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 0d6fb46a 2022-01-09 op * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 0d6fb46a 2022-01-09 op * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 0d6fb46a 2022-01-09 op * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 0d6fb46a 2022-01-09 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 0d6fb46a 2022-01-09 op * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 0d6fb46a 2022-01-09 op * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 0d6fb46a 2022-01-09 op * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 0d6fb46a 2022-01-09 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 0d6fb46a 2022-01-09 op * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 0d6fb46a 2022-01-09 op * fmt_scaled: Format numbers scaled for human comprehension
31 0d6fb46a 2022-01-09 op * scan_scaled: Scan numbers in this format.
33 0d6fb46a 2022-01-09 op * "Human-readable" output uses 4 digits max, and puts a unit suffix at
34 0d6fb46a 2022-01-09 op * the end. Makes output compact and easy-to-read esp. on huge disks.
35 0d6fb46a 2022-01-09 op * Formatting code was originally in OpenBSD "df", converted to library routine.
36 0d6fb46a 2022-01-09 op * Scanning code written for OpenBSD libutil.
39 0d6fb46a 2022-01-09 op #include "compat.h"
41 0d6fb46a 2022-01-09 op #include <stdio.h>
42 0d6fb46a 2022-01-09 op #include <stdlib.h>
43 0d6fb46a 2022-01-09 op #include <errno.h>
44 0d6fb46a 2022-01-09 op #include <string.h>
45 0d6fb46a 2022-01-09 op #include <ctype.h>
46 0d6fb46a 2022-01-09 op #include <limits.h>
48 0d6fb46a 2022-01-09 op typedef enum {
49 0d6fb46a 2022-01-09 op NONE = 0, KILO = 1, MEGA = 2, GIGA = 3, TERA = 4, PETA = 5, EXA = 6
52 0d6fb46a 2022-01-09 op /* These three arrays MUST be in sync! XXX make a struct */
53 0d6fb46a 2022-01-09 op static const unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
54 0d6fb46a 2022-01-09 op static const char scale_chars[] = "BKMGTPE";
55 0d6fb46a 2022-01-09 op static const long long scale_factors[] = {
59 0d6fb46a 2022-01-09 op 1024LL*1024*1024,
60 0d6fb46a 2022-01-09 op 1024LL*1024*1024*1024,
61 0d6fb46a 2022-01-09 op 1024LL*1024*1024*1024*1024,
62 0d6fb46a 2022-01-09 op 1024LL*1024*1024*1024*1024*1024,
64 0d6fb46a 2022-01-09 op #define SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
66 0d6fb46a 2022-01-09 op #define MAX_DIGITS (SCALE_LENGTH * 3) /* XXX strlen(sprintf("%lld", -1)? */
68 0d6fb46a 2022-01-09 op /* Format the given "number" into human-readable form in "result".
69 0d6fb46a 2022-01-09 op * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
70 0d6fb46a 2022-01-09 op * Return 0 on success, -1 and errno set if error.
73 0d6fb46a 2022-01-09 op fmt_scaled(long long number, char *result)
75 0d6fb46a 2022-01-09 op long long abval, fract = 0;
76 0d6fb46a 2022-01-09 op unsigned int i;
77 0d6fb46a 2022-01-09 op unit_type unit = NONE;
79 0d6fb46a 2022-01-09 op /* Not every negative long long has a positive representation. */
80 0d6fb46a 2022-01-09 op if (number == LLONG_MIN) {
81 0d6fb46a 2022-01-09 op errno = ERANGE;
85 0d6fb46a 2022-01-09 op abval = llabs(number);
87 0d6fb46a 2022-01-09 op /* Also check for numbers that are just too darned big to format. */
88 0d6fb46a 2022-01-09 op if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
89 0d6fb46a 2022-01-09 op errno = ERANGE;
93 0d6fb46a 2022-01-09 op /* scale whole part; get unscaled fraction */
94 0d6fb46a 2022-01-09 op for (i = 0; i < SCALE_LENGTH; i++) {
95 0d6fb46a 2022-01-09 op if (abval/1024 < scale_factors[i]) {
96 0d6fb46a 2022-01-09 op unit = units[i];
97 0d6fb46a 2022-01-09 op fract = (i == 0) ? 0 : abval % scale_factors[i];
98 0d6fb46a 2022-01-09 op number /= scale_factors[i];
100 0d6fb46a 2022-01-09 op fract /= scale_factors[i - 1];
105 0d6fb46a 2022-01-09 op fract = (10 * fract + 512) / 1024;
106 0d6fb46a 2022-01-09 op /* if the result would be >= 10, round main number */
107 0d6fb46a 2022-01-09 op if (fract >= 10) {
108 0d6fb46a 2022-01-09 op if (number >= 0)
113 0d6fb46a 2022-01-09 op } else if (fract < 0) {
114 0d6fb46a 2022-01-09 op /* shouldn't happen */
118 0d6fb46a 2022-01-09 op if (number == 0)
119 0d6fb46a 2022-01-09 op strlcpy(result, "0B", FMT_SCALED_STRSIZE);
120 0d6fb46a 2022-01-09 op else if (unit == NONE || number >= 100 || number <= -100) {
121 0d6fb46a 2022-01-09 op if (fract >= 5) {
122 0d6fb46a 2022-01-09 op if (number >= 0)
127 0d6fb46a 2022-01-09 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
128 0d6fb46a 2022-01-09 op number, scale_chars[unit]);
130 0d6fb46a 2022-01-09 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
131 0d6fb46a 2022-01-09 op number, fract, scale_chars[unit]);
138 0d6fb46a 2022-01-09 op * This is the original version of the program in the man page.
139 0d6fb46a 2022-01-09 op * Copy-and-paste whatever you need from it.
142 0d6fb46a 2022-01-09 op main(int argc, char **argv)
144 0d6fb46a 2022-01-09 op char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE];
145 0d6fb46a 2022-01-09 op long long ninput = 10483892, result;
147 0d6fb46a 2022-01-09 op if (scan_scaled(cinput, &result) == 0)
148 0d6fb46a 2022-01-09 op printf("\"%s\" -> %lld\n", cinput, result);
150 0d6fb46a 2022-01-09 op perror(cinput);
152 0d6fb46a 2022-01-09 op if (fmt_scaled(ninput, buf) == 0)
153 0d6fb46a 2022-01-09 op printf("%lld -> \"%s\"\n", ninput, buf);
155 0d6fb46a 2022-01-09 op fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));