1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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$ 31 */ 32 33 #include <sys/types.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include "convtbl.h" 37 38 #define BIT (8) 39 #define BITS (1) 40 #define KILOBIT (1000LL) 41 #define MEGABIT (KILOBIT * 1000) 42 #define GIGABIT (MEGABIT * 1000) 43 #define TERABIT (GIGABIT * 1000) 44 45 #define BYTE (1) 46 #define BYTES (1) 47 #define KILOBYTE (1024LL) 48 #define MEGABYTE (KILOBYTE * 1024) 49 #define GIGABYTE (MEGABYTE * 1024) 50 #define TERABYTE (GIGABYTE * 1024) 51 52 struct convtbl { 53 uintmax_t mul; 54 uintmax_t scale; 55 const char *str; 56 const char *name; 57 }; 58 59 static struct convtbl convtbl[] = { 60 /* mul, scale, str, name */ 61 [SC_BYTE] = { BYTE, BYTES, "B", "byte" }, 62 [SC_KILOBYTE] = { BYTE, KILOBYTE, "KB", "kbyte" }, 63 [SC_MEGABYTE] = { BYTE, MEGABYTE, "MB", "mbyte" }, 64 [SC_GIGABYTE] = { BYTE, GIGABYTE, "GB", "gbyte" }, 65 [SC_TERABYTE] = { BYTE, TERABYTE, "TB", "tbyte" }, 66 67 [SC_BIT] = { BIT, BITS, "b", "bit" }, 68 [SC_KILOBIT] = { BIT, KILOBIT, "Kb", "kbit" }, 69 [SC_MEGABIT] = { BIT, MEGABIT, "Mb", "mbit" }, 70 [SC_GIGABIT] = { BIT, GIGABIT, "Gb", "gbit" }, 71 [SC_TERABIT] = { BIT, TERABIT, "Tb", "tbit" }, 72 73 [SC_AUTO] = { 0, 0, "", "auto" } 74 }; 75 76 static 77 struct convtbl * 78 get_tbl_ptr(const uintmax_t size, const int scale) 79 { 80 uintmax_t tmp; 81 int idx; 82 83 /* If our index is out of range, default to auto-scaling. */ 84 idx = scale < SC_AUTO ? scale : SC_AUTO; 85 86 if (idx == SC_AUTO) 87 /* 88 * Simple but elegant algorithm. Count how many times 89 * we can shift our size value right by a factor of ten, 90 * incrementing an index each time. We then use the 91 * index as the array index into the conversion table. 92 */ 93 for (tmp = size, idx = SC_KILOBYTE; 94 tmp >= MEGABYTE && idx < SC_BIT - 1; 95 tmp >>= 10, idx++); 96 97 return (&convtbl[idx]); 98 } 99 100 double 101 convert(const uintmax_t size, const int scale) 102 { 103 struct convtbl *tp; 104 105 tp = get_tbl_ptr(size, scale); 106 return ((double)size * tp->mul / tp->scale); 107 108 } 109 110 const char * 111 get_string(const uintmax_t size, const int scale) 112 { 113 struct convtbl *tp; 114 115 tp = get_tbl_ptr(size, scale); 116 return (tp->str); 117 } 118 119 int 120 get_scale(const char *name) 121 { 122 int i; 123 124 for (i = 0; i <= SC_AUTO; i++) 125 if (strcmp(convtbl[i].name, name) == 0) 126 return (i); 127 return (-1); 128 } 129 130 const char * 131 get_helplist(void) 132 { 133 int i; 134 size_t len; 135 static char *buf; 136 137 if (buf == NULL) { 138 len = 0; 139 for (i = 0; i <= SC_AUTO; i++) 140 len += strlen(convtbl[i].name) + 2; 141 if ((buf = malloc(len)) != NULL) { 142 buf[0] = '\0'; 143 for (i = 0; i <= SC_AUTO; i++) { 144 strcat(buf, convtbl[i].name); 145 if (i < SC_AUTO) 146 strcat(buf, ", "); 147 } 148 } else 149 return (""); 150 } 151 return (buf); 152 } 153