Style fixes to: #include ordering; use const and static; ANSI functions;

various usage synopses; bogus and/or unnecessary casting; exit values;
use LINE_MAX instead of magic numbers; declare extern variables in a
header; add $FreeBSD$ where missing.

Reviewed by:    markm, obrien
Obtained from:  NetBSD | fanf
This commit is contained in:
Tony Finch 2002-10-09 19:38:55 +00:00
parent 5715307f74
commit 9a171f4fc4
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=104720
5 changed files with 60 additions and 70 deletions

View File

@ -56,7 +56,7 @@ static const char rcsid[] =
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
*
* usage:
* factor [number] ...
* factor [-h] [number] ...
*
* The form of the output is:
*
@ -67,8 +67,8 @@ static const char rcsid[] =
* If no args are given, the list of numbers are read from stdin.
*/
#include <err.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
@ -77,28 +77,17 @@ static const char rcsid[] =
#include "primes.h"
/*
* prime[i] is the (i-1)th prime.
*
* We are able to sieve 2^32-1 because this byte table yields all primes
* up to 65537 and 65537^2 > 2^32-1.
*/
extern ubig prime[];
extern ubig *pr_limit; /* largest prime in the prime array */
static int hflag;
int hflag;
void pr_fact(ubig); /* print factors of a value */
void usage(void);
static void pr_fact(ubig); /* print factors of a value */
static void usage(void);
int
main(argc, argv)
int argc;
char *argv[];
main(int argc, char *argv[])
{
ubig val;
int ch;
char *p, buf[100]; /* > max number of digits. */
char *p, buf[LINE_MAX]; /* > max number of digits. */
while ((ch = getopt(argc, argv, "h")) != -1)
switch (ch) {
@ -152,60 +141,55 @@ main(argc, argv)
/*
* pr_fact - print the factors of a number
*
* If the number is 0 or 1, then print the number and return.
* If the number is < 0, print -1, negate the number and continue
* processing.
*
* Print the factors of the number, from the lowest to the highest.
* A factor will be printed multiple times if it divides the value
* multiple times.
*
* Factors are printed with leading tabs.
*/
void
pr_fact(val)
ubig val; /* Factor this value. */
static void
pr_fact(ubig val)
{
ubig *fact; /* The factor found. */
const ubig *fact; /* The factor found. */
/* Firewall - catch 0 and 1. */
if (val == 0) /* Historical practice; 0 just exits. */
exit(0);
if (val == 1) {
(void)printf("1: 1\n");
printf("1: 1\n");
return;
}
/* Factor value. */
(void)printf(hflag ? "0x%lx:" : "%lu:", val);
printf(hflag ? "0x%lx:" : "%lu:", val);
for (fact = &prime[0]; val > 1; ++fact) {
/* Look for the smallest factor. */
do {
if (val % (long)*fact == 0)
if (val % *fact == 0)
break;
} while (++fact <= pr_limit);
/* Watch for primes larger than the table. */
if (fact > pr_limit) {
(void)printf(hflag ? " 0x%lx" : " %lu", val);
printf(hflag ? " 0x%lx" : " %lu", val);
break;
}
/* Divide factor out until none are left. */
do {
(void)printf(hflag ? " 0x%lx" : " %lu", *fact);
printf(hflag ? " 0x%lx" : " %lu", *fact);
val /= *fact;
} while ((val % *fact) == 0);
/* Let the user know we're doing something. */
(void)fflush(stdout);
fflush(stdout);
}
(void)putchar('\n');
putchar('\n');
}
void
usage()
static void
usage(void)
{
(void)fprintf(stderr, "usage: factor -h [value ...]\n");
exit (0);
fprintf(stderr, "usage: factor [-h] [value ...]\n");
exit(1);
}

View File

@ -56,7 +56,9 @@ static const char rcsid[] =
#include <stddef.h>
char pattern[] = {
#include "primes.h"
const char pattern[] = {
1,0,0,0,0,0,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,0,1,1,0,0,
1,0,1,0,0,1,0,0,0,1,0,1,1,0,1,1,0,1,0,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,1,1,0,0,
1,0,0,1,0,1,0,0,1,0,0,1,1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,1,0,1,
@ -443,4 +445,4 @@ char pattern[] = {
0,0,1,1,0,0,0,0,1,1,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,0,1,1,0,1,0,0,0,1,0,0,1,0,1,
0,0,1,1,0,1,0,0,1,1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0,0,1
};
size_t pattern_size = (sizeof(pattern)/sizeof(pattern[0]));
const size_t pattern_size = (sizeof(pattern)/sizeof(pattern[0]));

View File

@ -53,9 +53,11 @@ static const char rcsid[] =
* and 65537^2 > 2^32-1.
*/
#include <stddef.h>
#include "primes.h"
ubig prime[] = {
const ubig prime[] = {
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,
107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,
211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,
@ -547,4 +549,4 @@ ubig prime[] = {
};
/* pr_limit - largest prime in the prime table */
ubig *pr_limit = &prime[(sizeof(prime)/sizeof(prime[0]))-1];
const ubig *const pr_limit = &prime[(sizeof(prime)/sizeof(prime[0]))-1];

View File

@ -56,7 +56,7 @@ static const char rcsid[] =
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
*
* usage:
* primes [start [stop]]
* primes [-h] [start [stop]]
*
* Print primes >= start and < stop. If stop is omitted,
* the value 4294967295 (2^32-1) is assumed. If start is
@ -88,23 +88,6 @@ static const char rcsid[] =
*/
static char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */
/*
* prime[i] is the (i-1)th prime.
*
* We are able to sieve 2^32-1 because this byte table yields all primes
* up to 65537 and 65537^2 > 2^32-1.
*/
extern ubig prime[];
extern ubig *pr_limit; /* largest prime in the prime array */
/*
* To avoid excessive sieves for small factors, we use the table below to
* setup our sieve blocks. Each element represents a odd number starting
* with 1. All non-zero elements are factors of 3, 5, 7, 11 and 13.
*/
extern char pattern[];
extern size_t pattern_size; /* length of pattern array */
static int hflag;
static void primes(ubig, ubig);
@ -193,7 +176,7 @@ static ubig
read_num_buf(void)
{
ubig val;
char *p, buf[100]; /* > max number of digits. */
char *p, buf[LINE_MAX]; /* > max number of digits. */
for (;;) {
if (fgets(buf, sizeof(buf), stdin) == NULL) {
@ -225,8 +208,9 @@ primes(ubig start, ubig stop)
char *q; /* sieve spot */
ubig factor; /* index and factor */
char *tab_lim; /* the limit to sieve on the table */
ubig *p; /* prime table pointer */
const ubig *p; /* prime table pointer */
ubig fact_lim; /* highest prime for current block */
ubig mod; /* temp storage for mod */
/*
* A number of systems can not convert double values into unsigned
@ -296,28 +280,28 @@ primes(ubig start, ubig stop)
/* note highest useful factor and sieve spot */
if (stop-start > TABSIZE+TABSIZE) {
tab_lim = &table[TABSIZE]; /* sieve it all */
fact_lim = (int)sqrt(
(double)(start)+TABSIZE+TABSIZE+1.0);
fact_lim = sqrt(start+1.0+TABSIZE+TABSIZE);
} else {
tab_lim = &table[(stop-start)/2]; /* partial sieve */
fact_lim = (int)sqrt((double)(stop)+1.0);
fact_lim = sqrt(stop+1.0);
}
/* sieve for factors >= 17 */
factor = 17; /* 17 is first prime to use */
p = &prime[7]; /* 19 is next prime, pi(19)=7 */
do {
/* determine the factor's initial sieve point */
q = (char *)(start%factor); /* temp storage for mod */
if ((long)q & 0x1) {
q = &table[(factor-(long)q)/2];
mod = start%factor;
if (mod & 0x1) {
q = &table[(factor-mod)/2];
} else {
q = &table[q ? factor-((long)q/2) : 0];
q = &table[mod ? factor-(mod/2) : 0];
}
/* sive for our current factor */
for ( ; q < tab_lim; q += factor) {
*q = '\0'; /* sieve out a spot */
}
} while ((factor=(ubig)(*(p++))) <= fact_lim);
factor = *p++;
} while (factor <= fact_lim);
/*
* print generated primes
@ -333,6 +317,6 @@ primes(ubig start, ubig stop)
static void
usage(void)
{
(void)fprintf(stderr, "usage: primes [-h] [start [stop]]\n");
fprintf(stderr, "usage: primes [-h] [start [stop]]\n");
exit(1);
}

View File

@ -34,6 +34,7 @@
* SUCH DAMAGE.
*
* @(#)primes.h 8.2 (Berkeley) 3/1/94
* $FreeBSD$
*/
/*
@ -50,3 +51,20 @@ typedef unsigned long ubig; /* must be >=32 bit unsigned value */
/* bytes in sieve table (must be > 3*5*7*11) */
#define TABSIZE 256*1024
/*
* prime[i] is the (i-1)th prime.
*
* We are able to sieve 2^32-1 because this byte table yields all primes
* up to 65537 and 65537^2 > 2^32-1.
*/
extern const ubig prime[];
extern const ubig *const pr_limit; /* largest prime in the prime array */
/*
* To avoid excessive sieves for small factors, we use the table below to
* setup our sieve blocks. Each element represents a odd number starting
* with 1. All non-zero elements are factors of 3, 5, 7, 11 and 13.
*/
extern const char pattern[];
extern const size_t pattern_size; /* length of pattern array */