setup.c revision 1.10 1 /* $NetBSD: setup.c,v 1.10 1999/09/19 18:14:52 jsm Exp $ */
2
3 /*
4 * setup.c - set up all files for Phantasia
5 */
6 #include <sys/param.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include "include.h"
10
11 int main __P((int, char *[]));
12 void Error __P((const char *, const char *)) __attribute__((__noreturn__));
13 double drandom __P((void));
14
15 /**/
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 register const char *const *filename; /* for pointing to file names */
68 register int fd; /* file descriptor */
69 FILE *fp; /* for opening files */
70 struct stat fbuf; /* for getting files statistics */
71 int ch;
72 char path[MAXPATHLEN], *prefix;
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 prefix = getenv("DESTDIR");
91
92 /* try to create data files */
93 filename = &files[0];
94 while (*filename != NULL)
95 /* create each file */
96 {
97 snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", *filename);
98 if (stat(path, &fbuf) == 0)
99 /* file exists; remove it */
100 {
101 if (!strcmp(*filename, _PATH_PEOPLE))
102 /* do not reset character file if it already exists */
103 {
104 ++filename;
105 continue;
106 }
107
108 if (!strcmp(*filename, _PATH_SCORE))
109 /* do not reset score file if it already exists */
110 {
111 ++filename;
112 continue;
113 }
114
115 if (unlink(path) < 0)
116 Error("Cannot unlink %s.\n", path);
117 /*NOTREACHED*/
118 }
119
120 if ((fd = creat(path, 0660)) < 0)
121 Error("Cannot create %s.\n", path);
122 /*NOTREACHED*/
123
124 close(fd); /* close newly created file */
125
126 ++filename; /* process next file */
127 }
128
129 /* put holy grail info into energy void file */
130 Enrgyvoid.ev_active = TRUE;
131 Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
132 Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
133 snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_VOID);
134 if ((fp = fopen(path, "w")) == NULL)
135 Error("Cannot update %s.\n", path);
136 else
137 {
138 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
139 fflush(fp);
140 if (ferror(fp))
141 Error("Writing %s.\n", path);
142 fclose(fp);
143 }
144
145 /* create binary monster data base */
146 snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MONST);
147 if ((Monstfp = fopen(path, "w")) == NULL)
148 Error("Cannot update %s.\n", path);
149 else
150 {
151 if ((fp = fopen(monsterfile, "r")) == NULL)
152 {
153 fclose(Monstfp);
154 Error("cannot open %s to create monster database.\n", "monsters.asc");
155 }
156 else
157 {
158 Curmonster.m_o_strength =
159 Curmonster.m_o_speed =
160 Curmonster.m_maxspeed =
161 Curmonster.m_o_energy =
162 Curmonster.m_melee =
163 Curmonster.m_skirmish = 0.0;
164
165 while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
166 /* read in text file, convert to binary */
167 {
168 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
169 &Curmonster.m_strength, &Curmonster.m_brains,
170 &Curmonster.m_speed, &Curmonster.m_energy,
171 &Curmonster.m_experience, &Curmonster.m_treasuretype,
172 &Curmonster.m_type, &Curmonster.m_flock);
173 Databuf[24] = '\0';
174 strcpy(Curmonster.m_name, Databuf);
175 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
176 }
177 fclose(fp);
178 fflush(Monstfp);
179 if (ferror(Monstfp))
180 Error("Writing %s.\n", path);
181 fclose(Monstfp);
182 }
183 }
184
185 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
186 /* write to motd file */
187 printf("One line 'motd' ? ");
188 if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
189 Databuf[0] = '\0';
190 snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MOTD);
191 if ((fp = fopen(path, "w")) == NULL)
192 Error("Cannot update %s.\n", path);
193 else
194 {
195 fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
196 fclose(fp);
197 }
198
199 /* report compile-time options */
200 printf("Compiled options:\n\n");
201 printf("Phantasia destination directory: %s\n", _PATH_PHANTDIR);
202 printf("Wizard: root UID: 0\n");
203
204 #ifdef BSD41
205 printf("Compiled for BSD 4.1\n");
206 #endif
207
208 #ifdef BSD42
209 printf("Compiled for BSD 4.2\n");
210 #endif
211
212 #ifdef SYS3
213 printf("Compiled for System III\n");
214 #endif
215
216 #ifdef SYS5
217 printf("Compiled for System V\n");
218 #endif
219 #endif
220
221 exit(0);
222 /*NOTREACHED*/
223 }
224 /**/
226 /************************************************************************
227 /
228 / FUNCTION NAME: Error()
229 /
230 / FUNCTION: print an error message, and exit
231 /
232 / AUTHOR: E. A. Estes, 12/4/85
233 /
234 / ARGUMENTS:
235 / char *str - format string for printf()
236 / char *file - file which caused error
237 /
238 / RETURN VALUE: none
239 /
240 / MODULES CALLED: exit(), perror(), fprintf()
241 /
242 / GLOBAL INPUTS: _iob[]
243 /
244 / GLOBAL OUTPUTS: none
245 /
246 / DESCRIPTION:
247 / Print an error message, then exit.
248 /
249 / ************************************************************************/
250
251 void
252 Error(str, file)
253 const char *str, *file;
254 {
255 fprintf(stderr, "Error: ");
256 fprintf(stderr, str, file);
257 perror(file);
258 exit(1);
259 /*NOTREACHED*/
260 }
261 /**/
263 /************************************************************************
264 /
265 / FUNCTION NAME: drandom()
266 /
267 / FUNCTION: return a random number
268 /
269 / AUTHOR: E. A. Estes, 2/7/86
270 /
271 / ARGUMENTS: none
272 /
273 / RETURN VALUE: none
274 /
275 / MODULES CALLED: random()
276 /
277 / GLOBAL INPUTS: none
278 /
279 / GLOBAL OUTPUTS: none
280 /
281 / DESCRIPTION:
282 /
283 / ************************************************************************/
284
285 double
286 drandom()
287 {
288 if (sizeof(int) != 2)
289 return((double) (random() & 0x7fff) / 32768.0);
290 else
291 return((double) random() / 32768.0);
292 }
293