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