diff options
author | imp <imp@FreeBSD.org> | 1999-01-06 08:14:26 +0000 |
---|---|---|
committer | imp <imp@FreeBSD.org> | 1999-01-06 08:14:26 +0000 |
commit | 4695982235738320dd9801a117ce0e700b0b2fdf (patch) | |
tree | df7c21f08679025acb34002e2f1dee12f7b97fc0 /games/factor/factor.c | |
parent | 1eb0469a28ede5948a08d146e27a84ea0d435bf3 (diff) | |
download | FreeBSD-src-4695982235738320dd9801a117ce0e700b0b2fdf.zip FreeBSD-src-4695982235738320dd9801a117ce0e700b0b2fdf.tar.gz |
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
Diffstat (limited to 'games/factor/factor.c')
-rw-r--r-- | games/factor/factor.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/games/factor/factor.c b/games/factor/factor.c index 157cecb..c5fdbaa 100644 --- a/games/factor/factor.c +++ b/games/factor/factor.c @@ -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); } |