convtbl.c (164673) | convtbl.c (164675) |
---|---|
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 --- 11 unchanged lines hidden (view full) --- 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 * | 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 --- 11 unchanged lines hidden (view full) --- 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: head/usr.bin/systat/convtbl.c 164673 2006-11-27 16:23:09Z yar $ | 28 * $FreeBSD: head/usr.bin/systat/convtbl.c 164675 2006-11-27 16:33:44Z yar $ |
29 */ 30 31#include <sys/types.h> 32#include <stdlib.h> 33#include <string.h> 34#include "convtbl.h" 35 36#define BIT (8) 37#define BYTE (1) 38 39#define BITS (1) 40#define BYTES (1) 41#define KILO (1024LL) 42#define MEGA (KILO * 1024) 43#define GIGA (MEGA * 1024) | 29 */ 30 31#include <sys/types.h> 32#include <stdlib.h> 33#include <string.h> 34#include "convtbl.h" 35 36#define BIT (8) 37#define BYTE (1) 38 39#define BITS (1) 40#define BYTES (1) 41#define KILO (1024LL) 42#define MEGA (KILO * 1024) 43#define GIGA (MEGA * 1024) |
44#define TERA (GIGA * 1024) |
|
44 45struct convtbl { 46 uintmax_t mul; 47 uintmax_t scale; 48 const char *str; 49 const char *name; 50}; 51 52static struct convtbl convtbl[] = { 53 /* mul, scale, str, name */ 54 [SC_BYTE] = { BYTE, BYTES, "B", "byte" }, 55 [SC_KILOBYTE] = { BYTE, KILO, "KB", "kbyte" }, 56 [SC_MEGABYTE] = { BYTE, MEGA, "MB", "mbyte" }, 57 [SC_GIGABYTE] = { BYTE, GIGA, "GB", "gbyte" }, | 45 46struct convtbl { 47 uintmax_t mul; 48 uintmax_t scale; 49 const char *str; 50 const char *name; 51}; 52 53static struct convtbl convtbl[] = { 54 /* mul, scale, str, name */ 55 [SC_BYTE] = { BYTE, BYTES, "B", "byte" }, 56 [SC_KILOBYTE] = { BYTE, KILO, "KB", "kbyte" }, 57 [SC_MEGABYTE] = { BYTE, MEGA, "MB", "mbyte" }, 58 [SC_GIGABYTE] = { BYTE, GIGA, "GB", "gbyte" }, |
59 [SC_TERABYTE] = { BYTE, TERA, "TB", "tbyte" }, |
|
58 59 [SC_BIT] = { BIT, BITS, "b", "bit" }, 60 [SC_KILOBIT] = { BIT, KILO, "Kb", "kbit" }, 61 [SC_MEGABIT] = { BIT, MEGA, "Mb", "mbit" }, 62 [SC_GIGABIT] = { BIT, GIGA, "Gb", "gbit" }, | 60 61 [SC_BIT] = { BIT, BITS, "b", "bit" }, 62 [SC_KILOBIT] = { BIT, KILO, "Kb", "kbit" }, 63 [SC_MEGABIT] = { BIT, MEGA, "Mb", "mbit" }, 64 [SC_GIGABIT] = { BIT, GIGA, "Gb", "gbit" }, |
65 [SC_TERABIT] = { BIT, TERA, "Tb", "tbit" }, |
|
63 64 [SC_AUTO] = { 0, 0, "", "auto" } 65}; 66 67static 68struct convtbl * 69get_tbl_ptr(const uintmax_t size, const int scale) 70{ --- 6 unchanged lines hidden (view full) --- 77 if (idx == SC_AUTO) 78 /* 79 * Simple but elegant algorithm. Count how many times 80 * we can shift our size value right by a factor of ten, 81 * incrementing an index each time. We then use the 82 * index as the array index into the conversion table. 83 */ 84 for (tmp = size, idx = SC_KILOBYTE; | 66 67 [SC_AUTO] = { 0, 0, "", "auto" } 68}; 69 70static 71struct convtbl * 72get_tbl_ptr(const uintmax_t size, const int scale) 73{ --- 6 unchanged lines hidden (view full) --- 80 if (idx == SC_AUTO) 81 /* 82 * Simple but elegant algorithm. Count how many times 83 * we can shift our size value right by a factor of ten, 84 * incrementing an index each time. We then use the 85 * index as the array index into the conversion table. 86 */ 87 for (tmp = size, idx = SC_KILOBYTE; |
85 tmp >= MEGA && idx < SC_GIGABYTE; | 88 tmp >= MEGA && idx < SC_BIT - 1; |
86 tmp >>= 10, idx++); 87 88 return (&convtbl[idx]); 89} 90 91double 92convert(const uintmax_t size, const int scale) 93{ --- 50 unchanged lines hidden --- | 89 tmp >>= 10, idx++); 90 91 return (&convtbl[idx]); 92} 93 94double 95convert(const uintmax_t size, const int scale) 96{ --- 50 unchanged lines hidden --- |