From d0b79912ed6c0441ee96eaedca2fc064661c1633 Mon Sep 17 00:00:00 2001 From: PatR Date: Mon, 22 Sep 2025 14:47:54 -0700 Subject: [PATCH] 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/". --- doc/fixes3-7-0.txt | 2 ++ src/cfgfiles.c | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/doc/fixes3-7-0.txt b/doc/fixes3-7-0.txt index 70718735b..98d309738 100644 --- a/doc/fixes3-7-0.txt +++ b/doc/fixes3-7-0.txt @@ -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 diff --git a/src/cfgfiles.c b/src/cfgfiles.c index 3a3c0ff06..ca05d35cd 100644 --- a/src/cfgfiles.c +++ b/src/cfgfiles.c @@ -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