Subject: incompatibility between Torek stdio and gnu/lib/libstdc++
To: None <tech-toolchain@netbsd.org>
From: Mark Davies <mark@mcs.vuw.ac.nz>
List: tech-toolchain
Date: 09/28/2001 10:10:22
Hi,
   The night before last I sent the following to current-users

> The following little c++ program demonstrates a problem with the iostream clear
> method on at least the i386 port.

> #include <iostream.h>

> main()
> {
>         char ans;

>         cout << "Hello World: ";
>         cin >> ans;
>         cin.clear();
>         cout << "Hi again: ";
>         cin >> ans;
> }

> Compiling and running this on 1.5X, if you type CNTL-D at the first prompt,
> it drops through the second and exits with the cin.clear() appearing not to
> clear the EOF status on standard in. This is with the standard system g++
> and the same thing happens with the 2.95.2 from pkgsrc - I haven't tried
> with the new toolchain.

> This same code, compiled with various versions of g++, on Tru64 and
> Solaris8 sits at the second prompt waiting for more input so its not a
> general g++/libstdc++ problem but specific to NetBSD.

Poking at it some more it appears that the problem is due to differences in 
the way Torek stdio behaves vs other UNIX stdio's.  The following little C 
program behaves the same as the above C++ one (with NetBSD's behaviour being 
different from Solaris and Tru64)

#include <stdio.h>

main ()
{
  char ans;
  
  printf("Hello World: ");
  ans= getc(stdin);
  /* clearerr(stdin); */
  printf("Hi World: ");
  ans= getc(stdin);
  
}

If you enable the commented out clearerr(stdin) then you get consistent 
behaviour across the three UNIX systems.

Similarly the following change to the C++ program gets consistent behaviour 
across the systems:

#include <iostream.h>
#include <stdio.h>

main()
{
        char ans;

        cout << "Hello World: ";
        cin >> ans;
        cin.clear();
	clearerr(stdin);
        cout << "Hi again: ";
        cin >> ans;
}


So it appears to me that Torek stdio requires the clearerr() to clear the EOF 
status while the other stdio implementation lets you get away without doing it 
(and I have no problem with the Torek behaviour) but it also seems that 
libstdc++, when dealing with streams that are associated with stdio files is 
assuming the non Torek behaviour :-(

I have no idea how to fix it.  Well presumably the implementation of the clear 
method needs to work out if its dealing with a stdiostream and if so call 
clearerr() but that doesn't look at all easy to me.

comments?

cheers
mark