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