diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-14 14:23:49 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-14 14:23:49 +0200 |
commit | 8352b717ce615c4445d6e318019169a6e10ac8e1 (patch) | |
tree | ed657ce104dbabbcd569a4a2d277000c169b279f | |
parent | 44c0ab410278656b4b82ec20a24c3644a254da89 (diff) | |
download | busybox-w32-8352b717ce615c4445d6e318019169a6e10ac8e1.tar.gz busybox-w32-8352b717ce615c4445d6e318019169a6e10ac8e1.tar.bz2 busybox-w32-8352b717ce615c4445d6e318019169a6e10ac8e1.zip |
factor: support "no-argvs" usage
function old new delta
factorize_numstr - 72 +72
packed_usage 31562 31566 +4
factor_main 109 101 -8
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 76/-8) Total: 68 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/factor.c | 56 |
1 files changed, 39 insertions, 17 deletions
diff --git a/coreutils/factor.c b/coreutils/factor.c index 281753439..818414775 100644 --- a/coreutils/factor.c +++ b/coreutils/factor.c | |||
@@ -14,7 +14,7 @@ | |||
14 | //kbuild:lib-$(CONFIG_FACTOR) += factor.o | 14 | //kbuild:lib-$(CONFIG_FACTOR) += factor.o |
15 | 15 | ||
16 | //usage:#define factor_trivial_usage | 16 | //usage:#define factor_trivial_usage |
17 | //usage: "NUMBER..." | 17 | //usage: "[NUMBER]..." |
18 | //usage:#define factor_full_usage "\n\n" | 18 | //usage:#define factor_full_usage "\n\n" |
19 | //usage: "Print prime factors" | 19 | //usage: "Print prime factors" |
20 | 20 | ||
@@ -165,6 +165,20 @@ static NOINLINE void factorize(wide_t N) | |||
165 | bb_putchar('\n'); | 165 | bb_putchar('\n'); |
166 | } | 166 | } |
167 | 167 | ||
168 | static void factorize_numstr(const char *numstr) | ||
169 | { | ||
170 | wide_t N; | ||
171 | |||
172 | /* Leading + is ok (coreutils compat) */ | ||
173 | if (*numstr == '+') | ||
174 | numstr++; | ||
175 | N = bb_strtoull(numstr, NULL, 10); | ||
176 | if (errno) | ||
177 | bb_show_usage(); | ||
178 | printf("%llu:", N); | ||
179 | factorize(N); | ||
180 | } | ||
181 | |||
168 | int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 182 | int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
169 | int factor_main(int argc UNUSED_PARAM, char **argv) | 183 | int factor_main(int argc UNUSED_PARAM, char **argv) |
170 | { | 184 | { |
@@ -173,24 +187,32 @@ int factor_main(int argc UNUSED_PARAM, char **argv) | |||
173 | //argv += optind; | 187 | //argv += optind; |
174 | argv++; | 188 | argv++; |
175 | 189 | ||
176 | if (!*argv) | 190 | if (!*argv) { |
177 | //TODO: read from stdin | 191 | /* Read from stdin, several numbers per line are accepted */ |
178 | bb_show_usage(); | 192 | for (;;) { |
193 | char *numstr, *line; | ||
194 | line = xmalloc_fgetline(stdin); | ||
195 | if (!line) | ||
196 | return EXIT_SUCCESS; | ||
197 | numstr = line; | ||
198 | for (;;) { | ||
199 | char *end; | ||
200 | numstr = skip_whitespace(numstr); | ||
201 | if (!numstr[0]) | ||
202 | break; | ||
203 | end = skip_non_whitespace(numstr); | ||
204 | if (*end != '\0'); | ||
205 | *end++ = '\0'; | ||
206 | factorize_numstr(numstr); | ||
207 | numstr = end; | ||
208 | } | ||
209 | free(line); | ||
210 | } | ||
211 | } | ||
179 | 212 | ||
180 | do { | 213 | do { |
181 | wide_t N; | 214 | /* Leading spaces are ok (coreutils compat) */ |
182 | const char *numstr; | 215 | factorize_numstr(skip_whitespace(*argv)); |
183 | |||
184 | /* Coreutils compat */ | ||
185 | numstr = skip_whitespace(*argv); | ||
186 | if (*numstr == '+') | ||
187 | numstr++; | ||
188 | |||
189 | N = bb_strtoull(numstr, NULL, 10); | ||
190 | if (errno) | ||
191 | bb_show_usage(); | ||
192 | printf("%llu:", N); | ||
193 | factorize(N); | ||
194 | } while (*++argv); | 216 | } while (*++argv); |
195 | 217 | ||
196 | return EXIT_SUCCESS; | 218 | return EXIT_SUCCESS; |