Merge from vendor branch NTPD:
[dragonfly.git] / games / phantasia / setup.c
1 /*
2  * setup.c - set up all files for Phantasia
3  *
4  * $FreeBSD: src/games/phantasia/setup.c,v 1.11 1999/11/16 02:57:34 billf Exp $
5  * $DragonFly: src/games/phantasia/setup.c,v 1.2 2003/06/17 04:25:24 dillon Exp $
6  */
7 #include "include.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.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 void Error();
44
45 static char *files[] = {                /* all files to create */
46         _SPATH_MONST,
47         _SPATH_PEOPLE,
48         _SPATH_MESS,
49         _SPATH_LASTDEAD,
50         _SPATH_MOTD,
51         _SPATH_GOLD,
52         _SPATH_VOID,
53         _SPATH_SCORE,
54         NULL,
55 };
56
57 char *monsterfile="monsters.asc";
58
59 int
60 main(argc, argv)
61         int argc;
62         char *argv[];
63 {
64         char    **filename;     /* for pointing to file names */
65         register int    fd;             /* file descriptor */
66         FILE    *fp;                    /* for opening files */
67         struct stat     fbuf;           /* for getting files statistics */
68         int ch;
69
70         while ((ch = getopt(argc, argv, "m:")) != -1)
71                 switch(ch) {
72                 case 'm':
73                         monsterfile = optarg;
74                         break;
75                 case '?':
76                 default:
77                         break;
78                 }
79         argc -= optind;
80         argv += optind;
81
82     srandomdev();
83
84 #if 0
85     umask(0117);                /* only owner can read/write created files */
86 #endif
87
88     /* try to create data files */
89     filename = &files[0];
90     while (*filename != NULL)
91         /* create each file */
92         {
93         if (stat(*filename, &fbuf) == 0)
94             /* file exists; remove it */
95             {
96             if (!strcmp(*filename, _SPATH_PEOPLE))
97                 /* do not reset character file if it already exists */
98                 {
99                 ++filename;
100                 continue;
101                 }
102
103             if (unlink(*filename) < 0)
104                 Error("Cannot unlink %s.\n", *filename);
105                 /*NOTREACHED*/
106             }
107
108         if ((fd = creat(*filename, 0666)) < 0)
109             Error("Cannot create %s.\n", *filename);
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     if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
122         Error("Cannot update %s.\n", _SPATH_VOID);
123     else
124         {
125         fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
126         fclose(fp);
127         }
128
129     /* create binary monster data base */
130     if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
131         Error("Cannot update %s.\n", _SPATH_MONST);
132     else
133         {
134         if ((fp = fopen(monsterfile, "r")) == NULL)
135             {
136             fclose(Monstfp);
137             Error("cannot open %s to create monster database.\n", "monsters.asc");
138             }
139         else
140             {
141             Curmonster.m_o_strength =
142             Curmonster.m_o_speed =
143             Curmonster.m_maxspeed =
144             Curmonster.m_o_energy =
145             Curmonster.m_melee =
146             Curmonster.m_skirmish = 0.0;
147
148             while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
149                 /* read in text file, convert to binary */
150                 {
151                 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
152                     &Curmonster.m_strength, &Curmonster.m_brains,
153                     &Curmonster.m_speed, &Curmonster.m_energy,
154                     &Curmonster.m_experience, &Curmonster.m_treasuretype,
155                     &Curmonster.m_type, &Curmonster.m_flock);
156                 Databuf[24] = '\0';
157                 strcpy(Curmonster.m_name, Databuf);
158                 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
159                 }
160             fclose(fp);
161             fclose(Monstfp);
162             }
163         }
164
165 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
166     /* write to motd file */
167     printf("One line 'motd' ? ");
168     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
169         Databuf[0] = '\0';
170     if ((fp = fopen(_SPATH_MOTD, "w")) == NULL)
171         Error("Cannot update %s.\n", _SPATH_MOTD);
172     else
173         {
174         fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
175         fclose(fp);
176         }
177
178     /* report compile-time options */
179     printf("Compiled options:\n\n");
180     printf("Phantasia destination directory:  %s\n", _SPATH_PHANTDIR);
181     printf("Wizard: root UID: 0\n");
182
183 #ifdef BSD41
184     printf("Compiled for BSD 4.1\n");
185 #endif
186
187 #ifdef BSD42
188     printf("Compiled for BSD 4.2\n");
189 #endif
190
191 #ifdef SYS3
192     printf("Compiled for System III\n");
193 #endif
194
195 #ifdef SYS5
196     printf("Compiled for System V\n");
197 #endif
198 #endif
199
200     exit(0);
201     /*NOTREACHED*/
202 }
203 /*\f*/
204 /************************************************************************
205 /
206 / FUNCTION NAME: Error()
207 /
208 / FUNCTION: print an error message, and exit
209 /
210 / AUTHOR: E. A. Estes, 12/4/85
211 /
212 / ARGUMENTS:
213 /       char *str - format string for printf()
214 /       char *file - file which caused error
215 /
216 / RETURN VALUE: none
217 /
218 / MODULES CALLED: exit(), perror(), fprintf()
219 /
220 / GLOBAL INPUTS: _iob[]
221 /
222 / GLOBAL OUTPUTS: none
223 /
224 / DESCRIPTION:
225 /       Print an error message, then exit.
226 /
227 *************************************************************************/
228
229 void
230 Error(str, file)
231 char    *str, *file;
232 {
233     fprintf(stderr, "Error: ");
234     fprintf(stderr, str, file);
235     perror(file);
236     exit(1);
237     /*NOTREACHED*/
238 }
239 /*\f*/
240 /************************************************************************
241 /
242 / FUNCTION NAME: drandom()
243 /
244 / FUNCTION: return a random number
245 /
246 / AUTHOR: E. A. Estes, 2/7/86
247 /
248 / ARGUMENTS: none
249 /
250 / RETURN VALUE: none
251 /
252 / MODULES CALLED: random()
253 /
254 / GLOBAL INPUTS: none
255 /
256 / GLOBAL OUTPUTS: none
257 /
258 / DESCRIPTION:
259 /
260 *************************************************************************/
261
262 double
263 drandom()
264 {
265     if (sizeof(int) != 2)
266         return((double) (random() & 0x7fff) / 32768.0);
267     else
268         return((double) random() / 32768.0);
269 }