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