X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/a2515e35506e73e8f6422799dea7f4834cd0f558..bfb149ab182f4c873c3c8eed8a00bdb81539513a:/fish/fish.c diff --git a/fish/fish.c b/fish/fish.c index c81da3ff..becedf6a 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -1,4 +1,4 @@ -/* $NetBSD: fish.c,v 1.6 1998/09/13 15:27:27 hubertf Exp $ */ +/* $NetBSD: fish.c,v 1.19 2008/07/20 01:03:21 lukem Exp $ */ /*- * Copyright (c) 1990, 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) 1990, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1990, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: fish.c,v 1.6 1998/09/13 15:27:27 hubertf Exp $"); +__RCSID("$NetBSD: fish.c,v 1.19 2008/07/20 01:03:21 lukem Exp $"); #endif #endif /* not lint */ @@ -65,45 +61,47 @@ __RCSID("$NetBSD: fish.c,v 1.6 1998/09/13 15:27:27 hubertf Exp $"); #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[] = { +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 asked[RANKS], comphand[RANKS], deck[TOTCARDS]; int userasked[RANKS], userhand[RANKS]; - -void chkwinner __P((int, int *)); -int compmove __P((void)); -int countbooks __P((int *)); -int countcards __P((int *)); -int drawcard __P((int, int *)); -int gofish __P((int, int, int *)); -void goodmove __P((int, int, int *, int *)); -void init __P((void)); -void instructions __P((void)); -int main __P((int, char *[])); -int nrandom __P((int)); -void printhand __P((int *)); -void printplayer __P((int)); -int promove __P((void)); -void usage __P((void)) __attribute__((__noreturn__)); -int usermove __P((void)); +int curcard = TOTCARDS; + +void chkwinner(int, const int *); +int compmove(void); +int countbooks(const int *); +int countcards(const int *); +int drawcard(int, int *); +int gofish(int, int, int *); +void goodmove(int, int, int *, int *); +void init(void); +void instructions(void); +int nrandom(int); +void printhand(const int *); +void printplayer(int); +int promove(void); +void usage(void) __dead; +int usermove(void); int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { int ch, move; + /* Revoke setgid privileges */ + setgid(getgid()); + while ((ch = getopt(argc, argv, "p")) != -1) switch(ch) { case 'p': @@ -111,8 +109,7 @@ main(argc, argv) break; case '?': default: - (void)fprintf(stderr, "usage: fish [-p]\n"); - exit(1); + usage(); } srandom(time((time_t *)NULL)); @@ -150,10 +147,10 @@ istart: for (;;) { } int -usermove() +usermove(void) { int n; - char **p; + const char *const *p; char buf[256]; (void)printf("\nYour hand is:"); @@ -162,13 +159,13 @@ 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; @@ -205,7 +202,7 @@ usermove() } int -compmove() +compmove(void) { static int lmove; @@ -223,7 +220,7 @@ compmove() } int -promove() +promove(void) { int i, max; @@ -262,15 +259,11 @@ promove() } int -drawcard(player, hand) - int player; - int *hand; +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]); @@ -285,9 +278,7 @@ drawcard(player, hand) } int -gofish(askedfor, player, hand) - int askedfor, player; - int *hand; +gofish(int askedfor, int player, int *hand) { printplayer(OTHER(player)); (void)printf("say \"GO FISH!\"\n"); @@ -302,9 +293,7 @@ gofish(askedfor, player, hand) } void -goodmove(player, move, hand, opphand) - int player, move; - int *hand, *opphand; +goodmove(int player, int move, int *hand, int *opphand) { printplayer(OTHER(player)); (void)printf("have %d %s%s.\n", @@ -326,9 +315,7 @@ goodmove(player, move, hand, opphand) } void -chkwinner(player, hand) - int player; - int *hand; +chkwinner(int player, const int *hand) { int cb, i, ub; @@ -356,8 +343,7 @@ chkwinner(player, hand) } void -printplayer(player) - int player; +printplayer(int player) { switch (player) { case COMPUTER: @@ -370,8 +356,7 @@ printplayer(player) } void -printhand(hand) - int *hand; +printhand(const int *hand) { int book, i, j; @@ -391,8 +376,7 @@ printhand(hand) } int -countcards(hand) - int *hand; +countcards(const int *hand) { int i, count; @@ -402,8 +386,7 @@ countcards(hand) } int -countbooks(hand) - int *hand; +countbooks(const int *hand) { int i, count; @@ -419,37 +402,40 @@ countbooks(hand) } void -init() +init(void) { - 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]]; } } int -nrandom(n) - int n; +nrandom(int n) { return((int)random() % n); } void -instructions() +instructions(void) { int input; pid_t pid; + int fd; + const char *pager; int status; (void)printf("Would you like instructions (y or n)? "); @@ -460,10 +446,18 @@ instructions() switch (pid = fork()) { case 0: /* child */ - (void)setuid(getuid()); - (void)setgid(getgid()); - (void)execl(_PATH_MORE, "more", _PATH_INSTR, NULL); - err(1, "%s %s", _PATH_MORE, _PATH_INSTR); + 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"); @@ -477,7 +471,7 @@ instructions() } void -usage() +usage(void) { (void)fprintf(stderr, "usage: fish [-p]\n"); exit(1);