xref: /freebsd/usr.bin/systat/convtbl.c (revision b0b1dbdd)
1 /*
2  * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "convtbl.h"
35 
36 #define BIT		(8)
37 #define BITS		(1)
38 #define KILOBIT		(1000LL)
39 #define MEGABIT		(KILOBIT * 1000)
40 #define GIGABIT		(MEGABIT * 1000)
41 #define TERABIT		(GIGABIT * 1000)
42 
43 #define BYTE		(1)
44 #define BYTES		(1)
45 #define KILOBYTE	(1024LL)
46 #define MEGABYTE	(KILOBYTE * 1024)
47 #define GIGABYTE	(MEGABYTE * 1024)
48 #define TERABYTE	(GIGABYTE * 1024)
49 
50 struct convtbl {
51 	uintmax_t	 mul;
52 	uintmax_t	 scale;
53 	const char	*str;
54 	const char	*name;
55 };
56 
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" },
64 
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" },
70 
71 	[SC_AUTO] =	{ 0, 0, "", "auto" }
72 };
73 
74 static
75 struct convtbl *
76 get_tbl_ptr(const uintmax_t size, const int scale)
77 {
78 	uintmax_t	 tmp;
79 	int		 idx;
80 
81 	/* If our index is out of range, default to auto-scaling. */
82 	idx = scale < SC_AUTO ? scale : SC_AUTO;
83 
84 	if (idx == SC_AUTO)
85 		/*
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.
90 		 */
91 		for (tmp = size, idx = SC_KILOBYTE;
92 		     tmp >= MEGABYTE && idx < SC_BIT - 1;
93 		     tmp >>= 10, idx++);
94 
95 	return (&convtbl[idx]);
96 }
97 
98 double
99 convert(const uintmax_t size, const int scale)
100 {
101 	struct convtbl	*tp;
102 
103 	tp = get_tbl_ptr(size, scale);
104 	return ((double)size * tp->mul / tp->scale);
105 
106 }
107 
108 const char *
109 get_string(const uintmax_t size, const int scale)
110 {
111 	struct convtbl	*tp;
112 
113 	tp = get_tbl_ptr(size, scale);
114 	return (tp->str);
115 }
116 
117 int
118 get_scale(const char *name)
119 {
120 	int i;
121 
122 	for (i = 0; i <= SC_AUTO; i++)
123 		if (strcmp(convtbl[i].name, name) == 0)
124 			return (i);
125 	return (-1);
126 }
127 
128 const char *
129 get_helplist(void)
130 {
131 	int i;
132 	size_t len;
133 	static char *buf;
134 
135 	if (buf == NULL) {
136 		len = 0;
137 		for (i = 0; i <= SC_AUTO; i++)
138 			len += strlen(convtbl[i].name) + 2;
139 		if ((buf = malloc(len)) != NULL) {
140 			buf[0] = '\0';
141 			for (i = 0; i <= SC_AUTO; i++) {
142 				strcat(buf, convtbl[i].name);
143 				if (i < SC_AUTO)
144 					strcat(buf, ", ");
145 			}
146 		} else
147 			return ("");
148 	}
149 	return (buf);
150 }
151