xref: /netbsd-src/games/phantasia/setup.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: setup.c,v 1.16 2007/12/15 19:44:42 perry Exp $	*/
2 
3 /*
4  * setup.c - set up all files for Phantasia
5  */
6 #include <sys/cdefs.h>
7 #include <sys/param.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include "include.h"
11 
12 int main(int, char *[]);
13 void Error(const char *, const char *) __dead;
14 double drandom(void);
15 
16 /**/
17 /************************************************************************
18 /
19 / FUNCTION NAME: main()
20 /
21 / FUNCTION: setup files for Phantasia 3.3.2
22 /
23 / AUTHOR: E. A. Estes, 12/4/85
24 /
25 / ARGUMENTS: none
26 /
27 / RETURN VALUE: none
28 /
29 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
30 /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
31 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
32 /
33 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
34 /
35 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
36 /
37 / DESCRIPTION:
38 /
39 /	This program tries to verify the parameters specified in
40 /	the Makefile.
41 /
42 /	Create all necessary files.  Note that nothing needs to be
43 /	put in these files.
44 /	Also, the monster binary data base is created here.
45 /
46 / ************************************************************************/
47 
48 static const char *const files[] = {		/* all files to create */
49 	_PATH_MONST,
50 	_PATH_PEOPLE,
51 	_PATH_MESS,
52 	_PATH_LASTDEAD,
53 	_PATH_MOTD,
54 	_PATH_GOLD,
55 	_PATH_VOID,
56 	_PATH_SCORE,
57 	NULL,
58 };
59 
60 const char *monsterfile = "monsters.asc";
61 
62 int
63 main(argc, argv)
64 	int argc;
65 	char *argv[];
66 {
67 	const char *const *filename; /* for pointing to file names */
68 	int		fd;		/* file descriptor */
69 	FILE		*fp;			/* for opening files */
70 	struct stat	fbuf;		/* for getting files statistics */
71 	int ch;
72 	char *path;
73 
74 	while ((ch = getopt(argc, argv, "m:")) != -1)
75 		switch(ch) {
76 		case 'm':
77 			monsterfile = optarg;
78 			break;
79 		case '?':
80 		default:
81 			break;
82 		}
83 	argc -= optind;
84 	argv += optind;
85 
86     srandom((unsigned) time(NULL));	/* prime random numbers */
87 
88     umask(0117);		/* only owner can read/write created files */
89 
90     /* try to create data files */
91     filename = &files[0];
92     while (*filename != NULL)
93 	/* create each file */
94 	{
95 	path = strrchr(*filename, '/') + 1;
96 	if (stat(path, &fbuf) == 0)
97 	    /* file exists; remove it */
98 	    {
99 	    if (unlink(path) < 0)
100 		Error("Cannot unlink %s.\n", path);
101 		/*NOTREACHED*/
102 	    }
103 
104 	if ((fd = creat(path, 0660)) < 0)
105 	    Error("Cannot create %s.\n", path);
106 	    /*NOTREACHED*/
107 
108 	close(fd);			/* close newly created file */
109 
110 	++filename;			/* process next file */
111 	}
112 
113     /* Initialize an empty file placeholder for the grail location. */
114     if ((fp = fopen(path, "w")) == NULL)
115 	Error("Cannot create %s.\n", path);
116     fclose(fp);
117 
118     /* create binary monster data base */
119     path = strrchr(_PATH_MONST, '/') + 1;
120     if ((Monstfp = fopen(path, "w")) == NULL)
121 	Error("Cannot update %s.\n", path);
122     else
123 	{
124 	if ((fp = fopen(monsterfile, "r")) == NULL)
125 	    {
126 	    fclose(Monstfp);
127 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
128 	    }
129 	else
130 	    {
131 	    Curmonster.m_o_strength =
132 	    Curmonster.m_o_speed =
133 	    Curmonster.m_maxspeed =
134 	    Curmonster.m_o_energy =
135 	    Curmonster.m_melee =
136 	    Curmonster.m_skirmish = 0.0;
137 
138 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
139 		/* read in text file, convert to binary */
140 		{
141 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
142 		    &Curmonster.m_strength, &Curmonster.m_brains,
143 		    &Curmonster.m_speed, &Curmonster.m_energy,
144 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
145 		    &Curmonster.m_type, &Curmonster.m_flock);
146 		Databuf[24] = '\0';
147 		strcpy(Curmonster.m_name, Databuf);
148 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
149 		}
150 	    fclose(fp);
151 	    fflush(Monstfp);
152 	    if (ferror(Monstfp))
153 		Error("Writing %s.\n", path);
154 	    fclose(Monstfp);
155 	    }
156 	}
157 
158 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
159     /* write to motd file */
160     printf("One line 'motd' ? ");
161     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
162 	Databuf[0] = '\0';
163     path = strrchr(_PATH_MOTD, '/') + 1;
164     if ((fp = fopen(path, "w")) == NULL)
165 	Error("Cannot update %s.\n", path);
166     else
167 	{
168 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
169 	fclose(fp);
170 	}
171 
172     /* report compile-time options */
173     printf("Compiled options:\n\n");
174     printf("Phantasia destination directory:  %s\n", _PATH_PHANTDIR);
175     printf("Wizard: root UID: 0\n");
176 
177 #ifdef BSD41
178     printf("Compiled for BSD 4.1\n");
179 #endif
180 
181 #ifdef BSD42
182     printf("Compiled for BSD 4.2\n");
183 #endif
184 
185 #ifdef SYS3
186     printf("Compiled for System III\n");
187 #endif
188 
189 #ifdef SYS5
190     printf("Compiled for System V\n");
191 #endif
192 #endif
193 
194     exit(0);
195     /*NOTREACHED*/
196 }
197 /**/
198 /************************************************************************
199 /
200 / FUNCTION NAME: Error()
201 /
202 / FUNCTION: print an error message, and exit
203 /
204 / AUTHOR: E. A. Estes, 12/4/85
205 /
206 / ARGUMENTS:
207 /	char *str - format string for printf()
208 /	char *file - file which caused error
209 /
210 / RETURN VALUE: none
211 /
212 / MODULES CALLED: exit(), perror(), fprintf()
213 /
214 / GLOBAL INPUTS: _iob[]
215 /
216 / GLOBAL OUTPUTS: none
217 /
218 / DESCRIPTION:
219 /	Print an error message, then exit.
220 /
221 / ************************************************************************/
222 
223 void
224 Error(str, file)
225 	const char	*str, *file;
226 {
227     fprintf(stderr, "Error: ");
228     fprintf(stderr, str, file);
229     perror(file);
230     exit(1);
231     /*NOTREACHED*/
232 }
233 /**/
234 /************************************************************************
235 /
236 / FUNCTION NAME: drandom()
237 /
238 / FUNCTION: return a random number
239 /
240 / AUTHOR: E. A. Estes, 2/7/86
241 /
242 / ARGUMENTS: none
243 /
244 / RETURN VALUE: none
245 /
246 / MODULES CALLED: random()
247 /
248 / GLOBAL INPUTS: none
249 /
250 / GLOBAL OUTPUTS: none
251 /
252 / DESCRIPTION:
253 /
254 / ************************************************************************/
255 
256 double
257 drandom()
258 {
259     if (sizeof(int) != 2)
260 	return((double) (random() & 0x7fff) / 32768.0);
261     else
262 	return((double) random() / 32768.0);
263 }
264