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