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