subr_humanize.c revision 1.1.4.2 1 1.1.4.2 yamt /* $NetBSD: subr_humanize.c,v 1.1.4.2 2010/03/11 15:04:18 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*-
4 1.1.4.2 yamt * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5 1.1.4.2 yamt * All rights reserved.
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 yamt * by Luke Mewburn.
9 1.1.4.2 yamt *
10 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 yamt * modification, are permitted provided that the following conditions
12 1.1.4.2 yamt * are met:
13 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.4.2 yamt *
19 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 yamt */
31 1.1.4.2 yamt
32 1.1.4.2 yamt #include <sys/cdefs.h>
33 1.1.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: subr_humanize.c,v 1.1.4.2 2010/03/11 15:04:18 yamt Exp $");
34 1.1.4.2 yamt
35 1.1.4.2 yamt #include <sys/types.h>
36 1.1.4.2 yamt #include <sys/systm.h>
37 1.1.4.2 yamt
38 1.1.4.2 yamt /*
39 1.1.4.2 yamt * snprintf() `bytes' into `buf', reformatting it so that the number,
40 1.1.4.2 yamt * plus a possible `x' + suffix extension) fits into len bytes (including
41 1.1.4.2 yamt * the terminating NUL).
42 1.1.4.2 yamt * Returns the number of bytes stored in buf, or -1 if there was a problem.
43 1.1.4.2 yamt * E.g, given a len of 9 and a suffix of `B':
44 1.1.4.2 yamt * bytes result
45 1.1.4.2 yamt * ----- ------
46 1.1.4.2 yamt * 99999 `99999 B'
47 1.1.4.2 yamt * 100000 `97 kB'
48 1.1.4.2 yamt * 66715648 `65152 kB'
49 1.1.4.2 yamt * 252215296 `240 MB'
50 1.1.4.2 yamt */
51 1.1.4.2 yamt int
52 1.1.4.2 yamt humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix,
53 1.1.4.2 yamt int divisor)
54 1.1.4.2 yamt {
55 1.1.4.2 yamt /* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */
56 1.1.4.2 yamt const char *prefixes;
57 1.1.4.2 yamt int r;
58 1.1.4.2 yamt uint64_t umax;
59 1.1.4.2 yamt size_t i, suffixlen;
60 1.1.4.2 yamt
61 1.1.4.2 yamt if (buf == NULL || suffix == NULL)
62 1.1.4.2 yamt return (-1);
63 1.1.4.2 yamt if (len > 0)
64 1.1.4.2 yamt buf[0] = '\0';
65 1.1.4.2 yamt suffixlen = strlen(suffix);
66 1.1.4.2 yamt /* check if enough room for `x y' + suffix + `\0' */
67 1.1.4.2 yamt if (len < 4 + suffixlen)
68 1.1.4.2 yamt return (-1);
69 1.1.4.2 yamt
70 1.1.4.2 yamt if (divisor == 1024) {
71 1.1.4.2 yamt /*
72 1.1.4.2 yamt * binary multiplies
73 1.1.4.2 yamt * XXX IEC 60027-2 recommends Ki, Mi, Gi...
74 1.1.4.2 yamt */
75 1.1.4.2 yamt prefixes = " KMGTPE";
76 1.1.4.2 yamt } else
77 1.1.4.2 yamt prefixes = " kMGTPE"; /* SI for decimal multiplies */
78 1.1.4.2 yamt
79 1.1.4.2 yamt umax = 1;
80 1.1.4.2 yamt for (i = 0; i < len - suffixlen - 3; i++) {
81 1.1.4.2 yamt umax *= 10;
82 1.1.4.2 yamt if (umax > bytes)
83 1.1.4.2 yamt break;
84 1.1.4.2 yamt }
85 1.1.4.2 yamt for (i = 0; bytes >= umax && prefixes[i + 1]; i++)
86 1.1.4.2 yamt bytes /= divisor;
87 1.1.4.2 yamt
88 1.1.4.2 yamt r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
89 1.1.4.2 yamt i == 0 ? "" : " ", prefixes[i], suffix);
90 1.1.4.2 yamt
91 1.1.4.2 yamt return (r);
92 1.1.4.2 yamt }
93 1.1.4.2 yamt
94 1.1.4.2 yamt int
95 1.1.4.2 yamt format_bytes(char *buf, size_t len, uint64_t bytes)
96 1.1.4.2 yamt {
97 1.1.4.2 yamt int rv;
98 1.1.4.2 yamt size_t nlen;
99 1.1.4.2 yamt
100 1.1.4.2 yamt rv = humanize_number(buf, len, bytes, "B", 1024);
101 1.1.4.2 yamt if (rv != -1) {
102 1.1.4.2 yamt /* nuke the trailing ` B' if it exists */
103 1.1.4.2 yamt nlen = strlen(buf) - 2;
104 1.1.4.2 yamt if (strcmp(&buf[nlen], " B") == 0)
105 1.1.4.2 yamt buf[nlen] = '\0';
106 1.1.4.2 yamt }
107 1.1.4.2 yamt return (rv);
108 1.1.4.2 yamt }
109