Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / games / phantasia / setup.c
blobf38defc0a541dab6326cfdf4959828746f84b2c5
1 /*
2 * setup.c - set up all files for Phantasia
4 * $FreeBSD: src/games/phantasia/setup.c,v 1.11 1999/11/16 02:57:34 billf Exp $
5 * $DragonFly: src/games/phantasia/setup.c,v 1.3 2005/05/31 00:06:26 swildner Exp $
6 */
7 #include "include.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
12 /* functions which we need to know about */
13 /* phantglobs.c */
14 extern double drandom(void);
16 void Error(const char *, const char *);
18 static const char *files[] = { /* all files to create */
19 _SPATH_MONST,
20 _SPATH_PEOPLE,
21 _SPATH_MESS,
22 _SPATH_LASTDEAD,
23 _SPATH_MOTD,
24 _SPATH_GOLD,
25 _SPATH_VOID,
26 _SPATH_SCORE,
27 NULL,
30 const char *monsterfile="monsters.asc";
32 /*\f*/
33 /************************************************************************
35 / FUNCTION NAME: main()
37 / FUNCTION: setup files for Phantasia 3.3.2
39 / AUTHOR: E. A. Estes, 12/4/85
41 / ARGUMENTS: none
43 / RETURN VALUE: none
45 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
46 / fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
47 / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
49 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
51 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
53 / DESCRIPTION:
55 / This program tries to verify the parameters specified in
56 / the Makefile.
58 / Create all necessary files. Note that nothing needs to be
59 / put in these files.
60 / Also, the monster binary data base is created here.
62 *************************************************************************/
64 int
65 main(int argc, char **argv)
67 const char **filename; /* for pointing to file names */
68 int fd; /* file descriptor */
69 FILE *fp; /* for opening files */
70 struct stat fbuf; /* for getting files statistics */
71 int ch;
73 while ((ch = getopt(argc, argv, "m:")) != -1)
74 switch(ch) {
75 case 'm':
76 monsterfile = optarg;
77 break;
78 case '?':
79 default:
80 break;
82 argc -= optind;
83 argv += optind;
85 srandomdev();
87 #if 0
88 umask(0117); /* only owner can read/write created files */
89 #endif
91 /* try to create data files */
92 filename = &files[0];
93 while (*filename != NULL)
94 /* create each file */
96 if (stat(*filename, &fbuf) == 0)
97 /* file exists; remove it */
99 if (!strcmp(*filename, _SPATH_PEOPLE))
100 /* do not reset character file if it already exists */
102 ++filename;
103 continue;
106 if (unlink(*filename) < 0)
107 Error("Cannot unlink %s.\n", *filename);
108 /*NOTREACHED*/
111 if ((fd = creat(*filename, 0666)) < 0)
112 Error("Cannot create %s.\n", *filename);
113 /*NOTREACHED*/
115 close(fd); /* close newly created file */
117 ++filename; /* process next file */
120 /* put holy grail info into energy void file */
121 Enrgyvoid.ev_active = TRUE;
122 Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
123 Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
124 if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
125 Error("Cannot update %s.\n", _SPATH_VOID);
126 else
128 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
129 fclose(fp);
132 /* create binary monster data base */
133 if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
134 Error("Cannot update %s.\n", _SPATH_MONST);
135 else
137 if ((fp = fopen(monsterfile, "r")) == NULL)
139 fclose(Monstfp);
140 Error("cannot open %s to create monster database.\n", "monsters.asc");
142 else
144 Curmonster.m_o_strength =
145 Curmonster.m_o_speed =
146 Curmonster.m_maxspeed =
147 Curmonster.m_o_energy =
148 Curmonster.m_melee =
149 Curmonster.m_skirmish = 0.0;
151 while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
152 /* read in text file, convert to binary */
154 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
155 &Curmonster.m_strength, &Curmonster.m_brains,
156 &Curmonster.m_speed, &Curmonster.m_energy,
157 &Curmonster.m_experience, &Curmonster.m_treasuretype,
158 &Curmonster.m_type, &Curmonster.m_flock);
159 Databuf[24] = '\0';
160 strcpy(Curmonster.m_name, Databuf);
161 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
163 fclose(fp);
164 fclose(Monstfp);
168 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
169 /* write to motd file */
170 printf("One line 'motd' ? ");
171 if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
172 Databuf[0] = '\0';
173 if ((fp = fopen(_SPATH_MOTD, "w")) == NULL)
174 Error("Cannot update %s.\n", _SPATH_MOTD);
175 else
177 fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
178 fclose(fp);
181 /* report compile-time options */
182 printf("Compiled options:\n\n");
183 printf("Phantasia destination directory: %s\n", _SPATH_PHANTDIR);
184 printf("Wizard: root UID: 0\n");
186 #ifdef BSD41
187 printf("Compiled for BSD 4.1\n");
188 #endif
190 #ifdef BSD42
191 printf("Compiled for BSD 4.2\n");
192 #endif
194 #ifdef SYS3
195 printf("Compiled for System III\n");
196 #endif
198 #ifdef SYS5
199 printf("Compiled for System V\n");
200 #endif
201 #endif
203 exit(0);
204 /*NOTREACHED*/
206 /*\f*/
207 /************************************************************************
209 / FUNCTION NAME: Error()
211 / FUNCTION: print an error message, and exit
213 / AUTHOR: E. A. Estes, 12/4/85
215 / ARGUMENTS:
216 / char *str - format string for printf()
217 / char *file - file which caused error
219 / RETURN VALUE: none
221 / MODULES CALLED: exit(), perror(), fprintf()
223 / GLOBAL INPUTS: _iob[]
225 / GLOBAL OUTPUTS: none
227 / DESCRIPTION:
228 / Print an error message, then exit.
230 *************************************************************************/
232 void
233 Error(const char *str, const char *file)
235 fprintf(stderr, "Error: ");
236 fprintf(stderr, str, file);
237 perror(file);
238 exit(1);
239 /*NOTREACHED*/