use newer Windows file routines on bones

Also correct a double-open bug that resulted from
earlier code removal.
This commit is contained in:
nhmall
2026-04-26 22:31:52 -04:00
parent c25528dafe
commit 05b02c7b9b
+22 -5
View File
@@ -510,7 +510,7 @@ free_nhfile(NHFILE *nhfp)
{ {
if (nhfp) { if (nhfp) {
init_nhfile(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; const char *file;
NHFILE *nhfp = (NHFILE *) 0; NHFILE *nhfp = (NHFILE *) 0;
int failed = 0; int failed = 0;
#if defined(WIN32)
errno_t err;
#endif
if (errbuf) if (errbuf)
*errbuf = '\0'; *errbuf = '\0';
@@ -851,7 +854,7 @@ create_bonesfile(d_level *lev, char **bonesid, char errbuf[])
nhfp->style.binary = TRUE; nhfp->style.binary = TRUE;
nhfp->fnidx = historical; nhfp->fnidx = historical;
nhfp->fd = -1; nhfp->fd = -1;
nhfp->fpdef = fopen(file, nhfp->style.binary ? WRBMODE : WRTMODE); nhfp->fpdef = (FILE *) 0;
if (nhfp->fpdef) { if (nhfp->fpdef) {
#ifdef SAVEFILE_DEBUGGING #ifdef SAVEFILE_DEBUGGING
nhfp->fpdebug = fopen("create_bonesfile-debug.log", "a"); nhfp->fpdebug = fopen("create_bonesfile-debug.log", "a");
@@ -860,12 +863,16 @@ create_bonesfile(d_level *lev, char **bonesid, char errbuf[])
failed = errno; failed = errno;
} }
if (nhfp->structlevel) { if (nhfp->structlevel) {
#if defined(MICRO) || defined(WIN32) #if defined(MICRO)
/* Use O_TRUNC to force the file to be shortened if it already /* Use O_TRUNC to force the file to be shortened if it already
* exists and is currently longer. * exists and is currently longer.
*/ */
nhfp->fd = open(file, nhfp->fd = open(file,
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, FCMASK); 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 */ #else /* ?MICRO || WIN32 */
/* implies UNIX or MAC (MAC is for OS9 or earlier) */ /* implies UNIX or MAC (MAC is for OS9 or earlier) */
#ifdef MAC #ifdef MAC
@@ -931,6 +938,9 @@ open_bonesfile(d_level *lev, char **bonesid)
{ {
const char *fq_bones; const char *fq_bones;
NHFILE *nhfp = (NHFILE *) 0; NHFILE *nhfp = (NHFILE *) 0;
#if defined(WIN32)
errno_t err UNUSED;
#endif
*bonesid = set_bonesfile_name(gb.bones, lev); *bonesid = set_bonesfile_name(gb.bones, lev);
fq_bones = fqname(gb.bones, BONESPREFIX, 0); fq_bones = fqname(gb.bones, BONESPREFIX, 0);
@@ -938,6 +948,10 @@ open_bonesfile(d_level *lev, char **bonesid)
nhfp = new_nhfile(); nhfp = new_nhfile();
if (nhfp) { 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->structlevel = TRUE;
nhfp->fieldlevel = FALSE; nhfp->fieldlevel = FALSE;
nhfp->ftype = NHF_BONESFILE; nhfp->ftype = NHF_BONESFILE;
@@ -947,15 +961,18 @@ open_bonesfile(d_level *lev, char **bonesid)
nhfp->style.binary = (sysopt.bonesformat[0] != exportascii); nhfp->style.binary = (sysopt.bonesformat[0] != exportascii);
nhfp->fnidx = sysopt.bonesformat[0]; nhfp->fnidx = sysopt.bonesformat[0];
nhfp->fd = -1; nhfp->fd = -1;
nhfp->fpdef = fopen(fq_bones, nhfp->style.binary ? RDBMODE : RDTMODE); nhfp->fpdef = (FILE *) 0;
if (nhfp->fpdef) { if (nhfp->fpdef) {
#ifdef SAVEFILE_DEBUGGING #ifdef SAVEFILE_DEBUGGING
nhfp->fpdebug = fopen("open_bonesfile-debug.log", "a"); nhfp->fpdebug = fopen("open_bonesfile-debug.log", "a");
#endif #endif
} }
if (nhfp->structlevel) { if (nhfp->structlevel) {
#ifdef MAC #if defined(MAC)
nhfp->fd = macopen(fq_bones, O_RDONLY | O_BINARY, BONE_TYPE); 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 #else
nhfp->fd = open(fq_bones, O_RDONLY | O_BINARY, 0); nhfp->fd = open(fq_bones, O_RDONLY | O_BINARY, 0);
#endif #endif