2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
40 #define KILOBIT (1000LL)
41 #define MEGABIT (KILOBIT * 1000)
42 #define GIGABIT (MEGABIT * 1000)
43 #define TERABIT (GIGABIT * 1000)
47 #define KILOBYTE (1024LL)
48 #define MEGABYTE (KILOBYTE * 1024)
49 #define GIGABYTE (MEGABYTE * 1024)
50 #define TERABYTE (GIGABYTE * 1024)
59 static struct convtbl convtbl[] = {
60 /* mul, scale, str, name */
61 [SC_BYTE] = { BYTE, BYTES, "B", "byte" },
62 [SC_KILOBYTE] = { BYTE, KILOBYTE, "KB", "kbyte" },
63 [SC_MEGABYTE] = { BYTE, MEGABYTE, "MB", "mbyte" },
64 [SC_GIGABYTE] = { BYTE, GIGABYTE, "GB", "gbyte" },
65 [SC_TERABYTE] = { BYTE, TERABYTE, "TB", "tbyte" },
67 [SC_BIT] = { BIT, BITS, "b", "bit" },
68 [SC_KILOBIT] = { BIT, KILOBIT, "Kb", "kbit" },
69 [SC_MEGABIT] = { BIT, MEGABIT, "Mb", "mbit" },
70 [SC_GIGABIT] = { BIT, GIGABIT, "Gb", "gbit" },
71 [SC_TERABIT] = { BIT, TERABIT, "Tb", "tbit" },
73 [SC_AUTO] = { 0, 0, "", "auto" }
78 get_tbl_ptr(const uintmax_t size, const int scale)
83 /* If our index is out of range, default to auto-scaling. */
84 idx = scale < SC_AUTO ? scale : SC_AUTO;
88 * Simple but elegant algorithm. Count how many times
89 * we can shift our size value right by a factor of ten,
90 * incrementing an index each time. We then use the
91 * index as the array index into the conversion table.
93 for (tmp = size, idx = SC_KILOBYTE;
94 tmp >= MEGABYTE && idx < SC_BIT - 1;
97 return (&convtbl[idx]);
101 convert(const uintmax_t size, const int scale)
105 tp = get_tbl_ptr(size, scale);
106 return ((double)size * tp->mul / tp->scale);
111 get_string(const uintmax_t size, const int scale)
115 tp = get_tbl_ptr(size, scale);
120 get_scale(const char *name)
124 for (i = 0; i <= SC_AUTO; i++)
125 if (strcmp(convtbl[i].name, name) == 0)
139 for (i = 0; i <= SC_AUTO; i++)
140 len += strlen(convtbl[i].name) + 2;
141 if ((buf = malloc(len)) != NULL) {
143 for (i = 0; i <= SC_AUTO; i++) {
144 strcat(buf, convtbl[i].name);