[comp.sources.bugs] Nethack makedefs.c bug & fix

nathan@mit-eddie.UUCP (08/28/87)

I have figured out what the bug in makedefs.c is which causes onames.h
to always be missing the #define for AMULET_OF_YENDOR. Other symptoms
of this bug, which I didn't see complaints about, but which I noticed
were that trap.h and date.h were not correct either. In fact, date.h
would become empty after compiling a few times.

The problem is apparently in the MSC 4.0 freopen routine. It seems that
the first couple of lines written to the file pointer freopen'd are
lost. The fix is to use a substitute freopen function which opens
the file normally. The function may not be a completely general substitute
for the normal freopen, but is sufficient for makedefs.


Description of changes follows:

(Perhaps all this stuff should be enclosed in #ifndef __TURBOC__'s, but
I'll let someone else figure out whether this bug exists in that compiler.)

At the top of the file, either in the #ifdef MSDOS section where rename
is defined, or elsewhere, the following define should appear:

#ifdef MSDOS
#define freopen _freopen
#endif

---------
At the bottom of the file, either in the #ifdef MSDOS section where _rename
is, or elsewhere, the following code should appear:

#ifdef MSDOS
/* Get around bug in freopen when opening for writing */
#undef freopen
FILE *_freopen(fname, fmode, fp)
char *fname, *fmode;
FILE *fp;
{
    if (!strncmp(fmode,"w",1))
    {
        FILE *tmpfp;

        if ((tmpfp = fopen(fname,fmode)) == NULL)
            return(NULL);
        if (dup2(fileno(tmpfp),fileno(fp)) < 0)
            return(NULL);
        fclose(tmpfp);
        return(fp);
    }
    else
        return(freopen(fname,fmode,fp));
}
#endif

-- 
				Nathan Glasser
fnord				nathan@{mit-eddie.uucp, xx.lcs.mit.edu}
"A tribble is the only love that money can buy."	    
Presently there is insufficient evidence to conclude that tribbles spread AIDS.