1*2c81fb9cSAntonio Huete Jimenez /* $OpenBSD: fmt_scaled.c,v 1.21 2022/03/11 07:29:53 dtucker Exp $ */
22c0338ffSzrj
32c0338ffSzrj /*
42c0338ffSzrj * Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
52c0338ffSzrj *
62c0338ffSzrj * Redistribution and use in source and binary forms, with or without
72c0338ffSzrj * modification, are permitted provided that the following conditions
82c0338ffSzrj * are met:
92c0338ffSzrj * 1. Redistributions of source code must retain the above copyright
102c0338ffSzrj * notice, this list of conditions and the following disclaimer.
112c0338ffSzrj * 2. Redistributions in binary form must reproduce the above copyright
122c0338ffSzrj * notice, this list of conditions and the following disclaimer in the
132c0338ffSzrj * documentation and/or other materials provided with the distribution.
142c0338ffSzrj * 3. The name of the author may not be used to endorse or promote products
152c0338ffSzrj * derived from this software without specific prior written permission.
162c0338ffSzrj *
172c0338ffSzrj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182c0338ffSzrj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192c0338ffSzrj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202c0338ffSzrj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212c0338ffSzrj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222c0338ffSzrj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232c0338ffSzrj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242c0338ffSzrj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252c0338ffSzrj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262c0338ffSzrj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272c0338ffSzrj */
282c0338ffSzrj
292c0338ffSzrj /* OPENBSD ORIGINAL: lib/libutil/fmt_scaled.c */
302c0338ffSzrj
312c0338ffSzrj /*
322c0338ffSzrj * fmt_scaled: Format numbers scaled for human comprehension
332c0338ffSzrj * scan_scaled: Scan numbers in this format.
342c0338ffSzrj *
352c0338ffSzrj * "Human-readable" output uses 4 digits max, and puts a unit suffix at
362c0338ffSzrj * the end. Makes output compact and easy-to-read esp. on huge disks.
372c0338ffSzrj * Formatting code was originally in OpenBSD "df", converted to library routine.
382c0338ffSzrj * Scanning code written for OpenBSD libutil.
392c0338ffSzrj */
402c0338ffSzrj
412c0338ffSzrj #include "includes.h"
422c0338ffSzrj
432c0338ffSzrj #ifndef HAVE_FMT_SCALED
442c0338ffSzrj
452c0338ffSzrj #include <stdio.h>
462c0338ffSzrj #include <stdlib.h>
472c0338ffSzrj #include <errno.h>
482c0338ffSzrj #include <string.h>
492c0338ffSzrj #include <ctype.h>
502c0338ffSzrj #include <limits.h>
512c0338ffSzrj
522c0338ffSzrj typedef enum {
532c0338ffSzrj NONE = 0, KILO = 1, MEGA = 2, GIGA = 3, TERA = 4, PETA = 5, EXA = 6
542c0338ffSzrj } unit_type;
552c0338ffSzrj
562c0338ffSzrj /* These three arrays MUST be in sync! XXX make a struct */
57*2c81fb9cSAntonio Huete Jimenez static const unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
58*2c81fb9cSAntonio Huete Jimenez static const char scale_chars[] = "BKMGTPE";
59*2c81fb9cSAntonio Huete Jimenez static const long long scale_factors[] = {
602c0338ffSzrj 1LL,
612c0338ffSzrj 1024LL,
622c0338ffSzrj 1024LL*1024,
632c0338ffSzrj 1024LL*1024*1024,
642c0338ffSzrj 1024LL*1024*1024*1024,
652c0338ffSzrj 1024LL*1024*1024*1024*1024,
662c0338ffSzrj 1024LL*1024*1024*1024*1024*1024,
672c0338ffSzrj };
682c0338ffSzrj #define SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
692c0338ffSzrj
702c0338ffSzrj #define MAX_DIGITS (SCALE_LENGTH * 3) /* XXX strlen(sprintf("%lld", -1)? */
712c0338ffSzrj
722c0338ffSzrj /* Convert the given input string "scaled" into numeric in "result".
732c0338ffSzrj * Return 0 on success, -1 and errno set on error.
742c0338ffSzrj */
752c0338ffSzrj int
scan_scaled(char * scaled,long long * result)762c0338ffSzrj scan_scaled(char *scaled, long long *result)
772c0338ffSzrj {
782c0338ffSzrj char *p = scaled;
792c0338ffSzrj int sign = 0;
802c0338ffSzrj unsigned int i, ndigits = 0, fract_digits = 0;
812c0338ffSzrj long long scale_fact = 1, whole = 0, fpart = 0;
822c0338ffSzrj
832c0338ffSzrj /* Skip leading whitespace */
842c0338ffSzrj while (isascii((unsigned char)*p) && isspace((unsigned char)*p))
852c0338ffSzrj ++p;
862c0338ffSzrj
872c0338ffSzrj /* Then at most one leading + or - */
882c0338ffSzrj while (*p == '-' || *p == '+') {
892c0338ffSzrj if (*p == '-') {
902c0338ffSzrj if (sign) {
912c0338ffSzrj errno = EINVAL;
922c0338ffSzrj return -1;
932c0338ffSzrj }
942c0338ffSzrj sign = -1;
952c0338ffSzrj ++p;
962c0338ffSzrj } else if (*p == '+') {
972c0338ffSzrj if (sign) {
982c0338ffSzrj errno = EINVAL;
992c0338ffSzrj return -1;
1002c0338ffSzrj }
1012c0338ffSzrj sign = +1;
1022c0338ffSzrj ++p;
1032c0338ffSzrj }
1042c0338ffSzrj }
1052c0338ffSzrj
1062c0338ffSzrj /* Main loop: Scan digits, find decimal point, if present.
1072c0338ffSzrj * We don't allow exponentials, so no scientific notation
1082c0338ffSzrj * (but note that E for Exa might look like e to some!).
1092c0338ffSzrj * Advance 'p' to end, to get scale factor.
1102c0338ffSzrj */
1112c0338ffSzrj for (; isascii((unsigned char)*p) &&
1122c0338ffSzrj (isdigit((unsigned char)*p) || *p=='.'); ++p) {
1132c0338ffSzrj if (*p == '.') {
1142c0338ffSzrj if (fract_digits > 0) { /* oops, more than one '.' */
1152c0338ffSzrj errno = EINVAL;
1162c0338ffSzrj return -1;
1172c0338ffSzrj }
1182c0338ffSzrj fract_digits = 1;
1192c0338ffSzrj continue;
1202c0338ffSzrj }
1212c0338ffSzrj
1222c0338ffSzrj i = (*p) - '0'; /* whew! finally a digit we can use */
1232c0338ffSzrj if (fract_digits > 0) {
1242c0338ffSzrj if (fract_digits >= MAX_DIGITS-1)
1252c0338ffSzrj /* ignore extra fractional digits */
1262c0338ffSzrj continue;
1272c0338ffSzrj fract_digits++; /* for later scaling */
1282c0338ffSzrj if (fpart > LLONG_MAX / 10) {
1292c0338ffSzrj errno = ERANGE;
1302c0338ffSzrj return -1;
1312c0338ffSzrj }
1322c0338ffSzrj fpart *= 10;
1332c0338ffSzrj if (i > LLONG_MAX - fpart) {
1342c0338ffSzrj errno = ERANGE;
1352c0338ffSzrj return -1;
1362c0338ffSzrj }
1372c0338ffSzrj fpart += i;
1382c0338ffSzrj } else { /* normal digit */
1392c0338ffSzrj if (++ndigits >= MAX_DIGITS) {
1402c0338ffSzrj errno = ERANGE;
1412c0338ffSzrj return -1;
1422c0338ffSzrj }
1432c0338ffSzrj if (whole > LLONG_MAX / 10) {
1442c0338ffSzrj errno = ERANGE;
1452c0338ffSzrj return -1;
1462c0338ffSzrj }
1472c0338ffSzrj whole *= 10;
1482c0338ffSzrj if (i > LLONG_MAX - whole) {
1492c0338ffSzrj errno = ERANGE;
1502c0338ffSzrj return -1;
1512c0338ffSzrj }
1522c0338ffSzrj whole += i;
1532c0338ffSzrj }
1542c0338ffSzrj }
1552c0338ffSzrj
156*2c81fb9cSAntonio Huete Jimenez if (sign)
1572c0338ffSzrj whole *= sign;
1582c0338ffSzrj
1592c0338ffSzrj /* If no scale factor given, we're done. fraction is discarded. */
1602c0338ffSzrj if (!*p) {
1612c0338ffSzrj *result = whole;
1622c0338ffSzrj return 0;
1632c0338ffSzrj }
1642c0338ffSzrj
1652c0338ffSzrj /* Validate scale factor, and scale whole and fraction by it. */
1662c0338ffSzrj for (i = 0; i < SCALE_LENGTH; i++) {
1672c0338ffSzrj
1682c0338ffSzrj /* Are we there yet? */
1692c0338ffSzrj if (*p == scale_chars[i] ||
1702c0338ffSzrj *p == tolower((unsigned char)scale_chars[i])) {
1712c0338ffSzrj
1722c0338ffSzrj /* If it ends with alphanumerics after the scale char, bad. */
1732c0338ffSzrj if (isalnum((unsigned char)*(p+1))) {
1742c0338ffSzrj errno = EINVAL;
1752c0338ffSzrj return -1;
1762c0338ffSzrj }
1772c0338ffSzrj scale_fact = scale_factors[i];
1782c0338ffSzrj
1792c0338ffSzrj /* check for overflow and underflow after scaling */
1802c0338ffSzrj if (whole > LLONG_MAX / scale_fact ||
1812c0338ffSzrj whole < LLONG_MIN / scale_fact) {
1822c0338ffSzrj errno = ERANGE;
1832c0338ffSzrj return -1;
1842c0338ffSzrj }
1852c0338ffSzrj
1862c0338ffSzrj /* scale whole part */
1872c0338ffSzrj whole *= scale_fact;
1882c0338ffSzrj
1892c0338ffSzrj /* truncate fpart so it doesn't overflow.
1902c0338ffSzrj * then scale fractional part.
1912c0338ffSzrj */
192*2c81fb9cSAntonio Huete Jimenez while (fpart >= LLONG_MAX / scale_fact ||
193*2c81fb9cSAntonio Huete Jimenez fpart <= LLONG_MIN / scale_fact) {
1942c0338ffSzrj fpart /= 10;
1952c0338ffSzrj fract_digits--;
1962c0338ffSzrj }
1972c0338ffSzrj fpart *= scale_fact;
1982c0338ffSzrj if (fract_digits > 0) {
1992c0338ffSzrj for (i = 0; i < fract_digits -1; i++)
2002c0338ffSzrj fpart /= 10;
2012c0338ffSzrj }
202*2c81fb9cSAntonio Huete Jimenez if (sign == -1)
203*2c81fb9cSAntonio Huete Jimenez whole -= fpart;
204*2c81fb9cSAntonio Huete Jimenez else
2052c0338ffSzrj whole += fpart;
2062c0338ffSzrj *result = whole;
2072c0338ffSzrj return 0;
2082c0338ffSzrj }
2092c0338ffSzrj }
2102c0338ffSzrj
2112c0338ffSzrj /* Invalid unit or character */
2122c0338ffSzrj errno = EINVAL;
2132c0338ffSzrj return -1;
2142c0338ffSzrj }
2152c0338ffSzrj
2162c0338ffSzrj /* Format the given "number" into human-readable form in "result".
2172c0338ffSzrj * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
2182c0338ffSzrj * Return 0 on success, -1 and errno set if error.
2192c0338ffSzrj */
2202c0338ffSzrj int
fmt_scaled(long long number,char * result)2212c0338ffSzrj fmt_scaled(long long number, char *result)
2222c0338ffSzrj {
2232c0338ffSzrj long long abval, fract = 0;
2242c0338ffSzrj unsigned int i;
2252c0338ffSzrj unit_type unit = NONE;
2262c0338ffSzrj
227*2c81fb9cSAntonio Huete Jimenez /* Not every negative long long has a positive representation. */
228*2c81fb9cSAntonio Huete Jimenez if (number == LLONG_MIN) {
229*2c81fb9cSAntonio Huete Jimenez errno = ERANGE;
230*2c81fb9cSAntonio Huete Jimenez return -1;
231*2c81fb9cSAntonio Huete Jimenez }
232*2c81fb9cSAntonio Huete Jimenez
2332c0338ffSzrj abval = llabs(number);
2342c0338ffSzrj
235*2c81fb9cSAntonio Huete Jimenez /* Also check for numbers that are just too darned big to format. */
236*2c81fb9cSAntonio Huete Jimenez if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
2372c0338ffSzrj errno = ERANGE;
2382c0338ffSzrj return -1;
2392c0338ffSzrj }
2402c0338ffSzrj
2412c0338ffSzrj /* scale whole part; get unscaled fraction */
2422c0338ffSzrj for (i = 0; i < SCALE_LENGTH; i++) {
2432c0338ffSzrj if (abval/1024 < scale_factors[i]) {
2442c0338ffSzrj unit = units[i];
2452c0338ffSzrj fract = (i == 0) ? 0 : abval % scale_factors[i];
2462c0338ffSzrj number /= scale_factors[i];
2472c0338ffSzrj if (i > 0)
2482c0338ffSzrj fract /= scale_factors[i - 1];
2492c0338ffSzrj break;
2502c0338ffSzrj }
2512c0338ffSzrj }
2522c0338ffSzrj
2532c0338ffSzrj fract = (10 * fract + 512) / 1024;
2542c0338ffSzrj /* if the result would be >= 10, round main number */
2552c0338ffSzrj if (fract >= 10) {
2562c0338ffSzrj if (number >= 0)
2572c0338ffSzrj number++;
2582c0338ffSzrj else
2592c0338ffSzrj number--;
2602c0338ffSzrj fract = 0;
2612c0338ffSzrj } else if (fract < 0) {
2622c0338ffSzrj /* shouldn't happen */
2632c0338ffSzrj fract = 0;
2642c0338ffSzrj }
2652c0338ffSzrj
2662c0338ffSzrj if (number == 0)
2672c0338ffSzrj strlcpy(result, "0B", FMT_SCALED_STRSIZE);
2682c0338ffSzrj else if (unit == NONE || number >= 100 || number <= -100) {
2692c0338ffSzrj if (fract >= 5) {
2702c0338ffSzrj if (number >= 0)
2712c0338ffSzrj number++;
2722c0338ffSzrj else
2732c0338ffSzrj number--;
2742c0338ffSzrj }
2752c0338ffSzrj (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
2762c0338ffSzrj number, scale_chars[unit]);
2772c0338ffSzrj } else
2782c0338ffSzrj (void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
2792c0338ffSzrj number, fract, scale_chars[unit]);
2802c0338ffSzrj
2812c0338ffSzrj return 0;
2822c0338ffSzrj }
2832c0338ffSzrj
2842c0338ffSzrj #ifdef MAIN
2852c0338ffSzrj /*
2862c0338ffSzrj * This is the original version of the program in the man page.
2872c0338ffSzrj * Copy-and-paste whatever you need from it.
2882c0338ffSzrj */
2892c0338ffSzrj int
main(int argc,char ** argv)2902c0338ffSzrj main(int argc, char **argv)
2912c0338ffSzrj {
2922c0338ffSzrj char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE];
2932c0338ffSzrj long long ninput = 10483892, result;
2942c0338ffSzrj
2952c0338ffSzrj if (scan_scaled(cinput, &result) == 0)
2962c0338ffSzrj printf("\"%s\" -> %lld\n", cinput, result);
2972c0338ffSzrj else
2982c0338ffSzrj perror(cinput);
2992c0338ffSzrj
3002c0338ffSzrj if (fmt_scaled(ninput, buf) == 0)
3012c0338ffSzrj printf("%lld -> \"%s\"\n", ninput, buf);
3022c0338ffSzrj else
3032c0338ffSzrj fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));
3042c0338ffSzrj
3052c0338ffSzrj return 0;
3062c0338ffSzrj }
3072c0338ffSzrj #endif
3082c0338ffSzrj
3092c0338ffSzrj #endif /* HAVE_FMT_SCALED */
310