Expand PMF_FN_* macros.
[netbsd-mini2440.git] / games / phantasia / setup.c
blob3a026bfc7c6ae0889860027ba67636b0fa139131
1 /* $NetBSD: setup.c,v 1.20 2009/05/27 17:44:38 dholland Exp $ */
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
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <setjmp.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
20 #ifndef __dead /* Not NetBSD */
21 #define __dead
22 #endif
24 #include "phantdefs.h"
25 #include "phantstruct.h"
26 #include "phantglobs.h"
27 #include "pathnames.h"
29 int main(int, char *[]);
30 void Error(const char *, const char *) __dead;
31 double drandom(void);
33 /*\f*/
34 /************************************************************************
36 / FUNCTION NAME: main()
38 / FUNCTION: setup files for Phantasia 3.3.2
40 / AUTHOR: E. A. Estes, 12/4/85
42 / ARGUMENTS: none
44 / RETURN VALUE: none
46 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
47 / fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
48 / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
50 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
52 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
54 / DESCRIPTION:
56 / This program tries to verify the parameters specified in
57 / the Makefile.
59 / Create all necessary files. Note that nothing needs to be
60 / put in these files.
61 / Also, the monster binary data base is created here.
63 / ************************************************************************/
65 static const char *const files[] = { /* all files to create */
66 _PATH_MONST,
67 _PATH_PEOPLE,
68 _PATH_MESS,
69 _PATH_LASTDEAD,
70 _PATH_MOTD,
71 _PATH_GOLD,
72 _PATH_VOID,
73 _PATH_SCORE,
74 NULL,
77 const char *monsterfile = "monsters.asc";
79 int
80 main(int argc, char *argv[])
82 const char *const *filename; /* for pointing to file names */
83 int fd; /* file descriptor */
84 FILE *fp; /* for opening files */
85 struct stat fbuf; /* for getting files statistics */
86 int ch;
87 char *path;
89 while ((ch = getopt(argc, argv, "m:")) != -1)
90 switch(ch) {
91 case 'm':
92 monsterfile = optarg;
93 break;
94 case '?':
95 default:
96 break;
98 argc -= optind;
99 argv += optind;
101 srandom((unsigned) time(NULL)); /* prime random numbers */
103 umask(0117); /* only owner can read/write created files */
105 /* try to create data files */
106 filename = &files[0];
107 while (*filename != NULL)
108 /* create each file */
110 path = strrchr(*filename, '/') + 1;
111 if (stat(path, &fbuf) == 0)
112 /* file exists; remove it */
114 if (unlink(path) < 0)
115 Error("Cannot unlink %s.\n", path);
116 /*NOTREACHED*/
119 if ((fd = creat(path, 0660)) < 0)
120 Error("Cannot create %s.\n", path);
121 /*NOTREACHED*/
123 close(fd); /* close newly created file */
125 ++filename; /* process next file */
128 /* Initialize an empty file placeholder for the grail location. */
129 if ((fp = fopen(path, "w")) == NULL)
130 Error("Cannot create %s.\n", path);
131 fclose(fp);
133 /* create binary monster data base */
134 path = strrchr(_PATH_MONST, '/') + 1;
135 if ((Monstfp = fopen(path, "w")) == NULL)
136 Error("Cannot update %s.\n", path);
137 else
139 if ((fp = fopen(monsterfile, "r")) == NULL)
141 fclose(Monstfp);
142 Error("cannot open %s to create monster database.\n", "monsters.asc");
144 else
146 Curmonster.m_o_strength =
147 Curmonster.m_o_speed =
148 Curmonster.m_maxspeed =
149 Curmonster.m_o_energy =
150 Curmonster.m_melee =
151 Curmonster.m_skirmish = 0.0;
153 while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
154 /* read in text file, convert to binary */
156 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
157 &Curmonster.m_strength, &Curmonster.m_brains,
158 &Curmonster.m_speed, &Curmonster.m_energy,
159 &Curmonster.m_experience, &Curmonster.m_treasuretype,
160 &Curmonster.m_type, &Curmonster.m_flock);
161 Databuf[24] = '\0';
162 strcpy(Curmonster.m_name, Databuf);
163 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
165 fclose(fp);
166 fflush(Monstfp);
167 if (ferror(Monstfp))
168 Error("Writing %s.\n", path);
169 fclose(Monstfp);
173 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
174 /* write to motd file */
175 printf("One line 'motd' ? ");
176 if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
177 Databuf[0] = '\0';
178 path = strrchr(_PATH_MOTD, '/') + 1;
179 if ((fp = fopen(path, "w")) == NULL)
180 Error("Cannot update %s.\n", path);
181 else
183 fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
184 fclose(fp);
187 /* report compile-time options */
188 printf("Compiled options:\n\n");
189 printf("Phantasia destination directory: %s\n", _PATH_PHANTDIR);
190 printf("Wizard: root UID: 0\n");
192 #ifdef BSD41
193 printf("Compiled for BSD 4.1\n");
194 #endif
196 #ifdef BSD42
197 printf("Compiled for BSD 4.2\n");
198 #endif
200 #ifdef SYS3
201 printf("Compiled for System III\n");
202 #endif
204 #ifdef SYS5
205 printf("Compiled for System V\n");
206 #endif
207 #endif
209 exit(0);
210 /*NOTREACHED*/
212 /*\f*/
213 /************************************************************************
215 / FUNCTION NAME: Error()
217 / FUNCTION: print an error message, and exit
219 / AUTHOR: E. A. Estes, 12/4/85
221 / ARGUMENTS:
222 / char *str - format string for printf()
223 / char *file - file which caused error
225 / RETURN VALUE: none
227 / MODULES CALLED: exit(), perror(), fprintf()
229 / GLOBAL INPUTS: _iob[]
231 / GLOBAL OUTPUTS: none
233 / DESCRIPTION:
234 / Print an error message, then exit.
236 / ************************************************************************/
238 void
239 Error(const char *str, const char *file)
241 fprintf(stderr, "Error: ");
242 fprintf(stderr, str, file);
243 perror(file);
244 exit(1);
245 /*NOTREACHED*/
247 /*\f*/
248 /************************************************************************
250 / FUNCTION NAME: drandom()
252 / FUNCTION: return a random number
254 / AUTHOR: E. A. Estes, 2/7/86
256 / ARGUMENTS: none
258 / RETURN VALUE: none
260 / MODULES CALLED: random()
262 / GLOBAL INPUTS: none
264 / GLOBAL OUTPUTS: none
266 / DESCRIPTION:
268 / ************************************************************************/
270 double
271 drandom(void)
273 if (sizeof(int) != 2)
274 return((double) (random() & 0x7fff) / 32768.0);
275 else
276 return((double) random() / 32768.0);