]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - cribbage/crib.c
Replaced one instance of err() with errx(), as the value of errno might not
[bsdgames-darwin.git] / cribbage / crib.c
index 96bfe568e768df565ea6f1e570676a174c3d81a7..a60832ae081c99d854fe9dda525c0846e065649e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: crib.c,v 1.5 1995/03/21 15:08:42 cgd Exp $     */
+/*     $NetBSD: crib.c,v 1.20 2005/07/02 08:32:32 jmc Exp $    */
 
 /*-
  * Copyright (c) 1980, 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.
  *
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1980, 1993\n\
-       The Regents of the University of California.  All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n");
 #endif /* not lint */
 
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)crib.c     8.1 (Berkeley) 5/31/93";
 #else
-static char rcsid[] = "$NetBSD: crib.c,v 1.5 1995/03/21 15:08:42 cgd Exp $";
+__RCSID("$NetBSD: crib.c,v 1.20 2005/07/02 08:32:32 jmc Exp $");
 #endif
 #endif /* not lint */
 
 #include <curses.h>
+#include <err.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
@@ -59,15 +57,35 @@ static char rcsid[] = "$NetBSD: crib.c,v 1.5 1995/03/21 15:08:42 cgd Exp $";
 #include "pathnames.h"
 
 int
-main(argc, argv)
-       int argc;
-       char *argv[];
+main(int argc, char *argv[])
 {
        BOOLEAN playing;
        FILE *f;
        int ch;
+       int fd;
+       int flags;
 
-       while ((ch = getopt(argc, argv, "eqr")) != EOF)
+       f = fopen(_PATH_LOG, "a");
+       if (f == NULL)
+               warn("fopen %s", _PATH_LOG);
+       if (f != NULL && fileno(f) < 3)
+               exit(1);
+
+       /* Revoke setgid privileges */
+       setgid(getgid());
+
+       /* Set close-on-exec flag on log file */
+       if (f != NULL) {
+               fd = fileno(f);
+               flags = fcntl(fd, F_GETFD);
+               if (flags < 0)
+                       err(1, "fcntl F_GETFD");
+               flags |= FD_CLOEXEC;
+               if (fcntl(fd, F_SETFD, flags) == -1)
+                       err(1, "fcntl F_SETFD");
+       }
+
+       while ((ch = getopt(argc, argv, "eqr")) != -1)
                switch (ch) {
                case 'e':
                        explain = TRUE;
@@ -85,8 +103,8 @@ main(argc, argv)
                }
 
        initscr();
-       (void)signal(SIGINT, rint);
-       crmode();
+       (void)signal(SIGINT, receive_intr);
+       cbreak();
        noecho();
 
        Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
@@ -106,7 +124,7 @@ main(argc, argv)
                        mvcur(0, COLS - 1, LINES - 1, 0);
                        fflush(stdout);
                        instructions();
-                       crmode();
+                       cbreak();
                        noecho();
                        clear();
                        refresh();
@@ -126,17 +144,12 @@ main(argc, argv)
                playing = (getuchar() == 'Y');
        } while (playing);
 
-       if (f = fopen(_PATH_LOG, "a")) {
+       if (f != NULL) {
                (void)fprintf(f, "%s: won %5.5d, lost %5.5d\n",
                    getlogin(), cgames, pgames);
                (void) fclose(f);
        }
        bye();
-       if (!f) {
-               (void) fprintf(stderr, "\ncribbage: can't open %s.\n",
-                   _PATH_LOG);
-               exit(1);
-       }
        exit(0);
 }
 
@@ -145,7 +158,7 @@ main(argc, argv)
  *     Print out the initial board on the screen
  */
 void
-makeboard()
+makeboard(void)
 {
        mvaddstr(SCORE_Y + 0, SCORE_X,
            "+---------------------------------------+");
@@ -173,10 +186,8 @@ makeboard()
  *     Print out the current game score
  */
 void
-gamescore()
+gamescore(void)
 {
-       extern int Lastscore[];
-
        if (pgames || cgames) {
                mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
                mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
@@ -191,12 +202,13 @@ gamescore()
  *     player what card to turn.  We do a random one, anyway.
  */
 void
-game()
+game(void)
 {
-       register int i, j;
+       int i, j;
        BOOLEAN flag;
        BOOLEAN compcrib;
 
+       compcrib = FALSE;
        makedeck(deck);
        shuffle(deck);
        if (gamecount == 0) {
@@ -226,10 +238,13 @@ game()
                        } else
                                compcrib = (deck[i].rank > deck[j].rank);
                } while (flag);
+               do_wait();
                clear();
                makeboard();
                refresh();
        } else {
+               makeboard();
+               refresh();
                werase(Tablewin);
                wrefresh(Tablewin);
                werase(Compwin);
@@ -281,12 +296,14 @@ game()
  *     Do up one hand of the game
  */
 int
-playhand(mycrib)
-       BOOLEAN mycrib;
+playhand(BOOLEAN mycrib)
 {
-       register int deckpos;
+       int deckpos;
 
        werase(Compwin);
+       wrefresh(Compwin);
+       werase(Tablewin);
+       wrefresh(Tablewin);
 
        knownum = 0;
        deckpos = deal(mycrib);
@@ -310,10 +327,9 @@ playhand(mycrib)
  * deal cards to both players from deck
  */
 int
-deal(mycrib)
-       BOOLEAN mycrib;
+deal(BOOLEAN mycrib)
 {
-       register int i, j;
+       int i, j;
 
        for (i = j = 0; i < FULLHAND; i++) {
                if (mycrib) {
@@ -333,10 +349,9 @@ deal(mycrib)
  * Note: we call cdiscard() after prining first message so player doesn't wait
  */
 void
-discard(mycrib)
-       BOOLEAN mycrib;
+discard(BOOLEAN mycrib)
 {
-       register char *prompt;
+       const char *prompt;
        CARD crd;
 
        prcrib(mycrib, TRUE);
@@ -363,11 +378,9 @@ discard(mycrib)
  *     player what card to turn.  We do a random one, anyway.
  */
 int
-cut(mycrib, pos)
-       BOOLEAN mycrib;
-       int  pos;
+cut(BOOLEAN mycrib, int  pos)
 {
-       register int i;
+       int i;
        BOOLEAN win;
 
        win = FALSE;
@@ -407,10 +420,9 @@ cut(mycrib, pos)
  *     Print out the turnover card with crib indicator
  */
 void
-prcrib(mycrib, blank)
-       BOOLEAN mycrib, blank;
+prcrib(BOOLEAN mycrib, BOOLEAN blank)
 {
-       register int y, cardx;
+       int y, cardx;
 
        if (mycrib)
                cardx = CRIB_X;
@@ -427,6 +439,7 @@ prcrib(mycrib, blank)
 
        for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
                mvaddstr(y, cardx, "       ");
+       refresh();
 }
 
 /*
@@ -437,16 +450,16 @@ static CARD Table[14];
 static int Tcnt;
 
 int
-peg(mycrib)
-       BOOLEAN mycrib;
+peg(BOOLEAN mycrib)
 {
        static CARD ch[CINHAND], ph[CINHAND];
-       register int i, j, k;
-       register int l;
-       register int cnum, pnum, sum;
-       register BOOLEAN myturn, mego, ugo, last, played;
+       int i, j, k;
+       int l;
+       int cnum, pnum, sum;
+       BOOLEAN myturn, mego, ugo, last, played;
        CARD crd;
 
+       played = FALSE;
        cnum = pnum = CINHAND;
        for (i = 0; i < CINHAND; i++) { /* make copies of hands */
                ch[i] = chand[i];
@@ -473,6 +486,7 @@ peg(mycrib)
                                else {                  /* give him his point */
                                        msg(quiet ? "You get one" :
                                            "You get one point");
+                                       do_wait();
                                        if (chkscr(&pscore, 1))
                                                return TRUE;
                                        sum = 0;
@@ -550,6 +564,8 @@ peg(mycrib)
                                if (i > 0) {
                                        msg(quiet ? "You got %d" :
                                            "You got %d points", i);
+                                       if (pnum == 0)
+                                               do_wait();
                                        if (chkscr(&pscore, i))
                                                return TRUE;
                                }
@@ -570,7 +586,7 @@ peg(mycrib)
        prhand(ph, pnum, Playwin, FALSE);
        prhand(ch, cnum, Compwin, TRUE);
        prtable(sum);
-       if (last)
+       if (last) {
                if (played) {
                        msg(quiet ? "I get one for last" :
                            "I get one point for last");
@@ -580,9 +596,11 @@ peg(mycrib)
                } else {
                        msg(quiet ? "You get one for last" :
                            "You get one point for last");
+                       do_wait();
                        if (chkscr(&pscore, 1))
                                return TRUE;
                }
+       }
        return (FALSE);
 }
 
@@ -591,11 +609,10 @@ peg(mycrib)
  *     Print out the table with the current score
  */
 void
-prtable(score)
-       int score;
+prtable(int curscore)
 {
        prhand(Table, Tcnt, Tablewin, FALSE);
-       mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
+       mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", curscore);
        wrefresh(Tablewin);
 }
 
@@ -604,8 +621,7 @@ prtable(score)
  *     Handle the scoring of the hands
  */
 int
-score(mycrib)
-       BOOLEAN mycrib;
+score(BOOLEAN mycrib)
 {
        sorthand(crib, CINHAND);
        if (mycrib) {
@@ -616,6 +632,7 @@ score(mycrib)
                do_wait();
                if (comphand(crib, "crib"))
                        return (TRUE);
+               do_wait();
        } else {
                if (comphand(chand, "hand"))
                        return (TRUE);