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