X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/462fca95471224f85b85be911e72faee16cd1597..107ae55b63501f7ef44616f1625a6d1800e5289d:/fish/fish.c diff --git a/fish/fish.c b/fish/fish.c index 9eb93031..78e2dc52 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -1,6 +1,8 @@ +/* $NetBSD: fish.c,v 1.23 2018/03/05 04:59:54 eadler Exp $ */ + /*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Muffy Barkocy. @@ -13,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. * @@ -34,61 +32,87 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1990 The Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1990, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)fish.c 5.4 (Berkeley) 1/18/91";*/ -static char rcsid[] = "$Id: fish.c,v 1.2 1993/08/01 18:54:54 mycroft Exp $"; +#if 0 +static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: fish.c,v 1.23 2018/03/05 04:59:54 eadler Exp $"); +#endif #endif /* not lint */ #include -#include +#include +#include #include #include #include +#include #include +#include +#include #include "pathnames.h" #define RANKS 13 #define HANDSIZE 7 #define CARDS 4 +#define TOTCARDS RANKS * CARDS #define USER 1 #define COMPUTER 0 #define OTHER(a) (1 - (a)) -char *cards[] = { +static const char *const cards[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", NULL, }; #define PRC(card) (void)printf(" %s", cards[card]) -int promode; -int asked[RANKS], comphand[RANKS], deck[RANKS]; -int userasked[RANKS], userhand[RANKS]; - -main(argc, argv) - int argc; - char **argv; +static int promode; +static int asked[RANKS], comphand[RANKS], deck[TOTCARDS]; +static int userasked[RANKS], userhand[RANKS]; +static int curcard = TOTCARDS; + +static void chkwinner(int, const int *); +static int compmove(void); +static int countbooks(const int *); +static int countcards(const int *); +static int drawcard(int, int *); +static int gofish(int, int, int *); +static void goodmove(int, int, int *, int *); +static void init(void); +static void instructions(void); +static int nrandom(int); +static void printhand(const int *); +static void printplayer(int); +static int promove(void); +static void usage(void) __dead; +static int usermove(void); + +int +main(int argc, char **argv) { int ch, move; - while ((ch = getopt(argc, argv, "p")) != EOF) + /* Revoke setgid privileges */ + setgid(getgid()); + + while ((ch = getopt(argc, argv, "p")) != -1) switch(ch) { case 'p': promode = 1; break; case '?': default: - (void)fprintf(stderr, "usage: fish [-p]\n"); - exit(1); + usage(); } - srandom(time((time_t *)NULL)); + srandom(time(NULL)); instructions(); init(); @@ -122,10 +146,11 @@ istart: for (;;) { /* NOTREACHED */ } -usermove() +static int +usermove(void) { - register int n; - register char **p; + int n; + const char *const *p; char buf[256]; (void)printf("\nYour hand is:"); @@ -134,21 +159,26 @@ usermove() for (;;) { (void)printf("You ask me for: "); (void)fflush(stdout); - if (fgets(buf, BUFSIZ, stdin) == NULL) + if (fgets(buf, sizeof(buf), stdin) == NULL) exit(0); if (buf[0] == '\0') continue; if (buf[0] == '\n') { (void)printf("%d cards in my hand, %d in the pool.\n", - countcards(comphand), countcards(deck)); + countcards(comphand), curcard); (void)printf("My books:"); (void)countbooks(comphand); continue; } buf[strlen(buf) - 1] = '\0'; - if (!strcasecmp(buf, "p") && !promode) { - promode = 1; - (void)printf("Entering pro mode.\n"); + if (!strcasecmp(buf, "p")) { + if (!promode) { + promode = 1; + printf("Entering pro mode.\n"); + } + else { + printf("Already in pro mode.\n"); + } continue; } if (!strcasecmp(buf, "quit")) @@ -161,10 +191,15 @@ usermove() continue; } n = p - cards; - if (userhand[n]) { + if (userhand[n] <= 3) { userasked[n] = 1; return(n); } + if (userhand[n] == 4) { + printf("You already have all of those.\n"); + continue; + } + if (nrandom(3) == 1) (void)printf("You don't have any of those!\n"); else @@ -176,7 +211,8 @@ usermove() /* NOTREACHED */ } -compmove() +static int +compmove(void) { static int lmove; @@ -193,9 +229,10 @@ compmove() return(lmove); } -promove() +static int +promove(void) { - register int i, max; + int i, max; for (i = 0; i < RANKS; ++i) if (userasked[i] && @@ -231,15 +268,12 @@ promove() /* NOTREACHED */ } -drawcard(player, hand) - int player; - int *hand; +static int +drawcard(int player, int *hand) { int card; - while (deck[card = nrandom(RANKS)] == 0); - ++hand[card]; - --deck[card]; + ++hand[card = deck[--curcard]]; if (player == USER || hand[card] == CARDS) { printplayer(player); (void)printf("drew %s", cards[card]); @@ -253,9 +287,8 @@ drawcard(player, hand) return(card); } -gofish(askedfor, player, hand) - int askedfor, player; - int *hand; +static int +gofish(int askedfor, int player, int *hand) { printplayer(OTHER(player)); (void)printf("say \"GO FISH!\"\n"); @@ -269,9 +302,8 @@ gofish(askedfor, player, hand) return(0); } -goodmove(player, move, hand, opphand) - int player, move; - int *hand, *opphand; +static void +goodmove(int player, int move, int *hand, int *opphand) { printplayer(OTHER(player)); (void)printf("have %d %s%s.\n", @@ -292,11 +324,10 @@ goodmove(player, move, hand, opphand) (void)printf("get another guess!\n"); } -chkwinner(player, hand) - int player; - register int *hand; +static void +chkwinner(int player, const int *hand) { - register int cb, i, ub; + int cb, i, ub; for (i = 0; i < RANKS; ++i) if (hand[i] > 0 && hand[i] < CARDS) @@ -321,8 +352,8 @@ chkwinner(player, hand) exit(0); } -printplayer(player) - int player; +static void +printplayer(int player) { switch (player) { case COMPUTER: @@ -334,10 +365,10 @@ printplayer(player) } } -printhand(hand) - int *hand; +static void +printhand(const int *hand) { - register int book, i, j; + int book, i, j; for (book = i = 0; i < RANKS; i++) if (hand[i] < CARDS) @@ -354,18 +385,18 @@ printhand(hand) (void)putchar('\n'); } -countcards(hand) - register int *hand; +static int +countcards(const int *hand) { - register int i, count; + int i, count; for (count = i = 0; i < RANKS; i++) count += *hand++; return(count); } -countbooks(hand) - int *hand; +static int +countbooks(const int *hand) { int i, count; @@ -380,36 +411,42 @@ countbooks(hand) return(count); } -init() +static void +init(void) { - register int i, rank; + int i, j, temp; - for (i = 0; i < RANKS; ++i) - deck[i] = CARDS; - for (i = 0; i < HANDSIZE; ++i) { - while (!deck[rank = nrandom(RANKS)]); - ++userhand[rank]; - --deck[rank]; + for (i = 0; i < TOTCARDS; ++i) + deck[i] = i % RANKS; + for (i = 0; i < TOTCARDS - 1; ++i) { + j = nrandom(TOTCARDS-i); + if (j == 0) + continue; + temp = deck[i]; + deck[i] = deck[i+j]; + deck[i+j] = temp; } for (i = 0; i < HANDSIZE; ++i) { - while (!deck[rank = nrandom(RANKS)]); - ++comphand[rank]; - --deck[rank]; + ++userhand[deck[--curcard]]; + ++comphand[deck[--curcard]]; } } -nrandom(n) - int n; +static int +nrandom(int n) { - long random(); return((int)random() % n); } -instructions() +static void +instructions(void) { int input; - char buf[1024]; + pid_t pid; + int fd; + const char *pager; + int status; (void)printf("Would you like instructions (y or n)? "); input = getchar(); @@ -417,13 +454,34 @@ instructions() if (input != 'y') return; - (void)sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR); - (void)system(buf); + switch (pid = fork()) { + case 0: /* child */ + if (!isatty(1)) + pager = "cat"; + else { + if (!(pager = getenv("PAGER")) || (*pager == 0)) + pager = _PATH_MORE; + } + if ((fd = open(_PATH_INSTR, O_RDONLY)) == -1) + err(1, "open %s", _PATH_INSTR); + if (dup2(fd, 0) == -1) + err(1, "dup2"); + (void)execl("/bin/sh", "sh", "-c", pager, (char *) NULL); + err(1, "exec sh -c %s", pager); + /*NOTREACHED*/ + case -1: + err(1, "fork"); + /*NOTREACHED*/ + default: + (void)waitpid(pid, &status, 0); + break; + } (void)printf("Hit return to continue...\n"); while ((input = getchar()) != EOF && input != '\n'); } -usage() +static void +usage(void) { (void)fprintf(stderr, "usage: fish [-p]\n"); exit(1);