xref: /original-bsd/games/phantasia/setup.c (revision 44811ff2)
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_WANT_TO_HEAR_ABOUT_IT
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 
174     /* report compile-time options */
175     printf("Compiled options:\n\n");
176     printf("Phantasia destination directory:  %s\n", DEST");
177     printf("Wizard:  %s   UID:  %d\n", WIZARD, UID);
178 
179 #ifdef ENEMY
180     printf("Enemy list enabled.\n");
181 #else
182     printf("Enemy list disabled.\n");
183 #endif
184 
185 #ifdef SHELL
186     printf("Shell escapes enabled.  Default shell:  %s\n", SHELL);
187 #else
188     printf("Shell escapes disabled.\n");
189 #endif
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 /**/
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 /**/
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 }
277