X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/77e3814f0c0e3dea4d0032e25666f77e6f83bfff..3605b35b053f9bd5ad20e02585828040a3ce939b:/arithmetic/arithmetic.c?ds=sidebyside diff --git a/arithmetic/arithmetic.c b/arithmetic/arithmetic.c index 4fd8bf96..876153a9 100644 --- a/arithmetic/arithmetic.c +++ b/arithmetic/arithmetic.c @@ -1,6 +1,8 @@ +/* $NetBSD: arithmetic.c,v 1.18 2002/03/31 04:07:22 hubertf Exp $ */ + /* - * Copyright (c) 1989 The Regents of the University of California. - * All rights reserved. + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Eamonn McManus of Trinity College Dublin. @@ -34,14 +36,18 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1989 The Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ + The Regents of the University of California. All rights reserved.\n"); #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)arithmetic.c 5.5 (Berkeley) 2/27/91"; +#if 0 +static char sccsid[] = "@(#)arithmetic.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: arithmetic.c,v 1.18 2002/03/31 04:07:22 hubertf Exp $"); +#endif #endif /* not lint */ /* @@ -73,14 +79,27 @@ static char sccsid[] = "@(#)arithmetic.c 5.5 (Berkeley) 2/27/91"; */ #include -#include +#include #include +#include #include +#include #include +#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((int)); +void usage __P((void)) __attribute__((__noreturn__)); -char keylist[] = "+-x/"; -char defaultkeys[] = "+-"; -char *keys = defaultkeys; +const char keylist[] = "+-x/"; +const char defaultkeys[] = "+-"; +const char *keys = defaultkeys; int nkeys = sizeof(defaultkeys) - 1; int rangemax = 10; int nright, nwrong; @@ -94,37 +113,30 @@ time_t qtime; * bound is 10. After every NQUESTS questions, statistics on the performance * so far are printed. */ -void +int main(argc, argv) int argc; char **argv; { - extern char *optarg; - extern int optind; int ch, cnt; - time_t time(); - void intr(); - while ((ch = getopt(argc, argv, "r:o:")) != EOF) + /* Revoke setgid privileges */ + setgid(getgid()); + + while ((ch = getopt(argc, argv, "r:o:")) != -1) switch(ch) { case 'o': { - register char *p; + const char *p; for (p = keys = optarg; *p; ++p) - if (!index(keylist, *p)) { - (void)fprintf(stderr, - "arithmetic: unknown key.\n"); - exit(1); - } + if (!strchr(keylist, *p)) + errx(1, "arithmetic: unknown key."); nkeys = p - optarg; break; } case 'r': - if ((rangemax = atoi(optarg)) <= 0) { - (void)fprintf(stderr, - "arithmetic: invalid range.\n"); - exit(1); - } + if ((rangemax = atoi(optarg)) <= 0) + errx(1, "arithmetic: invalid range."); break; case '?': default: @@ -143,21 +155,24 @@ 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() +intr(dummy) + int dummy __attribute__((__unused__)); { - showstats(); + showstats(1); exit(0); } /* Print score. Original `arithmetic' had a delay after printing it. */ -showstats() +void +showstats(bool_sigint) + int bool_sigint; { if (nright + nwrong > 0) { (void)printf("\n\nRights %d; Wrongs %d; Score %d%%", @@ -166,6 +181,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"); } @@ -177,13 +196,15 @@ showstats() * answer causes the numbers in the problem to be penalised, so that they are * more likely to appear in subsequent problems. */ +int problem() { - register char *p; + char *p; time_t start, finish; int left, op, right, result; char line[80]; + right = left = result = 0; op = keys[random() % nkeys]; if (op != '/') right = getrandom(rangemax + 1, op, 1); @@ -288,11 +309,11 @@ 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; { struct penalty *p; - char *malloc(); op = opnum(op); if ((p = (struct penalty *)malloc((u_int)sizeof(*p))) == NULL) @@ -309,11 +330,12 @@ 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; { int value; - register struct penalty **pp, *p; + struct penalty **pp, *p; op = opnum(op); value = random() % (maxval + penalty[op][operand]); @@ -348,28 +370,28 @@ getrandom(maxval, op, operand) * correspond to the actual sum of penalties in the list. Provide an * obscure message. */ - (void)fprintf(stderr, "arithmetic: bug: inconsistent penalties\n"); - exit(1); + errx(1, "arithmetic: bug: inconsistent penalties."); /* NOTREACHED */ } /* Return an index for the character op, which is one of [+-x/]. */ +int opnum(op) int op; { char *p; - if (op == 0 || (p = index(keylist, op)) == NULL) { - (void)fprintf(stderr, - "arithmetic: bug: op %c not in keylist %s\n", op, keylist); - exit(1); - } + if (op == 0 || (p = strchr(keylist, op)) == NULL) + errx(1, "arithmetic: bug: op %c not in keylist %s", + op, keylist); return(p - keylist); } /* Print usage message and quit. */ +void usage() { - (void)fprintf(stderr, "usage: arithmetic [-o +-x/] [-r range]\n"); + (void)fprintf(stderr, "Usage: %s [-o +-x/] [-r range]\n", + getprogname()); exit(1); }