Add vfs(7) manpage.
[dragonfly.git] / games / phantasia / setup.c
blobde4cf71bc1e3638c2b0f9bf5363837df687e9428
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 */
6 #include "include.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
11 void Error(const char *, const char *) __dead2;
13 static const char *const files[] = { /* all files to create */
14 _SPATH_MONST,
15 _SPATH_PEOPLE,
16 _SPATH_MESS,
17 _SPATH_LASTDEAD,
18 _SPATH_MOTD,
19 _SPATH_GOLD,
20 _SPATH_VOID,
21 _SPATH_SCORE,
22 NULL,
25 const char *monsterfile = "monsters.asc";
28 * FUNCTION: setup files for Phantasia 3.3.2
30 * GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
32 * GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
34 * DESCRIPTION:
35 * This program tries to verify the parameters specified in
36 * the Makefile.
38 * Create all necessary files. Note that nothing needs to be
39 * put in these files.
40 * Also, the monster binary data base is created here.
43 int
44 main(int argc, char *argv[])
46 const char *const *filename; /* for pointing to file names */
47 int fd; /* file descriptor */
48 FILE *fp; /* for opening files */
49 struct stat fbuf; /* for getting files statistics */
50 int ch;
52 while ((ch = getopt(argc, argv, "m:")) != -1)
53 switch(ch) {
54 case 'm':
55 monsterfile = optarg;
56 break;
57 case '?':
58 default:
59 break;
61 argc -= optind;
62 argv += optind;
64 srandomdev();
66 #if 0
67 umask(0117); /* only owner can read/write created files */
68 #endif
70 /* try to create data files */
71 filename = &files[0];
72 /* create each file */
73 while (*filename != NULL) {
74 /* file exists; remove it */
75 if (stat(*filename, &fbuf) == 0) {
76 /* do not reset character file if it already exists */
77 if (!strcmp(*filename, _SPATH_PEOPLE)) {
78 ++filename;
79 continue;
82 if (unlink(*filename) < 0)
83 Error("Cannot unlink %s.\n", *filename);
84 /* NOTREACHED */
87 if ((fd = creat(*filename, 0666)) < 0)
88 Error("Cannot create %s.\n", *filename);
89 /* NOTREACHED */
91 close(fd); /* close newly created file */
93 ++filename; /* process next file */
96 /* put holy grail info into energy void file */
97 Enrgyvoid.ev_active = TRUE;
98 Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
99 Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
100 if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
101 Error("Cannot update %s.\n", _SPATH_VOID);
102 else {
103 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
104 fclose(fp);
107 /* create binary monster data base */
108 if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
109 Error("Cannot update %s.\n", _SPATH_MONST);
110 else if ((fp = fopen(monsterfile, "r")) == NULL) {
111 fclose(Monstfp);
112 Error("cannot open %s to create monster database.\n", "monsters.asc");
113 } else {
114 Curmonster.m_o_strength =
115 Curmonster.m_o_speed =
116 Curmonster.m_maxspeed =
117 Curmonster.m_o_energy =
118 Curmonster.m_melee =
119 Curmonster.m_skirmish = 0.0;
121 /* read in text file, convert to binary */
122 while (fgets(Databuf, SZ_DATABUF, fp) != NULL) {
123 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
124 &Curmonster.m_strength, &Curmonster.m_brains,
125 &Curmonster.m_speed, &Curmonster.m_energy,
126 &Curmonster.m_experience, &Curmonster.m_treasuretype,
127 &Curmonster.m_type, &Curmonster.m_flock);
128 Databuf[24] = '\0';
129 strcpy(Curmonster.m_name, Databuf);
130 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
132 fclose(fp);
133 fclose(Monstfp);
136 exit(0);
137 /* NOTREACHED */
141 * FUNCTION: print an error message, and exit
143 * ARGUMENTS:
144 * char *str - format string for printf()
145 * char *file - file which caused error
147 * GLOBAL INPUTS: _iob[]
149 * DESCRIPTION:
150 * Print an error message, then exit.
153 void
154 Error(const char *str, const char *file)
156 fprintf(stderr, "Error: ");
157 fprintf(stderr, str, file);
158 perror(file);
159 exit(1);
160 /* NOTREACHED */