Add __attribute__((__noreturn__)) to various function prototypes in games/.
[dragonfly.git] / games / phantasia / setup.c
CommitLineData
984263bc
MD
1/*
2 * setup.c - set up all files for Phantasia
3 *
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>
313fa7d1
SW
10
11/* functions which we need to know about */
12/* phantglobs.c */
13extern double drandom(void);
14
09ac707a 15void Error(const char *, const char *) __dead2;
313fa7d1
SW
16
17static const char *files[] = { /* all files to create */
18 _SPATH_MONST,
19 _SPATH_PEOPLE,
20 _SPATH_MESS,
21 _SPATH_LASTDEAD,
22 _SPATH_MOTD,
23 _SPATH_GOLD,
24 _SPATH_VOID,
25 _SPATH_SCORE,
26 NULL,
27};
28
6693db17
SW
29const char *monsterfile = "monsters.asc";
30
31/*
32 * FUNCTION: setup files for Phantasia 3.3.2
33 *
34 * GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
35 *
36 * GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
37 *
38 * DESCRIPTION:
39 * This program tries to verify the parameters specified in
40 * the Makefile.
41 *
42 * Create all necessary files. Note that nothing needs to be
43 * put in these files.
44 * Also, the monster binary data base is created here.
45 */
984263bc 46
984263bc 47int
6693db17 48main(int argc, char *argv[])
984263bc 49{
313fa7d1 50 const char **filename; /* for pointing to file names */
6693db17 51 int fd; /* file descriptor */
984263bc
MD
52 FILE *fp; /* for opening files */
53 struct stat fbuf; /* for getting files statistics */
54 int ch;
55
56 while ((ch = getopt(argc, argv, "m:")) != -1)
57 switch(ch) {
58 case 'm':
59 monsterfile = optarg;
60 break;
61 case '?':
62 default:
63 break;
64 }
65 argc -= optind;
66 argv += optind;
67
6693db17 68 srandomdev();
984263bc
MD
69
70#if 0
6693db17 71 umask(0117); /* only owner can read/write created files */
984263bc
MD
72#endif
73
6693db17
SW
74 /* try to create data files */
75 filename = &files[0];
984263bc 76 /* create each file */
6693db17
SW
77 while (*filename != NULL) {
78 /* file exists; remove it */
79 if (stat(*filename, &fbuf) == 0) {
80 /* do not reset character file if it already exists */
81 if (!strcmp(*filename, _SPATH_PEOPLE)) {
82 ++filename;
83 continue;
84 }
85
86 if (unlink(*filename) < 0)
87 Error("Cannot unlink %s.\n", *filename);
88 /* NOTREACHED */
984263bc
MD
89 }
90
6693db17
SW
91 if ((fd = creat(*filename, 0666)) < 0)
92 Error("Cannot create %s.\n", *filename);
93 /* NOTREACHED */
984263bc 94
6693db17 95 close(fd); /* close newly created file */
984263bc 96
6693db17 97 ++filename; /* process next file */
984263bc
MD
98 }
99
6693db17
SW
100 /* put holy grail info into energy void file */
101 Enrgyvoid.ev_active = TRUE;
102 Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
103 Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
104 if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
105 Error("Cannot update %s.\n", _SPATH_VOID);
106 else {
107 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
108 fclose(fp);
984263bc
MD
109 }
110
6693db17
SW
111 /* create binary monster data base */
112 if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
113 Error("Cannot update %s.\n", _SPATH_MONST);
114 else if ((fp = fopen(monsterfile, "r")) == NULL) {
115 fclose(Monstfp);
116 Error("cannot open %s to create monster database.\n", "monsters.asc");
117 } else {
118 Curmonster.m_o_strength =
119 Curmonster.m_o_speed =
120 Curmonster.m_maxspeed =
121 Curmonster.m_o_energy =
122 Curmonster.m_melee =
123 Curmonster.m_skirmish = 0.0;
124
984263bc 125 /* read in text file, convert to binary */
6693db17
SW
126 while (fgets(Databuf, SZ_DATABUF, fp) != NULL) {
127 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
128 &Curmonster.m_strength, &Curmonster.m_brains,
129 &Curmonster.m_speed, &Curmonster.m_energy,
130 &Curmonster.m_experience, &Curmonster.m_treasuretype,
131 &Curmonster.m_type, &Curmonster.m_flock);
132 Databuf[24] = '\0';
133 strcpy(Curmonster.m_name, Databuf);
134 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
984263bc 135 }
6693db17
SW
136 fclose(fp);
137 fclose(Monstfp);
984263bc
MD
138 }
139
6693db17
SW
140 exit(0);
141 /* NOTREACHED */
984263bc 142}
6693db17
SW
143
144/*
145 * FUNCTION: print an error message, and exit
146 *
147 * ARGUMENTS:
148 * char *str - format string for printf()
149 * char *file - file which caused error
150 *
151 * GLOBAL INPUTS: _iob[]
152 *
153 * DESCRIPTION:
154 * Print an error message, then exit.
155 */
984263bc
MD
156
157void
313fa7d1 158Error(const char *str, const char *file)
984263bc 159{
6693db17
SW
160 fprintf(stderr, "Error: ");
161 fprintf(stderr, str, file);
162 perror(file);
163 exit(1);
164 /* NOTREACHED */
984263bc 165}