summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authoreadler <eadler@FreeBSD.org>2014-07-17 06:54:12 +0000
committereadler <eadler@FreeBSD.org>2014-07-17 06:54:12 +0000
commit0da1002132bd602753fd19f5e1d96c8a5c6a39fd (patch)
tree9c112cd01b82a3eaad8f030e64eb20eb886edc4e /usr.bin
parent6164dd2fc4c26df1941e2d7628e3c4647e0aa7c6 (diff)
downloadFreeBSD-src-0da1002132bd602753fd19f5e1d96c8a5c6a39fd.zip
FreeBSD-src-0da1002132bd602753fd19f5e1d96c8a5c6a39fd.tar.gz
units(1): Add support for output-format
Add support for the output-format argument. This also exposes subtle rounding differences between GNU units and our units.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/units/units.14
-rw-r--r--usr.bin/units/units.c60
2 files changed, 50 insertions, 14 deletions
diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1
index a7173a5..751d9ce 100644
--- a/usr.bin/units/units.1
+++ b/usr.bin/units/units.1
@@ -17,6 +17,8 @@ The following options are available:
Show an overview of options
.It Fl f Ar filename No , Fl -file Ar filename
Specify the name of the units data file to load.
+.It Fl e , Fl -exponential
+Behave as if -o '%6e' was typed.
.It Fl q No , Fl -quiet
Suppress prompting of the user for units and the display of statistics
about the number of units loaded.
@@ -33,6 +35,8 @@ from other programs for easy to parse results.
.It Fl v No , Fl -verbose
Print the units in the conversion output.
Be more verbose in general.
+.It Fl o Ar format No , Fl -output-format Ar format
+Select the output format string by which numbers are printed.
.It Fl V No , Fl -version
Print the version number, usage, and then exit.
.It Ar from-unit to-unit
diff --git a/usr.bin/units/units.c b/usr.bin/units/units.c
index f6192c5..3d39d0e 100644
--- a/usr.bin/units/units.c
+++ b/usr.bin/units/units.c
@@ -75,6 +75,7 @@ static int unitcount;
static int prefixcount;
static bool verbose = false;
static bool terse = false;
+static const char * outputformat;
static const char * havestr;
static const char * wantstr;
@@ -649,6 +650,7 @@ static void
showanswer(struct unittype * have, struct unittype * want)
{
double ans;
+ char* oformat;
if (compareunits(have, want)) {
printf("conformability error\n");
@@ -668,11 +670,16 @@ showanswer(struct unittype * have, struct unittype * want)
else if (have->offset != want->offset) {
if (want->quantity)
printf("WARNING: conversion of non-proportional quantities.\n");
- if (have->quantity)
- printf("\t%.8g\n",
+ if (have->quantity) {
+ asprintf(&oformat, "\t%s\n", outputformat);
+ printf(oformat,
(have->factor + have->offset-want->offset)/want->factor);
+ free(oformat);
+ }
else {
- printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
+ asprintf(&oformat, "\t (-> x*%sg %sg)\n\t (<- y*%sg %sg)\n",
+ outputformat, outputformat, outputformat, outputformat);
+ printf(oformat,
have->factor / want->factor,
(have->offset-want->offset)/want->factor,
want->factor / have->factor,
@@ -681,17 +688,33 @@ showanswer(struct unittype * have, struct unittype * want)
}
else {
ans = have->factor / want->factor;
- if (verbose)
- printf("\t%s = %.8g * %s\n", havestr, ans, wantstr);
- else if (terse)
- printf("%.8g\n", ans);
- else
- printf("\t* %.8g\n", ans);
- if (verbose)
- printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr);
- else if (!terse)
- printf("\t/ %.8g\n", 1/ans);
+ if (verbose) {
+ printf("\t%s = ", havestr);
+ printf(outputformat, ans);
+ printf(" * %s", wantstr);
+ printf("\n");
+ }
+ else if (terse) {
+ printf(outputformat, ans);
+ printf("\n");
+ }
+ else {
+ printf("\t* ");
+ printf(outputformat, ans);
+ printf("\n");
+ }
+
+ if (verbose) {
+ printf("\t%s = (1 / ", havestr);
+ printf(outputformat, 1/ans);
+ printf(") * %s\n", wantstr);
+ }
+ else if (!terse) {
+ printf("\t/ ");
+ printf(outputformat, 1/ans);
+ printf("\n");
+ }
}
}
@@ -706,7 +729,9 @@ usage(void)
static struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
+ {"exponential", no_argument, NULL, 'e'},
{"file", required_argument, NULL, 'f'},
+ {"output-format", required_argument, NULL, 'o'},
{"quiet", no_argument, NULL, 'q'},
{"terse", no_argument, NULL, 't'},
{"unitsfile", no_argument, NULL, 'U'},
@@ -731,8 +756,12 @@ main(int argc, char **argv)
quiet = false;
readfile = false;
- while ((optchar = getopt_long(argc, argv, "+hf:qtvUV", longopts, NULL)) != -1) {
+ outputformat = "%.8g";
+ while ((optchar = getopt_long(argc, argv, "+ehf:oqtvUV", longopts, NULL)) != -1) {
switch (optchar) {
+ case 'e':
+ outputformat = "%6e";
+ break;
case 'f':
readfile = true;
if (strlen(optarg) == 0)
@@ -746,6 +775,9 @@ main(int argc, char **argv)
case 't':
terse = true;
break;
+ case 'o':
+ outputformat = optarg;
+ break;
case 'v':
verbose = true;
break;
OpenPOWER on IntegriCloud