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