2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2003-01-04 22:07:24 +00:00
|
|
|
* Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-11-27 16:14:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-01-04 22:07:24 +00:00
|
|
|
#include "convtbl.h"
|
|
|
|
|
2007-01-18 09:24:08 +00:00
|
|
|
#define BIT (8)
|
|
|
|
#define BITS (1)
|
|
|
|
#define KILOBIT (1000LL)
|
|
|
|
#define MEGABIT (KILOBIT * 1000)
|
|
|
|
#define GIGABIT (MEGABIT * 1000)
|
|
|
|
#define TERABIT (GIGABIT * 1000)
|
|
|
|
|
|
|
|
#define BYTE (1)
|
|
|
|
#define BYTES (1)
|
|
|
|
#define KILOBYTE (1024LL)
|
|
|
|
#define MEGABYTE (KILOBYTE * 1024)
|
|
|
|
#define GIGABYTE (MEGABYTE * 1024)
|
|
|
|
#define TERABYTE (GIGABYTE * 1024)
|
2006-11-27 16:23:09 +00:00
|
|
|
|
|
|
|
struct convtbl {
|
|
|
|
uintmax_t mul;
|
|
|
|
uintmax_t scale;
|
|
|
|
const char *str;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct convtbl convtbl[] = {
|
2006-11-27 16:14:32 +00:00
|
|
|
/* mul, scale, str, name */
|
2007-01-18 09:24:08 +00:00
|
|
|
[SC_BYTE] = { BYTE, BYTES, "B", "byte" },
|
|
|
|
[SC_KILOBYTE] = { BYTE, KILOBYTE, "KB", "kbyte" },
|
|
|
|
[SC_MEGABYTE] = { BYTE, MEGABYTE, "MB", "mbyte" },
|
|
|
|
[SC_GIGABYTE] = { BYTE, GIGABYTE, "GB", "gbyte" },
|
|
|
|
[SC_TERABYTE] = { BYTE, TERABYTE, "TB", "tbyte" },
|
|
|
|
|
|
|
|
[SC_BIT] = { BIT, BITS, "b", "bit" },
|
|
|
|
[SC_KILOBIT] = { BIT, KILOBIT, "Kb", "kbit" },
|
|
|
|
[SC_MEGABIT] = { BIT, MEGABIT, "Mb", "mbit" },
|
|
|
|
[SC_GIGABIT] = { BIT, GIGABIT, "Gb", "gbit" },
|
|
|
|
[SC_TERABIT] = { BIT, TERABIT, "Tb", "tbit" },
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2006-11-27 16:14:32 +00:00
|
|
|
[SC_AUTO] = { 0, 0, "", "auto" }
|
|
|
|
};
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2006-11-26 20:14:47 +00:00
|
|
|
static
|
2003-01-04 22:07:24 +00:00
|
|
|
struct convtbl *
|
2006-11-27 15:26:26 +00:00
|
|
|
get_tbl_ptr(const uintmax_t size, const int scale)
|
2003-01-04 22:07:24 +00:00
|
|
|
{
|
2006-11-27 15:26:26 +00:00
|
|
|
uintmax_t tmp;
|
|
|
|
int idx;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
/* If our index is out of range, default to auto-scaling. */
|
2006-11-27 15:11:30 +00:00
|
|
|
idx = scale < SC_AUTO ? scale : SC_AUTO;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2004-03-09 11:57:28 +00:00
|
|
|
if (idx == SC_AUTO)
|
2003-01-04 22:07:24 +00:00
|
|
|
/*
|
|
|
|
* Simple but elegant algorithm. Count how many times
|
|
|
|
* we can shift our size value right by a factor of ten,
|
|
|
|
* incrementing an index each time. We then use the
|
|
|
|
* index as the array index into the conversion table.
|
|
|
|
*/
|
2004-03-09 11:57:28 +00:00
|
|
|
for (tmp = size, idx = SC_KILOBYTE;
|
2007-01-18 09:24:08 +00:00
|
|
|
tmp >= MEGABYTE && idx < SC_BIT - 1;
|
2004-03-09 11:57:28 +00:00
|
|
|
tmp >>= 10, idx++);
|
2003-01-04 22:07:24 +00:00
|
|
|
|
2006-11-27 15:11:30 +00:00
|
|
|
return (&convtbl[idx]);
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2006-11-27 15:26:26 +00:00
|
|
|
convert(const uintmax_t size, const int scale)
|
2003-01-04 22:07:24 +00:00
|
|
|
{
|
2006-11-27 15:11:30 +00:00
|
|
|
struct convtbl *tp;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
tp = get_tbl_ptr(size, scale);
|
|
|
|
return ((double)size * tp->mul / tp->scale);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-03-09 11:57:28 +00:00
|
|
|
const char *
|
2006-11-27 15:26:26 +00:00
|
|
|
get_string(const uintmax_t size, const int scale)
|
2003-01-04 22:07:24 +00:00
|
|
|
{
|
2006-11-27 15:11:30 +00:00
|
|
|
struct convtbl *tp;
|
2003-01-04 22:07:24 +00:00
|
|
|
|
|
|
|
tp = get_tbl_ptr(size, scale);
|
2006-11-27 15:11:30 +00:00
|
|
|
return (tp->str);
|
2003-01-04 22:07:24 +00:00
|
|
|
}
|
2006-11-27 16:14:32 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
get_scale(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= SC_AUTO; i++)
|
|
|
|
if (strcmp(convtbl[i].name, name) == 0)
|
|
|
|
return (i);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2008-01-16 19:27:43 +00:00
|
|
|
get_helplist(void)
|
2006-11-27 16:14:32 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t len;
|
|
|
|
static char *buf;
|
|
|
|
|
|
|
|
if (buf == NULL) {
|
|
|
|
len = 0;
|
|
|
|
for (i = 0; i <= SC_AUTO; i++)
|
|
|
|
len += strlen(convtbl[i].name) + 2;
|
|
|
|
if ((buf = malloc(len)) != NULL) {
|
|
|
|
buf[0] = '\0';
|
|
|
|
for (i = 0; i <= SC_AUTO; i++) {
|
|
|
|
strcat(buf, convtbl[i].name);
|
|
|
|
if (i < SC_AUTO)
|
|
|
|
strcat(buf, ", ");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return ("");
|
|
|
|
}
|
|
|
|
return (buf);
|
|
|
|
}
|