=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/units/units.c,v retrieving revision 1.8 retrieving revision 1.9 diff -c -r1.8 -r1.9 *** src/usr.bin/units/units.c 2003/06/10 22:20:53 1.8 --- src/usr.bin/units/units.c 2003/07/02 01:57:15 1.9 *************** *** 1,4 **** ! /* $OpenBSD: units.c,v 1.8 2003/06/10 22:20:53 deraadt Exp $ */ /* $NetBSD: units.c,v 1.6 1996/04/06 06:01:03 thorpej Exp $ */ /* --- 1,4 ---- ! /* $OpenBSD: units.c,v 1.9 2003/07/02 01:57:15 deraadt Exp $ */ /* $NetBSD: units.c,v 1.6 1996/04/06 06:01:03 thorpej Exp $ */ /* *************** *** 68,73 **** --- 68,92 ---- int unitcount; int prefixcount; + char *dupstr(char *); + void readerror(int); + 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) *************** *** 83,89 **** } ! void readerror(int linenum) { fprintf(stderr, "Error in units file '%s' line %d\n", UNITSFILE, --- 102,108 ---- } ! void readerror(int linenum) { fprintf(stderr, "Error in units file '%s' line %d\n", UNITSFILE, *************** *** 91,102 **** } ! void readunits(char *userfile) { - FILE *unitfile; char line[80], *lineptr; int len, linenum, i; unitcount = 0; linenum = 0; --- 110,121 ---- } ! void readunits(char *userfile) { char line[80], *lineptr; int len, linenum, i; + FILE *unitfile; unitcount = 0; linenum = 0; *************** *** 111,119 **** } else { unitfile = fopen(UNITSFILE, "rt"); if (!unitfile) { char *direc, *env; - char filename[1000]; - char separator[2] = SEPERATOR; env = getenv("PATH"); if (env) { --- 130,137 ---- } else { unitfile = fopen(UNITSFILE, "rt"); if (!unitfile) { + char filename[1000], separator[2] = SEPERATOR; char *direc, *env; env = getenv("PATH"); if (env) { *************** *** 171,178 **** len = strcspn(lineptr, "\n\t"); 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", --- 189,195 ---- len = strcspn(lineptr, "\n\t"); 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", *************** *** 201,215 **** 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; --- 218,232 ---- 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; *************** *** 226,233 **** } ! void ! showunit(struct unittype * theunit) { char **ptr; int printedslash; --- 243,250 ---- } ! void ! showunit(struct unittype *theunit) { char **ptr; int printedslash; *************** *** 272,278 **** } ! void zeroerror(void) { fprintf(stderr, "Unit reduces to zero\n"); --- 289,295 ---- } ! void zeroerror(void) { fprintf(stderr, "Unit reduces to zero\n"); *************** *** 285,292 **** Returns 0 for successful addition, nonzero on error. */ ! int ! addunit(struct unittype * theunit, char *toadd, int flip) { char *scratch, *savescr; char *item; --- 302,309 ---- Returns 0 for successful addition, nonzero on error. */ ! int ! addunit(struct unittype *theunit, char *toadd, int flip) { char *scratch, *savescr; char *item; *************** *** 330,337 **** theunit->factor /= num; else theunit->factor *= num; ! } ! else { num = atof(item); if (!num) { zeroerror(); --- 347,353 ---- theunit->factor /= num; else theunit->factor *= num; ! } else { num = atof(item); if (!num) { zeroerror(); *************** *** 343,350 **** theunit->factor /= num; } ! } ! else { /* item is not a number */ int repeat = 1; if (strchr("23456789", --- 359,365 ---- theunit->factor /= num; } ! } else { /* item is not a number */ int repeat = 1; if (strchr("23456789", *************** *** 361,368 **** doingtop--; if (slash) { scratch = slash + 1; ! } ! else doingtop--; } while (doingtop >= 0); free(savescr); --- 376,382 ---- doingtop--; if (slash) { scratch = slash + 1; ! } else doingtop--; } while (doingtop >= 0); free(savescr); *************** *** 370,384 **** } ! int compare(const void *item1, const void *item2) { return strcmp(*(char **) item1, *(char **) item2); } ! void ! sortunit(struct unittype * theunit) { char **ptr; int count; --- 384,398 ---- } ! int compare(const void *item1, const void *item2) { return strcmp(*(char **) item1, *(char **) item2); } ! void ! sortunit(struct unittype *theunit) { char **ptr; int count; *************** *** 390,397 **** } ! void ! cancelunit(struct unittype * theunit) { char **den, **num; int comp; --- 404,411 ---- } ! void ! cancelunit(struct unittype *theunit) { char **den, **num; int comp; *************** *** 402,413 **** while (*num && *den) { comp = strcmp(*den, *num); if (!comp) { ! /* if (*den!=NULLUNIT) free(*den); ! if (*num!=NULLUNIT) free(*num);*/ *den++ = NULLUNIT; *num++ = NULLUNIT; ! } ! else if (comp < 0) den++; else num++; --- 416,430 ---- while (*num && *den) { comp = strcmp(*den, *num); if (!comp) { ! #if 0 ! if (*den!=NULLUNIT) ! free(*den); ! if (*num!=NULLUNIT) ! free(*num); ! #endif *den++ = NULLUNIT; *num++ = NULLUNIT; ! } else if (comp < 0) den++; else num++; *************** *** 499,510 **** #define ERROR 4 ! int ! reduceproduct(struct unittype * theunit, int flip) { ! ! char *toadd; ! char **product; int didsomething = 2; if (flip) --- 516,525 ---- #define ERROR 4 ! int ! reduceproduct(struct unittype *theunit, int flip) { ! char *toadd, **product; int didsomething = 2; if (flip) *************** *** 542,549 **** Returns 0 on success, or 1 on unknown unit error. */ ! int ! reduceunit(struct unittype * theunit) { int ret; --- 557,564 ---- Returns 0 on success, or 1 on unknown unit error. */ ! int ! reduceunit(struct unittype *theunit) { int ret; *************** *** 557,563 **** } ! int compareproducts(char **one, char **two) { while (*one || *two) { --- 572,578 ---- } ! int compareproducts(char **one, char **two) { while (*one || *two) { *************** *** 580,596 **** /* 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; --- 595,610 ---- /* 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; *************** *** 600,620 **** } ! 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, "units [-f unitsfile] [-q] [-v] [from-unit to-unit]\n"); --- 614,633 ---- } ! 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, "units [-f unitsfile] [-q] [-v] [from-unit to-unit]\n"); *************** *** 674,681 **** addunit(&want, wantstr, 0); completereduce(&want); showanswer(&have, &want); ! } ! else { if (!quiet) printf("%d units, %d prefixes\n", unitcount, prefixcount); --- 687,693 ---- addunit(&want, wantstr, 0); completereduce(&want); showanswer(&have, &want); ! } else { if (!quiet) printf("%d units, %d prefixes\n", unitcount, prefixcount);