From b833779cf25d4a06fd8f7fd2f40a31d243536d2d Mon Sep 17 00:00:00 2001 From: Thorsten Lockert Date: Sun, 6 Oct 1996 03:20:47 +0000 Subject: Store the card deck using fixed sizes and network byte order --- games/monop/cards.c | 34 ++++++++++++++++++++++++------- games/monop/deck.h | 8 ++++---- games/monop/initdeck.c | 55 +++++++++++++++++++++++++++++++++++++++----------- games/monop/monop.h | 2 +- 4 files changed, 75 insertions(+), 24 deletions(-) (limited to 'games') diff --git a/games/monop/cards.c b/games/monop/cards.c index b65f0f268ca..e29b8001bcd 100644 --- a/games/monop/cards.c +++ b/games/monop/cards.c @@ -69,8 +69,24 @@ file_err: perror(cardfile); exit(1); } - if (fread(deck, sizeof (DECK), 2, deckf) != 2) + if (fread(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, deckf) != 1) goto file_err; + if (fread(&deck[0].last_card, sizeof(deck[0].last_card), 1, deckf) != 1) + goto file_err; + if (fread(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, deckf) != 1) + goto file_err; + deck[0].num_cards = ntohs(deck[0].num_cards); + deck[0].last_card = ntohs(deck[0].last_card); + + if (fread(&deck[1].num_cards, sizeof(deck[1].num_cards), 1, deckf) != 1) + goto file_err; + if (fread(&deck[1].last_card, sizeof(deck[1].last_card), 1, deckf) != 1) + goto file_err; + if (fread(&deck[1].gojf_used, sizeof(deck[1].gojf_used), 1, deckf) != 1) + goto file_err; + deck[1].num_cards = ntohs(deck[1].num_cards); + deck[1].last_card = ntohs(deck[1].last_card); + set_up(&CC_D); set_up(&CH_D); } @@ -83,10 +99,13 @@ DECK *dp; { reg 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) { - perror(cardfile); - exit(1); + dp->offsets = (int32_t *) calloc(sizeof (int32_t), dp->num_cards); + for (i = 0 ; i < dp->num_cards ; i++) { + if (fread(&dp->offsets[i], sizeof(dp->offsets[i]), 1, deckf) != 1) { + perror(cardfile); + exit(1); + } + dp->offsets[i] = ntohl(dp->offsets[i]); } dp->last_card = 0; dp->gojf_used = FALSE; @@ -107,7 +126,7 @@ get_card(dp) DECK *dp; { reg char type_maj, type_min; - reg int num; + int16_t num; int i, per_h, per_H, num_h, num_H; OWN *op; @@ -117,7 +136,8 @@ DECK *dp; { type_maj = getc(deckf); } while (dp->gojf_used && type_maj == GOJF); type_min = getc(deckf); - num = getw(deckf); + fread(&num, sizeof(num), 1, deckf); + num = ntohs(num); printmes(); switch (type_maj) { case '+': /* get money */ diff --git a/games/monop/deck.h b/games/monop/deck.h index 5516623b8d6..47a3c6ff916 100644 --- a/games/monop/deck.h +++ b/games/monop/deck.h @@ -35,16 +35,16 @@ * @(#)deck.h 8.1 (Berkeley) 5/31/93 */ -# define bool char +# define bool int8_t # define CC_D deck[0] # define CH_D deck[1] struct dk_st { /* deck description structure */ - int num_cards; /* number of cards in deck */ - int last_card; /* number of last card picked */ + int16_t num_cards; /* number of cards in deck */ + int16_t last_card; /* number of last card picked */ bool gojf_used; /* set if gojf card out of deck */ - long *offsets; /* offests for start of cards */ + int32_t *offsets; /* offests for start of cards */ }; typedef struct dk_st DECK; diff --git a/games/monop/initdeck.c b/games/monop/initdeck.c index c2ff831052c..bef96552c12 100644 --- a/games/monop/initdeck.c +++ b/games/monop/initdeck.c @@ -64,7 +64,7 @@ static char rcsid[] = "$NetBSD: initdeck.c,v 1.3 1995/03/23 08:34:43 cgd Exp $"; # define TRUE 1 # define FALSE 0 -# define bool char +# define bool int8_t # define reg register char *infile = "cards.inp", /* input file */ @@ -80,6 +80,7 @@ FILE *inf, *outf; main(ac, av) int ac; char *av[]; { + int n; getargs(ac, av); if ((inf = fopen(infile, "r")) == NULL) { @@ -90,24 +91,52 @@ 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 = (int32_t *)calloc(CC_D.num_cards + 1, sizeof (int32_t)); + CH_D.offsets = (int32_t *)calloc(CH_D.num_cards + 1, sizeof (int32_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); + fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf); + fwrite(&deck[0].last_card, sizeof(deck[0].last_card), 1, outf); + fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf); + + fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf); + fwrite(&deck[0].last_card, sizeof(deck[0].last_card), 1, outf); + fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf); + + fwrite(CC_D.offsets, sizeof(CC_D.offsets[0]), CC_D.num_cards, outf); + fwrite(CH_D.offsets, sizeof(CH_D.offsets[0]), CH_D.num_cards, outf); 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); + + deck[0].num_cards = htons(deck[0].num_cards); + fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf); + deck[0].last_card = htons(deck[0].last_card); + fwrite(&deck[0].last_card, sizeof(deck[0].last_card), 1, outf); + fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf); + deck[0].num_cards = ntohs(deck[0].num_cards); + + deck[1].num_cards = htons(deck[1].num_cards); + fwrite(&deck[1].num_cards, sizeof(deck[1].num_cards), 1, outf); + deck[1].last_card = htons(deck[1].last_card); + fwrite(&deck[1].last_card, sizeof(deck[1].last_card), 1, outf); + fwrite(&deck[1].gojf_used, sizeof(deck[1].gojf_used), 1, outf); + deck[1].num_cards = ntohs(deck[1].num_cards); + + for (n = 0 ; n < CC_D.num_cards ; n++) { + CC_D.offsets[n] = htonl(CC_D.offsets[n]); + fwrite(&CC_D.offsets[n], sizeof(CC_D.offsets[n]), 1, outf); + } + for (n = 0 ; n < CH_D.num_cards ; n++) { + CH_D.offsets[n] = htonl(CH_D.offsets[n]); + fwrite(&CH_D.offsets[n], sizeof(CH_D.offsets[n]), 1, outf); + } + fclose(outf); printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards); exit(0); @@ -153,7 +182,7 @@ putem() { reg bool newline; reg DECK *in_deck; reg char c; - reg int num; + int16_t num; in_deck = &CC_D; CC_D.num_cards = 1; @@ -163,7 +192,8 @@ putem() { putc(getc(inf), outf); for (num = 0; (c=getc(inf)) != '\n'; ) num = num * 10 + (c - '0'); - putw(num, outf); + num = htons(num); + fwrite(&num, sizeof(num), 1, outf); newline = FALSE; while ((c=getc(inf)) != EOF) if (newline && c == '%') { @@ -180,7 +210,8 @@ putem() { putc(c = getc(inf), outf); for (num = 0; (c=getc(inf)) != EOF && c != '\n'; ) num = num * 10 + (c - '0'); - putw(num, outf); + num = htons(num); + fwrite(&num, sizeof(num), 1, outf); } else { putc(c, outf); diff --git a/games/monop/monop.h b/games/monop/monop.h index 2bc82dab8c6..0984c2ee94b 100644 --- a/games/monop/monop.h +++ b/games/monop/monop.h @@ -41,7 +41,7 @@ # define reg register # define shrt char -# define bool char +# define bool int8_t # define unsgn unsigned # define TRUE (1) -- cgit v1.2.3