+/* $NetBSD: fish.c,v 1.7 1999/04/24 22:09:06 kristerw 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.
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
- All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)fish.c 5.4 (Berkeley) 1/18/91";
+#if 0
+static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: fish.c,v 1.7 1999/04/24 22:09:06 kristerw Exp $");
+#endif
#endif /* not lint */
#include <sys/types.h>
-#include <sys/errno.h>
+#include <sys/wait.h>
+#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
+#include <time.h>
+#include <err.h>
#include "pathnames.h"
#define RANKS 13
int asked[RANKS], comphand[RANKS], deck[RANKS];
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
main(argc, argv)
int argc;
char **argv;
{
int ch, move;
- while ((ch = getopt(argc, argv, "p")) != EOF)
+ while ((ch = getopt(argc, argv, "p")) != -1)
switch(ch) {
case 'p':
promode = 1;
/* NOTREACHED */
}
+int
usermove()
{
- register int n;
- register char **p;
+ int n;
+ char **p;
char buf[256];
(void)printf("\nYour hand is:");
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;
/* NOTREACHED */
}
+int
compmove()
{
static int lmove;
return(lmove);
}
+int
promove()
{
- register int i, max;
+ int i, max;
for (i = 0; i < RANKS; ++i)
if (userasked[i] &&
/* NOTREACHED */
}
+int
drawcard(player, hand)
int player;
int *hand;
return(card);
}
+int
gofish(askedfor, player, hand)
int askedfor, player;
int *hand;
return(0);
}
+void
goodmove(player, move, hand, opphand)
int player, move;
int *hand, *opphand;
(void)printf("get another guess!\n");
}
+void
chkwinner(player, hand)
int player;
- register int *hand;
+ int *hand;
{
- register int cb, i, ub;
+ int cb, i, ub;
for (i = 0; i < RANKS; ++i)
if (hand[i] > 0 && hand[i] < CARDS)
exit(0);
}
+void
printplayer(player)
int player;
{
}
}
+void
printhand(hand)
int *hand;
{
- register int book, i, j;
+ int book, i, j;
for (book = i = 0; i < RANKS; i++)
if (hand[i] < CARDS)
(void)putchar('\n');
}
+int
countcards(hand)
- register int *hand;
+ int *hand;
{
- register int i, count;
+ int i, count;
for (count = i = 0; i < RANKS; i++)
count += *hand++;
return(count);
}
+int
countbooks(hand)
int *hand;
{
return(count);
}
+void
init()
{
- register int i, rank;
+ int i, rank;
for (i = 0; i < RANKS; ++i)
deck[i] = CARDS;
}
}
+int
nrandom(n)
int n;
{
- long random();
return((int)random() % n);
}
+void
instructions()
{
int input;
- char buf[1024];
+ pid_t pid;
+ int status;
(void)printf("Would you like instructions (y or n)? ");
input = getchar();
if (input != 'y')
return;
- (void)sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR);
- (void)system(buf);
+ 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);
+ /*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');
}
+void
usage()
{
(void)fprintf(stderr, "usage: fish [-p]\n");