Re: Gnulib's freopen replacement and MinGW

Eric Blake <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs,gmane.comp.gnu.utils.bugs
Organization Red Hat
Message-ID <[email protected]>
On 05/05/2012 05:18 AM, Eli Zaretskii wrote:
> Diffutils 3.2 call xfreopen with its first argument NULL, expecting
> the underlying reopen to handle this.  However, the MS runtime does
> not implement the Posix semantics of such a call,

POSIX says that such a call is implementation-defined, so technically,
mingw DOES implement the POSIX semantics (by defining that it is
unsupported).  However, you are correct that it is annoying, and
something that gnulib could work around for easier coding.

> The following change fixes this:
> 
> 2012-05-05  Eli Zaretskii  <[email protected]>
> 
> 	* lib/freopen.c [_WIN32]: Include io.h and fcntl.h.
> 	(rpl_freopen) [_WIN32]: If the first argument is NULL, call
> 	_setmode to switch STREAM to either binary or text mode, as
> 	specified by MODE.
> 
> --- lib/freopen.c~0	2011-09-02 01:35:11.000000000 +0300
> +++ lib/freopen.c	2012-05-05 13:46:36.389000000 +0300
> @@ -37,11 +37,23 @@ orig_freopen (const char *filename, cons
>  
>  #include <string.h>
>  
> +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
> +#include <io.h>
> +#include <fcntl.h>
> +#endif
> +
>  FILE *
>  rpl_freopen (const char *filename, const char *mode, FILE *stream)
>  {
>  #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
> -  if (filename != NULL && strcmp (filename, "/dev/null") == 0)
> +  if (filename == NULL)
> +    {
> +      if (strchr (mode, 'b'))
> +	return _setmode (_fileno (stream), _O_BINARY) == -1 ? NULL : stream;
> +      else
> +	return _setmode (_fileno (stream), _O_TEXT) == -1 ? NULL : stream;

Shouldn't we be doing some sanity checking, as in ensuring that a
read-only stream is not being reopened with 'w', or a write-only stream
is not being reopened with 'r'?  Also, shouldn't we be honoring 'w' vs.
'a' and adjusting the underlying fd's O_APPEND bit accordingly?

-- 
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc (application/pgp-signature, 620 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBCAAGBQJPp8OQAAoJEKeha0olJ0NqEsEH/iDl9q8/7zJbz7kULqUFqwhg
6wEJaDoOV3kzE5PVanqKSUKQRioYSjVECXdpkoog86ZNReQhiEtYPHU0Mi0ZMqua
2kUfPuuDhbnlLqDBhCldTd+/iIl5WG3AoFqwXjVJZsSV5G5Mr7NOZqlwn0GQisXo
u57x01+0oiscItyNxmPtD7rFMSjDypxQ5ym+jley8LITkAJVPIsJbfHbOCNexrFl
EvZKhY7VjDdplgvK3+aOLn+3UYxs3g0jJkk66I0LH804XH34lC4K2lwfaeRG1Vzz
cCSvIVgGaWz4IB9pQSJ1COr+cPsVbUcasu3rBJWpGE61wBbMp10cOQHMGMw8QJw=
=TIY1
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.