xref: /openbsd/games/phantasia/setup.c (revision d415bd75)
1 /*	$OpenBSD: setup.c,v 1.20 2019/06/28 13:32:52 deraadt Exp $	*/
2 /*	$NetBSD: setup.c,v 1.4 1995/04/24 12:24:41 cgd Exp $	*/
3 
4 /*
5  * setup.c - set up all files for Phantasia
6  */
7 #include <sys/stat.h>
8 
9 #include <fcntl.h>
10 #include <math.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 #include "macros.h"
18 #include "pathnames.h"
19 #include "phantdefs.h"
20 #include "phantglobs.h"
21 
22 __dead void Error(char *, char *);
23 
24 /**/
25 /************************************************************************
26 /
27 / FUNCTION NAME: main()
28 /
29 / FUNCTION: setup files for Phantasia 3.3.2
30 /
31 / AUTHOR: E. A. Estes, 12/4/85
32 /
33 / ARGUMENTS: none
34 /
35 / RETURN VALUE: none
36 /
37 / MODULES CALLED: exit(), stat(), Error(), open(), close(), fopen(),
38 /	fgets(), floor(), umask(), strlcpy(),
39 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
40 /
41 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
42 /
43 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
44 /
45 / DESCRIPTION:
46 /
47 /	This program tries to verify the parameters specified in
48 /	the Makefile.
49 /
50 /	Create all necessary files.  Note that nothing needs to be
51 /	put in these files.
52 /	Also, the monster binary data base is created here.
53 /
54 *************************************************************************/
55 
56 static char *files[] = {		/* all files to create */
57 	_PATH_MONST,
58 	_PATH_PEOPLE,
59 	_PATH_MESS,
60 	_PATH_LASTDEAD,
61 	_PATH_MOTD,
62 	_PATH_GOLD,
63 	_PATH_VOID,
64 	_PATH_SCORE,
65 	NULL,
66 };
67 
68 char *monsterfile="monsters.asc";
69 
70 int
71 main(int argc, char *argv[])
72 {
73 	char	**filename;	/* for pointing to file names */
74 	int	fd;		/* file descriptor */
75 	FILE	*fp;			/* for opening files */
76 	struct stat	fbuf;		/* for getting files statistics */
77 	int ch;
78 	char path[PATH_MAX], *prefix;
79 
80 	while ((ch = getopt(argc, argv, "hm:")) != -1)
81 		switch(ch) {
82 		case 'm':
83 			monsterfile = optarg;
84 			break;
85 		case 'h':
86 		default:
87 			break;
88 		}
89 	argc -= optind;
90 	argv += optind;
91 
92     umask(0117);		/* only owner can read/write created files */
93 
94     prefix = getenv("DESTDIR");
95 
96     /* try to create data files */
97     filename = &files[0];
98     while (*filename != NULL)
99 	/* create each file */
100 	{
101 	snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", *filename);
102 	if (stat(path, &fbuf) == 0)
103 	    /* file exists; remove it */
104 	    {
105 	    if (!strcmp(*filename, _PATH_PEOPLE))
106 		/* do not reset character file if it already exists */
107 		{
108 		++filename;
109 		continue;
110 		}
111 
112 	    if (!strcmp(*filename, _PATH_SCORE))
113 		/* do not reset score file if it already exists */
114 		{
115 		++filename;
116 		continue;
117 		}
118 
119 	    if (unlink(path) == -1)
120 		Error("Cannot unlink %s.\n", path);
121 	    }
122 
123 	if ((fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0660)) == -1)
124 	    Error("Cannot create %s.\n", path);
125 
126 	close(fd);			/* close newly created file */
127 
128 	++filename;			/* process next file */
129 	}
130 
131     /* put holy grail info into energy void file */
132     Enrgyvoid.ev_active = true;
133     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
134     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
135     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_VOID);
136     if ((fp = fopen(path, "w")) == NULL)
137 	Error("Cannot update %s.\n", path);
138     else
139 	{
140 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
141 	fclose(fp);
142 	}
143 
144     /* create binary monster data base */
145     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MONST);
146     if ((Monstfp = fopen(path, "w")) == NULL)
147 	Error("Cannot update %s.\n", path);
148     else
149 	{
150 	if ((fp = fopen(monsterfile, "r")) == NULL)
151 	    {
152 	    fclose(Monstfp);
153 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
154 	    }
155 	else
156 	    {
157 	    Curmonster.m_o_strength =
158 	    Curmonster.m_o_speed =
159 	    Curmonster.m_maxspeed =
160 	    Curmonster.m_o_energy =
161 	    Curmonster.m_melee =
162 	    Curmonster.m_skirmish = 0.0;
163 
164 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
165 		/* read in text file, convert to binary */
166 		{
167 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
168 		    &Curmonster.m_strength, &Curmonster.m_brains,
169 		    &Curmonster.m_speed, &Curmonster.m_energy,
170 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
171 		    &Curmonster.m_type, &Curmonster.m_flock);
172 		Databuf[24] = '\0';
173 		strlcpy(Curmonster.m_name, Databuf, sizeof Curmonster.m_name);
174 		fwrite(&Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
175 		}
176 	    fclose(fp);
177 	    fclose(Monstfp);
178 	    }
179 	}
180 
181     return 0;
182 }
183 /**/
184 /************************************************************************
185 /
186 / FUNCTION NAME: Error()
187 /
188 / FUNCTION: print an error message, and exit
189 /
190 / AUTHOR: E. A. Estes, 12/4/85
191 /
192 / ARGUMENTS:
193 /	char *str - format string for printf()
194 /	char *file - file which caused error
195 /
196 / RETURN VALUE: none
197 /
198 / MODULES CALLED: exit(), perror(), fprintf()
199 /
200 / GLOBAL INPUTS: _iob[]
201 /
202 / GLOBAL OUTPUTS: none
203 /
204 / DESCRIPTION:
205 /	Print an error message, then exit.
206 /
207 *************************************************************************/
208 
209 void
210 Error(char *str, char *file)
211 {
212 	fprintf(stderr, "Error: ");
213 	fprintf(stderr, str, file);
214 	perror(file);
215 	exit(1);
216 }
217 /**/
218 /************************************************************************
219 /
220 / FUNCTION NAME: drandom()
221 /
222 / FUNCTION: return a random number
223 /
224 / AUTHOR: E. A. Estes, 2/7/86
225 /
226 / ARGUMENTS: none
227 /
228 / RETURN VALUE: none
229 /
230 / MODULES CALLED: arc4random()
231 /
232 / GLOBAL INPUTS: none
233 /
234 / GLOBAL OUTPUTS: none
235 /
236 / DESCRIPTION:
237 /
238 *************************************************************************/
239 
240 double
241 drandom(void)
242 {
243 	return((double) arc4random() / (UINT32_MAX + 1.0));
244 }
245