xref: /original-bsd/games/phantasia/setup.c (revision 2ce9ec30)
1 /*
2  * setup.c - set up all files for Phantasia
3  */
4 #include "include.h"
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 /**/
8 /************************************************************************
9 /
10 / FUNCTION NAME: main()
11 /
12 / FUNCTION: setup files for Phantasia 3.3.2
13 /
14 / AUTHOR: E. A. Estes, 12/4/85
15 /
16 / ARGUMENTS: none
17 /
18 / RETURN VALUE: none
19 /
20 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
21 /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
22 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
23 /
24 / GLOBAL INPUTS: Peoplefile[], Curmonster, _iob[], Databuf[], *Monstfp,
25 /	Lastdead[], Goldfile[], Voidfile[], Motdfile[], Messfile[], Scorefile[],
26 /	Enemyfile[], Monstfile[], 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 main()
42 {
43 FILE	*fp;			/* for opening files */
44 struct stat	fbuf;		/* for getting files statistics */
45 register char	**filename;	/* for pointing to file names */
46 register int	fd;		/* file descriptor */
47 static char *files[] =		/* all files to create */
48 	{
49 	Monstfile,
50 	Peoplefile,
51 	Messfile,
52 	Lastdead,
53 	Motdfile,
54 	Goldfile,
55 	Voidfile,
56 	Scorefile,
57 #ifdef ENEMY
58 	Enemyfile,
59 #endif
60 	(char *) NULL
61 	};
62 
63     srandom((unsigned) time((long *) NULL));	/* prime random numbers */
64 
65     umask(0117);		/* only owner can read/write created files */
66 
67     if (getuid() != UID)
68 	fprintf(stderr, "Warning: UID (%d) is not equal to current uid.\n", UID);
69 
70     /* check Phantasia destination directory */
71     if (stat(DEST", &fbuf) < 0)
72 	/* not found */
73 	{
74 	Error("Cannot stat %s.\n", DEST");
75 	exit(1);
76 	/*NOTREACHED*/
77 	}
78 
79     if ((fbuf.st_mode & S_IFDIR) == 0)
80 	/* not a directory */
81 	Error("%s is not a directory.\n", DEST");
82 	/*NOTREACHED*/
83 
84     /* try to create data files */
85     filename = &files[0];
86     while (*filename != NULL)
87 	/* create each file */
88 	{
89 	if (stat(*filename, &fbuf) == 0)
90 	    /* file exists; remove it */
91 	    {
92 	    if (*filename == Peoplefile)
93 		/* do not reset character file if it already exists */
94 		{
95 		++filename;
96 		continue;
97 		}
98 
99 	    if (unlink(*filename) < 0)
100 		Error("Cannot unlink %s.\n", *filename);
101 		/*NOTREACHED*/
102 	    }
103 
104 	if ((fd = creat(*filename, 0660)) < 0)
105 	    Error("Cannot create %s.\n", *filename);
106 	    /*NOTREACHED*/
107 
108 	close(fd);			/* close newly created file */
109 
110 	++filename;			/* process next file */
111 	}
112 
113     /* put holy grail info into energy void file */
114     Enrgyvoid.ev_active = TRUE;
115     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
116     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
117     if ((fp = fopen(Voidfile, "w")) == NULL)
118 	Error("Cannot update %s.\n", Voidfile);
119     else
120 	{
121 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
122 	fclose(fp);
123 	}
124 
125     /* create binary monster data base */
126     if ((Monstfp = fopen(Monstfile, "w")) == NULL)
127 	Error("Cannot update %s.\n", Monstfile);
128     else
129 	{
130 	if ((fp = fopen("monsters.asc", "r")) == NULL)
131 	    {
132 	    fclose(Monstfp);
133 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
134 	    }
135 	else
136 	    {
137 	    Curmonster.m_o_strength =
138 	    Curmonster.m_o_speed =
139 	    Curmonster.m_maxspeed =
140 	    Curmonster.m_o_energy =
141 	    Curmonster.m_melee =
142 	    Curmonster.m_skirmish = 0.0;
143 
144 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
145 		/* read in text file, convert to binary */
146 		{
147 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
148 		    &Curmonster.m_strength, &Curmonster.m_brains,
149 		    &Curmonster.m_speed, &Curmonster.m_energy,
150 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
151 		    &Curmonster.m_type, &Curmonster.m_flock);
152 		Databuf[24] = '\0';
153 		strcpy(Curmonster.m_name, Databuf);
154 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
155 		}
156 	    fclose(fp);
157 	    fclose(Monstfp);
158 	    }
159 	}
160 
161 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_ANSWER_QUESTIONS
162     /* write to motd file */
163     printf("One line 'motd' ? ");
164     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
165 	Databuf[0] = '\0';
166     if ((fp = fopen(Motdfile, "w")) == NULL)
167 	Error("Cannot update %s.\n", Motdfile);
168     else
169 	{
170 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
171 	fclose(fp);
172 	}
173 #endif
174 
175     /* report compile-time options */
176     printf("Compiled options:\n\n");
177     printf("Phantasia destination directory:  %s\n", DEST");
178     printf("Wizard:  %s   UID:  %d\n", WIZARD, UID);
179 
180 #ifdef OK_TO_PLAY
181     printf("Restricted playing enabled.\n");
182 #else
183     printf("Playing unrestricted.\n");
184 #endif
185 
186 #ifdef ENEMY
187     printf("Enemy list enabled.\n");
188 #else
189     printf("Enemy list disabled.\n");
190 #endif
191 
192 #ifdef SHELL
193     printf("Shell escapes enabled.  Default shell:  %s\n", SHELL);
194 #else
195     printf("Shell escapes disabled.\n");
196 #endif
197 
198 #ifdef BSD41
199     printf("Compiled for BSD 4.1\n");
200 #endif
201 
202 #ifdef BSD42
203     printf("Compiled for BSD 4.2\n");
204 #endif
205 
206 #ifdef SYS3
207     printf("Compiled for System III\n");
208 #endif
209 
210 #ifdef SYS5
211     printf("Compiled for System V\n");
212 #endif
213 
214     exit(0);
215     /*NOTREACHED*/
216 }
217 /**/
218 /************************************************************************
219 /
220 / FUNCTION NAME: Error()
221 /
222 / FUNCTION: print an error message, and exit
223 /
224 / AUTHOR: E. A. Estes, 12/4/85
225 /
226 / ARGUMENTS:
227 /	char *str - format string for printf()
228 /	char *file - file which caused error
229 /
230 / RETURN VALUE: none
231 /
232 / MODULES CALLED: exit(), perror(), fprintf()
233 /
234 / GLOBAL INPUTS: _iob[]
235 /
236 / GLOBAL OUTPUTS: none
237 /
238 / DESCRIPTION:
239 /	Print an error message, then exit.
240 /
241 /************************************************************************/
242 
243 Error(str, file)
244 char	*str, *file;
245 {
246     fprintf(stderr, "Error: ");
247     fprintf(stderr, str, file);
248     perror(file);
249     exit(1);
250     /*NOTREACHED*/
251 }
252 /**/
253 /************************************************************************
254 /
255 / FUNCTION NAME: drandom()
256 /
257 / FUNCTION: return a random number
258 /
259 / AUTHOR: E. A. Estes, 2/7/86
260 /
261 / ARGUMENTS: none
262 /
263 / RETURN VALUE: none
264 /
265 / MODULES CALLED: random()
266 /
267 / GLOBAL INPUTS: none
268 /
269 / GLOBAL OUTPUTS: none
270 /
271 / DESCRIPTION:
272 /
273 /************************************************************************/
274 
275 double
276 drandom()
277 {
278     if (sizeof(int) != 2)
279 	return((double) (random() & 0x7fff) / 32768.0);
280     else
281 	return((double) random() / 32768.0);
282 }
283