This is the mail archive of the cygwin@sourceware.cygnus.com 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]

Re: getc() problem with Cygwin v1.0


> Hallo Jim, 

Hallo Georg,

> 
> at first in ANSI-C there is no function getc( void ). There is 
> int getc( FILE *stream ) and int getchar( void ).

Yes, thank you for pointing that out (though I did have getc(stdin) correct
in my code).

> 
> The problem is a little bit more complicated. getchar() is reading one character
> from the input-stream and the function is not waiting for "RETURN". The question
> is: When is a keystroke put in in the stream "stdin"?
> 
> The following little program shows this:
> 
> #include <stdio.h>
> #include <string.h>
> 
> int main( void )
> {
>     char buffer[81];
>     int i, ch;
> 
>     int re = 0;
> 
>    /*----------------------------------------*/
> 
>     printf( "Enter a line: " );
> 
>     /* Read in single line from "stdin": */
>     for( i = 0; (i < 80) &&  ((ch = getc( stdin )) != EOF) 
>              && (ch != '\n'); i++ )
>     {
>         printf( "!ch = %c\n", ch );
>         buffer[i] = (char)ch;
>     }
> 
>         /* Terminate string with null character: */
>     buffer[i] = '\0';
>     printf( "%s\n", buffer );
> 
> 
>     printf( "READY, press Enter\n" );
>     getchar();
>     return re;
> }
> 
>     /*--------------------*/
> 
> 
> On my systems [ 1. Cygwin 20.1, 2. WindowsNT +  MinGw] the loop starts when the
> "Return" is pressed and prints then one character at one time.

Yes, your program demonstrates the problem nicely.  On Cygwin 20.1, I
get the following result:
Enter a line: This is a test.
!ch = T
!ch = h
!ch = i
!ch = s
!ch =  
!ch = i
!ch = s
!ch =  
!ch = a
!ch =  
!ch = t
!ch = e
!ch = s
!ch = t
!ch = .
This is a test.
READY, press Enter

On Cygwin v1.0, I get the following:
Enter a line: T!ch = T
h!ch = h
i!ch = i
s!ch = s
 !ch =  
i!ch = i
s!ch = s
 !ch =  
a!ch = a
 !ch =  
t!ch = t
e!ch = e
s!ch = s
t!ch = t
!ch = .

This is a test.
READY, press Enter

> 
> If you need to wait until "RETURN" is pressed maybe you have succes with
> "gets()". Using "scanf()" is dangerous, because sometimes "RETURN" is left in
> the buffer.

Yes, using gets() or fgets() for a small program would work better, but
the real problem is that canonical mode is broken for v1.0 (the man
page for "termio" explains this in detail, but it basically means getc(stdin)
should block until a carriage return is entered).  Canonical mode also
allows the user to backspace over his mistakes -- which also doesn't work
with v1.0.

Pre-compiled programs distributed with Cygwin v1.0 such as bc.exe, dc.exe,
etc all rely on canonical mode.  It is especially annoying to not be able
to backspace over my mistakes with dc.exe -- I have to kill the program
each time and start over.  Even if I were to modify the souce code to
replace getc(stdin) with fgets(...), I still wouldn't be able to backspace...

And for that matter, you can't backspace with sh.exe, either.  I can't
believe I'm the first person to notice this.

Hey Cygnus, are you reading this?  Is there any way we can fix canonical
mode for v1.0?

Jim Grishaw
dtcohen@aol.com

> 
> Dtcohen@aol.com wrote:
> > 
> > For Cygwin v1.0, getc() returns after every key is typed, instead
> > of waiting for the carriage return.  This is incorrect behavior.
> > With Cygwin B20.1 it correctly waits for the carriage return.
> > 
> > Does anyone know how this can be fixed?  Any pointers to the
> > source of the problem are also appreciated.
> > 
> > Many thanks,
> > Jim Grishaw
> > dtcohen@aol.com
> 

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


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