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: src/usr.bin/systat/convtbl.c,v 1.13 2008/01/16 19:27:42 delphij Exp $ 29 */ 30 31 #include <sys/types.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include "convtbl.h" 35 36 #define KILO (1000LL) 37 #define MEGA (KILO * 1000) 38 #define GIGA (MEGA * 1000) 39 #define TERA (GIGA * 1000) 40 41 #define BYTE (1) 42 #define BIT (8) 43 44 struct convtbl { 45 uintmax_t mul; 46 uintmax_t scale; 47 const char *str; 48 const char *name; 49 }; 50 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" }, 59 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" }, 66 67 [SC_AUTO] = { 0, 0, "", "auto" } 68 }; 69 70 static 71 struct convtbl * 72 get_tbl_ptr(const uintmax_t size, const int scale) 73 { 74 uintmax_t tmp; 75 int disp_bits, idx; 76 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; 80 81 if (idx == SC_AUTOBYTE || idx == SC_AUTOBIT) 82 /* 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. 87 */ 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; 91 tmp /= 1000, idx++); 92 93 return (&convtbl[idx]); 94 } 95 96 double 97 convert(const uintmax_t size, const int scale) 98 { 99 struct convtbl *tp; 100 101 tp = get_tbl_ptr(size, scale); 102 return ((double)size * tp->mul / tp->scale); 103 104 } 105 106 const char * 107 get_string(const uintmax_t size, const int scale) 108 { 109 struct convtbl *tp; 110 111 tp = get_tbl_ptr(size, scale); 112 return (tp->str); 113 } 114 115 int 116 get_scale(const char *name) 117 { 118 int i; 119 120 for (i = 0; i <= SC_AUTO; i++) 121 if (strcmp(convtbl[i].name, name) == 0) 122 return (i); 123 return (-1); 124 } 125 126 const char * 127 get_helplist(void) 128 { 129 int i; 130 size_t len; 131 static char *buf; 132 133 if (buf == NULL) { 134 len = 0; 135 for (i = 0; i <= SC_AUTO; i++) 136 len += strlen(convtbl[i].name) + 2; 137 if ((buf = malloc(len)) != NULL) { 138 buf[0] = '\0'; 139 for (i = 0; i <= SC_AUTO; i++) { 140 strcat(buf, convtbl[i].name); 141 if (i < SC_AUTO) 142 strcat(buf, ", "); 143 } 144 } else 145 return (""); 146 } 147 return (buf); 148 } 149