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