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