From tri@rinne.iki.fi  Sun Jul 26 11:27:39 1998
Received: from lohi.clinet.fi (root@lohi.clinet.fi [194.100.0.7])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id LAA20056
          for <FreeBSD-gnats-submit@freebsd.org>; Sun, 26 Jul 1998 11:27:35 -0700 (PDT)
          (envelope-from tri@rinne.iki.fi)
Received: from rinne.iki.fi (tri@rinne.iki.fi [194.100.70.105])
	by lohi.clinet.fi (8.9.1/8.9.0) with ESMTP id VAA19565;
	Sun, 26 Jul 1998 21:33:03 +0300 (EEST)
Received: (from tri@localhost)
	by rinne.iki.fi (8.8.8/8.8.8) id VAA29456;
	Sun, 26 Jul 1998 21:27:05 +0300 (EEST)
	(envelope-from tri)
Message-Id: <199807261827.VAA29456@rinne.iki.fi>
Date: Sun, 26 Jul 1998 21:27:05 +0300 (EEST)
From: tri@iki.fi
Reply-To: tri@iki.fi
To: FreeBSD-gnats-submit@freebsd.org
Cc: tri@iki.fi
Subject: Games primes and factor don't understand hexadecimals
X-Send-Pr-Version: 3.2

>Number:         7402
>Category:       bin
>Synopsis:       Games primes and factor don't understand hexadecimals
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    imp
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jul 26 11:30:00 PDT 1998
>Closed-Date:    Wed Jan 6 01:14:29 MST 1999
>Last-Modified:  Wed Jan  6 01:15:10 MST 1999
>Originator:     Timo J. Rinne
>Release:        FreeBSD 3.0-971225-SNAP i386
>Organization:
Helsinki University of Technology
>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);
 
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: phk 
State-Changed-When: Mon Jul 27 06:12:00 PDT 1998 
State-Changed-Why:  
You should use the sepcial "base = 0" argument to strtoul, that way 
octal & hex input would always be available. 
State-Changed-From-To: feedback->closed 
State-Changed-By: phk 
State-Changed-When: Thu Aug 6 23:32:50 PDT 1998 
State-Changed-Why:  
committted, thanks 
State-Changed-From-To: closed->suspended 
State-Changed-By: phk 
State-Changed-When: Sun Aug 9 03:36:32 PDT 1998 
State-Changed-Why:  
reverted commit , too much trouble with this patch: 
PR7402 was even less suitable for committing almost verbatim than at 
first appearance.  Rev.1.9 of primes.c has at least the following defects. 

- no update for man page. 
- no update for usage string. 
- blowing away of a previous commit to change EOF to -1 in getopt() test. 
- blowing away of a previous commit to fix printf format errors. 
- new printf format errors. 
- one gratuitous ANSIism. 
- two style bugs. 
- ... and a partition in a pear tree. 

awaiting better patch & committer 
Responsible-Changed-From-To: freebsd-bugs->imp 
Responsible-Changed-By: imp 
Responsible-Changed-When: Wed Sep 16 11:10:50 MDT 1998 
Responsible-Changed-Why:  
Because I need something silly to go along with the serious PRs 
that I've saddled myself with. 
State-Changed-From-To: suspended->closed 
State-Changed-By: imp 
State-Changed-When: Wed Jan 6 01:14:29 MST 1999 
State-Changed-Why:  
fixed.  tahnks! 
Fixed Thanks! (first line typed too fast) 
>Unformatted:
 >Severit:       non-critical
