Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/units Allow a number and a unit to be juxtaposed wit...



details:   https://anonhg.NetBSD.org/src/rev/aa26200ca634
branches:  trunk
changeset: 783548:aa26200ca634
user:      apb <apb%NetBSD.org@localhost>
date:      Fri Dec 28 17:07:03 2012 +0000

description:
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".

diffstat:

 usr.bin/units/units.c |  63 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 40 insertions(+), 23 deletions(-)

diffs (90 lines):

diff -r cd4ec07a1b54 -r aa26200ca634 usr.bin/units/units.c
--- a/usr.bin/units/units.c     Fri Dec 28 13:53:12 2012 +0000
+++ b/usr.bin/units/units.c     Fri Dec 28 17:07:03 2012 +0000
@@ -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 (adrian%cam.cornell.edu@localhost)
@@ -321,13 +321,46 @@
        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 = strtod(divider + 1, &endptr);
+                                       if (!num) {
+                                               zeroerror();
+                                               return 1;
+                                       }
+                                       if (doingtop ^ flip)
+                                               theunit->factor /= num;
+                                       else
+                                               theunit->factor *= num;
+                                       if (*endptr) {
+                                               /* "6|2foo" is like "6|2 foo" */
+                                               item = endptr;
+                                               continue;
+                                       }
+                               }
+                               else {
+                                       num = strtod(item, &endptr);
                                        if (!num) {
                                                zeroerror();
                                                return 1;
@@ -336,27 +369,11 @@
                                                theunit->factor *= num;
                                        else
                                                theunit->factor /= num;
-                                       num = atof(divider + 1);
-                                       if (!num) {
-                                               zeroerror();
-                                               return 1;
+                                       if (*endptr) {
+                                               /* "3foo" is like "3 foo" */
+                                               item = endptr;
+                                               continue;
                                        }
-                                       if (doingtop ^ flip)
-                                               theunit->factor /= num;
-                                       else
-                                               theunit->factor *= num;
-                               }
-                               else {
-                                       num = atof(item);
-                                       if (!num) {
-                                               zeroerror();
-                                               return 1;
-                                       }
-                                       if (doingtop ^ flip)
-                                               theunit->factor *= num;
-                                       else
-                                               theunit->factor /= num;
-
                                }
                        }
                        else {  /* item is not a number */



Home | Main Index | Thread Index | Old Index