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