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

Function getfp

util/makedefs.c:465–512  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

463#endif
464
465static FILE *
466getfp(const char *template, const char *tag, const char *mode, int flg)
467{
468 char *name = name_file(template, tag);
469 FILE *rv = (FILE *) 0;
470 boolean istemp = (flg & FLG_TEMPFILE) != 0;
471#if !defined(HAS_NO_MKSTEMP) && !defined(MD_USE_TMPFILE_S)
472 char tmpfbuf[MAXFNAMELEN];
473 int tmpfd;
474#endif
475#ifdef MD_USE_TMPFILE_S
476 errno_t err;
477#endif
478
479#if defined(MD_USE_TMPFILE_S)
480 if (istemp) {
481 err = tmpfile_s(&rv);
482#if defined(MSDOS) || defined(WIN32)
483 if (!err && (!strcmp(mode, WRTMODE) || !strcmp(mode, RDTMODE))) {
484 (void) _setmode(fileno(rv), O_TEXT);
485 }
486#endif
487 } else
488#else /* MD_USE_TMPFILE_S */
489#ifndef HAS_NO_MKSTEMP
490 if (istemp) {
491 (void) snprintf(tmpfbuf, sizeof tmpfbuf, DATA_TEMPLATE, "mdXXXXXX");
492 tmpfd = mkstemp(tmpfbuf);
493 if (tmpfd >= 0) {
494 rv = fdopen(tmpfd, WRTMODE); /* temp file is always read+write */
495 Unlink(tmpfbuf);
496 }
497 } else
498#endif
499#endif /* MD_USE_TMPFILE_S */
500 rv = fopen(name, mode);
501
502 if (!rv) {
503 Fprintf(stderr, "Can't open '%s' (mode=%s).\n",
504#if !defined(HAS_NO_MKSTEMP) && !defined(MD_USE_TMPFILE_S)
505 istemp ? tmpfbuf :
506#endif
507 name, mode);
508 makedefs_exit(EXIT_FAILURE);
509 /*NOTREACHED*/
510 }
511 return rv;
512}
513
514RESTORE_WARNING_FORMAT_NONLITERAL
515

Callers 2

do_rnd_access_fileFunction · 0.85
do_dungeonFunction · 0.85

Calls 3

name_fileFunction · 0.85
mkstempFunction · 0.85
makedefs_exitFunction · 0.85

Tested by

no test coverage detected