From: Jim Meyering Date: Tue, 20 Dec 1994 05:24:13 +0000 (+0000) Subject: Begin overhaul. X-Git-Tag: textutils-1_12_1~374 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6bb619d33cebde41f996b8fade52cbab91de6a14;p=thirdparty%2Fcoreutils.git Begin overhaul. --- diff --git a/src/factor.c b/src/factor.c index d7c5e44116..f11c388f3d 100644 --- a/src/factor.c +++ b/src/factor.c @@ -17,22 +17,59 @@ /* Written by Paul Rubin . */ +#include #include +#include + +#include "system.h" +#include "version.h" +#include "long-options.h" +#include "error.h" + +/* The name this program was run with. */ +char *program_name; static void -factor (n0) +usage (status) + int status; +{ + if (status != 0) + fprintf (stderr, "Try `%s --help' for more information.\n", + program_name); + else + { + printf ("\ +Usage: %s [NUMBER]\n\ + or: %s OPTION\n\ +", + program_name, program_name); + printf ("\ +\n\ + --help display this help and exit\n\ + --version output version information and exit\n\ +"); + } + exit (status); +} + +static int +factor (n0, max_n_factors, factors) unsigned long n0; + int max_n_factors; + unsigned long *factors; { register unsigned long n = n0, d; + int n_factors = 0; if (n < 1) - return; + return n_factors; while (n % 2 == 0) { - printf ("\t2\n"); + factors[n_factors++] = 2; n /= 2; } + /* The exit condition in the following loop is correct because any time it is tested one of these 3 conditions holds: (1) d divides n @@ -44,12 +81,27 @@ factor (n0) { while (n % d == 0) { - printf ("\t%ld\n", d); + factors[n_factors++] = d; n /= d; } } if (n != 1 || n0 == 1) - printf ("\t%ld\n", n); + factors[n_factors++] = n; + + return n_factors; +} + +static void +print_factors (n) + unsigned long int n; +{ + unsigned long int factors[64]; + int n_factors; + int i; + + n_factors = factor (n, 64, factors); + for (i=0; i 2) + { + error (0, 0, "too many arguments"); + usage (1); + } + if (argc == 1) do_stdin (); else if (argc == 2) - factor ((unsigned) atoi (argv[1])); + { + print_factors ((unsigned long) atoi (argv[1])); + } else { fprintf (stderr, "Usage: %s [number]\n", argv[0]);