Module Name: src
Committed By: apb
Date: Fri Dec 28 17:07:04 UTC 2012
Modified Files:
src/usr.bin/units: units.c
Log Message:
Allow a number and a unit to be juxtaposed without an intervening space.
Now "litres/100km" works as desired, instead of silently being
treated as "litres/100".
To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/units/units.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/units/units.c
diff -u src/usr.bin/units/units.c:1.18 src/usr.bin/units/units.c:1.19
--- src/usr.bin/units/units.c:1.18 Tue Mar 20 20:34:59 2012
+++ src/usr.bin/units/units.c Fri Dec 28 17:07:03 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: units.c,v 1.18 2012/03/20 20:34:59 matt Exp $ */
+/* $NetBSD: units.c,v 1.19 2012/12/28 17:07:03 apb Exp $ */
/*
* units.c Copyright (c) 1993 by Adrian Mariano ([email protected])
@@ -321,22 +321,30 @@ addunit(struct unittype * theunit, const
do {
item = strtok(scratch, " *\t\n/");
while (item) {
- if (strchr("0123456789.", *item)) { /* item is a number */
+ if (strchr("0123456789.", *item)) {
+ /* item starts with a number */
+ char *endptr;
double num;
divider = strchr(item, '|');
if (divider) {
*divider = 0;
- num = atof(item);
+ num = strtod(item, &endptr);
if (!num) {
zeroerror();
return 1;
}
+ if (endptr != divider) {
+ /* "6foo|2" is an error */
+ warnx("Junk between number "
+ "and '|'");
+ return 1;
+ }
if (doingtop ^ flip)
theunit->factor *= num;
else
theunit->factor /= num;
- num = atof(divider + 1);
+ num = strtod(divider + 1, &endptr);
if (!num) {
zeroerror();
return 1;
@@ -345,9 +353,14 @@ addunit(struct unittype * theunit, const
theunit->factor /= num;
else
theunit->factor *= num;
+ if (*endptr) {
+ /* "6|2foo" is like "6|2 foo" */
+ item = endptr;
+ continue;
+ }
}
else {
- num = atof(item);
+ num = strtod(item, &endptr);
if (!num) {
zeroerror();
return 1;
@@ -356,7 +369,11 @@ addunit(struct unittype * theunit, const
theunit->factor *= num;
else
theunit->factor /= num;
-
+ if (*endptr) {
+ /* "3foo" is like "3 foo" */
+ item = endptr;
+ continue;
+ }
}
}
else { /* item is not a number */