lobase

Linux port of OpenBSDs userland.
Log | Files | Refs | README

commit 68d7b2b9a855c870f456e0d9e06c569cac0fc9e5
parent dba51b13d1e09fb5372f2ba2353f6e740dcbf342
Author: Duncaen <mail@duncano.de>
Date:   Wed,  8 Mar 2017 23:20:21 +0100

usr.bin/units: import

Diffstat:
usr.bin/Makefile | 4++--
usr.bin/units/Makefile | 11+++++++++++
usr.bin/units/README | 19+++++++++++++++++++
usr.bin/units/units.1 | 244+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
usr.bin/units/units.c | 720+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
usr.bin/units/units.lib | 768+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 1764 insertions(+), 2 deletions(-)

diff --git a/usr.bin/Makefile b/usr.bin/Makefile @@ -3,8 +3,8 @@ SUBDIR= apply awk basename bc biff cal calendar cmp colrm col column comm \ cut dc dirname du diff3 diff env expand false file fmt fold getopt \ grep head hexdump id indent join jot lam lndir logger logname look \ mktemp nice nl nohup paste patch printenv printf readlink renice rev \ - rs sed shar sort split stat tee time touch tr true tsort tty ul uname \ - unexpand uniq unvis uudecode uuencode vis wc what which xinstall \ + rs sed shar sort split stat tee time touch tr true tsort tty ul units \ + uname unexpand uniq unvis uudecode uuencode vis wc what which xinstall \ hexdump cu newsyslog sdiff SKIPDIR=file cu include ${.TOPDIR}/mk/bsd.subdir.mk diff --git a/usr.bin/units/Makefile b/usr.bin/units/Makefile @@ -0,0 +1,11 @@ +# $OpenBSD: Makefile,v 1.4 1997/04/27 20:56:56 millert Exp $ + +.TOPDIR?=../.. + +PROG= units + +beforeinstall: + ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 444 \ + ${.CURDIR}/units.lib ${DESTDIR}/usr/share/misc + +include ${.TOPDIR}/mk/bsd.prog.mk diff --git a/usr.bin/units/README b/usr.bin/units/README @@ -0,0 +1,19 @@ +# $OpenBSD: README,v 1.3 1996/06/26 05:42:14 deraadt Exp $ +# $NetBSD: README,v 1.2 1996/04/06 06:00:59 thorpej Exp $ + +This is a program which I wrote as a clone of the UNIX 'units' +command. I threw it together in a couple days, but it seems to work, +with some restrictions. I have tested it under DOS with Borland C and +Ultrix 4.2, and SunOS 4.1. + +This program differs from the unix units program in the following +ways: + it can gracefully handle exponents larger than 9 in output + it uses 'e' to denote exponentiation in numbers + prefixes are listed in the units file + it tries both -s and -es plurals + it allows use of * for multiply and ^ for exponentiation in the input + the output format is somewhat different + +Adrian Mariano (adrian@cam.cornell.edu or mariano@geom.umn.edu) + diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1 @@ -0,0 +1,244 @@ +.\" $OpenBSD: units.1,v 1.26 2014/09/08 01:27:55 schwarze Exp $ +.\" converted to new format by deraadt@openbsd.org +.\" +.\" Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu) +.\" +.\" 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. The name of the author may not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" Disclaimer: This software is provided by the author "as is". The author +.\" shall not be liable for any damages caused in any way by this software. +.\" +.\" I would appreciate (though I do not require) receiving a copy of any +.\" improvements you might make to this program. +.\" +.Dd $Mdocdate: September 8 2014 $ +.Dt UNITS 1 +.Os +.Sh NAME +.Nm units +.Nd conversion program +.Sh SYNOPSIS +.Nm units +.Op Fl qv +.Op Fl f Ar filename +.Op Oo Ar count Oc Ar from-unit to-unit +.Sh DESCRIPTION +The +.Nm +program converts quantities expressed in various scales to +their equivalents in other scales. +The +.Nm +program can only handle multiplicative scale changes. +It cannot convert Celsius +to Fahrenheit, for example. +It also does not handle logarithmic units such as bels. +.Pp +.Nm +works interactively by prompting the user for input: +.Bd -literal -offset indent +You have: meters +You want: feet + * 3.2808399 + / 0.3048 + +You have: cm^3 +You want: gallons + * 0.00026417205 + / 3785.4118 +.Ed +.Pp +That is, one meter equals roughly 3.28 feet +(or, conversely, one foot is roughly equal to 0.3 meters), +and one gallon is roughly equal to 3785 cubic cm. +.Pp +The +.Nm +program can handle numbers as well: +.Bd -literal -offset indent +You have: 60 miles/hr +You want: km/hr + * 96.56064 + / 0.010356187 + +You have: 100 USD +You want: 10 euro + * 7.4354971 + / 0.13449 +.Ed +.Pp +In other words, 60 miles per hour is about 96.6 km/hr, +and 100 US dollars will get you seven 10-Euro notes. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl f Ar filename +Specifies the name of the units data file to load. +.It Fl q +Suppresses prompting of the user for units and the display of statistics +about the number of units loaded. +.It Fl v +Prints the version number. +.It Oo Ar count Oc Ar from-unit to-unit +Allows a single unit conversion to be done directly from the command line. +No prompting will occur. +The units program will print out +only the result of this single conversion. +A +.Ar count +can be given to specify multiples of +.Ar from-unit . +.El +.Pp +Powers of units can be specified using the +.Ql ^ +character as shown in +the example, or by simple concatenation: +.Sq cm3 +is equivalent to +.Sq cm^3 . +Multiplication of units can be specified by using spaces, a dash or +an asterisk. +Division of units is indicated by the slash +.Pq Ql / . +Note that multiplication has a higher precedence than division, so +.Sq m/s/s +is the same as +.Sq m/s^2 +or +.Sq m/s s . +If the user enters incompatible unit types, the units program will +print a message indicating that the units are not conformable and +it will display the reduced form for each unit: +.Bd -literal -offset indent +You have: ergs/hour +You want: fathoms kg^2 / day +conformability error + 2.7777778e-11 kg m^2 / sec^3 + 2.1166667e-05 kg^2 m / sec +.Ed +.Pp +The conversion information is read from a units data file. +The default file includes definitions for most familiar units, +abbreviations and metric prefixes. +Some constants of nature included are: +.Pp +.Bl -tag -width mercuryXXX -offset indent -compact +.It pi +ratio of circumference to diameter +.It c +speed of light +.It e +charge on an electron +.It g +acceleration of gravity +.It force +same as g +.It mole +Avogadro's number +.It water +pressure per unit height of water (at 4 C) +.It mercury +pressure per unit height of mercury +.It ao +Bohr radius +.It AU +astronomical unit +.El +.Pp +.Sq Pound +is a unit of mass. +Compound names are run together so +.Sq poundforce +is a unit of force. +British units that differ from their US counterparts are prefixed with +.Sq br , +and currency is prefixed with its country name: +.Sq belgiumfranc , +.Sq britainpound . +When searching for +a unit, if the specified string does not appear exactly as a unit +name, then the units program will try to remove a trailing +.Sq s +or a trailing +.Sq es +and check again for a match. +.Pp +All of these definitions can be read in the standard units file, or you +can supply your own file. +A unit is specified on a single line by +giving its name and an equivalence. +One should be careful to define +new units in terms of old ones so that a reduction leads to the +primitive units which are marked with +.Ql \&! +characters. +The units program will not detect infinite loops that could be caused +by careless unit definitions. +.Pp +Prefixes are defined in the same way as standard units, but with +a trailing dash at the end of the prefix name. +Prefixes are applied +after the longest matching unit name is found; for example, +.Dq nmile +is taken to be a nautical mile rather than a nanomile. +.Sh FILES +.Bl -tag -width /usr/share/misc/units.lib +.It Pa /usr/share/misc/units.lib +the standard units library +.El +.Sh SEE ALSO +.Rs +.%I International Bureau of Weights and Measures +.%T The International System of Units +.%U http://www.bipm.org/utils/common/pdf/si_brochure_8.pdf +.Re +.Rs +.%R ISO 4217 +.%T Codes for the representation of currencies and funds +.%D 2008 +.Re +.Rs +.%R ISO/IEC 80000 +.%T Quantities and units \(em Part 13: Information science and technology +.Re +.Sh AUTHORS +.An Adrian Mariano Aq Mt adrian@cam.cornell.edu +or +.Aq Mt mariano@geom.umn.edu +.Sh BUGS +The effect of including a +.Ql / +in a prefix is surprising. +.Pp +Exponents of units entered by the user can be only one digit. +You can work around this by multiplying several terms. +.Pp +The user must use +.Ql | +to indicate division of numbers and +.Ql / +to indicate division of symbols. +This distinction should not be necessary. +.Pp +Prefixes specified without a unit are treated as dimensionless quantities. +This can lead to confusion when some prefixes are also defined as units +(e.g., m). +For example, Tera- / Giga- is 1000, but one Tesla (T) is 10,000 +Gauss (G). +.Pp +Some non-SI units have multiple definitions (e.g., barrel, calorie) and +others have changed over time (e.g., cubit). +In particular, monetary values fluctuate. +.Pp +The program contains various arbitrary limits on the length +of the units converted and on the length of the data file. +.Pp +The program should use a hash table to store units so that +it doesn't take so long to load the units list and check +for duplication. diff --git a/usr.bin/units/units.c b/usr.bin/units/units.c @@ -0,0 +1,720 @@ +/* $OpenBSD: units.c,v 1.22 2015/10/09 01:37:09 deraadt Exp $ */ +/* $NetBSD: units.c,v 1.6 1996/04/06 06:01:03 thorpej Exp $ */ + +/* + * units.c Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu) + * + * 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. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * Disclaimer: This software is provided by the author "as is". The author + * shall not be liable for any damages caused in any way by this software. + * + * I would appreciate (though I do not require) receiving a copy of any + * improvements you might make to this program. + */ + +#include <ctype.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <err.h> + +#define UNITSFILE "/usr/share/misc/units.lib" + +#define VERSION "1.0" + +#define MAXUNITS 1000 +#define MAXPREFIXES 100 + +#define MAXSUBUNITS 500 + +#define PRIMITIVECHAR '!' + +char *powerstring = "^"; + +struct { + char *uname; + char *uval; +} unittable[MAXUNITS]; + +struct unittype { + char *numerator[MAXSUBUNITS]; + char *denominator[MAXSUBUNITS]; + double factor; +}; + +struct { + char *prefixname; + char *prefixval; +} prefixtable[MAXPREFIXES]; + + +char *NULLUNIT = ""; + +int unitcount; +int prefixcount; + +char *dupstr(char *); +void readunits(char *); +void initializeunit(struct unittype *); +int addsubunit(char *[], char *); +void showunit(struct unittype *); +void zeroerror(void); +int addunit(struct unittype *, char *, int); +int compare(const void *, const void *); +void sortunit(struct unittype *); +void cancelunit(struct unittype *); +char *lookupunit(char *); +int reduceproduct(struct unittype *, int); +int reduceunit(struct unittype *); +int compareproducts(char **, char **); +int compareunits(struct unittype *, struct unittype *); +int completereduce(struct unittype *); +void showanswer(struct unittype *, struct unittype *); +void usage(void); + +char * +dupstr(char *str) +{ + char *ret; + + ret = strdup(str); + if (!ret) { + fprintf(stderr, "Memory allocation error\n"); + exit(3); + } + return (ret); +} + + +void +readunits(char *userfile) +{ + char line[512], *lineptr; + int len, linenum, i; + FILE *unitfile; + + unitcount = 0; + linenum = 0; + + if (userfile) { + unitfile = fopen(userfile, "r"); + if (!unitfile) { + fprintf(stderr, "Unable to open units file '%s'\n", + userfile); + exit(1); + } + } else { + unitfile = fopen(UNITSFILE, "r"); + if (!unitfile) { + fprintf(stderr, "Can't find units file '%s'\n", + UNITSFILE); + exit(1); + } + } + while (!feof(unitfile)) { + if (!fgets(line, sizeof(line), unitfile)) + break; + linenum++; + lineptr = line; + if (*lineptr == '/') + continue; + lineptr += strspn(lineptr, " \n\t"); + len = strcspn(lineptr, " \n\t"); + lineptr[len] = 0; + if (!strlen(lineptr)) + continue; + if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */ + if (prefixcount == MAXPREFIXES) { + fprintf(stderr, + "Memory for prefixes exceeded in line %d\n", + linenum); + continue; + } + + lineptr[strlen(lineptr) - 1] = 0; + for (i = 0; i < prefixcount; i++) { + if (!strcmp(prefixtable[i].prefixname, lineptr)) + break; + } + if (i < prefixcount) { + fprintf(stderr, "Redefinition of prefix '%s' " + "on line %d ignored\n", lineptr, linenum); + continue; /* skip duplicate prefix */ + } + + prefixtable[prefixcount].prefixname = dupstr(lineptr); + lineptr += len + 1; + lineptr += strspn(lineptr, " \n\t"); + len = strcspn(lineptr, "\n\t"); + if (len == 0) { + fprintf(stderr, "Unexpected end of prefix on " + "line %d\n", linenum); + free(prefixtable[prefixcount].prefixname); + continue; + } + lineptr[len] = 0; + prefixtable[prefixcount++].prefixval = dupstr(lineptr); + } else { /* it's not a prefix */ + if (unitcount == MAXUNITS) { + fprintf(stderr, + "Memory for units exceeded in line %d\n", + linenum); + continue; + } + + for (i = 0; i < unitcount; i++) { + if (!strcmp(unittable[i].uname, lineptr)) + break; + } + if (i < unitcount) { + fprintf(stderr, "Redefinition of unit '%s' " + "on line %d ignored\n", lineptr, linenum); + continue; /* skip duplicate unit */ + } + + unittable[unitcount].uname = dupstr(lineptr); + lineptr += len + 1; + lineptr += strspn(lineptr, " \n\t"); + if (!strlen(lineptr)) { + fprintf(stderr, "Unexpected end of unit on " + "line %d\n", linenum); + free(unittable[unitcount].uname); + continue; + } + len = strcspn(lineptr, "\n\t"); + lineptr[len] = 0; + unittable[unitcount++].uval = dupstr(lineptr); + } + } + fclose(unitfile); +} + +void +initializeunit(struct unittype *theunit) +{ + theunit->factor = 1.0; + theunit->numerator[0] = theunit->denominator[0] = NULL; +} + + +int +addsubunit(char *product[], char *toadd) +{ + char **ptr; + + for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++); + if (ptr >= product + MAXSUBUNITS) { + fprintf(stderr, "Memory overflow in unit reduction\n"); + return 1; + } + if (!*ptr) + *(ptr + 1) = 0; + *ptr = dupstr(toadd); + return 0; +} + + +void +showunit(struct unittype *theunit) +{ + char **ptr; + int printedslash; + int counter = 1; + + printf("\t%.8g", theunit->factor); + for (ptr = theunit->numerator; *ptr; ptr++) { + if (ptr > theunit->numerator && **ptr && + !strcmp(*ptr, *(ptr - 1))) + counter++; + else { + if (counter > 1) + printf("%s%d", powerstring, counter); + if (**ptr) + printf(" %s", *ptr); + counter = 1; + } + } + if (counter > 1) + printf("%s%d", powerstring, counter); + counter = 1; + printedslash = 0; + for (ptr = theunit->denominator; *ptr; ptr++) { + if (ptr > theunit->denominator && **ptr && + !strcmp(*ptr, *(ptr - 1))) + counter++; + else { + if (counter > 1) + printf("%s%d", powerstring, counter); + if (**ptr) { + if (!printedslash) + printf(" /"); + printedslash = 1; + printf(" %s", *ptr); + } + counter = 1; + } + } + if (counter > 1) + printf("%s%d", powerstring, counter); + printf("\n"); +} + + +void +zeroerror(void) +{ + fprintf(stderr, "Unit reduces to zero\n"); +} + +/* + Adds the specified string to the unit. + Flip is 0 for adding normally, 1 for adding reciprocal. + + Returns 0 for successful addition, nonzero on error. +*/ + +int +addunit(struct unittype *theunit, char *toadd, int flip) +{ + char *scratch, *savescr; + char *item; + char *divider, *slash; + int doingtop; + + savescr = scratch = dupstr(toadd); + for (slash = scratch + 1; *slash; slash++) + if (*slash == '-' && + (tolower((unsigned char)*(slash - 1)) != 'e' || + !strchr(".0123456789", *(slash + 1)))) + *slash = ' '; + slash = strchr(scratch, '/'); + if (slash) + *slash = 0; + doingtop = 1; + do { + item = strtok(scratch, " *\t\n/"); + while (item) { + if (strchr("0123456789.", *item)) { /* item is a number */ + double num; + + divider = strchr(item, '|'); + if (divider) { + *divider = 0; + num = atof(item); + if (!num) { + zeroerror(); + free(savescr); + return 1; + } + if (doingtop ^ flip) + theunit->factor *= num; + else + theunit->factor /= num; + num = atof(divider + 1); + if (!num) { + zeroerror(); + free(savescr); + return 1; + } + if (doingtop ^ flip) + theunit->factor /= num; + else + theunit->factor *= num; + } else { + num = atof(item); + if (!num) { + zeroerror(); + free(savescr); + return 1; + } + if (doingtop ^ flip) + theunit->factor *= num; + else + theunit->factor /= num; + + } + } else { /* item is not a number */ + int repeat = 1; + + if (strchr("23456789", + item[strlen(item) - 1])) { + repeat = item[strlen(item) - 1] - '0'; + item[strlen(item) - 1] = 0; + } + for (; repeat; repeat--) + if (addsubunit(doingtop ^ flip + ? theunit->numerator + : theunit->denominator, item)) { + free(savescr); + return 1; + } + } + item = strtok(NULL, " *\t/\n"); + } + doingtop--; + if (slash) { + scratch = slash + 1; + } else + doingtop--; + } while (doingtop >= 0); + free(savescr); + return 0; +} + + +int +compare(const void *item1, const void *item2) +{ + return strcmp(*(char **) item1, *(char **) item2); +} + + +void +sortunit(struct unittype *theunit) +{ + char **ptr; + int count; + + for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++); + qsort(theunit->numerator, count, sizeof(char *), compare); + for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++); + qsort(theunit->denominator, count, sizeof(char *), compare); +} + + +void +cancelunit(struct unittype *theunit) +{ + char **den, **num; + int comp; + + den = theunit->denominator; + num = theunit->numerator; + + while (*num && *den) { + comp = strcmp(*den, *num); + if (!comp) { + *den++ = NULLUNIT; + *num++ = NULLUNIT; + } else if (comp < 0) + den++; + else + num++; + } +} + + + + +/* + Looks up the definition for the specified unit. + Returns a pointer to the definition or a null pointer + if the specified unit does not appear in the units table. +*/ + +static char buffer[500]; /* buffer for lookupunit answers with + prefixes */ + +char * +lookupunit(char *unit) +{ + size_t len; + int i; + char *copy; + + for (i = 0; i < unitcount; i++) { + if (!strcmp(unittable[i].uname, unit)) + return unittable[i].uval; + } + + len = strlen(unit); + if (len == 0) + return NULL; + if (unit[len - 1] == '^') { + copy = dupstr(unit); + copy[len - 1] = '\0'; + for (i = 0; i < unitcount; i++) { + if (!strcmp(unittable[i].uname, copy)) { + strlcpy(buffer, copy, sizeof(buffer)); + free(copy); + return buffer; + } + } + free(copy); + } + if (unit[len - 1] == 's') { + copy = dupstr(unit); + copy[len - 1] = '\0'; + --len; + for (i = 0; i < unitcount; i++) { + if (!strcmp(unittable[i].uname, copy)) { + strlcpy(buffer, copy, sizeof(buffer)); + free(copy); + return buffer; + } + } + if (len != 0 && copy[len - 1] == 'e') { + copy[len - 1] = 0; + for (i = 0; i < unitcount; i++) { + if (!strcmp(unittable[i].uname, copy)) { + strlcpy(buffer, copy, sizeof(buffer)); + free(copy); + return buffer; + } + } + } + free(copy); + } + for (i = 0; i < prefixcount; i++) { + len = strlen(prefixtable[i].prefixname); + if (!strncmp(prefixtable[i].prefixname, unit, len)) { + if (!strlen(unit + len) || lookupunit(unit + len)) { + snprintf(buffer, sizeof(buffer), "%s %s", + prefixtable[i].prefixval, unit + len); + return buffer; + } + } + } + return NULL; +} + + + +/* + reduces a product of symbolic units to primitive units. + The three low bits are used to return flags: + + bit 0 (1) set on if reductions were performed without error. + bit 1 (2) set on if no reductions are performed. + bit 2 (4) set on if an unknown unit is discovered. +*/ + + +#define ERROR 4 + +int +reduceproduct(struct unittype *theunit, int flip) +{ + char *toadd, **product; + int didsomething = 2; + + if (flip) + product = theunit->denominator; + else + product = theunit->numerator; + + for (; *product; product++) { + + for (;;) { + if (!strlen(*product)) + break; + toadd = lookupunit(*product); + if (!toadd) { + printf("unknown unit '%s'\n", *product); + return ERROR; + } + if (strchr(toadd, PRIMITIVECHAR)) + break; + didsomething = 1; + if (*product != NULLUNIT) { + free(*product); + *product = NULLUNIT; + } + if (addunit(theunit, toadd, flip)) + return ERROR; + } + } + return didsomething; +} + + +/* + Reduces numerator and denominator of the specified unit. + Returns 0 on success, or 1 on unknown unit error. +*/ + +int +reduceunit(struct unittype *theunit) +{ + int ret; + + ret = 1; + while (ret & 1) { + ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1); + if (ret & 4) + return 1; + } + return 0; +} + + +int +compareproducts(char **one, char **two) +{ + while (*one || *two) { + if (!*one && *two != NULLUNIT) + return 1; + if (!*two && *one != NULLUNIT) + return 1; + if (*one == NULLUNIT) + one++; + else if (*two == NULLUNIT) + two++; + else if (strcmp(*one, *two)) + return 1; + else + one++, two++; + } + return 0; +} + + +/* Return zero if units are compatible, nonzero otherwise */ + +int +compareunits(struct unittype *first, struct unittype *second) +{ + return compareproducts(first->numerator, second->numerator) || + compareproducts(first->denominator, second->denominator); +} + + +int +completereduce(struct unittype *unit) +{ + if (reduceunit(unit)) + return 1; + sortunit(unit); + cancelunit(unit); + return 0; +} + + +void +showanswer(struct unittype *have, struct unittype *want) +{ + if (compareunits(have, want)) { + printf("conformability error\n"); + showunit(have); + showunit(want); + } else + printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor, + want->factor / have->factor); +} + + +void +usage(void) +{ + fprintf(stderr, + "usage: units [-qv] [-f filename] [[count] from-unit to-unit]\n"); + exit(3); +} + + +int +main(int argc, char **argv) +{ + + struct unittype have, want; + char havestr[81], wantstr[81]; + int optchar; + char *userfile = 0; + int quiet = 0; + + extern char *optarg; + extern int optind; + + if (pledge("stdio rpath", NULL) == -1) + err(1, "pledge"); + + while ((optchar = getopt(argc, argv, "vqf:")) != -1) { + switch (optchar) { + case 'f': + userfile = optarg; + break; + case 'q': + quiet = 1; + break; + case 'v': + fprintf(stderr, + "units version %s Copyright (c) 1993 by Adrian Mariano\n", + VERSION); + fprintf(stderr, + "This program may be freely distributed\n"); + usage(); + default: + usage(); + break; + } + } + + argc -= optind; + argv += optind; + + if (argc != 3 && argc != 2 && argc != 0) + usage(); + + readunits(userfile); + + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + + if (argc == 3) { + strlcpy(havestr, argv[0], sizeof(havestr)); + strlcat(havestr, " ", sizeof(havestr)); + strlcat(havestr, argv[1], sizeof(havestr)); + argc--; + argv++; + argv[0] = havestr; + } + + if (argc == 2) { + strlcpy(havestr, argv[0], sizeof(havestr)); + strlcpy(wantstr, argv[1], sizeof(wantstr)); + initializeunit(&have); + addunit(&have, havestr, 0); + completereduce(&have); + initializeunit(&want); + addunit(&want, wantstr, 0); + completereduce(&want); + showanswer(&have, &want); + } else { + if (!quiet) + printf("%d units, %d prefixes\n", unitcount, + prefixcount); + for (;;) { + do { + initializeunit(&have); + if (!quiet) + printf("You have: "); + if (!fgets(havestr, sizeof(havestr), stdin)) { + if (!quiet) + putchar('\n'); + exit(0); + } + } while (addunit(&have, havestr, 0) || + completereduce(&have)); + do { + initializeunit(&want); + if (!quiet) + printf("You want: "); + if (!fgets(wantstr, sizeof(wantstr), stdin)) { + if (!quiet) + putchar('\n'); + exit(0); + } + } while (addunit(&want, wantstr, 0) || + completereduce(&want)); + showanswer(&have, &want); + } + } + return (0); +} diff --git a/usr.bin/units/units.lib b/usr.bin/units/units.lib @@ -0,0 +1,768 @@ +/ $OpenBSD: units.lib,v 1.88 2016/07/01 07:00:02 jmc Exp $ +/ $NetBSD: units.lib,v 1.3 1996/04/06 06:01:04 thorpej Exp $ + +/ primitive units +m !a! +kg !b! +sec !c! +coul !d! +candela !e! +dollar !f! +bit !h! +erlang !i! +K !j! + +/ International System of Units (SI) prefixes +yotta- 1e24 +zetta- 1e21 +exa- 1e18 +peta- 1e15 +tera- 1e12 +giga- 1e9 +mega- 1e6 +kilo- 1e3 +hecto- 1e2 +deca- 1e1 +deka- deca +deci- 1e-1 +centi- 1e-2 +milli- 1e-3 +micro- 1e-6 +nano- 1e-9 +pico- 1e-12 +femto- 1e-15 +atto- 1e-18 +zepto- 1e-21 +yocto- 1e-24 + +/ SI symbols +Y- yotta +Z- zetta +E- exa +P- peta +T- tera +G- giga +M- mega +k- kilo +h- hecto +da- deca +d- deci +c- centi +m- milli +n- nano +p- pico +f- femto +a- atto +z- zepto +y- yocto + +/ ISO/IEC 80000 binary prefixes +yobi- 1208925819614629174706176 +zebi- 1180591620717411303424 +exbi- 1152921504606846976 +pebi- 1125899906842624 +tebi- 1099511627776 +gibi- 1073741824 +mebi- 1048576 +kibi- 1024 + +/ ISO/IEC 80000 symbols +Yi- yobi +Zi- zebi +Ei- exbi +Pi- pebi +Ti- tebi +Gi- gibi +Mi- mebi +Ki- kibi + +/ Other prefixes +myria- 1e4 +semi- .5 +demi- .5 + +/ constants + +fuzz 1 +pi 3.14159265358979323846 +c 2.99792458e+8 m/sec +g 9.80665 m/sec2 +bigG 6.67390e-11 m3/kg/s2 +AU 1.495978707e+11 m +au AU +mole 6.0221367e+23 fuzz +e 1.60217733e-19 coul fuzz +/ Next two to be multiplied by mass +energy c2 +force g +mercury 1.3332239e+5 kg/m2-sec2 +hg mercury +mmHg 0.001 m hg + +mu 4.e-7 pi-N/A2 +epsilon 1.0 /mu/c2 +alpha 0.5 mu-c-e2/planck +planck 6.6260755e-34 joule-sec +hbar 0.5 planck/pi +electronmass 9.1093897e-31 kg +protonmass 1.6726231e-27 kg +neutronmass 1.6749286e-27 kg + + +/ dimensionless + +radian .5 / pi +degree 1|180 pi-radian +circle 2 pi-radian +turn 2 pi-radian +revolution turn +rev turn +grade .9 degree +arcdeg 1 degree +arcmin 1|60 arcdeg +ccs 1|36 erlang +arcsec 1|60 arcmin + +steradian radian2 +sphere 4 pi-steradian +sr steradian + +/ Time + +second sec +s sec +minute 60 sec +min minute +hour 60 min +hr hour +day 24 hr +da day +week 7 day +year 3.15569259747e7 sec fuzz +yr year +month 1|12 year +ms millisec +us microsec + +/ Mass + +gram millikg +gm gram +mg milligram +metricton kilokg + +/ Avoirdupois + +lb .45359237 kg +pound lb +lbf lb g +ounce 1|16 lb +oz ounce +dram 1|16 oz +dr dram +grain 1|7000 lb +gr grain +shortton 2000 lb +ton shortton +longton 2240 lb + +/ Apothecary + +scruple 20 grain +apdram 60 grain +apounce 480 grain +appound 5760 grain +troypound appound + +/ Length + +meter m +micron micrometer +angstrom decinanometer +ao 0.25 alpha/pi/rydbergconst + +inch 2.54 cm +in inch +foot 12 in +feet foot +ft foot +yard 3 ft +yd yard +rod 5.5 yd +rd rod +mile 5280 ft +mi mile + +british 1200|3937 m/ft +/ Assume we do not want the prefix n- with mile +nmile 1852 m + +acre 4840 yd2 + +ox acre/day + +cc cm3 +liter kilocc +ml milliliter + +/ US Liquid + +gallon 231 in3 +imperial 1.20095 +gal gallon +quart 1|4 gal +qt quart +pint 1|2 qt +pt pint + +floz 1|16 pt +fldr 1|8 floz + +/ US Dry + +dry 268.8025 in3/gallon fuzz +peck 8 dry-quart +pk peck +bushel 4 peck +bu bushel +chaldron 36 bushel + +/ British + +brgallon 277.420 in3 fuzz +brquart 1|4 brgallon +brpint 1|2 brquart +brfloz 1|20 brpint +brpeck 554.84 in3 fuzz +brbushel 4 brpeck + +/ Russian +berkovets 10 pood +pood 40 funt +funt 0.40951 kg +lot 1|32 funt +zolotnik 1|3 lot +dolya 1|96 zolotnik +rumile 7 verst +mezhevayaverst 2 verst +verst 1066.8 m +sazhen 1|500 verst +kosayasazhen 1|430.2 verst +arshin 1|1500 verst +/ is not exactly defined +ruell 16.54 in +liniya 1|10 in +vershok 1.75 in +pyad 7 in +vedro 12.3 liter +shtoff 1|10 vedro +vinebottle 1|16 vedro +vodkabottle 1|20 vedro +charka 1|100 vedro +shkalik 1|200 vedro +desyatina_state 109.3 are +desyatina_farmery 0.75 desyatina_state +sqverst 104.2 desyatina_state +sqarshin 1|21600 desyatina_state + +/ Energy Work + +newton kg-m/sec2 +nt newton +N newton +joule nt-m +J joule +cal 4.1868 joule + +/ Electrical + +coulomb coul +C coul +ampere coul/sec +amp ampere +A amp +watt joule/sec +W watt +volt watt/amp +ohm volt/amp +mho /ohm +farad coul/volt +F farad +henry sec2/farad +H henry +weber volt-sec +Wb weber + +/ Light + +cd candela +lumen cd sr +lux cd sr/m2 + +/ http://www.federalreserve.gov/releases/h10/current/ +/ as of June 24, 2016 +$ dollar +australiadollar .7451 $ +brazilreal 1|3.3895 $ +canadadollar 1|1.2948 $ +chinayuan 1|6.6150 $ +denmarkkrone 1|6.6849 $ +euro 1.1126 $ +hongkongdollar 1|7.7587 $ +indiarupee 1|67.8200 $ +japanyen 1|102.2600 $ +malaysiaringgit 1|4.0900 $ +mexicopeso 1|18.8545 $ +newzealanddollar .7109 $ +norwaykrone 1|8.4412 $ +singaporedollar 1|1.3487 $ +southafricarand 1|15.0000 $ +southkoreawon 1|1167.5600 $ +srilankarupee 1|146.6000 $ +swedenkrona 1|8.4345 $ +switzerlandfranc 1|0.9702 $ +taiwandollar 1|32.3600 $ +thailandbaht 1|35.3200 $ +unitedkingdompound 1.3639 $ +venezuelabolivar 1|9.9750 $ + +/ The Economist, June 29, 2016 +argentinapeso 1|14.9 $ +chilepeso 1|660 $ +colombiapeso 1|2910 $ +czechkoruna 1|24.4 $ +egyptpound 1|8.88 $ +hungaryforint 1|285 $ +icelandkrona 1|123 $ +indonesiarupiah 1|13162 $ +israelshekel 1|3.85 $ +pakistanrupee 1|105 $ +perunuevosol 1|3.30 $ +philippinespeso 1|47.0 $ +polandzloty 1|3.98 $ +russiarouble 1|63.7 $ +saudiarabiariyal 1|3.75 $ +turkeylira 1|2.89 $ +ukrainehryvnia 1|24.9 $ +vietnamdong 1|22315 $ + +/ EMU currencies have constant exchange rate against Euro since 1.1.1999 +austriaschilling 1|13.7603 euro +belgiumfranc 1|40.3399 euro +cypruspound 1|0.585274 euro +finlandmarkka 1|5.94573 euro +francefranc 1|6.55957 euro +germanymark 1|1.95583 euro +greecedrachma 1|340.750 euro +irelandpunt 1|0.787564 euro +italylira 1|1936.27 euro +luxembourgfranc 1|40.3399 euro +maltalira 1|0.429300 euro +netherlandsguilder 1|2.20371 euro +portugalescudo 1|200.482 euro +spainpeseta 1|166.386 euro +slovakiakoruna 1|30.1260 euro +sloveniatolar 1|239.640 euro + +/ currency aliases +chinarenminbi chinayuan +markka finlandmarkka +franc francefranc +mark germanymark +drachma greecedrachma +rupee indiarupee +lira italylira +yen japanyen +peso mexicopeso +guilder netherlandsguilder +hollandguilder netherlandsguilder +sol perunuevosol +escudo portugalescudo +rand southafricarand +peseta spainpeseta +britainpound unitedkingdompound +greatbritainpound unitedkingdompound +poundsterling unitedkingdompound +bolivar venezuelabolivar + +/ ISO 4217 +ARS argentinapeso +AUD australiadollar +BRL brazilreal +CAD canadadollar +CHF switzerlandfranc +CLP chilepeso +CNY chinayuan +COP colombiapeso +CZK czechkoruna +DKK denmarkkrone +EGP egyptpound +EUR euro +GBP unitedkingdompound +HKD hongkongdollar +HUF hungaryforint +IDR indonesiarupiah +INR indiarupee +ILS israelshekel +ISK icelandkrona +JPY japanyen +KRW southkoreawon +LKR srilankarupee +MXN mexicopeso +MYR malaysiaringgit +NOK norwaykrone +NZD newzealanddollar +PEN perunuevosol +PHP philippinespeso +PKR pakistanrupee +PLN polandzloty +RUB russiarouble +SAR saudiarabiariyal +SEK swedenkrona +SGD singaporedollar +THB thailandbaht +TRY turkeylira +TWD taiwandollar +UAH ukrainehryvnia +USD dollar +VEF venezuelabolivar +VND vietnamdong +ZAR southafricarand + +/ computer + +baud bit/sec +byte 8 bit +/ Not necessarily true on all systems! +word 2 byte +block 512 byte +kbyte 1024 byte +megabyte 1024 kbyte +gigabyte 1024 megabyte +meg megabyte + +/ Trivia + +% 1|100 +abampere 10 ampere +admiraltyknot 6080 ft/hr +apostilb cd/pi-m2 +asb apostilb +are 1e+2 m2 +arpentcan 27.52 mi +arpentlin 191.835 ft +astronomicalunit AU +atmosphere 1.01325e+5 nt/m2 +atm atmosphere +atomicmassunit 1.6605402e-27 kg fuzz +amu atomicmassunit +bag 94 lb +bakersdozen 13 +bar 1e+5 nt/m2 +barie 1e-1 nt/m2 +barleycorn 1|3 in +barn 1e-28 m2 +/ oil barrel, nothing else +barrel 42 gal +barye 1e-1 nt/m2 +baryl microbar +becquerel /s +bev 1e+9 e-volt +biot 10 amp +blondel cd/pi-m2 +boardfoot 144 in3 +/ Defn of bolt is also an area, but depends on type of cloth +bolt 40 yd +bottommeasure 1|40 in +Bq becquerel +britishthermalunit 1.05506e+3 joule fuzz +btu britishthermalunit +refrigeration 12000 btu/ton-hour +buck dollar +cable 720 ft +caliber 1e-2 in +calorie cal +carat 205 mg +caratgold 1|24 +cent centidollar +cental 100 lb +centesimalminute 1e-2 grade +centesimalsecond 1e-4 grade +century 100 year +cfs ft3/sec +chain 66 ft +Ci curie +circularinch 1|4 pi-in2 +circularmil 1e-6|4 pi-in2 +clusec 1e-8 mm-hg m3/s +coomb 4 bu +cord 128 ft3 +cordfoot cord +crith 9.06e-2 gm +cubit 18 in +cup 1|2 pt +curie 3.7e+10 /sec +dalton amu +decade 10 yr +diopter dioptre +dioptre /m +displacementton 35 ft3 +doppelzentner 100 kg +dozen 12 +drop .03 cm3 +dyne cm-gm/sec2 +electronvolt e-volt +ell 45 in +engineerschain 100 ft +engineerslink 100|100 ft +equivalentfootcandle lumen/pi-ft2 +equivalentlux lumen/pi-m2 +equivalentphot cd/pi-cm2 +erg cm2-gm/sec2 +ev e-volt +faraday 9.6485309e+4 coul +fathom 6 ft +fermi 1e-15 m +fifth 4|5 qt +fin 5 dollar +finger 7|8 in +firkin 9 gal +footcandle lumen/ft2 +footlambert cd/pi-ft2 +fortnight 14 da +franklin 3.33564e-10 coul +frigorie kilocal +furlong 220 yd +galileo 1e-2 m/sec2 +gamma 1e-9 weber/m2 +gauss 1e-4 weber/m2 +G gauss +geodeticfoot british-ft +geographicalmile 1852 m +gilbert 2.5 amp/pi +gill 1|4 pt +gray J/kg +gross 144 +gunterschain 22 yd +Gy gray +hand 4 in +hartree 2 rydberg +hectare 1e+4 m2 +hefnercandle .92 cd +hertz /sec +Hz hertz +hogshead 2 barrel +hd hogshead +homestead 1|4 mi2 +/ horsepower is not uniquely defined +horsepower 735.50 watt +hp horsepower +hubble 1e9 ly +hyl gm force sec2/m +hz /sec +imaginarycubicfoot 1.4 ft3 +jansky 1e-26 W/m2-Hz +jeroboam 4|5 gal +Jy jansky +karat 1|24 +kayser /cm +kcal kilocal +kcalorie kilocal +key kg +kilderkin 18 gal +knot nmile/hr +kn knot +lambert cd/pi-cm2 +Lb lambert +langley cal/cm2 +last 80 bu +league 3 mi +lightyear c-yr +ly lightyear +line 1|12 in +link 66|100 ft +longhundredweight 112 lb +longquarter 28 lb +lusec 1e-6 mm-hg m3/s +m_earth 5.97223e24 kg +m_moon 7.34e22 kg +m_sun 1.98843e30 kg +mach 331.46 m/sec +magnum 2 qt +marineleague 3 nmile +maxwell 1e-8 weber +Mx maxwell +metriccarat 200 mg +mgd megagal/day +mh millihenry +mil 1e-3 in +millennium 1000 year +minersinch 1.5 ft3/min +minim 1|60 fldr +mo month +mpg mile/gal +mph mile/hr +nail 1|16 yd +nauticalmile nmile +nit cd/m2 +noggin 1|8 qt +nox 1e-3 lux +ns nanosec +oersted 2.5e+2 amp/m/pi +Oe oersted +pace 36 in +palm 3 in +pair 2 +parasang 3.5 mi +parsec AU-radian/arcsec +pascal nt/m2 +Pa pascal +pc parsec +pennyweight 1|20 oz +pwt pennyweight +percent % +perch rd +phot lumen/cm2 +pica 1|6 in +pieze 1e+3 nt/m2 +pipe 4 barrel +point 1|72 in +poise gm/cm-sec +P poise +pole rd +pond 9.80665e-3 nt +poundal ft-lb/sec2 +pdl poundal +proof 1|200 +psi lb-g/in2 +quarter 9 in +quartersection 1|4 mi2 +quintal 100 kg +quire 25 +r_earth 6378.1370 km +r_moon 0.2725076 r_earth +rackunit 1.75 in +rad 100 erg/gm +ream 500 +registerton 100 ft3 +rehoboam 156 floz +rhe 10 m2/nt-sec +rem 0.01 J/kg +rontgen 2.58e-4 curie/kg +roentgen rontgen +R roentgen +rood 1.21e+3 yd +rope 20 ft +RU rackunit +rutherford 1e+6 /sec +rydbergconst 0.5 electronmass-c-alpha2/planck +rydberg rydbergconst-planck-c +sabin 1 ft2 +sack 3 bu +score 20 +seam 8 bu +section mi2 +shippington 40 ft3 +shorthundredweight 100 lb +shortquarter 25 lb +siemens /ohm +sievert J/kg +sigma microsec +skein 120 yd +skot 1e-3 apostilb +slug lb-g-sec2/ft +smoot 67 in +span 9 in +spat 4 pi sr +spindle 14400 yd +square 100 ft2 +stere m3 +sthene 1e+3 nt +stilb cd/cm2 +sb stilb +stoke 1e-4 m2/sec +stone 14 lb +strike 2 bu +surveyfoot british-ft +surveyyard 3 surveyfoot +surveyorschain 66 ft +surveyorslink 66|100 ft +Sv sievert +tablespoon 4 fldr +teaspoon 4|3 fldr +tesla weber/m2 +T tesla +therm 1e+5 btu +thermie 1e+6 cal +timberfoot ft3 +tnt 4.6e+6 m2/sec2 +tonne 1e+6 gm +torr mm hg +township 36 mi2 +tun 8 barrel +water gram g / cc +/ pumps are often in "feet head", assuming feet of water +head water +wey 40 bu +weymass 252 lb +Xunit 1.00206e-13 m +k 1.38047e-16 erg/degC + + +degC K +kelvin K +brewster 1e-12 m2/newton +degF 5|9 degC +degreesrankine degF +degrankine degreesrankine +degreerankine degF +degreaumur 10|8 degC +drachm 60 grain +poncelet 100 kg m g / sec +denier .05|450 gram / m +tex .001 gram / m +englishell 45 inch +scottishell 37.2 inch +flemishell 27 inch +V volt +eV ev +bohrradius ao +fresnel 1e12 hertz +statcoul 1|2.99792458e9 coul +statamp 1|2.99792458e9 amp +statvolt 2.99792458e2 volt +statcoulomb statcoul +statampere statamp +debye 3.33564e-30 coul-m +pulsatance 2 pi/sec +rpm rev/minute +rps rev/sec +kilohm kiloohm +megohm megaohm +siderealyear 365.256360417 day +siderealday 23.934469444 hour +siderealhour 1|24 siderealday +lunarmonth 29.5305555 day +synodicmonth lunarmonth +siderealmonth 27.32152777 day +tropicalyear year +solaryear year +lunaryear 12 lunarmonth +cran 37.5 brgallon +kip 1000 lbf +frenchfoot 16|15 ft +frenchfeet frenchfoot +toise 6 frenchfeet +candle 1.02 candela +militarypace 2.5 feet +metre meter +litre liter +gramme gram +iudiptheria 62.8 microgram +iupenicillin .6 microgram +iuinsulin 41.67 microgram +cottonyarncount 2520 ft/pound +linenyarncount 900 ft/pound +worstedyarncount 1680 ft/pound +metricyarncount meter/gram +jewlerspoint 2 milligram