X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/045c162979af9765f1d2f9076378613da3cb28f7..bae50c403e053e5ef8d399f1adb6677e2c4fabf8:/phantasia/setup.c diff --git a/phantasia/setup.c b/phantasia/setup.c index da55980d..6a1d4c0e 100644 --- a/phantasia/setup.c +++ b/phantasia/setup.c @@ -1,10 +1,17 @@ +/* $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 #include -#include +#include +#include "include.h" + +int main __P((int, char *[])); +void Error __P((const char *, const char *)) __attribute__((__noreturn__)); +double drandom __P((void)); + /* */ /************************************************************************ / @@ -35,9 +42,9 @@ / 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, @@ -49,21 +56,21 @@ static char *files[] = { /* all files to create */ 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; @@ -75,28 +82,19 @@ main(argc, argv) 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*/ @@ -115,17 +113,20 @@ main(argc, argv) 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 @@ -157,6 +158,9 @@ main(argc, argv) fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp); } fclose(fp); + fflush(Monstfp); + if (ferror(Monstfp)) + Error("Writing %s.\n", path); fclose(Monstfp); } } @@ -166,7 +170,7 @@ main(argc, argv) 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 @@ -224,10 +228,11 @@ main(argc, argv) / 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); @@ -256,7 +261,7 @@ char *str, *file; / / DESCRIPTION: / -/************************************************************************/ +/ ************************************************************************/ double drandom()