aboutsummaryrefslogtreecommitdiff
path: root/cddl/contrib/opensolaris/lib/libcmdutils/common/nicenum.c
blob: 8e3202f7927083af94dc2501f93d14340499a2b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2017 Jason king
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/debug.h>
#include "libcmdutils.h"

/* The largest suffix that can fit, aka an exabyte (2^60 / 10^18) */
#define	INDEX_MAX	(6)

/* Verify INDEX_MAX fits */
CTASSERT(INDEX_MAX * 10 < sizeof (uint64_t) * 8);

void
nicenum_scale(uint64_t n, size_t units, char *buf, size_t buflen,
    uint32_t flags)
{
	uint64_t divamt = 1024;
	uint64_t divisor = 1;
	int index = 0;
	int rc = 0;
	char u;

	if (units == 0)
		units = 1;

	if (n > 0) {
		n *= units;
		if (n < units)
			goto overflow;
	}

	if (flags & NN_DIVISOR_1000)
		divamt = 1000;

	/*
	 * This tries to find the suffix S(n) such that
	 * S(n) <= n < S(n+1), where S(n) = 2^(n*10) | 10^(3*n)
	 * (i.e. 1024/1000, 1,048,576/1,000,000, etc).  Stop once S(n)
	 * is the largest prefix supported (i.e. don't bother computing
	 * and checking S(n+1).  Since INDEX_MAX should be the largest
	 * suffix that fits (currently an exabyte), S(INDEX_MAX + 1) is
	 * never checked as it would overflow.
	 */
	while (index < INDEX_MAX) {
		uint64_t newdiv = divisor * divamt;

		/* CTASSERT() guarantee these never trip */
		VERIFY3U(newdiv, >=, divamt);
		VERIFY3U(newdiv, >=, divisor);

		if (n < newdiv)
			break;

		divisor = newdiv;
		index++;
	}

	u = " KMGTPE"[index];

	if (index == 0) {
		rc = snprintf(buf, buflen, "%llu", n);
	} else if (n % divisor == 0) {
		/*
		 * If this is an even multiple of the base, always display
		 * without any decimal precision.
		 */
		rc = snprintf(buf, buflen, "%llu%c", n / divisor, u);
	} else {
		/*
		 * We want to choose a precision that reflects the best choice
		 * for fitting in 5 characters.  This can get rather tricky
		 * when we have numbers that are very close to an order of
		 * magnitude.  For example, when displaying 10239 (which is
		 * really 9.999K), we want only a single place of precision
		 * for 10.0K.  We could develop some complex heuristics for
		 * this, but it's much easier just to try each combination
		 * in turn.
		 */
		int i;
		for (i = 2; i >= 0; i--) {
			if ((rc = snprintf(buf, buflen, "%.*f%c", i,
			    (double)n / divisor, u)) <= 5)
				break;
		}
	}

	if (rc + 1 > buflen || rc < 0)
		goto overflow;

	return;

overflow:
	/* prefer a more verbose message if possible */
	if (buflen > 10)
		(void) strlcpy(buf, "<overflow>", buflen);
	else
		(void) strlcpy(buf, "??", buflen);
}

void
nicenum(uint64_t num, char *buf, size_t buflen)
{
	nicenum_scale(num, 1, buf, buflen, 0);
}