support 'nethack --nethackrc=~/File'

Substitute $HOME/File if command line specifies --nethackrc=~/File
to avoid "Access to ~/File denied (2)".

Only implemented for opening run-time config file on Unix.  Works for
NETHACKOPTIONS=@~/File too; the normally optional at-sign is required
since the tilde won't match a slash to distinguish file versus options.

Only supports "~/" file path prefix, not "~user/".
This commit is contained in:
PatR
2025-09-22 14:47:54 -07:00
parent 9b7b2421ac
commit d0b79912ed
2 changed files with 9 additions and 0 deletions

View File

@@ -2479,6 +2479,8 @@ Unix: add ../include/nhlua.h to the alloc.o dependencies in Makefile.utl to
Unix: implement SELF_RECOVER compile-time option, on by default on linux
Unix: allow build to succeed with musl (instead of glibc), by specifying
musl=1 on the make command line
Unix: support tilde expansion for home directory path, "~/relative-path", in
command line --nethackrc=path and environment NETHACKOPTIONS=@path
user_sounds: move the message hook from inside individual window display ports
to the core where it allows MSGTYP_NOSHOW msgtyp's to still trigger
sounds to correct a reported github issue; also fixes a past reported

View File

@@ -242,6 +242,13 @@ fopen_config_file(const char *filename, int src)
if (filename && *filename) {
set_configfile_name(filename);
#ifdef UNIX
if (!strncmp(configfile, "~/", 2) && (envp = nh_getenv("HOME")) != 0) {
/* support for command line '--nethackrc=~/path' (or for
NETHACKOPTIONS='@~/path'; we don't support ~user/path) */
Snprintf(tmp_config, sizeof tmp_config, "%s/%s",
envp, configfile + 2); /* insert $HOME/ and remove ~/ */
set_configfile_name(tmp_config);
}
if (access(configfile, 4) == -1) { /* 4 is R_OK on newer systems */
/* nasty sneaky attempt to read file through
* NetHack's setuid permissions -- this is the only