added -h flag to allow for hexidecimal output.
Use '0' for base rather than 10 to allow for more flexible input bases. Inspired by changes in PR 7402, but mostly redone by me to get past bde filter. Submitted by: Timo J. Rinne PR: 7402
This commit is contained in:
parent
99490ede3a
commit
be47d628ca
@ -45,14 +45,14 @@
|
||||
factor, primes \- factor a number, generate primes
|
||||
.SH SYNOPSIS
|
||||
.B factor
|
||||
[ number ] ...
|
||||
-[h] [ number ] ...
|
||||
.PP
|
||||
.B primes
|
||||
[ start [ stop ]]
|
||||
-[h] [ start [ stop ]]
|
||||
.SH DESCRIPTION
|
||||
The
|
||||
.I factor
|
||||
utility will factor integers between -2147483648 and 2147483647 inclusive.
|
||||
utility will factor integers between 0 and 4294967295 inclusive.
|
||||
When a number is factored, it is printed, followed by a ``:'',
|
||||
and the list of factors on a single line.
|
||||
Factors are listed in ascending order, and are preceded by a space.
|
||||
@ -106,6 +106,11 @@ The
|
||||
.B start
|
||||
value is terminated by a non-digit character (such as a newline).
|
||||
The input line must not be longer than 255 characters.
|
||||
.SH OPTIONS
|
||||
.LP
|
||||
.TP 8
|
||||
.B \-h
|
||||
Print the results in hexadecimal rather than decimal.
|
||||
.SH DIAGNOSTICS
|
||||
Out of range or invalid input results in `ouch' being
|
||||
written to standard error.
|
||||
|
@ -82,6 +82,8 @@ static char sccsid[] = "@(#)factor.c 8.4 (Berkeley) 5/4/95";
|
||||
extern ubig prime[];
|
||||
extern ubig *pr_limit; /* largest prime in the prime array */
|
||||
|
||||
int hflag;
|
||||
|
||||
void pr_fact __P((ubig)); /* print factors of a value */
|
||||
void usage __P((void));
|
||||
|
||||
@ -94,8 +96,11 @@ main(argc, argv)
|
||||
int ch;
|
||||
char *p, buf[100]; /* > max number of digits. */
|
||||
|
||||
while ((ch = getopt(argc, argv, "")) != -1)
|
||||
while ((ch = getopt(argc, argv, "h")) != -1)
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
hflag++;
|
||||
break;
|
||||
case '?':
|
||||
default:
|
||||
usage();
|
||||
@ -117,7 +122,7 @@ main(argc, argv)
|
||||
if (*p == '-')
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
errno = 0;
|
||||
val = strtoul(buf, &p, 10);
|
||||
val = strtoul(buf, &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", buf);
|
||||
if (*p != '\n')
|
||||
@ -130,7 +135,7 @@ main(argc, argv)
|
||||
if (argv[0][0] == '-')
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
errno = 0;
|
||||
val = strtoul(argv[0], &p, 10);
|
||||
val = strtoul(argv[0], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[0]);
|
||||
if (*p != '\0')
|
||||
@ -168,7 +173,7 @@ pr_fact(val)
|
||||
}
|
||||
|
||||
/* Factor value. */
|
||||
(void)printf("%lu:", val);
|
||||
(void)printf(hflag ? "0x%x:" : "%lu:", val);
|
||||
for (fact = &prime[0]; val > 1; ++fact) {
|
||||
/* Look for the smallest factor. */
|
||||
do {
|
||||
@ -178,13 +183,13 @@ pr_fact(val)
|
||||
|
||||
/* Watch for primes larger than the table. */
|
||||
if (fact > pr_limit) {
|
||||
(void)printf(" %lu", val);
|
||||
(void)printf(hflag ? " 0x%x" : " %lu", val);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Divide factor out until none are left. */
|
||||
do {
|
||||
(void)printf(" %lu", *fact);
|
||||
(void)printf(hflag ? " 0x%x" : " %lu", *fact);
|
||||
val /= (long)*fact;
|
||||
} while ((val % (long)*fact) == 0);
|
||||
|
||||
@ -197,6 +202,6 @@ pr_fact(val)
|
||||
void
|
||||
usage()
|
||||
{
|
||||
(void)fprintf(stderr, "usage: factor [value ...]\n");
|
||||
(void)fprintf(stderr, "usage: factor -h [value ...]\n");
|
||||
exit (0);
|
||||
}
|
||||
|
@ -101,6 +101,8 @@ extern ubig *pr_limit; /* largest prime in the prime array */
|
||||
extern char pattern[];
|
||||
extern int pattern_size; /* length of pattern array */
|
||||
|
||||
int hflag;
|
||||
|
||||
void primes __P((ubig, ubig));
|
||||
ubig read_num_buf __P((void));
|
||||
void usage __P((void));
|
||||
@ -115,8 +117,11 @@ main(argc, argv)
|
||||
int ch;
|
||||
char *p;
|
||||
|
||||
while ((ch = getopt(argc, argv, "")) != -1)
|
||||
while ((ch = getopt(argc, argv, "h")) != -1)
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
hflag++;
|
||||
break;
|
||||
case '?':
|
||||
default:
|
||||
usage();
|
||||
@ -140,14 +145,14 @@ main(argc, argv)
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
|
||||
errno = 0;
|
||||
start = strtoul(argv[0], &p, 10);
|
||||
start = strtoul(argv[0], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[0]);
|
||||
if (*p != '\0')
|
||||
errx(1, "%s: illegal numeric format.", argv[0]);
|
||||
|
||||
errno = 0;
|
||||
stop = strtoul(argv[1], &p, 10);
|
||||
stop = strtoul(argv[1], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[1]);
|
||||
if (*p != '\0')
|
||||
@ -159,7 +164,7 @@ main(argc, argv)
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
|
||||
errno = 0;
|
||||
start = strtoul(argv[0], &p, 10);
|
||||
start = strtoul(argv[0], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[0]);
|
||||
if (*p != '\0')
|
||||
@ -200,7 +205,7 @@ read_num_buf()
|
||||
if (*p == '-')
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
errno = 0;
|
||||
val = strtoul(buf, &p, 10);
|
||||
val = strtoul(buf, &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", buf);
|
||||
if (*p != '\n')
|
||||
@ -256,7 +261,7 @@ primes(start, stop)
|
||||
for (p = &prime[0], factor = prime[0];
|
||||
factor < stop && p <= pr_limit; factor = *(++p)) {
|
||||
if (factor >= start) {
|
||||
printf("%lu\n", factor);
|
||||
printf(hflag ? "0x%x\n" : "%lu\n", factor);
|
||||
}
|
||||
}
|
||||
/* return early if we are done */
|
||||
@ -319,7 +324,7 @@ primes(start, stop)
|
||||
*/
|
||||
for (q = table; q < tab_lim; ++q, start+=2) {
|
||||
if (*q) {
|
||||
printf("%lu\n", start);
|
||||
printf(hflag ? "0x%x\n" : "%lu\n", start);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -328,6 +333,6 @@ primes(start, stop)
|
||||
void
|
||||
usage()
|
||||
{
|
||||
(void)fprintf(stderr, "usage: primes [start [stop]]\n");
|
||||
(void)fprintf(stderr, "usage: primes [-h] [start [stop]]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user