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