From ea268b5b6f91c4cf05fd7b80f76ec3408c0a09a0 Mon Sep 17 00:00:00 2001 From: "nethack.allison" Date: Mon, 1 Jul 2002 22:42:57 +0000 Subject: [PATCH] early directory validation Allow early prefix directory validation to help prevent failed games and lost save files due to incorrect config file settings. --- doc/fixes34.1 | 1 + include/extern.h | 1 + src/files.c | 38 +++++++++++++++++++++++++++++++++++++- sys/share/pcmain.c | 11 +++++++++++ sys/share/pcunix.c | 2 +- 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/doc/fixes34.1 b/doc/fixes34.1 index d699c2753..25710ffef 100644 --- a/doc/fixes34.1 +++ b/doc/fixes34.1 @@ -168,3 +168,4 @@ travel option mouse_support wincap option debug mode: #panic routine to test panic() and panic save file generation a new PANICLOG optional file to log the reason for panic and impossible messages +added validate_prefix_locations() for early directory prefix validation diff --git a/include/extern.h b/include/extern.h index 938b6ca3c..7a5db9434 100644 --- a/include/extern.h +++ b/include/extern.h @@ -639,6 +639,7 @@ E void FDECL(check_recordfile, (const char *)); E void NDECL(read_wizkit); #endif E void FDECL(paniclog, (const char *, const char *)); +E int FDECL(validate_prefix_locations, (char *)); /* ### fountain.c ### */ diff --git a/src/files.c b/src/files.c index 6df6b9556..d9271250f 100644 --- a/src/files.c +++ b/src/files.c @@ -153,6 +153,42 @@ int whichprefix, buffnum; #endif } +/* reasonbuf must be at least BUFSZ, supplied by caller */ +int +validate_prefix_locations(reasonbuf) +char *reasonbuf; +{ +#if defined(NOCWD_ASSUMPTIONS) + FILE *fp; + const char *filename; + int prefcnt, failcount = 0; + char failbuf[BUFSZ]; + + failbuf[0] = '\0'; + if (reasonbuf) reasonbuf[0] = '\0'; + for (prefcnt = 1; prefcnt < PREFIX_COUNT; prefcnt++) { + filename = fqname("validate", prefcnt, 3); + if ((fp = fopen(filename, "w"))) { + fclose(fp); + (void) unlink(filename); + } else { + if (failcount) { + Strcat(failbuf,", "); + if (reasonbuf) Strcat(reasonbuf,", "); + } + /* the paniclog entry gets the value of errno */ + Sprintf(eos(failbuf), "%s:%d", fqn_prefix_names[prefcnt], errno); + if (reasonbuf) Strcat(reasonbuf, fqn_prefix_names[prefcnt]); + failcount++; + } + } + if (failcount) { + paniclog("Invalid locations", failbuf); + return 0; + } +#endif + return 1; +} /* fopen a file, with OS-dependent bells and whistles */ /* NOTE: a simpler version of this routine also exists in util/dlb_main.c */ @@ -1289,7 +1325,7 @@ char *tmp_levels; } else if (match_varname(buf, "LEVELDIR", 4) || match_varname(buf, "LEVELS", 4)) { adjust_prefix(bufp, LEVELPREFIX); - } else if (match_varname(buf, "SAVE", 4)) { + } else if (match_varname(buf, "SAVEDIR", 4)) { adjust_prefix(bufp, SAVEPREFIX); } else if (match_varname(buf, "BONESDIR", 5)) { adjust_prefix(bufp, BONESPREFIX); diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 1ce86ea0f..58d87dd57 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -105,6 +105,9 @@ char *argv[]; register int fd; register char *dir; +#ifdef NOCWD_ASSUMPTIONS + char failbuf[BUFSZ]; +#endif #if defined(__BORLANDC__) && !defined(_WIN32) startup(); @@ -171,6 +174,14 @@ char *argv[]; #endif initoptions(); +#ifdef NOCWD_ASSUMPTIONS + if (!validate_prefix_locations(failbuf)) { + raw_printf("Some invalid directory locations were specified:\n\t%s\n", + failbuf); + nethack_exit(EXIT_FAILURE); + } +#endif + #if defined(TOS) && defined(TEXTCOLOR) if (iflags.BIOS && iflags.use_color) set_colors(); diff --git a/sys/share/pcunix.c b/sys/share/pcunix.c index 04620c9ab..bda838f0f 100644 --- a/sys/share/pcunix.c +++ b/sys/share/pcunix.c @@ -186,7 +186,7 @@ gotlock: chdirx(orgdir, 0); #if defined(WIN32) error("cannot creat file (%s.)\n%s\n%s\"%s\" exists?\n", - fq_lock, strerror(ern), "Are you sure that the directory", + fq_lock, strerror(ern), " Are you sure that the directory", fqn_prefix[LEVELPREFIX]); #else error("cannot creat file (%s.)", fq_lock);