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 /* Convert the given input string "scaled" into numeric in "result".
69 0e1eff5b 2021-06-23 op * Return 0 on success, -1 and errno set on error.
72 0e1eff5b 2021-06-23 op scan_scaled(char *scaled, long long *result)
74 0e1eff5b 2021-06-23 op char *p = scaled;
76 0e1eff5b 2021-06-23 op unsigned int i, ndigits = 0, fract_digits = 0;
77 0e1eff5b 2021-06-23 op long long scale_fact = 1, whole = 0, fpart = 0;
79 0e1eff5b 2021-06-23 op /* Skip leading whitespace */
80 0e1eff5b 2021-06-23 op while (isascii((unsigned char)*p) && isspace((unsigned char)*p))
83 0e1eff5b 2021-06-23 op /* Then at most one leading + or - */
84 0e1eff5b 2021-06-23 op while (*p == '-' || *p == '+') {
85 0e1eff5b 2021-06-23 op if (*p == '-') {
87 0e1eff5b 2021-06-23 op errno = EINVAL;
92 0e1eff5b 2021-06-23 op } else if (*p == '+') {
94 0e1eff5b 2021-06-23 op errno = EINVAL;
102 0e1eff5b 2021-06-23 op /* Main loop: Scan digits, find decimal point, if present.
103 0e1eff5b 2021-06-23 op * We don't allow exponentials, so no scientific notation
104 0e1eff5b 2021-06-23 op * (but note that E for Exa might look like e to some!).
105 0e1eff5b 2021-06-23 op * Advance 'p' to end, to get scale factor.
107 0e1eff5b 2021-06-23 op for (; isascii((unsigned char)*p) &&
108 0e1eff5b 2021-06-23 op (isdigit((unsigned char)*p) || *p=='.'); ++p) {
109 0e1eff5b 2021-06-23 op if (*p == '.') {
110 0e1eff5b 2021-06-23 op if (fract_digits > 0) { /* oops, more than one '.' */
111 0e1eff5b 2021-06-23 op errno = EINVAL;
114 0e1eff5b 2021-06-23 op fract_digits = 1;
118 0e1eff5b 2021-06-23 op i = (*p) - '0'; /* whew! finally a digit we can use */
119 0e1eff5b 2021-06-23 op if (fract_digits > 0) {
120 0e1eff5b 2021-06-23 op if (fract_digits >= MAX_DIGITS-1)
121 0e1eff5b 2021-06-23 op /* ignore extra fractional digits */
123 0e1eff5b 2021-06-23 op fract_digits++; /* for later scaling */
124 0e1eff5b 2021-06-23 op if (fpart > LLONG_MAX / 10) {
125 0e1eff5b 2021-06-23 op errno = ERANGE;
129 0e1eff5b 2021-06-23 op if (i > LLONG_MAX - fpart) {
130 0e1eff5b 2021-06-23 op errno = ERANGE;
134 0e1eff5b 2021-06-23 op } else { /* normal digit */
135 0e1eff5b 2021-06-23 op if (++ndigits >= MAX_DIGITS) {
136 0e1eff5b 2021-06-23 op errno = ERANGE;
139 0e1eff5b 2021-06-23 op if (whole > LLONG_MAX / 10) {
140 0e1eff5b 2021-06-23 op errno = ERANGE;
144 0e1eff5b 2021-06-23 op if (i > LLONG_MAX - whole) {
145 0e1eff5b 2021-06-23 op errno = ERANGE;
153 0e1eff5b 2021-06-23 op whole *= sign;
154 0e1eff5b 2021-06-23 op fpart *= sign;
157 0e1eff5b 2021-06-23 op /* If no scale factor given, we're done. fraction is discarded. */
159 0e1eff5b 2021-06-23 op *result = whole;
163 0e1eff5b 2021-06-23 op /* Validate scale factor, and scale whole and fraction by it. */
164 0e1eff5b 2021-06-23 op for (i = 0; i < SCALE_LENGTH; i++) {
166 0e1eff5b 2021-06-23 op /* Are we there yet? */
167 0e1eff5b 2021-06-23 op if (*p == scale_chars[i] ||
168 0e1eff5b 2021-06-23 op *p == tolower((unsigned char)scale_chars[i])) {
170 0e1eff5b 2021-06-23 op /* If it ends with alphanumerics after the scale char, bad. */
171 0e1eff5b 2021-06-23 op if (isalnum((unsigned char)*(p+1))) {
172 0e1eff5b 2021-06-23 op errno = EINVAL;
175 0e1eff5b 2021-06-23 op scale_fact = scale_factors[i];
177 0e1eff5b 2021-06-23 op /* check for overflow and underflow after scaling */
178 0e1eff5b 2021-06-23 op if (whole > LLONG_MAX / scale_fact ||
179 0e1eff5b 2021-06-23 op whole < LLONG_MIN / scale_fact) {
180 0e1eff5b 2021-06-23 op errno = ERANGE;
184 0e1eff5b 2021-06-23 op /* scale whole part */
185 0e1eff5b 2021-06-23 op whole *= scale_fact;
187 0e1eff5b 2021-06-23 op /* truncate fpart so it does't overflow.
188 0e1eff5b 2021-06-23 op * then scale fractional part.
190 0e1eff5b 2021-06-23 op while (fpart >= LLONG_MAX / scale_fact) {
192 0e1eff5b 2021-06-23 op fract_digits--;
194 0e1eff5b 2021-06-23 op fpart *= scale_fact;
195 0e1eff5b 2021-06-23 op if (fract_digits > 0) {
196 0e1eff5b 2021-06-23 op for (i = 0; i < fract_digits -1; i++)
199 0e1eff5b 2021-06-23 op whole += fpart;
200 0e1eff5b 2021-06-23 op *result = whole;
205 0e1eff5b 2021-06-23 op /* Invalid unit or character */
206 0e1eff5b 2021-06-23 op errno = EINVAL;
210 0e1eff5b 2021-06-23 op /* Format the given "number" into human-readable form in "result".
211 0e1eff5b 2021-06-23 op * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
212 0e1eff5b 2021-06-23 op * Return 0 on success, -1 and errno set if error.
215 0e1eff5b 2021-06-23 op fmt_scaled(long long number, char *result)
217 0e1eff5b 2021-06-23 op long long abval, fract = 0;
218 0e1eff5b 2021-06-23 op unsigned int i;
219 0e1eff5b 2021-06-23 op unit_type unit = NONE;
221 0e1eff5b 2021-06-23 op /* Not every negative long long has a positive representation. */
222 0e1eff5b 2021-06-23 op if (number == LLONG_MIN) {
223 0e1eff5b 2021-06-23 op errno = ERANGE;
227 0e1eff5b 2021-06-23 op abval = llabs(number);
229 0e1eff5b 2021-06-23 op /* Also check for numbers that are just too darned big to format. */
230 0e1eff5b 2021-06-23 op if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
231 0e1eff5b 2021-06-23 op errno = ERANGE;
235 0e1eff5b 2021-06-23 op /* scale whole part; get unscaled fraction */
236 0e1eff5b 2021-06-23 op for (i = 0; i < SCALE_LENGTH; i++) {
237 0e1eff5b 2021-06-23 op if (abval/1024 < scale_factors[i]) {
238 0e1eff5b 2021-06-23 op unit = units[i];
239 0e1eff5b 2021-06-23 op fract = (i == 0) ? 0 : abval % scale_factors[i];
240 0e1eff5b 2021-06-23 op number /= scale_factors[i];
242 0e1eff5b 2021-06-23 op fract /= scale_factors[i - 1];
247 0e1eff5b 2021-06-23 op fract = (10 * fract + 512) / 1024;
248 0e1eff5b 2021-06-23 op /* if the result would be >= 10, round main number */
249 0e1eff5b 2021-06-23 op if (fract >= 10) {
250 0e1eff5b 2021-06-23 op if (number >= 0)
255 0e1eff5b 2021-06-23 op } else if (fract < 0) {
256 0e1eff5b 2021-06-23 op /* shouldn't happen */
260 0e1eff5b 2021-06-23 op if (number == 0)
261 0e1eff5b 2021-06-23 op strlcpy(result, "0B", FMT_SCALED_STRSIZE);
262 0e1eff5b 2021-06-23 op else if (unit == NONE || number >= 100 || number <= -100) {
263 0e1eff5b 2021-06-23 op if (fract >= 5) {
264 0e1eff5b 2021-06-23 op if (number >= 0)
269 0e1eff5b 2021-06-23 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
270 0e1eff5b 2021-06-23 op number, scale_chars[unit]);
272 0e1eff5b 2021-06-23 op (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
273 0e1eff5b 2021-06-23 op number, fract, scale_chars[unit]);
280 0e1eff5b 2021-06-23 op * This is the original version of the program in the man page.
281 0e1eff5b 2021-06-23 op * Copy-and-paste whatever you need from it.
284 0e1eff5b 2021-06-23 op main(int argc, char **argv)
286 0e1eff5b 2021-06-23 op char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE];
287 0e1eff5b 2021-06-23 op long long ninput = 10483892, result;
289 0e1eff5b 2021-06-23 op if (scan_scaled(cinput, &result) == 0)
290 0e1eff5b 2021-06-23 op printf("\"%s\" -> %lld\n", cinput, result);
292 0e1eff5b 2021-06-23 op perror(cinput);
294 0e1eff5b 2021-06-23 op if (fmt_scaled(ninput, buf) == 0)
295 0e1eff5b 2021-06-23 op printf("%lld -> \"%s\"\n", ninput, buf);
297 0e1eff5b 2021-06-23 op fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));