diff options
author | Rob Landley <rob@landley.net> | 2014-12-24 16:13:08 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2014-12-24 16:13:08 -0600 |
commit | 4391e57913ab84f1edea56157192692d2e41458c (patch) | |
tree | 918b192bee2187493500f728cdce1162616914ad /toys/other | |
parent | d5c66a9fd36777f80ba05301dcfa6789b103e486 (diff) | |
download | toybox-4391e57913ab84f1edea56157192692d2e41458c.tar.gz |
Teach factor to accept whitespace separated arguments (reported by Robert Thompson).
(The diff looks bigger than it is because of reindenting.)
Diffstat (limited to 'toys/other')
-rw-r--r-- | toys/other/factor.c | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/toys/other/factor.c b/toys/other/factor.c index 9dc1cca8..bf454b44 100644 --- a/toys/other/factor.c +++ b/toys/other/factor.c @@ -21,46 +21,51 @@ static void factor(char *s) { long l, ll; - l = strtol(s, &s, 0); - if (*s) { - error_msg("%s: not integer"); - return; - } - - printf("%ld:", l); + for (;;) { + while(isspace(*s)) s++; + if (!*s) return; - // Negative numbers have -1 as a factor - if (l < 0) { - printf(" -1"); - l *= -1; - } + l = strtol(s, &s, 0); + if (*s && !isspace(*s)) { + error_msg("%s: not integer"); + return; + } - // Deal with 0 and 1 (and 2 since we're here) - if (l < 3) { - printf(" %ld\n", l); - return; - } + printf("%ld:", l); - // Special case factors of 2 - while (l && !(l&1)) { - printf(" 2"); - l >>= 1; - } + // Negative numbers have -1 as a factor + if (l < 0) { + printf(" -1"); + l *= -1; + } - // test odd numbers. - for (ll=3; ;ll += 2) { - long lll = ll*ll; + // Nothing below 4 has factors + if (l < 4) { + printf(" %ld\n", l); + continue; + } - if (lll>l || lll<ll) { - if (l>1) printf(" %ld", l); - break; + // Special case factors of 2 + while (l && !(l&1)) { + printf(" 2"); + l >>= 1; } - while (!(l%ll)) { - printf(" %ld", ll); - l /= ll; + + // test odd numbers. + for (ll=3; ;ll += 2) { + long lll = ll*ll; + + if (lll>l || lll<ll) { + if (l>1) printf(" %ld", l); + break; + } + while (!(l%ll)) { + printf(" %ld", ll); + l /= ll; + } } + xputc('\n'); } - xputc('\n'); } void factor_main(void) |