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