+/* $NetBSD: setup.c,v 1.22 2017/10/02 22:03:10 joerg Exp $ */
+
/*
* setup.c - set up all files for Phantasia
+ * n.b.: this is used at build-time - i.e. during build.sh.
*/
-#include "include.h"
-#include <sys/param.h>
+#ifdef __NetBSD__
+#include <sys/cdefs.h>
+#endif
+
#include <sys/stat.h>
+#include <fcntl.h>
+#include <setjmp.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#ifndef __dead /* Not NetBSD */
+#define __dead
+#endif
+
+#include "phantdefs.h"
+#include "phantstruct.h"
+#include "phantglobs.h"
+#include "pathnames.h"
+
+int main(int, char *[]);
+void Error(const char *, const char *) __dead;
+double drandom(void);
+
/*\f*/
/************************************************************************
/
/ put in these files.
/ Also, the monster binary data base is created here.
/
-/************************************************************************/
+/ ************************************************************************/
-static char *files[] = { /* all files to create */
+static const char *const files[] = { /* all files to create */
_PATH_MONST,
_PATH_PEOPLE,
_PATH_MESS,
NULL,
};
-char *monsterfile="monsters.asc";
+const char *monsterfile = "monsters.asc";
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
- register char **filename; /* for pointing to file names */
- register int fd; /* file descriptor */
- FILE *fp; /* for opening files */
+ const char *const *filename; /* for pointing to file names */
+ int fd; /* file descriptor */
+ FILE *fp; /* for opening files */
struct stat fbuf; /* for getting files statistics */
int ch;
- char path[MAXPATHLEN], *prefix;
+ char *path;
- while ((ch = getopt(argc, argv, "m:")) != EOF)
+ while ((ch = getopt(argc, argv, "m:")) != -1)
switch(ch) {
case 'm':
monsterfile = optarg;
argc -= optind;
argv += optind;
- srandom((unsigned) time((long *) NULL)); /* prime random numbers */
+ srandom((unsigned) time(NULL)); /* prime random numbers */
umask(0117); /* only owner can read/write created files */
- prefix = getenv("DESTDIR");
-
/* try to create data files */
filename = &files[0];
while (*filename != NULL)
/* create each file */
{
- snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", *filename);
+ path = strrchr(*filename, '/') + 1;
if (stat(path, &fbuf) == 0)
/* file exists; remove it */
{
- if (!strcmp(*filename, _PATH_PEOPLE))
- /* do not reset character file if it already exists */
- {
- ++filename;
- continue;
- }
-
if (unlink(path) < 0)
Error("Cannot unlink %s.\n", path);
/*NOTREACHED*/
++filename; /* process next file */
}
- /* put holy grail info into energy void file */
- Enrgyvoid.ev_active = TRUE;
- Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
- Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
- snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_VOID);
+ /* Initialize an empty file placeholder for the grail location. */
if ((fp = fopen(path, "w")) == NULL)
- Error("Cannot update %s.\n", path);
- else
- {
- fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
- fclose(fp);
- }
+ Error("Cannot create %s.\n", path);
+ fclose(fp);
/* create binary monster data base */
- snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MONST);
+ path = strrchr(_PATH_MONST, '/') + 1;
if ((Monstfp = fopen(path, "w")) == NULL)
Error("Cannot update %s.\n", path);
else
fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
}
fclose(fp);
+ fflush(Monstfp);
+ if (ferror(Monstfp))
+ Error("Writing %s.\n", path);
fclose(Monstfp);
}
}
printf("One line 'motd' ? ");
if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
Databuf[0] = '\0';
- snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MOTD);
+ path = strrchr(_PATH_MOTD, '/') + 1;
if ((fp = fopen(path, "w")) == NULL)
Error("Cannot update %s.\n", path);
else
/ DESCRIPTION:
/ Print an error message, then exit.
/
-/************************************************************************/
+/ ************************************************************************/
-Error(str, file)
-char *str, *file;
+void
+Error(const char *str, const char *file)
{
fprintf(stderr, "Error: ");
fprintf(stderr, str, file);
/
/ DESCRIPTION:
/
-/************************************************************************/
+/ ************************************************************************/
double
-drandom()
+drandom(void)
{
if (sizeof(int) != 2)
return((double) (random() & 0x7fff) / 32768.0);