[Toybox] [PATCH] fix newline on stdin for

Robert Thompson robertt.thompson at gmail.com
Fri Dec 12 15:19:22 PST 2014


I ran across a variance between toybox factor and coreutils factor.

Coreutils factor will accept numbers on stdin separated by any whitespace
(including newlines and tabs) between integers, but toybox factor was only
accepting one integer per line.

I added a test for this, and hacked factor to give the expected behavior.
It's not properly indented, and it depends on isspace(), but it seems to be
doing the job.



diff -r 51b7d1af353b tests/factor.test
--- a/tests/factor.test Thu Dec 11 20:17:28 2014 -0600
+++ b/tests/factor.test Fri Dec 12 17:10:49 2014 -0600
@@ -16,3 +16,7 @@
         "10000000018: 2 131 521 73259\n" "" ""
 testing "factor 10000000019" "factor 10000000019" \
         "10000000019: 10000000019\n" "" ""
+
+testing "factor 3 6 from stdin" "factor" "3: 3\n6: 2 3\n" "" "3 6"
+testing "factor stdin newline" "factor" "3: 3\n6: 2 3\n" "" "3\n6\n"
+
diff -r 51b7d1af353b toys/other/factor.c
--- a/toys/other/factor.c Thu Dec 11 20:17:28 2014 -0600
+++ b/toys/other/factor.c Fri Dec 12 17:10:49 2014 -0600
@@ -20,9 +20,11 @@
 static void factor(char *s)
 {
   long l, ll;
+  while( *s && s[0] && ! isspace(s[0]) ) {
+    printf("->: %s\n",s);

   l = strtol(s, &s, 0);
-  if (*s) {
+  if (*s && s[0] > 32 ) {
     error_msg("%s: not integer");
     return;
   }
@@ -35,10 +37,10 @@
     l *= -1;
   }

-  // Deal with 0 and 1 (and 2 since we're here)
-  if (l < 3) {
+  // Deal with 0..3
+  if (l < 4) {
     printf(" %ld\n", l);
-    return;
+    continue;
   }

   // Special case factors of 2
@@ -61,6 +63,7 @@
     }
   }
   xputc('\n');
+  }
 }

 void factor_main(void)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20141212/84602fec/attachment.htm>


More information about the Toybox mailing list