From 05b02c7b9b3db414b58210a07db34f68d9a01aa7 Mon Sep 17 00:00:00 2001 From: nhmall Date: Sun, 26 Apr 2026 22:31:52 -0400 Subject: [PATCH] use newer Windows file routines on bones Also correct a double-open bug that resulted from earlier code removal. --- src/files.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/files.c b/src/files.c index 74f600ef2..a55616382 100644 --- a/src/files.c +++ b/src/files.c @@ -510,7 +510,7 @@ free_nhfile(NHFILE *nhfp) { if (nhfp) { init_nhfile(nhfp); - free(nhfp); + free((genericptr_t) nhfp); } } @@ -833,6 +833,9 @@ create_bonesfile(d_level *lev, char **bonesid, char errbuf[]) const char *file; NHFILE *nhfp = (NHFILE *) 0; int failed = 0; +#if defined(WIN32) + errno_t err; +#endif if (errbuf) *errbuf = '\0'; @@ -851,7 +854,7 @@ create_bonesfile(d_level *lev, char **bonesid, char errbuf[]) nhfp->style.binary = TRUE; nhfp->fnidx = historical; nhfp->fd = -1; - nhfp->fpdef = fopen(file, nhfp->style.binary ? WRBMODE : WRTMODE); + nhfp->fpdef = (FILE *) 0; if (nhfp->fpdef) { #ifdef SAVEFILE_DEBUGGING nhfp->fpdebug = fopen("create_bonesfile-debug.log", "a"); @@ -860,12 +863,16 @@ create_bonesfile(d_level *lev, char **bonesid, char errbuf[]) failed = errno; } if (nhfp->structlevel) { -#if defined(MICRO) || defined(WIN32) +#if defined(MICRO) /* Use O_TRUNC to force the file to be shortened if it already * exists and is currently longer. */ nhfp->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, FCMASK); +#elif defined(WIN32) + err = _sopen_s(&nhfp->fd, file, + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, + _SH_DENYRW, _S_IREAD | _S_IWRITE); #else /* ?MICRO || WIN32 */ /* implies UNIX or MAC (MAC is for OS9 or earlier) */ #ifdef MAC @@ -931,6 +938,9 @@ open_bonesfile(d_level *lev, char **bonesid) { const char *fq_bones; NHFILE *nhfp = (NHFILE *) 0; +#if defined(WIN32) + errno_t err UNUSED; +#endif *bonesid = set_bonesfile_name(gb.bones, lev); fq_bones = fqname(gb.bones, BONESPREFIX, 0); @@ -938,6 +948,10 @@ open_bonesfile(d_level *lev, char **bonesid) nhfp = new_nhfile(); if (nhfp) { +#if defined(WIN32) && defined(DEBUG) + if (nhfp->fd >= 0) + impossible("bones file NHFILE * has odd fd (%d)", nhfp->fd); +#endif nhfp->structlevel = TRUE; nhfp->fieldlevel = FALSE; nhfp->ftype = NHF_BONESFILE; @@ -947,15 +961,18 @@ open_bonesfile(d_level *lev, char **bonesid) nhfp->style.binary = (sysopt.bonesformat[0] != exportascii); nhfp->fnidx = sysopt.bonesformat[0]; nhfp->fd = -1; - nhfp->fpdef = fopen(fq_bones, nhfp->style.binary ? RDBMODE : RDTMODE); + nhfp->fpdef = (FILE *) 0; if (nhfp->fpdef) { #ifdef SAVEFILE_DEBUGGING nhfp->fpdebug = fopen("open_bonesfile-debug.log", "a"); #endif } if (nhfp->structlevel) { -#ifdef MAC +#if defined(MAC) nhfp->fd = macopen(fq_bones, O_RDONLY | O_BINARY, BONE_TYPE); +#elif defined(WIN32) + err = _sopen_s(&nhfp->fd, fq_bones, _O_RDONLY | _O_BINARY, + _SH_DENYRW, _S_IREAD | _S_IWRITE); #else nhfp->fd = open(fq_bones, O_RDONLY | O_BINARY, 0); #endif