1 /* $OpenBSD: initdeck.c,v 1.17 2016/01/08 18:20:33 mestre Exp $ */ 2 /* $NetBSD: initdeck.c,v 1.3 1995/03/23 08:34:43 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <err.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <unistd.h> 37 38 #include "deck.h" 39 40 /* 41 * This program initializes the card files for monopoly. 42 * It reads in a data file with Com. Chest cards, followed by 43 * the Chance card. The two are separated by a line of "%-". 44 * All other cards are separated by lines of "%%". In the front 45 * of the file is the data for the decks in the same order. 46 * This includes the seek pointer for the start of each card. 47 * All cards start with their execution code, followed by the 48 * string to print, terminated with a null byte. 49 */ 50 51 #define TRUE 1 52 #define FALSE 0 53 54 #define bool int8_t 55 56 char *infile = "cards.inp", /* input file */ 57 *outfile = "cards.pck"; /* "packed" file */ 58 59 DECK deck[2]; 60 61 FILE *inf, *outf; 62 63 static void getargs(int, char *[]); 64 static void count(void); 65 static void putem(void); 66 67 int 68 main(int ac, char *av[]) 69 { 70 int n; 71 72 if (pledge("stdio rpath wpath cpath", NULL) == -1) 73 err(1, "pledge"); 74 75 getargs(ac, av); 76 if ((inf = fopen(infile, "r")) == NULL) 77 err(1, "%s", infile); 78 count(); 79 /* 80 * allocate space for pointers. 81 */ 82 if ((CC_D.offsets = calloc(CC_D.num_cards + 1, 83 sizeof (int32_t))) == NULL || 84 (CH_D.offsets = calloc(CH_D.num_cards + 1, 85 sizeof (int32_t))) == NULL) 86 err(1, NULL); 87 fseek(inf, 0L, SEEK_SET); 88 if ((outf = fopen(outfile, "w")) == NULL) 89 err(1, "%s", outfile); 90 91 if (pledge("stdio", NULL) == -1) 92 err(1, "pledge"); 93 94 fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf); 95 fwrite(&deck[0].top_card, sizeof(deck[0].top_card), 1, outf); 96 fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf); 97 98 fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf); 99 fwrite(&deck[0].top_card, sizeof(deck[0].top_card), 1, outf); 100 fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf); 101 102 fwrite(CC_D.offsets, sizeof(CC_D.offsets[0]), CC_D.num_cards, outf); 103 fwrite(CH_D.offsets, sizeof(CH_D.offsets[0]), CH_D.num_cards, outf); 104 putem(); 105 106 fclose(inf); 107 fseek(outf, 0L, SEEK_SET); 108 109 deck[0].num_cards = htons(deck[0].num_cards); 110 fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf); 111 deck[0].top_card = htons(deck[0].top_card); 112 fwrite(&deck[0].top_card, sizeof(deck[0].top_card), 1, outf); 113 fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf); 114 deck[0].num_cards = ntohs(deck[0].num_cards); 115 116 deck[1].num_cards = htons(deck[1].num_cards); 117 fwrite(&deck[1].num_cards, sizeof(deck[1].num_cards), 1, outf); 118 deck[1].top_card = htons(deck[1].top_card); 119 fwrite(&deck[1].top_card, sizeof(deck[1].top_card), 1, outf); 120 fwrite(&deck[1].gojf_used, sizeof(deck[1].gojf_used), 1, outf); 121 deck[1].num_cards = ntohs(deck[1].num_cards); 122 123 for (n = 0 ; n < CC_D.num_cards ; n++) { 124 CC_D.offsets[n] = htonl(CC_D.offsets[n]); 125 fwrite(&CC_D.offsets[n], sizeof(CC_D.offsets[n]), 1, outf); 126 } 127 for (n = 0 ; n < CH_D.num_cards ; n++) { 128 CH_D.offsets[n] = htonl(CH_D.offsets[n]); 129 fwrite(&CH_D.offsets[n], sizeof(CH_D.offsets[n]), 1, outf); 130 } 131 132 fclose(outf); 133 printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, 134 CH_D.num_cards); 135 return 0; 136 } 137 138 static void 139 getargs(int ac, char *av[]) 140 { 141 if (ac > 1) 142 infile = av[1]; 143 if (ac > 2) 144 outfile = av[2]; 145 } 146 147 /* 148 * count the cards 149 */ 150 static void 151 count(void) 152 { 153 bool newline; 154 DECK *in_deck; 155 int c; 156 157 newline = TRUE; 158 in_deck = &CC_D; 159 while ((c=getc(inf)) != EOF) 160 if (newline && c == '%') { 161 newline = FALSE; 162 in_deck->num_cards++; 163 if (getc(inf) == '-') 164 in_deck = &CH_D; 165 } 166 else 167 newline = (c == '\n'); 168 in_deck->num_cards++; 169 } 170 /* 171 * put strings in the file 172 */ 173 static void 174 putem(void) 175 { 176 bool newline; 177 DECK *in_deck; 178 int c; 179 int16_t num; 180 181 in_deck = &CC_D; 182 CC_D.num_cards = 1; 183 CH_D.num_cards = 0; 184 CC_D.offsets[0] = ftell(outf); 185 putc(getc(inf), outf); 186 putc(getc(inf), outf); 187 for (num = 0; (c=getc(inf)) != '\n'; ) 188 num = num * 10 + (c - '0'); 189 num = htons(num); 190 fwrite(&num, sizeof(num), 1, outf); 191 newline = FALSE; 192 while ((c=getc(inf)) != EOF) 193 if (newline && c == '%') { 194 putc('\0', outf); 195 newline = FALSE; 196 if (getc(inf) == '-') 197 in_deck = &CH_D; 198 while (getc(inf) != '\n') 199 continue; 200 in_deck->offsets[in_deck->num_cards++] = ftell(outf); 201 if ((c=getc(inf)) == EOF) 202 break; 203 putc(c, outf); 204 putc(c = getc(inf), outf); 205 for (num = 0; (c=getc(inf)) != EOF && c != '\n'; ) 206 num = num * 10 + (c - '0'); 207 num = htons(num); 208 fwrite(&num, sizeof(num), 1, outf); 209 } 210 else { 211 putc(c, outf); 212 newline = (c == '\n'); 213 } 214 putc('\0', outf); 215 } 216