2 * units.c Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. The name of the author may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
11 * Disclaimer: This software is provided by the author "as is". The author
12 * shall not be liable for any damages caused in any way by this software.
14 * I would appreciate (though I do not require) receiving a copy of any
15 * improvements you might make to this program.
17 * $FreeBSD: src/usr.bin/units/units.c,v 1.11 2008/08/16 16:27:41 dwmalone Exp $
27 #include "pathnames.h"
32 #define UNITSFILE _PATH_UNITSLIB
36 #define MAXPREFIXES 100
38 #define MAXSUBUNITS 500
40 #define PRIMITIVECHAR '!'
42 const char *powerstring = "^";
47 } unittable[MAXUNITS];
50 char *numerator[MAXSUBUNITS];
51 char *denominator[MAXSUBUNITS];
60 } prefixtable[MAXPREFIXES];
75 static int addsubunit(char *[], char *);
76 static int addunit(struct unittype *, char *, int, int);
77 static void cancelunit(struct unittype *);
78 static int compare(const void *, const void *);
79 static int compareproducts(char **, char **);
80 static int compareunits(struct unittype *, struct unittype *);
81 static int completereduce(struct unittype *);
82 static char *dupstr(const char *);
83 static void initializeunit(struct unittype *);
84 static char *lookupunit(const char *);
85 static void readunits(const char *);
86 static int reduceproduct(struct unittype *, int);
87 static int reduceunit(struct unittype *);
88 static void showanswer(struct unittype *, struct unittype *);
89 static void showunit(struct unittype *);
90 static void sortunit(struct unittype *);
91 static void usage(void);
92 static void zeroerror(void);
96 dupstr(const char *str)
100 ret = malloc(strlen(str) + 1);
102 errx(3, "memory allocation error");
109 readunits(const char *userfile)
112 char line[512], *lineptr;
119 unitfile = fopen(userfile, "rt");
121 errx(1, "unable to open units file '%s'", userfile);
124 unitfile = fopen(UNITSFILE, "rt");
129 env = getenv("PATH");
131 direc = strtok(env, SEPARATOR);
133 snprintf(filename, sizeof(filename),
134 "%s/%s", direc, UNITSFILE);
135 unitfile = fopen(filename, "rt");
138 direc = strtok(NULL, SEPARATOR);
142 errx(1, "can't find units file '%s'", UNITSFILE);
145 while (!feof(unitfile)) {
146 if (!fgets(line, sizeof(line), unitfile))
152 lineptr += strspn(lineptr, " \n\t");
153 len = strcspn(lineptr, " \n\t");
155 if (!strlen(lineptr))
157 if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
158 if (prefixcount == MAXPREFIXES) {
159 warnx("memory for prefixes exceeded in line %d", linenum);
162 lineptr[strlen(lineptr) - 1] = 0;
163 prefixtable[prefixcount].prefixname = dupstr(lineptr);
164 for (i = 0; i < prefixcount; i++)
165 if (!strcmp(prefixtable[i].prefixname, lineptr)) {
166 warnx("redefinition of prefix '%s' on line %d ignored",
171 lineptr += strspn(lineptr, " \n\t");
172 len = strcspn(lineptr, "\n\t");
174 warnx("unexpected end of prefix on line %d",
179 prefixtable[prefixcount++].prefixval = dupstr(lineptr);
181 else { /* it's not a prefix */
182 if (unitcount == MAXUNITS) {
183 warnx("memory for units exceeded in line %d", linenum);
186 unittable[unitcount].uname = dupstr(lineptr);
187 for (i = 0; i < unitcount; i++)
188 if (!strcmp(unittable[i].uname, lineptr)) {
189 warnx("redefinition of unit '%s' on line %d ignored",
194 lineptr += strspn(lineptr, " \n\t");
195 if (!strlen(lineptr)) {
196 warnx("unexpected end of unit on line %d",
200 len = strcspn(lineptr, "\n\t");
202 unittable[unitcount++].uval = dupstr(lineptr);
209 initializeunit(struct unittype * theunit)
211 theunit->numerator[0] = theunit->denominator[0] = NULL;
212 theunit->factor = 1.0;
213 theunit->offset = 0.0;
214 theunit->quantity = 0;
219 addsubunit(char *product[], char *toadd)
223 for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
224 if (ptr >= product + MAXSUBUNITS) {
225 warnx("memory overflow in unit reduction");
230 *ptr = dupstr(toadd);
236 showunit(struct unittype * theunit)
242 printf("\t%.8g", theunit->factor);
244 printf("&%.8g", theunit->offset);
245 for (ptr = theunit->numerator; *ptr; ptr++) {
246 if (ptr > theunit->numerator && **ptr &&
247 !strcmp(*ptr, *(ptr - 1)))
251 printf("%s%d", powerstring, counter);
258 printf("%s%d", powerstring, counter);
261 for (ptr = theunit->denominator; *ptr; ptr++) {
262 if (ptr > theunit->denominator && **ptr &&
263 !strcmp(*ptr, *(ptr - 1)))
267 printf("%s%d", powerstring, counter);
278 printf("%s%d", powerstring, counter);
286 warnx("unit reduces to zero");
290 Adds the specified string to the unit.
291 Flip is 0 for adding normally, 1 for adding reciprocal.
292 Quantity is 1 if this is a quantity to be converted rather than a pure unit.
294 Returns 0 for successful addition, nonzero on error.
298 addunit(struct unittype * theunit, char *toadd, int flip, int quantity)
300 char *scratch, *savescr;
302 char *divider, *slash, *offset;
308 savescr = scratch = dupstr(toadd);
309 for (slash = scratch + 1; *slash; slash++)
311 (tolower(*(slash - 1)) != 'e' ||
312 !strchr(".0123456789", *(slash + 1))))
314 slash = strchr(scratch, '/');
319 item = strtok(scratch, " *\t\n/");
321 if (strchr("0123456789.", *item)) { /* item is a number */
322 double num, offsetnum;
325 theunit->quantity = 1;
327 offset = strchr(item, '&');
330 offsetnum = atof(offset+1);
334 divider = strchr(item, '|');
342 if (doingtop ^ flip) {
343 theunit->factor *= num;
344 theunit->offset *= num;
346 theunit->factor /= num;
347 theunit->offset /= num;
349 num = atof(divider + 1);
354 if (doingtop ^ flip) {
355 theunit->factor /= num;
356 theunit->offset /= num;
358 theunit->factor *= num;
359 theunit->offset *= num;
368 if (doingtop ^ flip) {
369 theunit->factor *= num;
370 theunit->offset *= num;
372 theunit->factor /= num;
373 theunit->offset /= num;
377 theunit->offset += offsetnum;
379 else { /* item is not a number */
382 if (strchr("23456789",
383 item[strlen(item) - 1])) {
384 repeat = item[strlen(item) - 1] - '0';
385 item[strlen(item) - 1] = 0;
387 for (; repeat; repeat--)
388 if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
391 item = strtok(NULL, " *\t/\n");
399 } while (doingtop >= 0);
406 compare(const void *item1, const void *item2)
408 return strcmp(*(const char * const *)item1, *(const char * const *)item2);
413 sortunit(struct unittype * theunit)
418 for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
419 qsort(theunit->numerator, count, sizeof(char *), compare);
420 for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
421 qsort(theunit->denominator, count, sizeof(char *), compare);
426 cancelunit(struct unittype * theunit)
431 den = theunit->denominator;
432 num = theunit->numerator;
434 while (*num && *den) {
435 comp = strcmp(*den, *num);
437 /* if (*den!=NULLUNIT) free(*den);
438 if (*num!=NULLUNIT) free(*num);*/
453 Looks up the definition for the specified unit.
454 Returns a pointer to the definition or a null pointer
455 if the specified unit does not appear in the units table.
458 static char buffer[100]; /* buffer for lookupunit answers with
462 lookupunit(const char *unit)
467 for (i = 0; i < unitcount; i++) {
468 if (!strcmp(unittable[i].uname, unit))
469 return unittable[i].uval;
472 if (unit[strlen(unit) - 1] == '^') {
474 copy[strlen(copy) - 1] = 0;
475 for (i = 0; i < unitcount; i++) {
476 if (!strcmp(unittable[i].uname, copy)) {
477 strlcpy(buffer, copy, sizeof(buffer));
484 if (unit[strlen(unit) - 1] == 's') {
486 copy[strlen(copy) - 1] = 0;
487 for (i = 0; i < unitcount; i++) {
488 if (!strcmp(unittable[i].uname, copy)) {
489 strlcpy(buffer, copy, sizeof(buffer));
494 if (copy[strlen(copy) - 1] == 'e') {
495 copy[strlen(copy) - 1] = 0;
496 for (i = 0; i < unitcount; i++) {
497 if (!strcmp(unittable[i].uname, copy)) {
498 strlcpy(buffer, copy, sizeof(buffer));
506 for (i = 0; i < prefixcount; i++) {
507 size_t len = strlen(prefixtable[i].prefixname);
508 if (!strncmp(prefixtable[i].prefixname, unit, len)) {
509 if (!strlen(unit + len) || lookupunit(unit + len)) {
510 snprintf(buffer, sizeof(buffer), "%s %s",
511 prefixtable[i].prefixval, unit + len);
522 reduces a product of symbolic units to primitive units.
523 The three low bits are used to return flags:
525 bit 0 (1) set on if reductions were performed without error.
526 bit 1 (2) set on if no reductions are performed.
527 bit 2 (4) set on if an unknown unit is discovered.
534 reduceproduct(struct unittype * theunit, int flip)
539 int didsomething = 2;
542 product = theunit->denominator;
544 product = theunit->numerator;
546 for (; *product; product++) {
549 if (!strlen(*product))
551 toadd = lookupunit(*product);
553 printf("unknown unit '%s'\n", *product);
556 if (strchr(toadd, PRIMITIVECHAR))
559 if (*product != NULLUNIT) {
563 if (addunit(theunit, toadd, flip, 0))
572 Reduces numerator and denominator of the specified unit.
573 Returns 0 on success, or 1 on unknown unit error.
577 reduceunit(struct unittype * theunit)
583 ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
592 compareproducts(char **one, char **two)
594 while (*one || *two) {
595 if (!*one && *two != NULLUNIT)
597 if (!*two && *one != NULLUNIT)
599 if (*one == NULLUNIT)
601 else if (*two == NULLUNIT)
603 else if (strcmp(*one, *two))
612 /* Return zero if units are compatible, nonzero otherwise */
615 compareunits(struct unittype * first, struct unittype * second)
618 compareproducts(first->numerator, second->numerator) ||
619 compareproducts(first->denominator, second->denominator);
624 completereduce(struct unittype * unit)
626 if (reduceunit(unit))
635 showanswer(struct unittype * have, struct unittype * want)
637 if (compareunits(have, want)) {
638 printf("conformability error\n");
642 else if (have->offset != want->offset) {
644 printf("WARNING: conversion of non-proportional quantities.\n");
648 (have->factor + have->offset-want->offset)/want->factor);
650 printf(" (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
651 have->factor / want->factor,
652 (have->offset-want->offset)/want->factor,
653 want->factor / have->factor,
654 (want->offset - have->offset)/have->factor);
657 printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,
658 want->factor / have->factor);
666 "usage: units [-f unitsfile] [-q] [-v] [from-unit to-unit]\n");
672 main(int argc, char **argv)
675 struct unittype have, want;
676 char havestr[81], wantstr[81];
678 char *userfile = NULL;
681 while ((optchar = getopt(argc, argv, "vqf:")) != -1) {
690 fprintf(stderr, "\n units version %s Copyright (c) 1993 by Adrian Mariano\n",
692 fprintf(stderr, " This program may be freely distributed\n");
700 if (optind != argc - 2 && optind != argc)
705 if (optind == argc - 2) {
706 strlcpy(havestr, argv[optind], sizeof(havestr));
707 strlcpy(wantstr, argv[optind + 1], sizeof(wantstr));
708 initializeunit(&have);
709 addunit(&have, havestr, 0, 1);
710 completereduce(&have);
711 initializeunit(&want);
712 addunit(&want, wantstr, 0, 1);
713 completereduce(&want);
714 showanswer(&have, &want);
718 printf("%d units, %d prefixes\n", unitcount,
722 initializeunit(&have);
724 printf("You have: ");
725 if (!fgets(havestr, sizeof(havestr), stdin)) {
730 } while (addunit(&have, havestr, 0, 1) ||
731 completereduce(&have));
733 initializeunit(&want);
735 printf("You want: ");
736 if (!fgets(wantstr, sizeof(wantstr), stdin)) {
741 } while (addunit(&want, wantstr, 0, 1) ||
742 completereduce(&want));
743 showanswer(&have, &want);