xref: /openbsd-src/games/adventure/setup.c (revision 17641e3181064a4b256bd1af9f64d90af0ac5a36)
1*17641e31Stb /*	$OpenBSD: setup.c,v 1.13 2016/01/07 16:00:31 tb Exp $	*/
2df930be7Sderaadt /*	$NetBSD: setup.c,v 1.2 1995/03/21 12:05:10 cgd Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1991, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
9df930be7Sderaadt  * Jim Gillogly at The Rand Corporation.
10df930be7Sderaadt  *
11df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt  * modification, are permitted provided that the following conditions
13df930be7Sderaadt  * are met:
14df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
197a09557bSmillert  * 3. Neither the name of the University nor the names of its contributors
20df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
21df930be7Sderaadt  *    without specific prior written permission.
22df930be7Sderaadt  *
23df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df930be7Sderaadt  * SUCH DAMAGE.
34df930be7Sderaadt  */
35df930be7Sderaadt 
36df930be7Sderaadt /*
37df930be7Sderaadt  * Setup: keep the structure of the original Adventure port, but use an
38df930be7Sderaadt  * internal copy of the data file, serving as a sort of virtual disk.  It's
39d03921ceStedu  * lightly obfuscated to prevent casual snooping of the executable.
40df930be7Sderaadt  *
41df930be7Sderaadt  * Also do appropriate things to tabs so that bogus editors will do the right
42df930be7Sderaadt  * thing with the data file.
43df930be7Sderaadt  *
44df930be7Sderaadt  */
45df930be7Sderaadt 
46df930be7Sderaadt #define SIG1 " *      Jim Gillogly"
47df930be7Sderaadt #define SIG2 " *      Sterday, 6 Thrimidge S.R. 1993, 15:24"
48df930be7Sderaadt 
49bda940d3Spjanzen #include <err.h>
50df930be7Sderaadt #include <stdio.h>
51bda940d3Spjanzen #include <stdlib.h>
52*17641e31Stb #include <unistd.h>
53df930be7Sderaadt 
54de4a7fbbSpjanzen #define USAGE "Usage: setup file > data.c (file is typically glorkz)\n"
55df930be7Sderaadt 
56df930be7Sderaadt #define YES 1
57df930be7Sderaadt #define NO  0
58df930be7Sderaadt 
59df930be7Sderaadt #define LINE 10		/* How many values do we get on a line? */
60df930be7Sderaadt 
61bda940d3Spjanzen int
main(int argc,char * argv[])62a5ca3416Sderaadt main(int argc, char *argv[])
63df930be7Sderaadt {
64df930be7Sderaadt 	FILE	*infile;
65df930be7Sderaadt 	int	c, count, linestart;
66df930be7Sderaadt 
67*17641e31Stb 	if (pledge("stdio rpath", NULL) == -1)
68*17641e31Stb 	    err(1, "pledge");
69*17641e31Stb 
70de4a7fbbSpjanzen 	if (argc != 2) {
71de4a7fbbSpjanzen 		fprintf(stderr, USAGE);
72*17641e31Stb 		return 1;
73de4a7fbbSpjanzen 	}
74df930be7Sderaadt 
75df930be7Sderaadt 	if ((infile = fopen(argv[1], "r")) == NULL)
762deb5d9fSpjanzen 		err(1, "Can't read file %s", argv[1]);
77*17641e31Stb 
78*17641e31Stb 	if (pledge("stdio", NULL) == -1)
79*17641e31Stb 		err(1, "pledge");
80*17641e31Stb 
81df930be7Sderaadt 	puts("/*\n * data.c: created by setup from the ascii data file.");
82df930be7Sderaadt 	puts(SIG1);
83df930be7Sderaadt 	puts(SIG2);
84df930be7Sderaadt 	puts(" */");
85df930be7Sderaadt 	printf("\n\nchar data_file[] =\n{");
86df930be7Sderaadt 	count = 0;
87df930be7Sderaadt 	linestart = YES;
88df930be7Sderaadt 
891cc65eecStedu 	srandom_deterministic(1);
901cc65eecStedu 
91bda940d3Spjanzen 	while ((c = getc(infile)) != EOF) {
923aa33c14Sderaadt 		if (count++ % LINE == 0)
933aa33c14Sderaadt 			printf("\n\t");
94bda940d3Spjanzen 		if (linestart && c == ' ') { /* Convert first spaces to tab */
952deb5d9fSpjanzen 			printf("0x%02x,", (unsigned int)('\t' ^ random()) & 0xFF);
96df930be7Sderaadt 			while ((c = getc(infile)) == ' ' && c != EOF);
97df930be7Sderaadt 			/* Drop the non-whitespace character through */
98df930be7Sderaadt 			linestart = NO;
99df930be7Sderaadt 		}
100bda940d3Spjanzen 		switch (c) {
101df930be7Sderaadt 		case '\t':
102df930be7Sderaadt 			linestart = NO; /* Don't need to convert spaces */
103df930be7Sderaadt 			break;
104df930be7Sderaadt 		case '\n':
105df930be7Sderaadt 			linestart = YES; /* Ready to convert spaces again */
106df930be7Sderaadt 			break;
107df930be7Sderaadt 		}
1083aa33c14Sderaadt 		if (count++ % LINE == 0)
109df930be7Sderaadt 			printf("\n\t");
1102deb5d9fSpjanzen 		printf("0x%02x,", (unsigned int)(c ^ random()) & 0xFF);
111df930be7Sderaadt 	}
112df930be7Sderaadt 	puts("\n\t0\n};");
113df930be7Sderaadt 	fclose(infile);
114*17641e31Stb 	return 0;
115df930be7Sderaadt }
116