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.
19 static const char rcsid[] =
30 #include "pathnames.h"
35 #define UNITSFILE _PATH_UNITSLIB
39 #define MAXPREFIXES 50
41 #define MAXSUBUNITS 500
43 #define PRIMITIVECHAR '!'
45 char *powerstring = "^";
50 } unittable[MAXUNITS];
53 char *numerator[MAXSUBUNITS];
54 char *denominator[MAXSUBUNITS];
61 } prefixtable[MAXPREFIXES];
75 ret = malloc(strlen(str) + 1);
77 errx(3, "memory allocation error");
84 readerror(int linenum)
86 warnx("error in units file '%s' line %d", UNITSFILE, linenum);
91 readunits(char *userfile)
94 char line[80], *lineptr;
101 unitfile = fopen(userfile, "rt");
103 errx(1, "unable to open units file '%s'", userfile);
106 unitfile = fopen(UNITSFILE, "rt");
112 env = getenv("PATH");
114 if (strchr(env, ';'))
115 strcpy(separator, ";");
117 strcpy(separator, ":");
118 direc = strtok(env, separator);
120 strcpy(filename, "");
121 strncat(filename, direc, 999);
122 strncat(filename, "/",
123 999 - strlen(filename));
124 strncat(filename, UNITSFILE,
125 999 - strlen(filename));
126 unitfile = fopen(filename, "rt");
129 direc = strtok(NULL, separator);
133 errx(1, "can't find units file '%s'", UNITSFILE);
136 while (!feof(unitfile)) {
137 if (!fgets(line, 79, unitfile))
143 lineptr += strspn(lineptr, " \n\t");
144 len = strcspn(lineptr, " \n\t");
146 if (!strlen(lineptr))
148 if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
149 if (prefixcount == MAXPREFIXES) {
150 warnx("memory for prefixes exceeded in line %d", linenum);
153 lineptr[strlen(lineptr) - 1] = 0;
154 prefixtable[prefixcount].prefixname = dupstr(lineptr);
155 for (i = 0; i < prefixcount; i++)
156 if (!strcmp(prefixtable[i].prefixname, lineptr)) {
157 warnx("redefinition of prefix '%s' on line %d ignored",
162 if (!strlen(lineptr)) {
166 lineptr += strspn(lineptr, " \n\t");
167 len = strcspn(lineptr, "\n\t");
169 prefixtable[prefixcount++].prefixval = dupstr(lineptr);
171 else { /* it's not a prefix */
172 if (unitcount == MAXUNITS) {
173 warnx("memory for units exceeded in line %d", linenum);
176 unittable[unitcount].uname = dupstr(lineptr);
177 for (i = 0; i < unitcount; i++)
178 if (!strcmp(unittable[i].uname, lineptr)) {
179 warnx("redefinition of unit '%s' on line %d ignored",
184 lineptr += strspn(lineptr, " \n\t");
185 if (!strlen(lineptr)) {
189 len = strcspn(lineptr, "\n\t");
191 unittable[unitcount++].uval = dupstr(lineptr);
198 initializeunit(struct unittype * theunit)
200 theunit->factor = 1.0;
201 theunit->numerator[0] = theunit->denominator[0] = NULL;
206 addsubunit(char *product[], char *toadd)
210 for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
211 if (ptr >= product + MAXSUBUNITS) {
212 warnx("memory overflow in unit reduction");
217 *ptr = dupstr(toadd);
223 showunit(struct unittype * theunit)
229 printf("\t%.8g", theunit->factor);
230 for (ptr = theunit->numerator; *ptr; ptr++) {
231 if (ptr > theunit->numerator && **ptr &&
232 !strcmp(*ptr, *(ptr - 1)))
236 printf("%s%d", powerstring, counter);
243 printf("%s%d", powerstring, counter);
246 for (ptr = theunit->denominator; *ptr; ptr++) {
247 if (ptr > theunit->denominator && **ptr &&
248 !strcmp(*ptr, *(ptr - 1)))
252 printf("%s%d", powerstring, counter);
263 printf("%s%d", powerstring, counter);
271 warnx("unit reduces to zero");
275 Adds the specified string to the unit.
276 Flip is 0 for adding normally, 1 for adding reciprocal.
278 Returns 0 for successful addition, nonzero on error.
282 addunit(struct unittype * theunit, char *toadd, int flip)
284 char *scratch, *savescr;
286 char *divider, *slash;
289 savescr = scratch = dupstr(toadd);
290 for (slash = scratch + 1; *slash; slash++)
292 (tolower(*(slash - 1)) != 'e' ||
293 !strchr(".0123456789", *(slash + 1))))
295 slash = strchr(scratch, '/');
300 item = strtok(scratch, " *\t\n/");
302 if (strchr("0123456789.", *item)) { /* item is a number */
305 divider = strchr(item, '|');
314 theunit->factor *= num;
316 theunit->factor /= num;
317 num = atof(divider + 1);
323 theunit->factor /= num;
325 theunit->factor *= num;
334 theunit->factor *= num;
336 theunit->factor /= num;
340 else { /* item is not a number */
343 if (strchr("23456789",
344 item[strlen(item) - 1])) {
345 repeat = item[strlen(item) - 1] - '0';
346 item[strlen(item) - 1] = 0;
348 for (; repeat; repeat--)
349 if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
352 item = strtok(NULL, " *\t/\n");
360 } while (doingtop >= 0);
367 compare(const void *item1, const void *item2)
369 return strcmp(*(char **) item1, *(char **) item2);
374 sortunit(struct unittype * theunit)
379 for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
380 qsort(theunit->numerator, count, sizeof(char *), compare);
381 for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
382 qsort(theunit->denominator, count, sizeof(char *), compare);
387 cancelunit(struct unittype * theunit)
392 den = theunit->denominator;
393 num = theunit->numerator;
395 while (*num && *den) {
396 comp = strcmp(*den, *num);
398 /* if (*den!=NULLUNIT) free(*den);
399 if (*num!=NULLUNIT) free(*num);*/
414 Looks up the definition for the specified unit.
415 Returns a pointer to the definition or a null pointer
416 if the specified unit does not appear in the units table.
419 static char buffer[100]; /* buffer for lookupunit answers with
423 lookupunit(char *unit)
428 for (i = 0; i < unitcount; i++) {
429 if (!strcmp(unittable[i].uname, unit))
430 return unittable[i].uval;
433 if (unit[strlen(unit) - 1] == '^') {
435 copy[strlen(copy) - 1] = 0;
436 for (i = 0; i < unitcount; i++) {
437 if (!strcmp(unittable[i].uname, copy)) {
438 strcpy(buffer, copy);
445 if (unit[strlen(unit) - 1] == 's') {
447 copy[strlen(copy) - 1] = 0;
448 for (i = 0; i < unitcount; i++) {
449 if (!strcmp(unittable[i].uname, copy)) {
450 strcpy(buffer, copy);
455 if (copy[strlen(copy) - 1] == 'e') {
456 copy[strlen(copy) - 1] = 0;
457 for (i = 0; i < unitcount; i++) {
458 if (!strcmp(unittable[i].uname, copy)) {
459 strcpy(buffer, copy);
467 for (i = 0; i < prefixcount; i++) {
468 if (!strncmp(prefixtable[i].prefixname, unit,
469 strlen(prefixtable[i].prefixname))) {
470 unit += strlen(prefixtable[i].prefixname);
471 if (!strlen(unit) || lookupunit(unit)) {
472 strcpy(buffer, prefixtable[i].prefixval);
474 strcat(buffer, unit);
485 reduces a product of symbolic units to primitive units.
486 The three low bits are used to return flags:
488 bit 0 (1) set on if reductions were performed without error.
489 bit 1 (2) set on if no reductions are performed.
490 bit 2 (4) set on if an unknown unit is discovered.
497 reduceproduct(struct unittype * theunit, int flip)
502 int didsomething = 2;
505 product = theunit->denominator;
507 product = theunit->numerator;
509 for (; *product; product++) {
512 if (!strlen(*product))
514 toadd = lookupunit(*product);
516 printf("unknown unit '%s'\n", *product);
519 if (strchr(toadd, PRIMITIVECHAR))
522 if (*product != NULLUNIT) {
526 if (addunit(theunit, toadd, flip))
535 Reduces numerator and denominator of the specified unit.
536 Returns 0 on success, or 1 on unknown unit error.
540 reduceunit(struct unittype * theunit)
546 ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
555 compareproducts(char **one, char **two)
557 while (*one || *two) {
558 if (!*one && *two != NULLUNIT)
560 if (!*two && *one != NULLUNIT)
562 if (*one == NULLUNIT)
564 else if (*two == NULLUNIT)
566 else if (strcmp(*one, *two))
575 /* Return zero if units are compatible, nonzero otherwise */
578 compareunits(struct unittype * first, struct unittype * second)
581 compareproducts(first->numerator, second->numerator) ||
582 compareproducts(first->denominator, second->denominator);
587 completereduce(struct unittype * unit)
589 if (reduceunit(unit))
598 showanswer(struct unittype * have, struct unittype * want)
600 if (compareunits(have, want)) {
601 printf("conformability error\n");
606 printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,
607 want->factor / have->factor);
615 "usage: units [-f unitsfile] [-q] [-v] [from-unit to-unit]\n");
621 main(int argc, char **argv)
624 struct unittype have, want;
625 char havestr[81], wantstr[81];
630 while ((optchar = getopt(argc, argv, "vqf:")) != -1) {
639 fprintf(stderr, "\n units version %s Copyright (c) 1993 by Adrian Mariano\n",
641 fprintf(stderr, " This program may be freely distributed\n");
649 if (optind != argc - 2 && optind != argc)
654 if (optind == argc - 2) {
655 strcpy(havestr, argv[optind]);
656 strcpy(wantstr, argv[optind + 1]);
657 initializeunit(&have);
658 addunit(&have, havestr, 0);
659 completereduce(&have);
660 initializeunit(&want);
661 addunit(&want, wantstr, 0);
662 completereduce(&want);
663 showanswer(&have, &want);
667 printf("%d units, %d prefixes\n", unitcount,
671 initializeunit(&have);
673 printf("You have: ");
674 if (!fgets(havestr, 80, stdin)) {
679 } while (addunit(&have, havestr, 0) ||
680 completereduce(&have));
682 initializeunit(&want);
684 printf("You want: ");
685 if (!fgets(wantstr, 80, stdin)) {
690 } while (addunit(&want, wantstr, 0) ||
691 completereduce(&want));
692 showanswer(&have, &want);