makedefs temp files issue with mingw

The unlink call wasn't operating the same on a makedefs built
on mingw, and dat/mdXXXX files were being left behind post-build.

Provide an alternative way of doing the temporary files if
MD_USE_TMPFILE_S is defined during the compile of makedefs.c
This commit is contained in:
nhmall
2022-12-12 12:53:51 -05:00
parent 092068495b
commit 9e91064659
2 changed files with 33 additions and 9 deletions

View File

@@ -103,6 +103,11 @@ extern void interject(int);
*/
#ifdef __MINGW32__
#define MD_USE_TMPFILE_S
#if !defined(__cplusplus)
extern errno_t tmpfile_s(FILE * restrict * restrict streamptr);
#endif
#
#ifdef strncasecmp
#undef strncasecmp
#endif
@@ -115,6 +120,7 @@ extern void interject(int);
#endif
#ifdef _MSC_VER
#define MD_USE_TMPFILE_S
#define HAS_STDINT
#if (_MSC_VER > 1000)
/* Visual C 8 warning elimination */

View File

@@ -35,6 +35,10 @@
#define SpinCursor(x)
#endif
#ifdef MD_USE_TMPFILE_S
#include <errno.h>
#endif
#define Fprintf (void) fprintf
#define Fclose (void) fclose
#define Unlink (void) unlink
@@ -374,30 +378,44 @@ getfp(const char* template, const char* tag, const char* mode, int flg)
{
char *name = name_file(template, tag);
FILE *rv = (FILE *) 0;
#ifndef HAS_NO_MKSTEMP
boolean istemp = (flg & FLG_TEMPFILE) != 0;
#ifdef MD_USE_TMPFILE_S
errno_t err;
#endif
#if !defined(HAS_NO_MKSTEMP) && !defined(MD_USE_TMPFILE_S)
char tmpfbuf[MAXFNAMELEN];
int tmpfd;
#endif
#if !defined(HAS_NO_MKSTEMP) || defined(MD_USE_TMPFILE_S)
if (istemp) {
#ifdef MD_USE_TMPFILE_S
err = tmpfile_s(&rv);
#if defined(MSDOS) || defined(WIN32)
if (!err && (!strcmp(mode, WRTMODE) || !strcmp(mode, RDTMODE))) {
_setmode(fileno(rv), O_TEXT);
}
#endif
#else /* !MD_USE_TMPFILE_S */
(void) snprintf(tmpfbuf, sizeof tmpfbuf, DATA_TEMPLATE, "mdXXXXXX");
tmpfd = mkstemp(tmpfbuf);
if (tmpfd >= 0) {
rv = fdopen(tmpfd, WRTMODE); /* temp file is always read+write */
Unlink(tmpfbuf);
}
}
else
#else
flg; // unused
#endif
rv = fopen(name, mode);
#endif /* ?MD_USE_TMPFILE_S */
} else
#endif /* !HAS_NO_MKSTEMP || MD_USE_TMPFILE_S */
rv = fopen(name, mode);
if (!rv) {
Fprintf(stderr, "Can't open '%s'.\n",
Fprintf(stderr, "Can't open '%s' (mode=%s).\n",
#ifndef HAS_NO_MKSTEMP
#if !defined(MD_USE_TMPFILE_S)
istemp ? tmpfbuf :
#endif
name);
#endif
name, mode);
makedefs_exit(EXIT_FAILURE);
/*NOTREACHED*/
}