summaryrefslogtreecommitdiffstats
path: root/monop
diff options
context:
space:
mode:
authorsimonb <simonb@NetBSD.org>1999-08-21 09:23:44 +0000
committersimonb <simonb@NetBSD.org>1999-08-21 09:23:44 +0000
commitca104a29f1236b5c4ee454e8c132ed8518df7a27 (patch)
tree314b1914aaf8b83354a3f37d0e390f6b752d955f /monop
parent9f5f92a0f3b2deed923c1d9c5613fe2dc55e605f (diff)
downloadbsdgames-darwin-ca104a29f1236b5c4ee454e8c132ed8518df7a27.tar.gz
bsdgames-darwin-ca104a29f1236b5c4ee454e8c132ed8518df7a27.tar.zst
bsdgames-darwin-ca104a29f1236b5c4ee454e8c132ed8518df7a27.zip
Instead of writing out a structure that contains pointers as the header
of the card decks file, just write out the number of cards for each deck. Also use "off_t" for offsets into the file (that are stored after the number of cards) instead of "long". /usr/share/games/cards.pck is now MI.
Diffstat (limited to 'monop')
-rw-r--r--monop/cards.c26
-rw-r--r--monop/deck.h6
-rw-r--r--monop/initdeck.c48
3 files changed, 62 insertions, 18 deletions
diff --git a/monop/cards.c b/monop/cards.c
index 18e5fa0c..5f1adabc 100644
--- a/monop/cards.c
+++ b/monop/cards.c
@@ -1,4 +1,4 @@
-/* $NetBSD: cards.c,v 1.5 1998/08/30 09:19:39 veego Exp $ */
+/* $NetBSD: cards.c,v 1.6 1999/08/21 09:23:44 simonb Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -38,10 +38,12 @@
#if 0
static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: cards.c,v 1.5 1998/08/30 09:19:39 veego Exp $");
+__RCSID("$NetBSD: cards.c,v 1.6 1999/08/21 09:23:44 simonb Exp $");
#endif
#endif /* not lint */
+# include <sys/types.h>
+# include <sys/endian.h>
# include "monop.ext"
# include "pathnames.h"
@@ -69,14 +71,22 @@ static void printmes __P((void));
void
init_decks()
{
+ int32_t nc;
if ((deckf=fopen(cardfile, "r")) == NULL) {
file_err:
perror(cardfile);
exit(1);
}
- if (fread(deck, sizeof (DECK), 2, deckf) != 2)
+
+ /* read number of community chest cards... */
+ if (fread(&nc, sizeof(nc), 1, deckf) != 1)
+ goto file_err;
+ CC_D.num_cards = be32toh(nc);
+ /* ... and number of community chest cards. */
+ if (fread(&nc, sizeof(nc), 1, deckf) != 1)
goto file_err;
+ CH_D.num_cards = be32toh(nc);
set_up(&CC_D);
set_up(&CH_D);
}
@@ -90,15 +100,19 @@ DECK *dp; {
int r1, r2;
int i;
- dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
- if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
+ dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
+ if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
+ dp->num_cards) {
perror(cardfile);
exit(1);
}
+ /* convert offsets from big-endian byte order */
+ for (i = 0; i < dp->num_cards; i++)
+ BE64TOH(dp->offsets[i]);
dp->last_card = 0;
dp->gojf_used = FALSE;
for (i = 0; i < dp->num_cards; i++) {
- long temp;
+ off_t temp;
r1 = roll(1, dp->num_cards) - 1;
r2 = roll(1, dp->num_cards) - 1;
diff --git a/monop/deck.h b/monop/deck.h
index 5516623b..618fc2a7 100644
--- a/monop/deck.h
+++ b/monop/deck.h
@@ -1,4 +1,4 @@
-/* $NetBSD: deck.h,v 1.3 1995/03/23 08:34:36 cgd Exp $ */
+/* $NetBSD: deck.h,v 1.4 1999/08/21 09:23:44 simonb Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -35,6 +35,8 @@
* @(#)deck.h 8.1 (Berkeley) 5/31/93
*/
+#include <sys/types.h>
+
# define bool char
# define CC_D deck[0]
@@ -44,7 +46,7 @@ struct dk_st { /* deck description structure */
int num_cards; /* number of cards in deck */
int last_card; /* number of last card picked */
bool gojf_used; /* set if gojf card out of deck */
- long *offsets; /* offests for start of cards */
+ off_t *offsets; /* offsets for start of cards */
};
typedef struct dk_st DECK;
diff --git a/monop/initdeck.c b/monop/initdeck.c
index f1e36c4c..d066ed6d 100644
--- a/monop/initdeck.c
+++ b/monop/initdeck.c
@@ -1,4 +1,4 @@
-/* $NetBSD: initdeck.c,v 1.5 1997/10/12 17:45:12 christos Exp $ */
+/* $NetBSD: initdeck.c,v 1.6 1999/08/21 09:23:44 simonb Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,12 +43,14 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
#if 0
static char sccsid[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: initdeck.c,v 1.5 1997/10/12 17:45:12 christos Exp $");
+__RCSID("$NetBSD: initdeck.c,v 1.6 1999/08/21 09:23:44 simonb Exp $");
#endif
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/endian.h>
#include "deck.h"
/*
@@ -86,6 +88,9 @@ main(ac, av)
int ac;
char *av[]; {
+ int i;
+ int32_t nc;
+
getargs(ac, av);
if ((inf = fopen(infile, "r")) == NULL) {
perror(infile);
@@ -95,24 +100,47 @@ char *av[]; {
/*
* allocate space for pointers.
*/
- CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long));
- CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long));
+ CC_D.offsets = (off_t *)calloc(CC_D.num_cards + 1, sizeof (off_t));
+ CH_D.offsets = (off_t *)calloc(CH_D.num_cards + 1, sizeof (off_t));
fseek(inf, 0L, 0);
if ((outf = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(0);
}
- fwrite(deck, sizeof (DECK), 2, outf);
- fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
- fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
+ /*
+ * these fields will be overwritten after the offsets are calculated,
+ * so byte-order doesn't matter yet.
+ */
+ fwrite(&nc, sizeof(nc), 1, outf);
+ fwrite(&nc, sizeof(nc), 1, outf);
+ fwrite(CC_D.offsets, sizeof (off_t), CC_D.num_cards, outf);
+ fwrite(CH_D.offsets, sizeof (off_t), CH_D.num_cards, outf);
+
+ /*
+ * write out the cards themselves (calculating the offsets).
+ */
putem();
fclose(inf);
fseek(outf, 0, 0L);
- fwrite(deck, sizeof (DECK), 2, outf);
- fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
- fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
+
+ /* number of community chest cards first... */
+ nc = htobe32(CC_D.num_cards);
+ fwrite(&nc, sizeof(nc), 1, outf);
+ /* ... then number of chance cards. */
+ nc = htobe32(CH_D.num_cards);
+ fwrite(&nc, sizeof(nc), 1, outf);
+
+ /* convert offsets to big-endian byte order */
+ for (i = 0; i < CC_D.num_cards; i++)
+ HTOBE64(CC_D.offsets[i]);
+ for (i = 0; i < CH_D.num_cards; i++)
+ HTOBE64(CH_D.offsets[i]);
+ /* then dump the offsets out */
+ fwrite(CC_D.offsets, sizeof (off_t), CC_D.num_cards, outf);
+ fwrite(CH_D.offsets, sizeof (off_t), CH_D.num_cards, outf);
+
fclose(outf);
printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
exit(0);