changeset 1767:12fa15de1cd4

Fix printf bug (%.s should be %.0s not %s) reported by Isabella Parakiss.
author Rob Landley <rob@landley.net>
date Sat, 28 Mar 2015 13:22:27 -0500
parents 190ecf70fbe5
children 4c92484c2646
files tests/printf.test toys/posix/printf.c
diffstat 2 files changed, 7 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/tests/printf.test	Sat Mar 28 13:13:42 2015 -0500
+++ b/tests/printf.test	Sat Mar 28 13:22:27 2015 -0500
@@ -57,3 +57,5 @@
 testing "printf '%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" ""
 testing "printf '%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" ""
 testing "printf '%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" ""
+
+testing "printf %.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" ""
--- a/toys/posix/printf.c	Sat Mar 28 13:13:42 2015 -0500
+++ b/toys/posix/printf.c	Sat Mar 28 13:22:27 2015 -0500
@@ -91,19 +91,17 @@
       // Handle %escape
       else {
         char c, *end = 0, *aa, *to = toybuf;
-        int wp[] = {0,-1}, i;
+        int wp[] = {0,-1}, i = 0;
 
         // Parse width.precision between % and type indicator.
         *to++ = '%';
         while (strchr("-+# '0", *f) && (to-toybuf)<10) *to++ = *f++;
-        for (i=0; i<2; i++) {
+        for (;;) {
           if (eat(&f, '*')) {
             if (*arg) wp[i] = atolx(*arg++);
-          } else while (*f >= '0' && *f <= '9') {
-            if (wp[i]<0) wp[i] = 0;
-            wp[i] = (wp[i]*10)+(*f++)-'0';
-          }
-          if (!eat(&f, '.')) break;
+          } else while (*f >= '0' && *f <= '9') wp[i] = (wp[i]*10)+(*f++)-'0';
+          if (i++ || !eat(&f, '.')) break;
+          wp[1] = 0;
         }
         c = *f++;
         seen = sprintf(to, "*.*%c", c);;