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
28 * $FreeBSD: src/usr.bin/systat/convtbl.c,v 1.13 2008/01/16 19:27:42 delphij Exp $
31 #include <sys/types.h>
37 #define MEGA (KILO * 1000)
38 #define GIGA (MEGA * 1000)
39 #define TERA (GIGA * 1000)
51 static struct convtbl convtbl
[] = {
52 /* mul, scale, str, name */
53 [SC_BYTE
] = { BYTE
, 1, "B", "byte" },
54 [SC_KILOBYTE
] = { BYTE
, KILO
, "KB", "kbyte" },
55 [SC_MEGABYTE
] = { BYTE
, MEGA
, "MB", "mbyte" },
56 [SC_GIGABYTE
] = { BYTE
, GIGA
, "GB", "gbyte" },
57 [SC_TERABYTE
] = { BYTE
, TERA
, "TB", "tbyte" },
58 [SC_AUTOBYTE
] = { BYTE
, 0, "", "autobyte" },
60 [SC_BIT
] = { BIT
, 1, "b", "bit" },
61 [SC_KILOBIT
] = { BIT
, KILO
, "Kb", "kbit" },
62 [SC_MEGABIT
] = { BIT
, MEGA
, "Mb", "mbit" },
63 [SC_GIGABIT
] = { BIT
, GIGA
, "Gb", "gbit" },
64 [SC_TERABIT
] = { BIT
, TERA
, "Tb", "tbit" },
65 [SC_AUTOBIT
] = { BIT
, 0, "", "autobit" },
67 [SC_AUTO
] = { 0, 0, "", "auto" }
72 get_tbl_ptr(const uintmax_t size
, const int scale
)
77 /* If our index is out of range, default to auto-scaling in bits. */
78 idx
= scale
< SC_AUTOBIT
? scale
: SC_AUTOBIT
;
79 disp_bits
= idx
> SC_AUTOBYTE
;
81 if (idx
== SC_AUTOBYTE
|| idx
== SC_AUTOBIT
)
83 * Simple but elegant algorithm. Count how many times
84 * we can divide our size value by 1000,
85 * incrementing an index each time. We then use the
86 * index as the array index into the conversion table.
88 for (tmp
= size
, idx
= disp_bits
? SC_BIT
: SC_BYTE
;
89 tmp
>= 1000 / (disp_bits
? BIT
: BYTE
) &&
90 idx
< (disp_bits
? SC_AUTOBIT
: SC_AUTOBYTE
) - 1;
93 return (&convtbl
[idx
]);
97 convert(const uintmax_t size
, const int scale
)
101 tp
= get_tbl_ptr(size
, scale
);
102 return ((double)size
* tp
->mul
/ tp
->scale
);
107 get_string(const uintmax_t size
, const int scale
)
111 tp
= get_tbl_ptr(size
, scale
);
116 get_scale(const char *name
)
120 for (i
= 0; i
<= SC_AUTO
; i
++)
121 if (strcmp(convtbl
[i
].name
, name
) == 0)
135 for (i
= 0; i
<= SC_AUTO
; i
++)
136 len
+= strlen(convtbl
[i
].name
) + 2;
137 if ((buf
= malloc(len
)) != NULL
) {
139 for (i
= 0; i
<= SC_AUTO
; i
++) {
140 strcat(buf
, convtbl
[i
].name
);