MCPcopy Create free account
hub / github.com/NetHack/NetHack / fopen_config_file

Function fopen_config_file

src/cfgfiles.c:222–372  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

220}
221
222staticfn FILE *
223fopen_config_file(const char *filename, int src)
224{
225 FILE *fp;
226#if defined(UNIX) || defined(VMS)
227 char tmp_config[BUFSZ];
228 char *envp;
229#endif
230
231 if (src == set_in_sysconf) {
232 /* SYSCF_FILE; if we can't open it, caller will bail */
233 if (filename && *filename) {
234 set_configfile_name(fqname(filename, SYSCONFPREFIX, 0));
235 fp = fopen(configfile, "r");
236 } else
237 fp = (FILE *) 0;
238 return fp;
239 }
240 /* If src != set_in_sysconf, "filename" is an environment variable, so it
241 * should hang around. If set, it is expected to be a full path name
242 * (if relevant)
243 */
244 if (filename && *filename) {
245 set_configfile_name(filename);
246#ifdef UNIX
247 if (!strncmp(configfile, "~/", 2) && (envp = nh_getenv("HOME")) != 0) {
248 /* support for command line '--nethackrc=~/path' (or for
249 NETHACKOPTIONS='@~/path'; we don't support ~user/path) */
250 Snprintf(tmp_config, sizeof tmp_config, "%s/%s",
251 envp, configfile + 2); /* insert $HOME/ and remove ~/ */
252 set_configfile_name(tmp_config);
253 }
254 if (access(configfile, 4) == -1) { /* 4 is R_OK on newer systems */
255 /* nasty sneaky attempt to read file through
256 * NetHack's setuid permissions -- this is the only
257 * place a file name may be wholly under the player's
258 * control (but SYSCF_FILE is not under the player's
259 * control so it's OK).
260 */
261 raw_printf("Access to %s denied (%d).", configfile, errno);
262 wait_synch();
263 /* fall through to standard names */
264 } else
265#endif
266 if ((fp = fopen(configfile, "r")) != (FILE *) 0) {
267 return fp;
268#if defined(UNIX) || defined(VMS)
269 } else {
270 /* access() above probably caught most problems for UNIX */
271 raw_printf("Couldn't open requested config file %s (%d).",
272 configfile, errno);
273 wait_synch();
274#endif
275 }
276 }
277 /* fall through to standard names */
278
279#if defined(MICRO) || defined(MACOS9) || defined(__BEOS__) || defined(WIN32)

Callers 1

read_config_fileFunction · 0.85

Calls 6

set_configfile_nameFunction · 0.85
fqnameFunction · 0.85
accessFunction · 0.85
wait_synchFunction · 0.85
nh_getenvFunction · 0.70
raw_printfFunction · 0.70

Tested by

no test coverage detected