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