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