]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - phantasia/setup.c
ANSIfy function declarations. Object file diffs cross-checked.
[bsdgames-darwin.git] / phantasia / setup.c
1 /* $NetBSD: setup.c,v 1.19 2009/05/25 23:08:45 dholland 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 #ifdef __NetBSD__
8 #include <sys/cdefs.h>
9 #endif
10
11 #include <sys/param.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include "include.h"
15
16 #ifndef __dead /* Not NetBSD */
17 #define __dead
18 #endif
19
20 int main(int, char *[]);
21 void Error(const char *, const char *) __dead;
22 double drandom(void);
23
24 /*\f*/
25 /************************************************************************
26 /
27 / FUNCTION NAME: main()
28 /
29 / FUNCTION: setup files for Phantasia 3.3.2
30 /
31 / AUTHOR: E. A. Estes, 12/4/85
32 /
33 / ARGUMENTS: none
34 /
35 / RETURN VALUE: none
36 /
37 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
38 / fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
39 / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
40 /
41 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
42 /
43 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
44 /
45 / DESCRIPTION:
46 /
47 / This program tries to verify the parameters specified in
48 / the Makefile.
49 /
50 / Create all necessary files. Note that nothing needs to be
51 / put in these files.
52 / Also, the monster binary data base is created here.
53 /
54 / ************************************************************************/
55
56 static const char *const files[] = { /* all files to create */
57 _PATH_MONST,
58 _PATH_PEOPLE,
59 _PATH_MESS,
60 _PATH_LASTDEAD,
61 _PATH_MOTD,
62 _PATH_GOLD,
63 _PATH_VOID,
64 _PATH_SCORE,
65 NULL,
66 };
67
68 const char *monsterfile = "monsters.asc";
69
70 int
71 main(int argc, 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 /*\f*/
204 /************************************************************************
205 /
206 / FUNCTION NAME: Error()
207 /
208 / FUNCTION: print an error message, and exit
209 /
210 / AUTHOR: E. A. Estes, 12/4/85
211 /
212 / ARGUMENTS:
213 / char *str - format string for printf()
214 / char *file - file which caused error
215 /
216 / RETURN VALUE: none
217 /
218 / MODULES CALLED: exit(), perror(), fprintf()
219 /
220 / GLOBAL INPUTS: _iob[]
221 /
222 / GLOBAL OUTPUTS: none
223 /
224 / DESCRIPTION:
225 / Print an error message, then exit.
226 /
227 / ************************************************************************/
228
229 void
230 Error(const char *str, const char *file)
231 {
232 fprintf(stderr, "Error: ");
233 fprintf(stderr, str, file);
234 perror(file);
235 exit(1);
236 /*NOTREACHED*/
237 }
238 /*\f*/
239 /************************************************************************
240 /
241 / FUNCTION NAME: drandom()
242 /
243 / FUNCTION: return a random number
244 /
245 / AUTHOR: E. A. Estes, 2/7/86
246 /
247 / ARGUMENTS: none
248 /
249 / RETURN VALUE: none
250 /
251 / MODULES CALLED: random()
252 /
253 / GLOBAL INPUTS: none
254 /
255 / GLOBAL OUTPUTS: none
256 /
257 / DESCRIPTION:
258 /
259 / ************************************************************************/
260
261 double
262 drandom(void)
263 {
264 if (sizeof(int) != 2)
265 return((double) (random() & 0x7fff) / 32768.0);
266 else
267 return((double) random() / 32768.0);
268 }