From b5ea937ee8abc00f218c186759abaf13273b43a8 Mon Sep 17 00:00:00 2001 From: fanf Date: Wed, 9 Oct 2002 19:38:55 +0000 Subject: 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 --- games/factor/factor.c | 58 ++++++++++++++++++-------------------------------- games/primes/pattern.c | 6 ++++-- games/primes/pr_tbl.c | 6 ++++-- games/primes/primes.c | 42 +++++++++++------------------------- games/primes/primes.h | 18 ++++++++++++++++ 5 files changed, 60 insertions(+), 70 deletions(-) (limited to 'games') diff --git a/games/factor/factor.c b/games/factor/factor.c index 7097d57..b0ce72c 100644 --- a/games/factor/factor.c +++ b/games/factor/factor.c @@ -56,7 +56,7 @@ static const char rcsid[] = * chongo /\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 #include +#include #include #include #include @@ -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 */ - -int hflag; +static 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); } diff --git a/games/primes/pattern.c b/games/primes/pattern.c index 1d72fb2..dff1f4d 100644 --- a/games/primes/pattern.c +++ b/games/primes/pattern.c @@ -56,7 +56,9 @@ static const char rcsid[] = #include -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])); diff --git a/games/primes/pr_tbl.c b/games/primes/pr_tbl.c index dd02b85..6447916 100644 --- a/games/primes/pr_tbl.c +++ b/games/primes/pr_tbl.c @@ -53,9 +53,11 @@ static const char rcsid[] = * and 65537^2 > 2^32-1. */ +#include + #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]; diff --git a/games/primes/primes.c b/games/primes/primes.c index cf23fef..08141b6 100644 --- a/games/primes/primes.c +++ b/games/primes/primes.c @@ -56,7 +56,7 @@ static const char rcsid[] = * chongo /\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); } diff --git a/games/primes/primes.h b/games/primes/primes.h index 611c86d..f4a90fa 100644 --- a/games/primes/primes.h +++ b/games/primes/primes.h @@ -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 */ -- cgit v1.1