Bug in Control-d handling?

Takashi Yano takashi.yano@nifty.ne.jp
Mon Jun 20 08:59:35 GMT 2022


On Sun, 19 Jun 2022 15:08:19 -0400
Ken Brown wrote:
> Consider the following program, which reads from standard input a line at a time 
> and then echoes the input back to the terminal:
> 
> $ cat cat_line.c
> #include <stdio.h>
> 
> int main ()
> {
>    char buf[BUFSIZ];
> 
>    while (fgets (buf, BUFSIZ, stdin))
>      fputs (buf, stdout);
> }
> 
> Run the program, type one or more characters (without hitting Enter), and type 
> Ctrl-d until the program exits.  What I expect is that nothing visible happens 
> on the first Ctrl-d [but the input is sent to the internal stdin buffer], and 
> that the input is echoed and the program exits after the second Ctrl-d [the 
> program sees EOF].  This is what happens on Linux.  On Cygwin, however, the 
> program keeps running after the second Ctrl-d and doesn't exit until Ctrl-d is 
> pressed a third time.
> 
> I observed this problem because of a failing Emacs test, in which the program 
> "rev" was not seeing EOF after being sent Ctrl-d; "rev" does something like the 
> test case above, but using fgetws instead of fgets.

Isn't this a bug of newlib? Try following code.

#include <stdio.h>

int main()
{
	printf("%d\n", getchar());
	printf("%d\n", feof(stdin));
	printf("%d\n", getchar());
	return 0;
}

If you press Ctrl-D at the first getchar(), the second getchar()
does not return EOF while it does in linux.

The following patch seems to resolve the issue.

diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index ccedc7af7..843163b7e 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -47,11 +47,9 @@ __srefill_r (struct _reent * ptr,
 
   fp->_r = 0;			/* largely a convenience for callers */
 
-#ifndef __CYGWIN__
   /* SysV does not make this test; take it out for compatibility */
   if (fp->_flags & __SEOF)
     return EOF;
-#endif
 
   /* if not already reading, have to be reading and writing */
   if ((fp->_flags & __SRD) == 0)

However, I am not sure what this #ifndef __CYGWIN__ is for.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>


More information about the Cygwin mailing list