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