]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - monop/cards.c
Sanity fixes for input buffer handling. From OpenBSD
[bsdgames-darwin.git] / monop / cards.c
index 5f1adabc57aa2b52ab9ddcfa80c9c3b1bdc8d557..ca27b4634ad4b8c7894f98109ccc32d95c2a2a7d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: cards.c,v 1.6 1999/08/21 09:23:44 simonb Exp $ */
+/*     $NetBSD: cards.c,v 1.20 2008/02/23 19:54:06 dholland 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.
  *
 #if 0
 static char sccsid[] = "@(#)cards.c    8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: cards.c,v 1.6 1999/08/21 09:23:44 simonb Exp $");
+__RCSID("$NetBSD: cards.c,v 1.20 2008/02/23 19:54:06 dholland Exp $");
 #endif
 #endif /* not lint */
 
-# include      <sys/types.h>
-# include      <sys/endian.h>
-# include      "monop.ext"
-# include      "pathnames.h"
+#include <sys/types.h>
+#include <sys/endian.h>
+#include "monop.ext"
+#include "pathnames.h"
 
 /*
  *     These routine deal with the card decks
  */
 
-# define       GOJF    'F'     /* char for get-out-of-jail-free cards  */
+#define        GOJF    'F'     /* char for get-out-of-jail-free cards  */
 
-# ifndef DEV
-static char    *cardfile       = _PATH_CARDS;
-# else
-static char    *cardfile       = "cards.pck";
-# endif
+#ifndef DEV
+static const char      *cardfile       = _PATH_CARDS;
+#else
+static const char      *cardfile       = "cards.pck";
+#endif
 
 static FILE    *deckf;
 
-static void set_up __P((DECK *));
-static void printmes __P((void));
+static void set_up(DECK *);
+static void printmes(void);
 
 /*
  *     This routine initializes the decks from the data file,
@@ -73,10 +69,9 @@ init_decks()
 {
        int32_t nc;
 
-       if ((deckf=fopen(cardfile, "r")) == NULL) {
+       if ((deckf = fopen(cardfile, "r")) == NULL) {
 file_err:
-               perror(cardfile);
-               exit(1);
+               err(1, "%s", cardfile);
        }
 
        /* read number of community chest cards... */
@@ -90,26 +85,28 @@ file_err:
        set_up(&CC_D);
        set_up(&CH_D);
 }
+
 /*
  *     This routine sets up the offset pointers for the given deck.
  */
 static void
 set_up(dp)
-DECK   *dp; {
-
-       int     r1, r2;
-       int     i;
+       DECK *dp;
+{
+       int r1, r2;
+       int i;
 
-       dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
+       dp->offsets = (off_t *) calloc(dp->num_cards, sizeof (off_t));
+       if (dp->offsets == NULL)
+               errx(1, "out of memory");
        if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
-           dp->num_cards) {
-               perror(cardfile);
-               exit(1);
+           (unsigned) dp->num_cards) {
+               err(1, "%s", cardfile);
        }
        /* convert offsets from big-endian byte order */
        for (i = 0; i < dp->num_cards; i++)
                BE64TOH(dp->offsets[i]);
-       dp->last_card = 0;
+       dp->top_card = 0;
        dp->gojf_used = FALSE;
        for (i = 0; i < dp->num_cards; i++) {
                off_t   temp;
@@ -121,26 +118,26 @@ DECK      *dp; {
                dp->offsets[r1] = temp;
        }
 }
+
 /*
  *     This routine draws a card from the given deck
  */
 void
 get_card(dp)
-DECK   *dp;
+       DECK *dp;
 {
-
-       char    type_maj, type_min;
-       int             num;
-       int             i, per_h, per_H, num_h, num_H;
-       OWN             *op;
+       char type_maj, type_min;
+       int num;
+       int i, per_h, per_H, num_h, num_H;
+       OWN *op;
 
        do {
-               fseek(deckf, dp->offsets[dp->last_card], 0);
-               dp->last_card = ++(dp->last_card) % dp->num_cards;
+               fseek(deckf, dp->offsets[dp->top_card], SEEK_SET);
+               dp->top_card = ++(dp->top_card) % dp->num_cards;
                type_maj = getc(deckf);
        } while (dp->gojf_used && type_maj == GOJF);
        type_min = getc(deckf);
-       num = getw(deckf);
+       num = ntohl(getw(deckf));
        printmes();
        switch (type_maj) {
          case '+':             /* get money            */
@@ -209,7 +206,9 @@ DECK        *dp;
                                        num_h += op->sqr->desc->houses;
                        }
                num = per_h * num_h + per_H * num_H;
-               printf("You had %d Houses and %d Hotels, so that cost you $%d\n", num_h, num_H, num);
+               printf(
+                   "You had %d Houses and %d Hotels, so that cost you $%d\n",
+                   num_h, num_H, num);
                if (num == 0)
                        lucky("");
                else
@@ -227,9 +226,9 @@ DECK        *dp;
  *     This routine prints out the message on the card
  */
 static void
-printmes() {
-
-       char    c;
+printmes()
+{
+       char c;
 
        printline();
        fflush(stdout);
@@ -238,3 +237,51 @@ printmes() {
        printline();
        fflush(stdout);
 }
+
+/*
+ *     This routine returns the players get-out-of-jail-free card
+ * to the bottom of a deck.  XXX currently does not return to the correct
+ * deck.
+ */
+void
+ret_card(plr)
+       PLAY *plr;
+{
+       char type_maj;
+       int gojfpos, last_card;
+       int i;
+       DECK *dp;
+       off_t temp;
+
+       plr->num_gojf--;
+       if (CC_D.gojf_used)
+               dp = &CC_D;
+       else
+               dp = &CH_D;
+       dp->gojf_used = FALSE;
+
+       /* Put at bottom of deck (top_card - 1) and remove it from wherever else
+        * it used to be.
+        */
+       last_card = dp->top_card - 1;
+       if (last_card < 0)
+               last_card += dp->num_cards;
+       gojfpos = dp->top_card;
+       do {
+               gojfpos = (gojfpos + 1) % dp->num_cards;
+               fseek(deckf, dp->offsets[gojfpos], SEEK_SET);
+               type_maj = getc(deckf);
+       } while (type_maj != GOJF);
+       temp = dp->offsets[gojfpos];
+       /* Only one of the next two loops does anything */
+       for (i = gojfpos - 1; i > last_card; i--)
+               dp->offsets[i + 1] = dp->offsets[i];
+       for (i = gojfpos; i < last_card; i++)
+               dp->offsets[i] = dp->offsets[i + 1];
+       if (gojfpos > last_card) {
+               dp->offsets[dp->top_card] = temp;
+               dp->top_card++;
+               dp->top_card %= dp->num_cards;
+       } else
+               dp->offsets[last_card] = temp;
+}