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