X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/88eff08098072c4b923af3a7180b14d6dfcf66ef..HEAD:/arithmetic/arithmetic.c diff --git a/arithmetic/arithmetic.c b/arithmetic/arithmetic.c index c3ac455f..743948e6 100644 --- a/arithmetic/arithmetic.c +++ b/arithmetic/arithmetic.c @@ -1,4 +1,4 @@ -/* $NetBSD: arithmetic.c,v 1.15 2000/05/08 07:56:00 mycroft Exp $ */ +/* $NetBSD: arithmetic.c,v 1.27 2012/06/19 05:46:08 dholland Exp $ */ /* * Copyright (c) 1989, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -38,15 +34,15 @@ #include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1989, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)arithmetic.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: arithmetic.c,v 1.15 2000/05/08 07:56:00 mycroft Exp $"); +__RCSID("$NetBSD: arithmetic.c,v 1.27 2012/06/19 05:46:08 dholland Exp $"); #endif #endif /* not lint */ @@ -88,22 +84,21 @@ __RCSID("$NetBSD: arithmetic.c,v 1.15 2000/05/08 07:56:00 mycroft Exp $"); #include #include -int getrandom __P((int, int, int)); -void intr __P((int)) __attribute__((__noreturn__)); -int main __P((int, char *[])); -int opnum __P((int)); -void penalise __P((int, int, int)); -int problem __P((void)); -void showstats __P((void)); -void usage __P((void)) __attribute__((__noreturn__)); - -const char keylist[] = "+-x/"; -const char defaultkeys[] = "+-"; -const char *keys = defaultkeys; -int nkeys = sizeof(defaultkeys) - 1; -int rangemax = 10; -int nright, nwrong; -time_t qtime; +static int getrandom(int, int, int); +static void intr(int) __dead; +static int opnum(int); +static void penalise(int, int, int); +static int problem(void); +static void showstats(int); +static void usage(void) __dead; + +static const char keylist[] = "+-x/"; +static const char defaultkeys[] = "+-"; +static const char *keys = defaultkeys; +static int nkeys = sizeof(defaultkeys) - 1; +static int rangemax = 10; +static int nright, nwrong; +static time_t qtime; #define NQUESTS 20 /* @@ -114,9 +109,7 @@ time_t qtime; * so far are printed. */ int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { int ch, cnt; @@ -146,7 +139,7 @@ main(argc, argv) usage(); /* Seed the random-number generator. */ - srandom((int)time((time_t *)NULL)); + srandom((int)time(NULL)); (void)signal(SIGINT, intr); @@ -155,23 +148,22 @@ main(argc, argv) for (cnt = NQUESTS; cnt--;) if (problem() == EOF) exit(0); - showstats(); + showstats(0); } /* NOTREACHED */ } /* Handle interrupt character. Print score and exit. */ -void -intr(dummy) - int dummy __attribute__((__unused__)); +static void +intr(int dummy __unused) { - showstats(); + showstats(1); exit(0); } /* Print score. Original `arithmetic' had a delay after printing it. */ -void -showstats() +static void +showstats(int bool_sigint) { if (nright + nwrong > 0) { (void)printf("\n\nRights %d; Wrongs %d; Score %d%%", @@ -180,6 +172,10 @@ showstats() (void)printf("\nTotal time %ld seconds; %.1f seconds per problem\n\n", (long)qtime, (float)qtime / nright); } + if(!bool_sigint) { + (void)printf("Press RETURN to continue...\n"); + while(!getchar()) ; + } (void)printf("\n"); } @@ -191,8 +187,8 @@ showstats() * answer causes the numbers in the problem to be penalised, so that they are * more likely to appear in subsequent problems. */ -int -problem() +static int +problem(void) { char *p; time_t start, finish; @@ -245,8 +241,8 @@ retry: (void)printf("\n"); return(EOF); } - for (p = line; *p && isspace(*p); ++p); - if (!isdigit(*p)) { + for (p = line; *p && isspace((unsigned char)*p); ++p); + if (!isdigit((unsigned char)*p)) { (void)printf("Please type a number.\n"); continue; } @@ -291,8 +287,8 @@ retry: * penalties themselves. */ -int penalty[sizeof(keylist) - 1][2]; -struct penalty { +static int penalty[sizeof(keylist) - 1][2]; +static struct penalty { int value, penalty; /* Penalised value and its penalty. */ struct penalty *next; } *penlist[sizeof(keylist) - 1][2]; @@ -304,14 +300,13 @@ struct penalty { * operand number `operand' (0 or 1). If we run out of memory, we just * forget about the penalty (how likely is this, anyway?). */ -void -penalise(value, op, operand) - int value, op, operand; +static void +penalise(int value, int op, int operand) { struct penalty *p; op = opnum(op); - if ((p = (struct penalty *)malloc((u_int)sizeof(*p))) == NULL) + if ((p = malloc(sizeof(*p))) == NULL) return; p->next = penlist[op][operand]; penlist[op][operand] = p; @@ -325,9 +320,8 @@ penalise(value, op, operand) * as a value, or represents a position in the penalty list. If the latter, * we find the corresponding value and return that, decreasing its penalty. */ -int -getrandom(maxval, op, operand) - int maxval, op, operand; +static int +getrandom(int maxval, int op, int operand) { int value; struct penalty **pp, *p; @@ -370,9 +364,8 @@ getrandom(maxval, op, operand) } /* Return an index for the character op, which is one of [+-x/]. */ -int -opnum(op) - int op; +static int +opnum(int op) { char *p; @@ -383,12 +376,10 @@ opnum(op) } /* Print usage message and quit. */ -void -usage() +static void +usage(void) { - extern char *__progname; /* from crt0.o */ - - (void)fprintf(stderr, "usage: %s [-o +-x/] [-r range]\n", - __progname); + (void)fprintf(stderr, "Usage: %s [-o +-x/] [-r range]\n", + getprogname()); exit(1); }