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