+/* $NetBSD: setup.c,v 1.11 2001/03/27 02:23:28 simonb Exp $ */
+
/*
* setup.c - set up all files for Phantasia
*/
-#include "include.h"
#include <sys/param.h>
#include <sys/stat.h>
-#include <stdlib.h>
+#include <fcntl.h>
+#include "include.h"
+
+int main __P((int, char *[]));
+void Error __P((const char *, const char *)) __attribute__((__noreturn__));
+double drandom __P((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[];
{
- 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*/
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);
+ path = strrchr(_PATH_VOID, '/') + 1;
if ((fp = fopen(path, "w")) == NULL)
Error("Cannot update %s.\n", path);
else
{
fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
+ fflush(fp);
+ if (ferror(fp))
+ Error("Writing %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.
/
-/************************************************************************/
+/ ************************************************************************/
+void
Error(str, file)
-char *str, *file;
+ const char *str, *file;
{
fprintf(stderr, "Error: ");
fprintf(stderr, str, file);
/
/ DESCRIPTION:
/
-/************************************************************************/
+/ ************************************************************************/
double
drandom()