This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: bug in freopen


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Christopher Faylor on 7/14/2005 5:32 PM:
>>#include <stdio.h>
>>#include <errno.h>
>>int main(void)
>>{
>>  FILE* f = freopen (NULL, "rb", stdin); /* Ensure that stdin is binary */
>>  printf ("file is %s, errno %d:%s\n", f ? "good" : "null", errno,
>>          strerror(errno));
>>  return 0;
>>}
>>
> 
> Hmm.  It should be pretty simple to just make this always fail with
> EBADF.  That seems to be a valid thing to do as far as SUSv3 is
> concerned.

Just because SUSv3 permits EBADF doesn't make it a very good QofI choice.
 I don't think it would be too hard to add code that at least gives an
attempt to do the right thing using setmode()/fcntl(F_SETFL).

> 
> I am curious, though, as to the rationale behind this change.  I just
> tried this with -mno-cygwin and it dies with a ENOENT.  So, they
> apparently removed windows-specific code and replaced it with code
> that doesn't work on windows...

The rationale was that the main body of code should be as portable as
possible by sticking to standards, and that platforms that don't meet the
standards then use wrappers from gnulib.  If newlib's freopen is not
patched, then coreutils will probably have to add an autoconf test to
detect that, then add something like this to gnulib:

[freopen.h]
#if FREOPEN_NULL_BROKEN
# include <stdio.h>
# define freopen rpl_freopen
#endif

[freopen.c]
FILE*
rpl_freopen (const char *name, const char *mode, FILE* f)
{
  if (name)
    return freopen (name, mode, f);
  /* Some combination of setmode()/fcntl(fileno(f),F_SETFL,...) */
}

The other part of the rationale is that if cygwin implements this part of
freopen, it can choose to change the modes on regular files but be a no-op
on ttys.  In other words, it would replace code that looks like:
int fd = fileno (f);
if (! isatty (fd))
  setmode (fd, O_BINARY);

with the easier to read
freopen (NULL, "wb", f);

- --
Life is short - so eat dessert first!

Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFC1zJU84KuGfSFAYARAtinAJ9uep5VI7eg56YvNSg6EeirY1tttwCcCv2s
mrKruO0cE0QyiTlbrsyHjVk=
=3qVU
-----END PGP SIGNATURE-----

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]