1*1de7b4b8SPedro F. Giffuni /*-
2*1de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*1de7b4b8SPedro F. Giffuni *
4b59ba7dfSPoul-Henning Kamp * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
5b59ba7dfSPoul-Henning Kamp * All rights reserved.
6b59ba7dfSPoul-Henning Kamp *
7b59ba7dfSPoul-Henning Kamp * Redistribution and use in source and binary forms, with or without
8b59ba7dfSPoul-Henning Kamp * modification, are permitted provided that the following conditions
9b59ba7dfSPoul-Henning Kamp * are met:
10b59ba7dfSPoul-Henning Kamp * 1. Redistributions of source code must retain the above copyright
11b59ba7dfSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer.
12b59ba7dfSPoul-Henning Kamp * 2. Redistributions in binary form must reproduce the above copyright
13b59ba7dfSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer in the
14b59ba7dfSPoul-Henning Kamp * documentation and/or other materials provided with the distribution.
15b59ba7dfSPoul-Henning Kamp * 3. The name of the author may not be used to endorse or promote products
16b59ba7dfSPoul-Henning Kamp * derived from this software without specific prior written permission.
17b59ba7dfSPoul-Henning Kamp *
18b59ba7dfSPoul-Henning Kamp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b59ba7dfSPoul-Henning Kamp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b59ba7dfSPoul-Henning Kamp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b59ba7dfSPoul-Henning Kamp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b59ba7dfSPoul-Henning Kamp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b59ba7dfSPoul-Henning Kamp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b59ba7dfSPoul-Henning Kamp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b59ba7dfSPoul-Henning Kamp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b59ba7dfSPoul-Henning Kamp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b59ba7dfSPoul-Henning Kamp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b59ba7dfSPoul-Henning Kamp * SUCH DAMAGE.
29b59ba7dfSPoul-Henning Kamp */
30b59ba7dfSPoul-Henning Kamp
31b59ba7dfSPoul-Henning Kamp #include <sys/types.h>
32aee371e3SYaroslav Tykhiy #include <stdlib.h>
33aee371e3SYaroslav Tykhiy #include <string.h>
34b59ba7dfSPoul-Henning Kamp #include "convtbl.h"
35b59ba7dfSPoul-Henning Kamp
36afe7b9fbSYaroslav Tykhiy #define BIT (8)
37afe7b9fbSYaroslav Tykhiy #define BITS (1)
38bc68fcf3SRuslan Ermilov #define KILOBIT (1000LL)
39bc68fcf3SRuslan Ermilov #define MEGABIT (KILOBIT * 1000)
40bc68fcf3SRuslan Ermilov #define GIGABIT (MEGABIT * 1000)
41bc68fcf3SRuslan Ermilov #define TERABIT (GIGABIT * 1000)
42bc68fcf3SRuslan Ermilov
43bc68fcf3SRuslan Ermilov #define BYTE (1)
44afe7b9fbSYaroslav Tykhiy #define BYTES (1)
45bc68fcf3SRuslan Ermilov #define KILOBYTE (1024LL)
46bc68fcf3SRuslan Ermilov #define MEGABYTE (KILOBYTE * 1024)
47bc68fcf3SRuslan Ermilov #define GIGABYTE (MEGABYTE * 1024)
48bc68fcf3SRuslan Ermilov #define TERABYTE (GIGABYTE * 1024)
49afe7b9fbSYaroslav Tykhiy
50afe7b9fbSYaroslav Tykhiy struct convtbl {
51afe7b9fbSYaroslav Tykhiy uintmax_t mul;
52afe7b9fbSYaroslav Tykhiy uintmax_t scale;
53afe7b9fbSYaroslav Tykhiy const char *str;
54afe7b9fbSYaroslav Tykhiy const char *name;
55afe7b9fbSYaroslav Tykhiy };
56afe7b9fbSYaroslav Tykhiy
57afe7b9fbSYaroslav Tykhiy static struct convtbl convtbl[] = {
58aee371e3SYaroslav Tykhiy /* mul, scale, str, name */
59aee371e3SYaroslav Tykhiy [SC_BYTE] = { BYTE, BYTES, "B", "byte" },
60bc68fcf3SRuslan Ermilov [SC_KILOBYTE] = { BYTE, KILOBYTE, "KB", "kbyte" },
61bc68fcf3SRuslan Ermilov [SC_MEGABYTE] = { BYTE, MEGABYTE, "MB", "mbyte" },
62bc68fcf3SRuslan Ermilov [SC_GIGABYTE] = { BYTE, GIGABYTE, "GB", "gbyte" },
63bc68fcf3SRuslan Ermilov [SC_TERABYTE] = { BYTE, TERABYTE, "TB", "tbyte" },
64b59ba7dfSPoul-Henning Kamp
65aee371e3SYaroslav Tykhiy [SC_BIT] = { BIT, BITS, "b", "bit" },
66bc68fcf3SRuslan Ermilov [SC_KILOBIT] = { BIT, KILOBIT, "Kb", "kbit" },
67bc68fcf3SRuslan Ermilov [SC_MEGABIT] = { BIT, MEGABIT, "Mb", "mbit" },
68bc68fcf3SRuslan Ermilov [SC_GIGABIT] = { BIT, GIGABIT, "Gb", "gbit" },
69bc68fcf3SRuslan Ermilov [SC_TERABIT] = { BIT, TERABIT, "Tb", "tbit" },
70b59ba7dfSPoul-Henning Kamp
71aee371e3SYaroslav Tykhiy [SC_AUTO] = { 0, 0, "", "auto" }
72b59ba7dfSPoul-Henning Kamp };
73b59ba7dfSPoul-Henning Kamp
7400603ea2SYaroslav Tykhiy static
75b59ba7dfSPoul-Henning Kamp struct convtbl *
get_tbl_ptr(const uintmax_t size,const int scale)76f5d4751cSYaroslav Tykhiy get_tbl_ptr(const uintmax_t size, const int scale)
77b59ba7dfSPoul-Henning Kamp {
78f5d4751cSYaroslav Tykhiy uintmax_t tmp;
79f5d4751cSYaroslav Tykhiy int idx;
80b59ba7dfSPoul-Henning Kamp
81b59ba7dfSPoul-Henning Kamp /* If our index is out of range, default to auto-scaling. */
82f3014885SYaroslav Tykhiy idx = scale < SC_AUTO ? scale : SC_AUTO;
83b59ba7dfSPoul-Henning Kamp
8433dc5491SDavid Malone if (idx == SC_AUTO)
85b59ba7dfSPoul-Henning Kamp /*
86b59ba7dfSPoul-Henning Kamp * Simple but elegant algorithm. Count how many times
87b59ba7dfSPoul-Henning Kamp * we can shift our size value right by a factor of ten,
88b59ba7dfSPoul-Henning Kamp * incrementing an index each time. We then use the
89b59ba7dfSPoul-Henning Kamp * index as the array index into the conversion table.
90b59ba7dfSPoul-Henning Kamp */
9133dc5491SDavid Malone for (tmp = size, idx = SC_KILOBYTE;
92bc68fcf3SRuslan Ermilov tmp >= MEGABYTE && idx < SC_BIT - 1;
9333dc5491SDavid Malone tmp >>= 10, idx++);
94b59ba7dfSPoul-Henning Kamp
95f3014885SYaroslav Tykhiy return (&convtbl[idx]);
96b59ba7dfSPoul-Henning Kamp }
97b59ba7dfSPoul-Henning Kamp
98b59ba7dfSPoul-Henning Kamp double
convert(const uintmax_t size,const int scale)99f5d4751cSYaroslav Tykhiy convert(const uintmax_t size, const int scale)
100b59ba7dfSPoul-Henning Kamp {
101f3014885SYaroslav Tykhiy struct convtbl *tp;
102b59ba7dfSPoul-Henning Kamp
103b59ba7dfSPoul-Henning Kamp tp = get_tbl_ptr(size, scale);
104b59ba7dfSPoul-Henning Kamp return ((double)size * tp->mul / tp->scale);
105b59ba7dfSPoul-Henning Kamp
106b59ba7dfSPoul-Henning Kamp }
107b59ba7dfSPoul-Henning Kamp
10833dc5491SDavid Malone const char *
get_string(const uintmax_t size,const int scale)109f5d4751cSYaroslav Tykhiy get_string(const uintmax_t size, const int scale)
110b59ba7dfSPoul-Henning Kamp {
111f3014885SYaroslav Tykhiy struct convtbl *tp;
112b59ba7dfSPoul-Henning Kamp
113b59ba7dfSPoul-Henning Kamp tp = get_tbl_ptr(size, scale);
114f3014885SYaroslav Tykhiy return (tp->str);
115b59ba7dfSPoul-Henning Kamp }
116aee371e3SYaroslav Tykhiy
117aee371e3SYaroslav Tykhiy int
get_scale(const char * name)118aee371e3SYaroslav Tykhiy get_scale(const char *name)
119aee371e3SYaroslav Tykhiy {
120aee371e3SYaroslav Tykhiy int i;
121aee371e3SYaroslav Tykhiy
122aee371e3SYaroslav Tykhiy for (i = 0; i <= SC_AUTO; i++)
123aee371e3SYaroslav Tykhiy if (strcmp(convtbl[i].name, name) == 0)
124aee371e3SYaroslav Tykhiy return (i);
125aee371e3SYaroslav Tykhiy return (-1);
126aee371e3SYaroslav Tykhiy }
127aee371e3SYaroslav Tykhiy
128aee371e3SYaroslav Tykhiy const char *
get_helplist(void)12993b9f504SXin LI get_helplist(void)
130aee371e3SYaroslav Tykhiy {
131aee371e3SYaroslav Tykhiy int i;
132aee371e3SYaroslav Tykhiy size_t len;
133aee371e3SYaroslav Tykhiy static char *buf;
134aee371e3SYaroslav Tykhiy
135aee371e3SYaroslav Tykhiy if (buf == NULL) {
136aee371e3SYaroslav Tykhiy len = 0;
137aee371e3SYaroslav Tykhiy for (i = 0; i <= SC_AUTO; i++)
138aee371e3SYaroslav Tykhiy len += strlen(convtbl[i].name) + 2;
139aee371e3SYaroslav Tykhiy if ((buf = malloc(len)) != NULL) {
140aee371e3SYaroslav Tykhiy buf[0] = '\0';
141aee371e3SYaroslav Tykhiy for (i = 0; i <= SC_AUTO; i++) {
142aee371e3SYaroslav Tykhiy strcat(buf, convtbl[i].name);
143aee371e3SYaroslav Tykhiy if (i < SC_AUTO)
144aee371e3SYaroslav Tykhiy strcat(buf, ", ");
145aee371e3SYaroslav Tykhiy }
146aee371e3SYaroslav Tykhiy } else
147aee371e3SYaroslav Tykhiy return ("");
148aee371e3SYaroslav Tykhiy }
149aee371e3SYaroslav Tykhiy return (buf);
150aee371e3SYaroslav Tykhiy }
151