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