Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 26 Jul 1998 21:27:05 +0300 (EEST)
From:      tri@iki.fi
To:        FreeBSD-gnats-submit@FreeBSD.ORG
Cc:        tri@iki.fi
Subject:   bin/7402: Games primes and factor don't understand hexadecimals
Message-ID:  <199807261827.VAA29456@rinne.iki.fi>

next in thread | raw e-mail | index | archive | help

>Number:         7402
>Category:       bin
>Synopsis:       Games primes and factor don't understand hexadecimals
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jul 26 11:30:00 PDT 1998
>Last-Modified:
>Originator:     Timo J. Rinne
>Organization:
Helsinki University of Technology
>Release:        FreeBSD 3.0-971225-SNAP i386
>Environment:

	FreeBSD  3.0-971225-SNAP i386

>Description:

	Games primes and factor don't understand hexadecimals.
	This would make these `games' somewhat useful tools.

>How-To-Repeat:

	-

>Fix:
	
	I implemented -h flag for both of the programs.
	Giving this option, all input and output of the
	programs is in hexadecimal.

--- primes.c.orig	Wed Jul  1 21:42:50 1998
+++ primes.c	Sat Jul 25 17:06:36 1998
@@ -101,8 +101,8 @@
 extern char pattern[];
 extern int pattern_size;	/* length of pattern array */
 
-void	primes __P((ubig, ubig));
-ubig	read_num_buf __P((void));
+void	primes __P((ubig, ubig, int));
+ubig	read_num_buf __P((int));
 void	usage __P((void));
 
 int
@@ -114,9 +114,13 @@
 	ubig stop;		/* don't generate at or above this value */
 	int ch;
 	char *p;
+	int hexa = 0;
 
-	while ((ch = getopt(argc, argv, "")) != -1)
+	while ((ch = getopt(argc, argv, "h")) != EOF)
 		switch (ch) {
+		case 'h':
+			hexa = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -140,14 +144,14 @@
 			errx(1, "negative numbers aren't permitted.");
 
 		errno = 0;
-		start = strtoul(argv[0], &p, 10);
+		start = strtoul(argv[0], &p, hexa ? 16 : 10);
 		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, hexa ? 16 : 10);
 		if (errno)
 			err(1, "%s", argv[1]);
 		if (*p != '\0')
@@ -159,14 +163,14 @@
 			errx(1, "negative numbers aren't permitted.");
 
 		errno = 0;
-		start = strtoul(argv[0], &p, 10);
+		start = strtoul(argv[0], &p, hexa ? 16 : 10);
 		if (errno)
 			err(1, "%s", argv[0]);
 		if (*p != '\0')
 			errx(1, "%s: illegal numeric format.", argv[0]);
 		break;
 	case 0:
-		start = read_num_buf();
+		start = read_num_buf(hexa);
 		break;
 	default:
 		usage();
@@ -174,7 +178,7 @@
 
 	if (start > stop)
 		errx(1, "start value must be less than stop value.");
-	primes(start, stop);
+	primes(start, stop, hexa);
 	exit(0);
 }
 
@@ -183,7 +187,7 @@
  *	This routine returns a number n, where 0 <= n && n <= BIG.
  */
 ubig
-read_num_buf()
+read_num_buf(int hexa)
 {
 	ubig val;
 	char *p, buf[100];		/* > max number of digits. */
@@ -200,7 +204,7 @@
 		if (*p == '-')
 			errx(1, "negative numbers aren't permitted.");
 		errno = 0;
-		val = strtoul(buf, &p, 10);
+		val = strtoul(buf, &p, hexa ? 16 : 10);
 		if (errno)
 			err(1, "%s", buf);
 		if (*p != '\n')
@@ -213,9 +217,10 @@
  * primes - sieve and print primes from start up to and but not including stop
  */
 void
-primes(start, stop)
+primes(start, stop, hexa)
 	ubig start;	/* where to start generating */
 	ubig stop;	/* don't generate at or above this value */
+	int hexa;
 {
 	register char *q;		/* sieve spot */
 	register ubig factor;		/* index and factor */
@@ -256,7 +261,7 @@
 		for (p = &prime[0], factor = prime[0];
 		    factor < stop && p <= pr_limit; factor = *(++p)) {
 			if (factor >= start) {
-				printf("%lu\n", factor);
+				printf(hexa ? "0x%08x\n" : "%u\n", factor);
 			}
 		}
 		/* return early if we are done */
@@ -319,7 +324,7 @@
 		 */
 		for (q = table; q < tab_lim; ++q, start+=2) {
 			if (*q) {
-				printf("%lu\n", start);
+				printf(hexa ? "0x%08x\n" : "%u\n", start);
 			}
 		}
 	}


--- factor.c.orig	Tue Mar  3 19:17:22 1998
+++ factor.c	Sun Jul 26 20:04:36 1998
@@ -82,7 +82,7 @@
 extern ubig prime[];
 extern ubig *pr_limit;		/* largest prime in the prime array */
 
-void	pr_fact __P((ubig));	/* print factors of a value */
+void	pr_fact __P((ubig, int));	/* print factors of a value */
 void	usage __P((void));
 
 int
@@ -92,10 +92,15 @@
 {
 	ubig val;
 	int ch;
+	int hexa;
 	char *p, buf[100];		/* > max number of digits. */
 
-	while ((ch = getopt(argc, argv, "")) != -1)
+	hexa = 0;
+	while ((ch = getopt(argc, argv, "h")) != -1)
 		switch (ch) {
+		case 'h':
+			hexa = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -117,12 +122,12 @@
 			if (*p == '-')
 				errx(1, "negative numbers aren't permitted.");
 			errno = 0;
-			val = strtoul(buf, &p, 10);
+			val = strtoul(buf, &p, hexa ? 16 : 10);
 			if (errno)
 				err(1, "%s", buf);
 			if (*p != '\n')
 				errx(1, "%s: illegal numeric format.", buf);
-			pr_fact(val);
+			pr_fact(val, hexa);
 		}
 	/* Factor the arguments. */
 	else
@@ -130,12 +135,12 @@
 			if (argv[0][0] == '-')
 				errx(1, "negative numbers aren't permitted.");
 			errno = 0;
-			val = strtoul(argv[0], &p, 10);
+			val = strtoul(argv[0], &p, hexa ? 16 : 10);
 			if (errno)
 				err(1, "%s", argv[0]);
 			if (*p != '\0')
 				errx(1, "%s: illegal numeric format.", argv[0]);
-			pr_fact(val);
+			pr_fact(val, hexa);
 		}
 	exit(0);
 }
@@ -154,8 +159,9 @@
  * Factors are printed with leading tabs.
  */
 void
-pr_fact(val)
+pr_fact(val, hexa)
 	ubig val;		/* Factor this value. */
+	int hexa;
 {
 	ubig *fact;		/* The factor found. */
 
@@ -168,7 +174,7 @@
 	}
 
 	/* Factor value. */
-	(void)printf("%lu:", val);
+	(void)printf(hexa ? "0x%08x:" : "%lu:", val);
 	for (fact = &prime[0]; val > 1; ++fact) {
 		/* Look for the smallest factor. */
 		do {
@@ -178,13 +184,13 @@
 
 		/* Watch for primes larger than the table. */
 		if (fact > pr_limit) {
-			(void)printf(" %lu", val);
+			(void)printf(hexa ? " 0x%08x" : " %lu", val);
 			break;
 		}
 
 		/* Divide factor out until none are left. */
 		do {
-			(void)printf(" %lu", *fact);
+			(void)printf(hexa ? " 0x%08x" : " %lu", *fact);
 			val /= (long)*fact;
 		} while ((val % (long)*fact) == 0);
 
>Audit-Trail:
>Unformatted:
>Severit:       non-critical

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199807261827.VAA29456>