]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - phantasia/setup.c
Only pull in <sys/cdefs.h> on NetBSD, to fix build on Solaris 10.
[bsdgames-darwin.git] / phantasia / setup.c
1 /* $NetBSD: setup.c,v 1.18 2008/01/16 23:23:25 lukem 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(argc, argv)
72 int argc;
73 char *argv[];
74 {
75 const char *const *filename; /* for pointing to file names */
76 int fd; /* file descriptor */
77 FILE *fp; /* for opening files */
78 struct stat fbuf; /* for getting files statistics */
79 int ch;
80 char *path;
81
82 while ((ch = getopt(argc, argv, "m:")) != -1)
83 switch(ch) {
84 case 'm':
85 monsterfile = optarg;
86 break;
87 case '?':
88 default:
89 break;
90 }
91 argc -= optind;
92 argv += optind;
93
94 srandom((unsigned) time(NULL)); /* prime random numbers */
95
96 umask(0117); /* only owner can read/write created files */
97
98 /* try to create data files */
99 filename = &files[0];
100 while (*filename != NULL)
101 /* create each file */
102 {
103 path = strrchr(*filename, '/') + 1;
104 if (stat(path, &fbuf) == 0)
105 /* file exists; remove it */
106 {
107 if (unlink(path) < 0)
108 Error("Cannot unlink %s.\n", path);
109 /*NOTREACHED*/
110 }
111
112 if ((fd = creat(path, 0660)) < 0)
113 Error("Cannot create %s.\n", path);
114 /*NOTREACHED*/
115
116 close(fd); /* close newly created file */
117
118 ++filename; /* process next file */
119 }
120
121 /* Initialize an empty file placeholder for the grail location. */
122 if ((fp = fopen(path, "w")) == NULL)
123 Error("Cannot create %s.\n", path);
124 fclose(fp);
125
126 /* create binary monster data base */
127 path = strrchr(_PATH_MONST, '/') + 1;
128 if ((Monstfp = fopen(path, "w")) == NULL)
129 Error("Cannot update %s.\n", path);
130 else
131 {
132 if ((fp = fopen(monsterfile, "r")) == NULL)
133 {
134 fclose(Monstfp);
135 Error("cannot open %s to create monster database.\n", "monsters.asc");
136 }
137 else
138 {
139 Curmonster.m_o_strength =
140 Curmonster.m_o_speed =
141 Curmonster.m_maxspeed =
142 Curmonster.m_o_energy =
143 Curmonster.m_melee =
144 Curmonster.m_skirmish = 0.0;
145
146 while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
147 /* read in text file, convert to binary */
148 {
149 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
150 &Curmonster.m_strength, &Curmonster.m_brains,
151 &Curmonster.m_speed, &Curmonster.m_energy,
152 &Curmonster.m_experience, &Curmonster.m_treasuretype,
153 &Curmonster.m_type, &Curmonster.m_flock);
154 Databuf[24] = '\0';
155 strcpy(Curmonster.m_name, Databuf);
156 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
157 }
158 fclose(fp);
159 fflush(Monstfp);
160 if (ferror(Monstfp))
161 Error("Writing %s.\n", path);
162 fclose(Monstfp);
163 }
164 }
165
166 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
167 /* write to motd file */
168 printf("One line 'motd' ? ");
169 if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
170 Databuf[0] = '\0';
171 path = strrchr(_PATH_MOTD, '/') + 1;
172 if ((fp = fopen(path, "w")) == NULL)
173 Error("Cannot update %s.\n", path);
174 else
175 {
176 fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
177 fclose(fp);
178 }
179
180 /* report compile-time options */
181 printf("Compiled options:\n\n");
182 printf("Phantasia destination directory: %s\n", _PATH_PHANTDIR);
183 printf("Wizard: root UID: 0\n");
184
185 #ifdef BSD41
186 printf("Compiled for BSD 4.1\n");
187 #endif
188
189 #ifdef BSD42
190 printf("Compiled for BSD 4.2\n");
191 #endif
192
193 #ifdef SYS3
194 printf("Compiled for System III\n");
195 #endif
196
197 #ifdef SYS5
198 printf("Compiled for System V\n");
199 #endif
200 #endif
201
202 exit(0);
203 /*NOTREACHED*/
204 }
205 /*\f*/
206 /************************************************************************
207 /
208 / FUNCTION NAME: Error()
209 /
210 / FUNCTION: print an error message, and exit
211 /
212 / AUTHOR: E. A. Estes, 12/4/85
213 /
214 / ARGUMENTS:
215 / char *str - format string for printf()
216 / char *file - file which caused error
217 /
218 / RETURN VALUE: none
219 /
220 / MODULES CALLED: exit(), perror(), fprintf()
221 /
222 / GLOBAL INPUTS: _iob[]
223 /
224 / GLOBAL OUTPUTS: none
225 /
226 / DESCRIPTION:
227 / Print an error message, then exit.
228 /
229 / ************************************************************************/
230
231 void
232 Error(str, file)
233 const char *str, *file;
234 {
235 fprintf(stderr, "Error: ");
236 fprintf(stderr, str, file);
237 perror(file);
238 exit(1);
239 /*NOTREACHED*/
240 }
241 /*\f*/
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 }