1 /* $NetBSD: setup.c,v 1.20 2009/05/27 17:44:38 dholland Exp $ */
4 * setup.c - set up all files for Phantasia
5 * n.b.: this is used at build-time - i.e. during build.sh.
11 #include <sys/types.h>
20 #ifndef __dead /* Not NetBSD */
24 #include "phantdefs.h"
25 #include "phantstruct.h"
26 #include "phantglobs.h"
27 #include "pathnames.h"
29 int main(int, char *[]);
30 void Error(const char *, const char *) __dead
;
34 /************************************************************************
36 / FUNCTION NAME: main()
38 / FUNCTION: setup files for Phantasia 3.3.2
40 / AUTHOR: E. A. Estes, 12/4/85
46 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
47 / fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
48 / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
50 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
52 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
56 / This program tries to verify the parameters specified in
59 / Create all necessary files. Note that nothing needs to be
61 / Also, the monster binary data base is created here.
63 / ************************************************************************/
65 static const char *const files
[] = { /* all files to create */
77 const char *monsterfile
= "monsters.asc";
80 main(int argc
, char *argv
[])
82 const char *const *filename
; /* for pointing to file names */
83 int fd
; /* file descriptor */
84 FILE *fp
; /* for opening files */
85 struct stat fbuf
; /* for getting files statistics */
89 while ((ch
= getopt(argc
, argv
, "m:")) != -1)
101 srandom((unsigned) time(NULL
)); /* prime random numbers */
103 umask(0117); /* only owner can read/write created files */
105 /* try to create data files */
106 filename
= &files
[0];
107 while (*filename
!= NULL
)
108 /* create each file */
110 path
= strrchr(*filename
, '/') + 1;
111 if (stat(path
, &fbuf
) == 0)
112 /* file exists; remove it */
114 if (unlink(path
) < 0)
115 Error("Cannot unlink %s.\n", path
);
119 if ((fd
= creat(path
, 0660)) < 0)
120 Error("Cannot create %s.\n", path
);
123 close(fd
); /* close newly created file */
125 ++filename
; /* process next file */
128 /* Initialize an empty file placeholder for the grail location. */
129 if ((fp
= fopen(path
, "w")) == NULL
)
130 Error("Cannot create %s.\n", path
);
133 /* create binary monster data base */
134 path
= strrchr(_PATH_MONST
, '/') + 1;
135 if ((Monstfp
= fopen(path
, "w")) == NULL
)
136 Error("Cannot update %s.\n", path
);
139 if ((fp
= fopen(monsterfile
, "r")) == NULL
)
142 Error("cannot open %s to create monster database.\n", "monsters.asc");
146 Curmonster
.m_o_strength
=
147 Curmonster
.m_o_speed
=
148 Curmonster
.m_maxspeed
=
149 Curmonster
.m_o_energy
=
151 Curmonster
.m_skirmish
= 0.0;
153 while (fgets(Databuf
, SZ_DATABUF
, fp
) != NULL
)
154 /* read in text file, convert to binary */
156 sscanf(&Databuf
[24], "%lf%lf%lf%lf%lf%d%d%lf",
157 &Curmonster
.m_strength
, &Curmonster
.m_brains
,
158 &Curmonster
.m_speed
, &Curmonster
.m_energy
,
159 &Curmonster
.m_experience
, &Curmonster
.m_treasuretype
,
160 &Curmonster
.m_type
, &Curmonster
.m_flock
);
162 strcpy(Curmonster
.m_name
, Databuf
);
163 fwrite((char *) &Curmonster
, SZ_MONSTERSTRUCT
, 1, Monstfp
);
168 Error("Writing %s.\n", path
);
173 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
174 /* write to motd file */
175 printf("One line 'motd' ? ");
176 if (fgets(Databuf
, SZ_DATABUF
, stdin
) == NULL
)
178 path
= strrchr(_PATH_MOTD
, '/') + 1;
179 if ((fp
= fopen(path
, "w")) == NULL
)
180 Error("Cannot update %s.\n", path
);
183 fwrite(Databuf
, sizeof(char), strlen(Databuf
), fp
);
187 /* report compile-time options */
188 printf("Compiled options:\n\n");
189 printf("Phantasia destination directory: %s\n", _PATH_PHANTDIR
);
190 printf("Wizard: root UID: 0\n");
193 printf("Compiled for BSD 4.1\n");
197 printf("Compiled for BSD 4.2\n");
201 printf("Compiled for System III\n");
205 printf("Compiled for System V\n");
213 /************************************************************************
215 / FUNCTION NAME: Error()
217 / FUNCTION: print an error message, and exit
219 / AUTHOR: E. A. Estes, 12/4/85
222 / char *str - format string for printf()
223 / char *file - file which caused error
227 / MODULES CALLED: exit(), perror(), fprintf()
229 / GLOBAL INPUTS: _iob[]
231 / GLOBAL OUTPUTS: none
234 / Print an error message, then exit.
236 / ************************************************************************/
239 Error(const char *str
, const char *file
)
241 fprintf(stderr
, "Error: ");
242 fprintf(stderr
, str
, file
);
248 /************************************************************************
250 / FUNCTION NAME: drandom()
252 / FUNCTION: return a random number
254 / AUTHOR: E. A. Estes, 2/7/86
260 / MODULES CALLED: random()
262 / GLOBAL INPUTS: none
264 / GLOBAL OUTPUTS: none
268 / ************************************************************************/
273 if (sizeof(int) != 2)
274 return((double) (random() & 0x7fff) / 32768.0);
276 return((double) random() / 32768.0);