[m-rev.] for review: binary standard input/output streams on Windows

Peter Wang novalazy at gmail.com
Fri Apr 20 12:46:59 AEST 2012


On Fri, 20 Apr 2012 12:16:27 +1000 (EST), Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> 
> On Fri, 20 Apr 2012, Peter Wang wrote:
> >
> > diff --git a/library/io.m b/library/io.m
> > index 9a13c75..5d12eb3 100644
> > --- a/library/io.m
> > +++ b/library/io.m
> > @@ -6300,6 +6300,22 @@ MR_Unsigned mercury_current_text_output_index;
> > MR_Unsigned mercury_current_binary_input_index;
> > MR_Unsigned mercury_current_binary_output_index;
> >
> > +static void
> > +mercury_set_binary_mode(FILE *f)
> > +{
> > +#if defined(MR_MSVC) || defined(MR_MINGW)
> > +    /*
> > +    ** Calling fdopen with 'b' in the mode string does not necessarily put the
> > +    ** file into binary translation mode on Windows.
> 
> According to MSDN
> (<http://msdn.microsoft.com/en-us/library/dye30d82.aspx), having 'b' in
> the mode string _should_ do that.  (We should probably be using _fdopen
> with MSVC rather than fdopen since the latter is deprecated, but that's
> another issue.)  Is this just (another) case of Microsoft's API's
> behaving in a completely stupid way?

It does work for normal files but not for stdin/out.  Maybe it wasn't
deliberate but now the behaviour is ingrained.  Rewording the comment:

    /*
    ** Calling fdopen with 'b' in the mode string does not necessarily put the
    ** standard input or standard output file into binary translation mode on
    ** Windows. This is the case with MinGW and MSVC. The cause is likely the
    ** MSVC CRT. The solution is to change the mode on the file after opening.
    */

> >      This is the case with
> > +    ** MinGW and, reportedly, MSVC. The cause is likely the MSVC CRT.
> > +    ** The solution is to change the mode on the file after opening.
> > +    */
> > +    _setmode(_fileno(f), _O_BINARY);
> > +#else
> > +    (void)f;
> > +#endif
> 
> Why #else branch here?

To avoid a potential warning about the unused parameter, but we don't
enable those warnings.  Deleted.

Peter
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list