-/* $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
* 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.
*
#include <sys/cdefs.h>
#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 */
#include <time.h>
#include <unistd.h>
-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
/*
* so far are printed.
*/
int
-main(argc, argv)
- int argc;
- char **argv;
+main(int argc, char **argv)
{
int ch, cnt;
usage();
/* Seed the random-number generator. */
- srandom((int)time((time_t *)NULL));
+ srandom((int)time(NULL));
(void)signal(SIGINT, intr);
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%%",
(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");
}
* 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;
(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;
}
* 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];
* 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;
* 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;
}
/* Return an index for the character op, which is one of [+-x/]. */
-int
-opnum(op)
- int op;
+static int
+opnum(int op)
{
char *p;
}
/* 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);
}