Blame


1 0d6fb46a 2022-01-09 op /* $OpenBSD: fmt_scaled.c,v 1.19 2020/10/12 22:08:34 deraadt Exp $ */
2 0d6fb46a 2022-01-09 op
3 0d6fb46a 2022-01-09 op /*
4 0d6fb46a 2022-01-09 op * Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
5 0d6fb46a 2022-01-09 op *
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
8 0d6fb46a 2022-01-09 op * are met:
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.
16 0d6fb46a 2022-01-09 op *
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.
27 0d6fb46a 2022-01-09 op */
28 0d6fb46a 2022-01-09 op
29 0d6fb46a 2022-01-09 op /*
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.
32 0d6fb46a 2022-01-09 op *
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.
37 0d6fb46a 2022-01-09 op */
38 0d6fb46a 2022-01-09 op
39 0d6fb46a 2022-01-09 op #include "compat.h"
40 0d6fb46a 2022-01-09 op
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>
47 0d6fb46a 2022-01-09 op
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
50 0d6fb46a 2022-01-09 op } unit_type;
51 0d6fb46a 2022-01-09 op
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[] = {
56 0d6fb46a 2022-01-09 op 1LL,
57 0d6fb46a 2022-01-09 op 1024LL,
58 0d6fb46a 2022-01-09 op 1024LL*1024,
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,
63 0d6fb46a 2022-01-09 op };
64 0d6fb46a 2022-01-09 op #define SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
65 0d6fb46a 2022-01-09 op
66 0d6fb46a 2022-01-09 op #define MAX_DIGITS (SCALE_LENGTH * 3) /* XXX strlen(sprintf("%lld", -1)? */
67 0d6fb46a 2022-01-09 op
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.
71 0d6fb46a 2022-01-09 op */
72 0d6fb46a 2022-01-09 op int
73 0d6fb46a 2022-01-09 op fmt_scaled(long long number, char *result)
74 0d6fb46a 2022-01-09 op {
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;
78 0d6fb46a 2022-01-09 op
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;
82 0d6fb46a 2022-01-09 op return -1;
83 0d6fb46a 2022-01-09 op }
84 0d6fb46a 2022-01-09 op
85 0d6fb46a 2022-01-09 op abval = llabs(number);
86 0d6fb46a 2022-01-09 op
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;
90 0d6fb46a 2022-01-09 op return -1;
91 0d6fb46a 2022-01-09 op }
92 0d6fb46a 2022-01-09 op
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];
99 0d6fb46a 2022-01-09 op if (i > 0)
100 0d6fb46a 2022-01-09 op fract /= scale_factors[i - 1];
101 0d6fb46a 2022-01-09 op break;
102 0d6fb46a 2022-01-09 op }
103 0d6fb46a 2022-01-09 op }
104 0d6fb46a 2022-01-09 op
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)
109 0d6fb46a 2022-01-09 op number++;
110 0d6fb46a 2022-01-09 op else
111 0d6fb46a 2022-01-09 op number--;
112 0d6fb46a 2022-01-09 op fract = 0;
113 0d6fb46a 2022-01-09 op } else if (fract < 0) {
114 0d6fb46a 2022-01-09 op /* shouldn't happen */
115 0d6fb46a 2022-01-09 op fract = 0;
116 0d6fb46a 2022-01-09 op }
117 0d6fb46a 2022-01-09 op
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)
123 0d6fb46a 2022-01-09 op number++;
124 0d6fb46a 2022-01-09 op else
125 0d6fb46a 2022-01-09 op number--;
126 0d6fb46a 2022-01-09 op }
127 0d6fb46a 2022-01-09 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
128 0d6fb46a 2022-01-09 op number, scale_chars[unit]);
129 0d6fb46a 2022-01-09 op } else
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]);
132 0d6fb46a 2022-01-09 op
133 0d6fb46a 2022-01-09 op return 0;
134 0d6fb46a 2022-01-09 op }
135 0d6fb46a 2022-01-09 op
136 0d6fb46a 2022-01-09 op #ifdef MAIN
137 0d6fb46a 2022-01-09 op /*
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.
140 0d6fb46a 2022-01-09 op */
141 0d6fb46a 2022-01-09 op int
142 0d6fb46a 2022-01-09 op main(int argc, char **argv)
143 0d6fb46a 2022-01-09 op {
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;
146 0d6fb46a 2022-01-09 op
147 0d6fb46a 2022-01-09 op if (scan_scaled(cinput, &result) == 0)
148 0d6fb46a 2022-01-09 op printf("\"%s\" -> %lld\n", cinput, result);
149 0d6fb46a 2022-01-09 op else
150 0d6fb46a 2022-01-09 op perror(cinput);
151 0d6fb46a 2022-01-09 op
152 0d6fb46a 2022-01-09 op if (fmt_scaled(ninput, buf) == 0)
153 0d6fb46a 2022-01-09 op printf("%lld -> \"%s\"\n", ninput, buf);
154 0d6fb46a 2022-01-09 op else
155 0d6fb46a 2022-01-09 op fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));
156 0d6fb46a 2022-01-09 op
157 0d6fb46a 2022-01-09 op return 0;
158 0d6fb46a 2022-01-09 op }
159 0d6fb46a 2022-01-09 op #endif