convtbl.c revision 285830
11573Srgrimes/* 21573Srgrimes * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>. 31573Srgrimes * All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 3. The name of the author may not be used to endorse or promote products 141573Srgrimes * derived from this software without specific prior written permission. 151573Srgrimes * 161573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 171573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 181573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 191573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 201573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 211573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 221573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 231573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 241573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261573Srgrimes * SUCH DAMAGE. 271573Srgrimes * 281573Srgrimes * $FreeBSD: releng/10.2/usr.bin/systat/convtbl.c 175387 2008-01-16 19:27:43Z delphij $ 2950476Speter */ 301573Srgrimes 31119140Swollman#include <sys/types.h> 321573Srgrimes#include <stdlib.h> 3379531Sru#include <string.h> 341573Srgrimes#include "convtbl.h" 351573Srgrimes 361573Srgrimes#define BIT (8) 371573Srgrimes#define BITS (1) 3859460Sphantom#define KILOBIT (1000LL) 3959460Sphantom#define MEGABIT (KILOBIT * 1000) 401573Srgrimes#define GIGABIT (MEGABIT * 1000) 4184306Sru#define TERABIT (GIGABIT * 1000) 421573Srgrimes 43119140Swollman#define BYTE (1) 441573Srgrimes#define BYTES (1) 451573Srgrimes#define KILOBYTE (1024LL) 461573Srgrimes#define MEGABYTE (KILOBYTE * 1024) 47108030Sru#define GIGABYTE (MEGABYTE * 1024) 48108030Sru#define TERABYTE (GIGABYTE * 1024) 49108030Sru 501573Srgrimesstruct convtbl { 511573Srgrimes uintmax_t mul; 521573Srgrimes uintmax_t scale; 53108087Sru const char *str; 541573Srgrimes const char *name; 55108087Sru}; 5667967Sasmodai 571573Srgrimesstatic struct convtbl convtbl[] = { 58119140Swollman /* mul, scale, str, name */ 59119140Swollman [SC_BYTE] = { BYTE, BYTES, "B", "byte" }, 601573Srgrimes [SC_KILOBYTE] = { BYTE, KILOBYTE, "KB", "kbyte" }, 61108030Sru [SC_MEGABYTE] = { BYTE, MEGABYTE, "MB", "mbyte" }, 62108030Sru [SC_GIGABYTE] = { BYTE, GIGABYTE, "GB", "gbyte" }, 63108030Sru [SC_TERABYTE] = { BYTE, TERABYTE, "TB", "tbyte" }, 641573Srgrimes 651573Srgrimes [SC_BIT] = { BIT, BITS, "b", "bit" }, 661573Srgrimes [SC_KILOBIT] = { BIT, KILOBIT, "Kb", "kbit" }, 671573Srgrimes [SC_MEGABIT] = { BIT, MEGABIT, "Mb", "mbit" }, 681573Srgrimes [SC_GIGABIT] = { BIT, GIGABIT, "Gb", "gbit" }, 691573Srgrimes [SC_TERABIT] = { BIT, TERABIT, "Tb", "tbit" }, 70119140Swollman 71119140Swollman [SC_AUTO] = { 0, 0, "", "auto" } 72119140Swollman}; 73119140Swollman 741573Srgrimesstatic 7582642Srustruct convtbl * 761573Srgrimesget_tbl_ptr(const uintmax_t size, const int scale) 771573Srgrimes{ 781573Srgrimes uintmax_t tmp; 791573Srgrimes int idx; 801573Srgrimes 811573Srgrimes /* If our index is out of range, default to auto-scaling. */ 821573Srgrimes idx = scale < SC_AUTO ? scale : SC_AUTO; 831573Srgrimes 84108087Sru if (idx == SC_AUTO) 851573Srgrimes /* 86119140Swollman * Simple but elegant algorithm. Count how many times 87119140Swollman * we can shift our size value right by a factor of ten, 88131539Sru * incrementing an index each time. We then use the 89131539Sru * index as the array index into the conversion table. 90119140Swollman */ 91119140Swollman for (tmp = size, idx = SC_KILOBYTE; 921573Srgrimes tmp >= MEGABYTE && idx < SC_BIT - 1; 93119140Swollman tmp >>= 10, idx++); 941573Srgrimes 951573Srgrimes return (&convtbl[idx]); 96119140Swollman} 9714038Smpp 98119140Swollmandouble 99119140Swollmanconvert(const uintmax_t size, const int scale) 100119140Swollman{ 101119140Swollman struct convtbl *tp; 102119140Swollman 103119140Swollman tp = get_tbl_ptr(size, scale); 104119140Swollman return ((double)size * tp->mul / tp->scale); 105119140Swollman 106119140Swollman} 107119140Swollman 108119893Sruconst char * 109119140Swollmanget_string(const uintmax_t size, const int scale) 110119140Swollman{ 111119140Swollman struct convtbl *tp; 112119140Swollman 113119140Swollman tp = get_tbl_ptr(size, scale); 114119140Swollman return (tp->str); 115119140Swollman} 116119140Swollman 1171573Srgrimesint 1181573Srgrimesget_scale(const char *name) 11917780Smpp{ 120108030Sru int i; 1211573Srgrimes 122119140Swollman for (i = 0; i <= SC_AUTO; i++) 123119140Swollman if (strcmp(convtbl[i].name, name) == 0) 124119140Swollman return (i); 125119140Swollman return (-1); 126119140Swollman} 127119140Swollman 128119140Swollmanconst char * 129119140Swollmanget_helplist(void) 130119140Swollman{ 131119140Swollman 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